-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter_test.go
265 lines (238 loc) · 7.77 KB
/
router_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
package jrpc_test
import (
"context"
"fmt"
"reflect"
"sort"
"testing"
"github.com/goccy/go-json"
"github.com/ananaslegend/jrpc"
)
type handler struct {
method string
handlerFunc func(ctx context.Context) (any, error)
}
var (
subtractHandler = handler{
method: "subtract",
handlerFunc: func(ctx context.Context) (any, error) {
params := jrpc.Params(ctx)
var p [2]int
if err := json.Unmarshal(params, &p); err != nil {
return nil, jrpc.InvalidParamsError()
}
return p[0] - p[1], nil
},
}
namedSubtractHandler = handler{
method: "subtract",
handlerFunc: func(ctx context.Context) (any, error) {
params, err := jrpc.ParamsTo[struct {
Subtrahend int `json:"subtrahend"`
Minuend int `json:"minuend"`
}](ctx)
if err != nil {
return nil, err
}
return params.Minuend - params.Subtrahend, nil
},
}
notificationHandler = handler{
method: "update",
handlerFunc: func(ctx context.Context) (any, error) {
return nil, nil
},
}
sumHandler = handler{
method: "sum",
handlerFunc: func(ctx context.Context) (any, error) {
params := jrpc.Params(ctx)
var p [3]int
if err := json.Unmarshal(params, &p); err != nil {
return nil, jrpc.InvalidParamsError()
}
return p[0] + p[1] + p[2], nil
},
}
notificationHelloHandler = handler{
method: "notify_hello",
handlerFunc: func(ctx context.Context) (any, error) {
return nil, nil
},
}
getDataHandler = handler{
method: "get_data",
handlerFunc: func(ctx context.Context) (any, error) {
return []any{"hello", 5}, nil
},
}
)
// examples from https://www.jsonrpc.org/specification
func Test_JSON_RPC_2_0_Specification_Examples(t *testing.T) {
tests := []struct {
name string
rpcHandlers []handler
request []byte
result []byte
}{
{
name: "rpc call with positional parameters",
rpcHandlers: []handler{subtractHandler},
request: []byte(`{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}`),
result: []byte(`{"jsonrpc": "2.0", "result": 19, "id": 1}`),
},
{
name: "rpc call with positional parameters",
rpcHandlers: []handler{subtractHandler},
request: []byte(`{"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2}`),
result: []byte(`{"jsonrpc": "2.0", "result": -19, "id": 2}`),
},
{
name: "rpc call with named parameters",
rpcHandlers: []handler{namedSubtractHandler},
request: []byte(`{"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3}`),
result: []byte(`{"jsonrpc": "2.0", "result": 19, "id": 3}`),
},
{
name: "rpc call with named parameters",
rpcHandlers: []handler{namedSubtractHandler},
request: []byte(`{"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4}`),
result: []byte(`{"jsonrpc": "2.0", "result": 19, "id": 4}`),
},
{
name: "a notification",
rpcHandlers: []handler{notificationHandler},
request: []byte(`{"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]}`),
result: []byte(``),
},
{
name: "a notification",
rpcHandlers: []handler{notificationHandler},
request: []byte(`{"jsonrpc": "2.0", "method": "foobar"}`),
result: []byte(``),
},
{
name: "rpc call of non-existent method",
rpcHandlers: []handler{subtractHandler},
request: []byte(`{"jsonrpc": "2.0", "method": "foobar", "id": "1"}`),
result: []byte(`{"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "1"}`),
},
{
name: "rpc call with invalid JSON",
rpcHandlers: []handler{subtractHandler},
request: []byte(`{"jsonrpc": "2.0", "method": "foobar, "params": "bar", "baz]`),
result: []byte(`{"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null}`),
},
{
name: "rpc call with invalid Request object",
rpcHandlers: []handler{subtractHandler},
request: []byte(`{"jsonrpc": "2.0", "method": 1, "params": "bar"}`),
result: []byte(`{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}`),
},
{
name: "rpc call Batch, invalid JSON",
rpcHandlers: []handler{subtractHandler},
request: []byte(`[
{"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
{"jsonrpc": "2.0", "method"
]`),
result: []byte(`{"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null}`),
},
{
name: "rpc call with invalid Request object",
rpcHandlers: []handler{subtractHandler},
request: []byte(`[]`),
result: []byte(`{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}`),
},
{
name: "rpc call with an invalid Batch (but not empty)",
rpcHandlers: []handler{subtractHandler},
request: []byte(`[1]`),
result: []byte(`[
{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}
]`),
},
{
name: "rpc call with invalid Batch",
rpcHandlers: []handler{subtractHandler},
request: []byte(`[1,2,3]`),
result: []byte(`[
{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null},
{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null},
{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}
]`),
},
{
name: "rpc call with invalid Request object",
rpcHandlers: []handler{sumHandler, notificationHelloHandler, subtractHandler, getDataHandler},
request: []byte(`[
{"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
{"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
{"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"},
{"foo": "boo"},
{"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"},
{"jsonrpc": "2.0", "method": "get_data", "id": "9"}
]`),
result: []byte(`[
{"jsonrpc": "2.0", "result": 7, "id": "1"},
{"jsonrpc": "2.0", "result": 19, "id": "2"},
{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null},
{"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "5"},
{"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"}
]`),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
router := jrpc.NewRouter()
for _, h := range tt.rpcHandlers {
router.Method(h.method, h.handlerFunc)
}
result := router.Handle(context.Background(), tt.request)
equals, err := resultsEquals(string(result), string(tt.result))
if err != nil {
t.Errorf("error comparing results: %s", err.Error())
}
if !equals {
t.Errorf("got %s, want %s", string(result), string(tt.result))
}
})
}
}
func resultsEquals(actual, expected string) (bool, error) {
if actual == expected {
return true, nil
}
var o1 interface{}
var o2 interface{}
err := json.Unmarshal([]byte(actual), &o1)
if err != nil {
return false, fmt.Errorf("error unmarshalling actual result string: %s", err.Error())
}
err = json.Unmarshal([]byte(expected), &o2)
if err != nil {
return false, fmt.Errorf("error unmarshalling expected result string: %s", err.Error())
}
o1 = sortJSONData(o1)
o2 = sortJSONData(o2)
return reflect.DeepEqual(o1, o2), nil
}
func sortJSONData(data interface{}) interface{} {
switch v := data.(type) {
case []interface{}:
for i := range v {
v[i] = sortJSONData(v[i])
}
sort.SliceStable(v, func(i, j int) bool {
return fmt.Sprintf("%v", v[i]) < fmt.Sprintf("%v", v[j])
})
return v
case map[string]interface{}:
for key := range v {
v[key] = sortJSONData(v[key])
}
return v
default:
return v
}
}