Skip to content

Commit 787a96a

Browse files
authored
Merge pull request #94 from filecoin-project/fix/handle-no-req-params
fix: Handle missing params field correctly
2 parents 97bec81 + e8cd034 commit 787a96a

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

handler.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,13 @@ func (s *handler) handle(ctx context.Context, req request, w func(func(io.Writer
338338
// "normal" param list; no good way to do named params in Golang
339339

340340
var ps []param
341-
err := json.Unmarshal(req.Params, &ps)
342-
if err != nil {
343-
rpcError(w, &req, rpcParseError, xerrors.Errorf("unmarshaling param array: %w", err))
344-
stats.Record(ctx, metrics.RPCRequestError.M(1))
345-
return
341+
if len(req.Params) > 0 {
342+
err := json.Unmarshal(req.Params, &ps)
343+
if err != nil {
344+
rpcError(w, &req, rpcParseError, xerrors.Errorf("unmarshaling param array: %w", err))
345+
stats.Record(ctx, metrics.RPCRequestError.M(1))
346+
return
347+
}
346348
}
347349

348350
if len(ps) != handler.nParams {

rpc_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"io/ioutil"
1010
"net"
11+
"net/http"
1112
"net/http/httptest"
1213
"reflect"
1314
"strconv"
@@ -45,6 +46,12 @@ type TestOut struct {
4546
Ok bool
4647
}
4748

49+
func (h *SimpleServerHandler) Inc() error {
50+
h.n++
51+
52+
return nil
53+
}
54+
4855
func (h *SimpleServerHandler) Add(in int) error {
4956
if in == -3546 {
5057
return errors.New("test")
@@ -72,6 +79,37 @@ func (h *SimpleServerHandler) StringMatch(t TestType, i2 int64) (out TestOut, er
7279
return
7380
}
7481

82+
func TestRawRequests(t *testing.T) {
83+
rpcHandler := SimpleServerHandler{}
84+
85+
rpcServer := NewServer()
86+
rpcServer.Register("SimpleServerHandler", &rpcHandler)
87+
88+
testServ := httptest.NewServer(rpcServer)
89+
defer testServ.Close()
90+
91+
tc := func(req, resp string, n int) func(t *testing.T) {
92+
return func(t *testing.T) {
93+
rpcHandler.n = 0
94+
95+
res, err := http.Post(testServ.URL, "application/json", strings.NewReader(req))
96+
require.NoError(t, err)
97+
98+
b, err := ioutil.ReadAll(res.Body)
99+
require.NoError(t, err)
100+
101+
assert.Equal(t, resp, strings.TrimSpace(string(b)))
102+
require.Equal(t, n, rpcHandler.n)
103+
}
104+
}
105+
106+
t.Run("inc", tc(`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Inc", "params": [], "id": 1}`, `{"jsonrpc":"2.0","id":1}`, 1))
107+
t.Run("inc-null", tc(`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Inc", "params": null, "id": 1}`, `{"jsonrpc":"2.0","id":1}`, 1))
108+
t.Run("inc-noparam", tc(`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Inc", "id": 2}`, `{"jsonrpc":"2.0","id":2}`, 1))
109+
t.Run("add", tc(`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [10], "id": 4}`, `{"jsonrpc":"2.0","id":4}`, 10))
110+
111+
}
112+
75113
func TestReconnection(t *testing.T) {
76114
var rpcClient struct {
77115
Add func(int) error

0 commit comments

Comments
 (0)