diff --git a/handler_func_test.go b/handler_func_test.go index 2f9bbbd..4149874 100644 --- a/handler_func_test.go +++ b/handler_func_test.go @@ -16,7 +16,7 @@ type handler struct { F func(c context.Context, params *json.RawMessage) (interface{}, *Error) } -func (h handler) ServeJSONRPC(c context.Context, params *json.RawMessage) (interface{}, *Error) { +func (h *handler) ServeJSONRPC(c context.Context, params *json.RawMessage) (interface{}, *Error) { return h.F(c, params) } @@ -47,11 +47,16 @@ func TestHandler(t *testing.T) { require.NoError(t, err) assert.NotNil(t, res.Error) - h := handler{} - h.F = func(c context.Context, params *json.RawMessage) (interface{}, *Error) { + h1 := &handler{} + h1.F = func(c context.Context, params *json.RawMessage) (interface{}, *Error) { return "hello", nil } - require.NoError(t, RegisterMethod("hello", h, nil, nil)) + require.NoError(t, RegisterMethod("hello", h1, nil, nil)) + h2 := &handler{} + h2.F = func(c context.Context, params *json.RawMessage) (interface{}, *Error) { + return nil, ErrInternal() + } + require.NoError(t, RegisterMethod("bye", h2, nil, nil)) rec = httptest.NewRecorder() r, err = http.NewRequest("", "", bytes.NewReader([]byte(`{"jsonrpc":"2.0","id":"test","method":"hello","params":{}}`))) @@ -64,4 +69,15 @@ func TestHandler(t *testing.T) { require.NoError(t, err) assert.Nil(t, res.Error) assert.Equal(t, "hello", res.Result) + + rec = httptest.NewRecorder() + r, err = http.NewRequest("", "", bytes.NewReader([]byte(`{"jsonrpc":"2.0","id":"test","method":"bye","params":{}}`))) + require.NoError(t, err) + r.Header.Set("Content-Type", "application/json") + + HandlerFunc(c, rec, r) + res = Response{} + err = json.NewDecoder(rec.Body).Decode(&res) + require.NoError(t, err) + assert.NotNil(t, res.Error) } diff --git a/method_test.go b/method_test.go index 2b7ef97..f69f861 100644 --- a/method_test.go +++ b/method_test.go @@ -60,5 +60,5 @@ func SampleHandler() Handler { h.F = func(c context.Context, params *json.RawMessage) (result interface{}, err *Error) { return nil, nil } - return h + return &h }