This repository has been archived by the owner on Apr 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_test.go
234 lines (222 loc) · 5.5 KB
/
manager_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
package jrpc2go_test
import (
"bytes"
"context"
"io"
"strings"
"testing"
"time"
jrpc "github.com/fabiodcorreia/jrpc2go"
)
type addMethod struct{}
type addMethodParams struct {
V1 int64 `json:"v1"`
V2 int64 `json:"v2"`
}
func (m *addMethod) Execute(req *jrpc.Request, resp *jrpc.Response) {
var p addMethodParams
if err := req.ParseParams(&p); err != nil {
resp.Error = err
return
}
r := p.V1 + p.V2
if r == 20 {
time.Sleep(10 * time.Second) // Simulate timeout
}
if r == 1 {
//To cover the case where the response returns Error and Result
resp.Error = &jrpc.Error{
Code: 1,
Message: "Fake error for test",
}
}
resp.Result = r
}
func TestManagerBuilder_Add(t *testing.T) {
type args struct {
methodName string
method jrpc.Method
wantErr bool
}
tests := []struct {
name string
args args
}{
{
name: "Valid Method",
args: args{
methodName: "add",
method: &addMethod{},
wantErr: false,
},
},
{
name: "Invalid Method Name",
args: args{
methodName: "",
method: &addMethod{},
wantErr: true,
},
},
{
name: "Invalid Method",
args: args{
methodName: "methodX",
method: nil,
wantErr: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
r := recover()
if (r != nil) && !tt.args.wantErr {
t.Errorf("Manager.Add() recover = %v, wantPanic = %v", r, tt.args.wantErr)
}
}()
_ = jrpc.NewManagerBuilder().Add(tt.args.methodName, tt.args.method).Build()
})
}
}
func TestManager(t *testing.T) {
m := jrpc.NewManagerBuilder().
SetTimeout(1*time.Second).
Add("add", &addMethod{}).
Add("sum", &addMethod{}).
Build()
type args struct {
ctx context.Context
r io.Reader
w io.Writer
}
tests := []struct {
name string
args args
wantW string
wantErr bool
}{
{
name: "No Reader",
args: args{
r: nil,
w: &bytes.Buffer{},
},
wantErr: true,
},
{
name: "No Writer",
args: args{
r: bytes.NewReader([]byte(`{"jsonrpc": "2.0","method": "add","id": "1","params": {"v1": 10,"v2": 120}}`)),
w: nil,
},
wantErr: true,
},
{
name: "Valid Request",
args: args{
r: bytes.NewReader([]byte(`{"jsonrpc": "2.0","method": "add","id": "1","params": {"v1": 10,"v2": 120}}`)),
ctx: context.Background(),
w: &bytes.Buffer{},
},
wantW: `{"jsonrpc":"2.0","id":"1","result":130}`,
},
{
name: "Valid Request no Context",
args: args{
r: bytes.NewReader([]byte(`{"jsonrpc": "2.0","method": "add","id": "1","params": {"v1": 10,"v2": 120}}`)),
w: &bytes.Buffer{},
},
wantW: `{"jsonrpc":"2.0","id":"1","result":130}`,
},
{
name: "Valid Batch Request",
args: args{
r: bytes.NewReader([]byte(`[{"jsonrpc":"2.0","method":"add","id":"1","params":{"v1":10,"v2":120}},{"jsonrpc":"2.0","method":"sum","id":"2","params":{"v1":10,"v2":20}}]`)),
ctx: context.Background(),
w: &bytes.Buffer{},
},
wantW: `[{"jsonrpc":"2.0","id":"1","result":130},{"jsonrpc":"2.0","id":"2","result":30}]`,
},
{
name: "Empty Request",
args: args{
r: bytes.NewReader([]byte(``)),
ctx: context.Background(),
w: &bytes.Buffer{},
},
wantErr: true,
},
{
name: "Empty Request Object",
args: args{
r: bytes.NewReader([]byte(`{}`)),
ctx: context.Background(),
w: &bytes.Buffer{},
},
wantW: `{"jsonrpc":"","id":null,"error":{"code":-32001,"message":"JSON RPC Version must be 2.0","data":""}}`,
},
{
name: "Empty Batch Request Object",
args: args{
r: bytes.NewReader([]byte(`[]`)),
ctx: context.Background(),
w: &bytes.Buffer{},
},
wantErr: true,
},
{
name: "Method Not Found",
args: args{
r: bytes.NewReader([]byte(`{"jsonrpc": "2.0","method": "no-method","id": "1","params": {"v1": 10,"v2": 1}}`)),
ctx: context.Background(),
w: &bytes.Buffer{},
},
wantW: `{"jsonrpc":"2.0","id":"1","error":{"code":-32601,"message":"Method not found","data":"no-method"}}`,
},
{
name: "No Method Found",
args: args{
r: bytes.NewReader([]byte(`{"jsonrpc": "2.0","id": "1","params": {"v1": 10,"v2": 1}}`)),
ctx: context.Background(),
w: &bytes.Buffer{},
},
wantW: `{"jsonrpc":"2.0","id":"1","error":{"code":-32601,"message":"Method not found","data":"Method not specified or empty"}}`,
},
{
name: "Request Timeout",
args: args{
r: bytes.NewReader([]byte(`{"jsonrpc": "2.0","method": "sum","id": "1","params": {"v1": 10,"v2": 10}}`)),
ctx: context.Background(),
w: &bytes.Buffer{},
},
wantW: `{"jsonrpc":"2.0","id":"1","error":{"code":-32002,"message":"Method execution timeout"}}`,
},
{
name: "Response Error with Result",
args: args{
r: bytes.NewReader([]byte(`{"jsonrpc": "2.0","method": "sum","id": "1","params": {"v1": 0,"v2": 1}}`)),
ctx: context.Background(),
w: &bytes.Buffer{},
},
wantW: `{"jsonrpc":"2.0","id":"1","error":{"code":1,"message":"Fake error for test"}}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := m.Handle(tt.args.ctx, tt.args.r, tt.args.w)
if err != nil && !tt.wantErr {
t.Errorf("Manager.Handle() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err != nil {
//TODO check the error
return //Skip for now
}
wt := tt.args.w.(*bytes.Buffer)
if gotW := wt.String(); strings.Compare(strings.TrimSpace(gotW), tt.wantW) != 0 {
t.Errorf("Manager.Handle() result = '%v', want %v", gotW, tt.wantW)
}
})
}
}