forked from semrush/zenrpc
-
Notifications
You must be signed in to change notification settings - Fork 7
/
handlers_test.go
444 lines (397 loc) · 14.5 KB
/
handlers_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package zenrpc_test
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/gorilla/websocket"
"github.com/vmkteam/zenrpc/v2"
"github.com/vmkteam/zenrpc/v2/testdata"
)
func TestServer_ServeHTTPWithHeaders(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(rpc.ServeHTTP))
defer ts.Close()
var tc = []struct {
h string
s int
}{
{
h: "application/json",
s: 200,
},
{
h: "application/json; charset=utf-8",
s: 200,
},
{
h: "application/text; charset=utf-8",
s: 415,
},
}
for _, c := range tc {
res, err := http.Post(ts.URL, c.h, bytes.NewBufferString(`{"jsonrpc": "2.0", "method": "arith.pi", "id": 2 }`))
if err != nil {
t.Fatal(err)
}
if res.StatusCode != c.s {
t.Errorf("Input: %s\n got %d expected %d", c.h, res.StatusCode, c.s)
}
res.Body.Close()
}
}
func TestServer_ServeHTTP(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(rpc.ServeHTTP))
defer ts.Close()
var tc = []struct {
in, out string
}{
{
in: `{"jsonrpc": "2.0", "method": "arith.divide", "params": { "a": 1, "b": 24 }, "id": 1 }`,
out: `{"jsonrpc":"2.0","id":1,"result":{"Quo":0,"rem":1}}`},
{
in: `{"jsonrpc": "2.0", "method": "arith.divide", "params": [ 1, 24 ], "id": 1 }`,
out: `{"jsonrpc":"2.0","id":1,"result":{"Quo":0,"rem":1}}`},
{
in: `{"jsonrpc": "2.0", "method": "arith.divide", "params": { "a": 1, "b": 0 }, "id": 1 }`,
out: `{"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"divide by zero"}}`},
{
in: `{"jsonrpc": "2.0", "method": "Arith.Divide", "params": { "a": 1, "b": 1 }, "id": "1" }`,
out: `{"jsonrpc":"2.0","id":"1","error":{"code":401,"message":"we do not serve 1"}}`},
{
in: `{"jsonrpc": "2.0", "method": "arith.multiply", "params": { "a": 3, "b": 2 }, "id": 0 }`,
out: `{"jsonrpc":"2.0","id":0,"result":6}`},
{
in: `{"jsonrpc": "2.0", "method": "multiply", "params": { "a": 4, "b": 2 }, "id": 0 }`,
out: `{"jsonrpc":"2.0","id":0,"result":8}`},
{
in: `{"jsonrpc": "2.0", "method": "arith.pow", "params": { "base": 3, "exp": 3 }, "id": 0 }`,
out: `{"jsonrpc":"2.0","id":0,"result":27}`},
{
in: `{"jsonrpc": "2.0", "method": "arith.sum", "params": { "a": 3, "b": 3 }, "id": 1 }`,
out: `{"jsonrpc":"2.0","id":1,"error":{"code":6,"message":"` + ts.Listener.Addr().String() + `"}}`},
{
in: `{"jsonrpc": "2.0", "method": "arith.pow", "params": { "base": 3 }, "id": 0 }`,
out: `{"jsonrpc":"2.0","id":0,"result":9}`},
{
in: `{"jsonrpc": "2.0", "method": "arith.pow", "params": [ 3 ], "id": 0 }`,
out: `{"jsonrpc":"2.0","id":0,"result":9}`},
{
in: `{"jsonrpc": "2.0", "method": "arith.pow", "params": [ 3, 3 ], "id": 0 }`,
out: `{"jsonrpc":"2.0","id":0,"result":27}`},
{
in: `{"jsonrpc": "2.0", "method": "arith.pi", "id": 0 }`,
out: `{"jsonrpc":"2.0","id":0,"result":3.141592653589793}`},
{
in: `{"jsonrpc": "2.0", "method": "arith.checkerror", "id": 0, "params": [ false ] }`,
out: `{"jsonrpc":"2.0","id":0,"result":null}`},
{
in: `{"jsonrpc": "2.0", "method": "arith.checkerror", "id": 0, "params": [ true ] }`,
out: `{"jsonrpc":"2.0","id":0,"error":{"code":-32603,"message":"test"}}`},
{
in: `{"jsonrpc": "2.0", "method": "arith.checkzenrpcerror", "id": 0, "params": [ false ] }`,
out: `{"jsonrpc":"2.0","id":0,"result":null}`},
{
in: `{"jsonrpc": "2.0", "method": "arith.checkzenrpcerror", "id": 0, "params": [ true ] }`,
out: `{"jsonrpc":"2.0","id":0,"error":{"code":500,"message":"test"}}`},
}
for _, c := range tc {
res, err := http.Post(ts.URL, "application/json", bytes.NewBufferString(c.in))
if err != nil {
t.Fatal(err)
}
resp, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
if string(resp) != c.out {
t.Errorf("Input: %s\n got %s expected %s", c.in, resp, c.out)
}
}
}
func TestServer_ServeHTTPNotifications(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(rpc.ServeHTTP))
defer ts.Close()
var tc = []struct {
in, out string
}{
{
in: `{"jsonrpc": "2.0", "method": "arith.divide", "params": { "a": 1, "b": 24 }}`,
out: ``},
{
// should be empty even with error
in: `{"jsonrpc": "2.0", "method": "arith.divide", "params": { "a": 1, "b": 0 }}`,
out: ``},
{
// but parse errors should be displayed
in: `{"jsonrpc": "1.0", "method": "Arith.Divide", "params": { "a": 1, "b": 1 }`,
out: `{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}}`},
{
// in batch requests notifications should not be listed in response
in: `[{"jsonrpc": "2.0", "method": "arith.multiply", "params": { "a": 3, "b": 2 }, "id": 0 },
{"jsonrpc": "2.0", "method": "arith.pow", "params": { "base": 2, "exp": 2 } }]`,
out: `[{"jsonrpc":"2.0","id":0,"result":6}]`},
{
// order doesn't matter
in: `[{"jsonrpc": "2.0", "method": "arith.multiply", "params": { "a": 3, "b": 2 } },
{"jsonrpc": "2.0", "method": "arith.pow", "params": { "base": 2, "exp": 2 }, "id": 0 }]`,
out: `[{"jsonrpc":"2.0","id":0,"result":4}]`},
{
// all notifications
in: `[{"jsonrpc": "2.0", "method": "arith.multiply", "params": { "a": 3, "b": 2 } },
{"jsonrpc": "2.0", "method": "arith.pow", "params": { "base": 2, "exp": 2 }}]`,
out: ``},
}
for _, c := range tc {
res, err := http.Post(ts.URL, "application/json", bytes.NewBufferString(c.in))
if err != nil {
t.Fatal(err)
}
resp, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
if string(resp) != c.out {
t.Errorf("Input: %s\n got %s expected %s", c.in, resp, c.out)
}
}
}
func TestServer_ServeHTTPBatch(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(rpc.ServeHTTP))
defer ts.Close()
var tc = []struct {
in string
out []string
}{
{
// batch requests should process asynchronously, any order in responses accepted
in: `[{"jsonrpc": "2.0", "method": "arith.multiply", "params": { "a": 3, "b": 2 }, "id": 0 },
{"jsonrpc": "2.0", "method": "arith.multiply", "params": { "a": 3, "b": 3 }, "id": 1 },
{"jsonrpc": "2.0", "method": "arith.pow", "params": { "a": 2, "b": 3 } },
{"jsonrpc": "2.0", "method": "arith.pow", "params": { "base": 2, "exp": 2 }, "id": 2 }]`,
out: []string{
`{"jsonrpc":"2.0","id":1,"result":9}`,
`{"jsonrpc":"2.0","id":0,"result":6}`,
`{"jsonrpc":"2.0","id":2,"result":4}`}},
{
// one of the requests errored
in: `[{"jsonrpc": "2.0", "method": "arith.multiply1", "params": { "a": 3, "b": 2 }, "id": 0 },
{"jsonrpc": "2.0", "method": "arith.multiply", "params": { "a": 3, "b": 3 }, "id": 1 },
{"jsonrpc": "2.0", "method": "arith.pow", "params": { "a": 2, "b": 3 } },
{"jsonrpc": "2.0", "method": "arith.pow", "params": { "base": 2, "exp": 2 }, "id": 2 }]`,
out: []string{
`{"jsonrpc":"2.0","id":1,"result":9}`,
`{"jsonrpc":"2.0","id":0,"error":{"code":-32601,"message":"Method not found"}}`,
`{"jsonrpc":"2.0","id":2,"result":4}`}},
{
// to much batch requests
in: `[{"jsonrpc": "2.0", "method": "arith.multiply1", "params": { "a": 3, "b": 2 }, "id": 0 },
{"jsonrpc": "2.0", "method": "arith.multiply", "params": { "a": 3, "b": 3 }, "id": 1 },
{"jsonrpc": "2.0", "method": "arith.pow", "params": { "a": 2, "b": 3 } },
{"jsonrpc": "2.0", "method": "arith.pow", "params": { "a": 2, "b": 3 } },
{"jsonrpc": "2.0", "method": "arith.pow", "params": { "a": 2, "b": 3 } },
{"jsonrpc": "2.0", "method": "arith.pow", "params": { "base": 2, "exp": 2 }, "id": 2 }]`,
out: []string{
`{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid Request","data":"max requests length in batch exceeded"}}`}},
}
for _, c := range tc {
res, err := http.Post(ts.URL, "application/json", bytes.NewBufferString(c.in))
if err != nil {
t.Fatal(err)
}
resp, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
// checking if count of responses is correct
if cnt := strings.Count(string(resp), `"jsonrpc":"2.0"`); len(c.out) != cnt {
t.Errorf("Input: %s\n got %d in batch expected %d", c.in, cnt, len(c.out))
}
// checking every response variant to be in response
for _, check := range c.out {
if !strings.Contains(string(resp), check) {
t.Errorf("Input: %s\n not found %s in batch %s", c.in, check, resp)
}
}
}
}
func TestServer_ServeHTTPWithErrors(t *testing.T) {
rpcHiddenErrorField := zenrpc.NewServer(zenrpc.Options{AllowCORS: true, HideErrorDataField: true})
rpcHiddenErrorField.Register("arith", &testdata.ArithService{})
ts := httptest.NewServer(http.HandlerFunc(rpc.ServeHTTP))
defer ts.Close()
tsHid := httptest.NewServer(http.HandlerFunc(rpcHiddenErrorField.ServeHTTP))
defer tsHid.Close()
var tc = []struct {
url string
in, out string
}{
{
url: ts.URL,
in: `{"jsonrpc": "2.0", "method": "multiple1", "id": 1 }`,
out: `{"jsonrpc":"2.0","id":1,"error":{"code":-32601,"message":"Method not found"}}`},
{
url: ts.URL,
in: `{"jsonrpc": "2.0", "method": "test.multiple1", "id": 1 }`,
out: `{"jsonrpc":"2.0","id":1,"error":{"code":-32601,"message":"Method not found"}}`},
{
url: ts.URL,
in: `{"jsonrpc": "2.0", "method": "foobar, "params": "bar", "baz]`,
out: `{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}}`},
{
url: ts.URL,
in: `{"jsonrpc": "2.0", "params": { "a": 1, "b": 0 }, "id": 1 }`,
out: `{"jsonrpc":"2.0","id":1,"error":{"code":-32600,"message":"Invalid Request"}}`},
{
url: ts.URL,
in: `{"jsonrpc": "2.0", "method": 1, "params": "bar"}`,
out: `{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}}`,
// in spec: {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}
},
{
url: ts.URL,
in: `{"jsonrpc": "2.0", "method": "arith.pow", "params": { "base": "3" }, "id": 0 }`,
out: `{"jsonrpc":"2.0","id":0,"error":{"code":-32602,"message":"Invalid params","data":"json: cannot unmarshal string into Go struct field .base of type float64"}}`,
},
{
url: tsHid.URL,
in: `{"jsonrpc": "2.0", "method": "arith.pow", "params": { "base": "3" }, "id": 0 }`,
out: `{"jsonrpc":"2.0","id":0,"error":{"code":-32602,"message":"Invalid params"}}`,
},
}
for _, c := range tc {
res, err := http.Post(c.url, "application/json", bytes.NewBufferString(c.in))
if err != nil {
t.Fatal(err)
}
resp, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
if string(resp) != c.out {
t.Errorf("Input: %s\n got %s expected %s", c.in, resp, c.out)
}
}
}
func TestServer_Extensions(t *testing.T) {
middleware := func(h zenrpc.InvokeFunc) zenrpc.InvokeFunc {
return func(ctx context.Context, method string, params json.RawMessage) zenrpc.Response {
r := h(ctx, method, params)
// ignore multiply method
if method != testdata.RPC.ArithService.Multiply {
r.Extensions = map[string]interface{}{"debug": "true"}
}
return r
}
}
server := zenrpc.NewServer(zenrpc.Options{AllowCORS: true, HideErrorDataField: true})
server.Register("arith", &testdata.ArithService{})
server.Use(middleware)
ts := httptest.NewServer(http.HandlerFunc(server.ServeHTTP))
defer ts.Close()
var tc = []struct {
url string
in, out string
}{
{
url: ts.URL,
in: `{"jsonrpc": "2.0", "method": "multiple1", "id": 1 }`,
out: `{"jsonrpc":"2.0","id":1,"error":{"code":-32601,"message":"Method not found"}}`},
{
url: ts.URL,
in: `{"jsonrpc": "2.0", "method": "arith.divide", "params": { "a": 1, "b": 24 }}`,
out: ``},
{
url: ts.URL,
in: `{"jsonrpc": "2.0", "method": "arith.divide", "params": { "a": 1, "b": 24 }, "id": 1 }`,
out: `{"jsonrpc":"2.0","id":1,"result":{"Quo":0,"rem":1},"extensions":{"debug":"true"}}`},
{
url: ts.URL,
in: `{"jsonrpc": "2.0", "method": "arith.multiply", "params": { "a": 1, "b": 24 }, "id": 1 }`,
out: `{"jsonrpc":"2.0","id":1,"result":24}`},
{
url: ts.URL,
in: `{"jsonrpc": "2.0", "method": "arith.checkerror", "params": [ true ], "id": 1 }`,
out: `{"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"test"},"extensions":{"debug":"true"}}`},
}
for _, c := range tc {
res, err := http.Post(c.url, "application/json", bytes.NewBufferString(c.in))
if err != nil {
t.Fatal(err)
}
resp, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
if string(resp) != c.out {
t.Errorf("Input: %s\n got %s expected %s", c.in, resp, c.out)
}
}
}
func TestServer_ServeWS(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(rpc.ServeWS))
defer ts.Close()
u, _ := url.Parse(ts.URL)
u.Scheme = "ws"
//nolint:bodyclose
ws, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
t.Fatal(err)
}
defer ws.Close()
var tc = []struct {
in, out string
}{
{
in: `{"jsonrpc": "2.0", "method": "arith.divide", "params": { "a": 1, "b": 24 }, "id": 1 }`,
out: `{"jsonrpc":"2.0","id":1,"result":{"Quo":0,"rem":1}}`},
{
in: `{"jsonrpc": "2.0", "method": "arith.divide", "params": { "a": 1, "b": 0 }, "id": 1 }`,
out: `{"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"divide by zero"}}`},
{
in: `{"jsonrpc": "2.0", "method": "Arith.Divide", "params": { "a": 1, "b": 1 }, "id": "1" }`,
out: `{"jsonrpc":"2.0","id":"1","error":{"code":401,"message":"we do not serve 1"}}`},
{
in: `{"jsonrpc": "2.0", "method": "arith.multiply", "params": { "a": 3, "b": 2 }, "id": 0 }`,
out: `{"jsonrpc":"2.0","id":0,"result":6}`},
{
in: `{"jsonrpc": "2.0", "method": "multiply", "params": { "a": 4, "b": 2 }, "id": 0 }`,
out: `{"jsonrpc":"2.0","id":0,"result":8}`},
{
in: `{"jsonrpc": "2.0", "method": "arith.pow", "params": { "base": 3, "exp": 3 }, "id": 0 }`,
out: `{"jsonrpc":"2.0","id":0,"result":27}`},
{
in: `{"jsonrpc": "2.0", "method": "arith.sum", "params": { "a": 3, "b": 3 }, "id": 1 }`,
out: `{"jsonrpc":"2.0","id":1,"error":{"code":6,"message":"` + ts.Listener.Addr().String() + `"}}`},
{
in: `{"jsonrpc": "2.0", "method": "arith.pow", "params": { "base": 3 }, "id": 0 }`,
out: `{"jsonrpc":"2.0","id":0,"result":9}`},
}
for _, c := range tc {
if err := ws.WriteMessage(websocket.TextMessage, []byte(c.in)); err != nil {
t.Fatal(err)
return
}
_, resp, err := ws.ReadMessage()
if err != nil {
t.Fatal(err)
return
}
if string(resp) != c.out {
t.Errorf("Input: %s\n got %s expected %s", c.in, resp, c.out)
}
}
if err := ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")); err != nil {
t.Fatal(err)
return
}
}