diff options
author | Chris <ccbrown112@gmail.com> | 2017-08-28 11:27:18 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-28 11:27:18 -0500 |
commit | 6215c9159acb85033616d2937edf3d87ef7ca79b (patch) | |
tree | 54e53392804bda3a30aa9615fef0cd01ca189152 /plugin/rpcplugin/http_test.go | |
parent | 510b1a18f5282981a70503c0cde474e121c9e651 (diff) | |
download | chat-6215c9159acb85033616d2937edf3d87ef7ca79b.tar.gz chat-6215c9159acb85033616d2937edf3d87ef7ca79b.tar.bz2 chat-6215c9159acb85033616d2937edf3d87ef7ca79b.zip |
add plugin http handler (#7289)
Diffstat (limited to 'plugin/rpcplugin/http_test.go')
-rw-r--r-- | plugin/rpcplugin/http_test.go | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/plugin/rpcplugin/http_test.go b/plugin/rpcplugin/http_test.go new file mode 100644 index 000000000..afaaf7756 --- /dev/null +++ b/plugin/rpcplugin/http_test.go @@ -0,0 +1,61 @@ +package rpcplugin + +import ( + "io" + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" +) + +func testHTTPResponseWriterRPC(w http.ResponseWriter, f func(w http.ResponseWriter)) { + r1, w1 := io.Pipe() + r2, w2 := io.Pipe() + + c1 := NewMuxer(NewReadWriteCloser(r1, w2), false) + defer c1.Close() + + c2 := NewMuxer(NewReadWriteCloser(r2, w1), true) + defer c2.Close() + + id, server := c1.Serve() + go ServeHTTPResponseWriter(w, server) + + remote := ConnectHTTPResponseWriter(c2.Connect(id)) + defer remote.Close() + + f(remote) +} + +func TestHTTP(t *testing.T) { + w := httptest.NewRecorder() + + testHTTPResponseWriterRPC(w, func(w http.ResponseWriter) { + headers := w.Header() + headers.Set("Test-Header-A", "a") + headers.Set("Test-Header-B", "b") + w.Header().Set("Test-Header-C", "c") + w.WriteHeader(http.StatusPaymentRequired) + n, err := w.Write([]byte("this is ")) + assert.Equal(t, 8, n) + assert.NoError(t, err) + n, err = w.Write([]byte("a test")) + assert.Equal(t, 6, n) + assert.NoError(t, err) + }) + + r := w.Result() + defer r.Body.Close() + + assert.Equal(t, http.StatusPaymentRequired, r.StatusCode) + + body, err := ioutil.ReadAll(r.Body) + assert.NoError(t, err) + assert.EqualValues(t, "this is a test", body) + + assert.Equal(t, "a", r.Header.Get("Test-Header-A")) + assert.Equal(t, "b", r.Header.Get("Test-Header-B")) + assert.Equal(t, "c", r.Header.Get("Test-Header-C")) +} |