From 6215c9159acb85033616d2937edf3d87ef7ca79b Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 28 Aug 2017 11:27:18 -0500 Subject: add plugin http handler (#7289) --- plugin/rpcplugin/hooks_test.go | 62 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 4 deletions(-) (limited to 'plugin/rpcplugin/hooks_test.go') diff --git a/plugin/rpcplugin/hooks_test.go b/plugin/rpcplugin/hooks_test.go index 6cd7ff547..eb684956b 100644 --- a/plugin/rpcplugin/hooks_test.go +++ b/plugin/rpcplugin/hooks_test.go @@ -2,6 +2,10 @@ package rpcplugin import ( "io" + "io/ioutil" + "net/http" + "net/http/httptest" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -45,6 +49,31 @@ func TestHooks(t *testing.T) { hooks.On("OnDeactivate").Return(nil) assert.NoError(t, remote.OnDeactivate()) + + hooks.On("ServeHTTP", mock.AnythingOfType("*rpcplugin.RemoteHTTPResponseWriter"), mock.AnythingOfType("*http.Request")).Run(func(args mock.Arguments) { + w := args.Get(0).(http.ResponseWriter) + r := args.Get(1).(*http.Request) + assert.Equal(t, "/foo", r.URL.Path) + assert.Equal(t, "POST", r.Method) + body, err := ioutil.ReadAll(r.Body) + assert.NoError(t, err) + assert.Equal(t, "asdf", string(body)) + assert.Equal(t, "header", r.Header.Get("Test-Header")) + w.Write([]byte("bar")) + }) + + w := httptest.NewRecorder() + r, err := http.NewRequest("POST", "/foo", strings.NewReader("asdf")) + r.Header.Set("Test-Header", "header") + assert.NoError(t, err) + remote.ServeHTTP(w, r) + + resp := w.Result() + defer resp.Body.Close() + assert.Equal(t, http.StatusOK, resp.StatusCode) + body, err := ioutil.ReadAll(resp.Body) + assert.NoError(t, err) + assert.Equal(t, "bar", string(body)) })) } @@ -73,9 +102,18 @@ func TestHooks_PartiallyImplemented(t *testing.T) { })) } -func BenchmarkOnDeactivate(b *testing.B) { - var hooks plugintest.Hooks - hooks.On("OnDeactivate").Return(nil) +type benchmarkHooks struct{} + +func (*benchmarkHooks) OnDeactivate() error { return nil } + +func (*benchmarkHooks) ServeHTTP(w http.ResponseWriter, r *http.Request) { + ioutil.ReadAll(r.Body) + w.Header().Set("Foo-Header", "foo") + http.Error(w, "foo", http.StatusBadRequest) +} + +func BenchmarkHooks_OnDeactivate(b *testing.B) { + var hooks benchmarkHooks if err := testHooksRPC(&hooks, func(remote *RemoteHooks) { b.ResetTimer() @@ -88,7 +126,23 @@ func BenchmarkOnDeactivate(b *testing.B) { } } -func BenchmarkOnDeactivate_Unimplemented(b *testing.B) { +func BenchmarkHooks_ServeHTTP(b *testing.B) { + var hooks benchmarkHooks + + if err := testHooksRPC(&hooks, func(remote *RemoteHooks) { + b.ResetTimer() + for n := 0; n < b.N; n++ { + w := httptest.NewRecorder() + r, _ := http.NewRequest("POST", "/foo", strings.NewReader("12345678901234567890")) + remote.ServeHTTP(w, r) + } + b.StopTimer() + }); err != nil { + b.Fatal(err.Error()) + } +} + +func BenchmarkHooks_Unimplemented(b *testing.B) { var hooks testHooks if err := testHooksRPC(&hooks, func(remote *RemoteHooks) { -- cgit v1.2.3-1-g7c22