-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpc_test.go
396 lines (349 loc) · 9.24 KB
/
rpc_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
package rpc_test
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"github.com/reddec/rpc"
)
type SomeObj struct {
Hello string
}
type api struct {
t *testing.T
reached string
}
func (api *api) Nothing() {
api.reached = "Nothing"
}
func (api *api) Foo0(ctx context.Context) {
api.reached = "Foo0"
if ctx == nil {
api.t.Error("empty context")
}
}
func (api *api) Foo1(ctx context.Context, bar int, baz SomeObj) {
api.reached = "Foo1"
if ctx == nil {
api.t.Error("empty context")
}
if bar != 123 {
api.t.Error("invalid bar")
}
if baz.Hello != "hello" {
api.t.Error("invalid baz")
}
}
func (api *api) Foo2(bar int, baz string) {
api.reached = "Foo2"
if bar != 123 {
api.t.Error("invalid bar")
}
if baz != "hello" {
api.t.Error("invalid baz")
}
}
func (api *api) Foo3() error {
api.reached = "Foo3"
return nil
}
func (api *api) Foo4() int {
api.reached = "Foo4"
return 123
}
func (api *api) Foo5() (int, error) {
api.reached = "Foo5"
return 0, nil
}
func (api *api) Skipped() (int, int) {
return 0, 0
}
func (api *api) unexported() {
}
func (api *api) Calc(a, b int) int {
api.reached = "Calc"
return a + b
}
func (api *api) Fail() error {
api.reached = "Fail"
return errors.New("fail")
}
func TestIndex(t *testing.T) {
t.Run("skip wrong", func(t *testing.T) {
r := &api{t: t}
index := rpc.Index(r)
if _, ok := index["Skipped"]; ok {
t.Error("should skip wrong method")
}
})
t.Run("skip unexported", func(t *testing.T) {
r := &api{t: t}
index := rpc.Index(r)
if _, ok := index["unexported"]; ok {
t.Error("should skip unexported method")
}
})
t.Run("call no args no return", testReach("Nothing"))
t.Run("call with context", testReach("Foo0"))
t.Run("call with context and args", testReach("Foo1", 123, SomeObj{Hello: "hello"}))
t.Run("call only with args", testReach("Foo2", 123, "hello"))
t.Run("call method which returns error obj", testReach("Foo3"))
t.Run("call method which returns value", testReach("Foo4"))
t.Run("call method which returns value and error obj", testReach("Foo5"))
t.Run("call and check result", func(t *testing.T) {
const method = "Calc"
r := &api{t: t}
index := rpc.Index(r)
handler, ok := index[method]
if !ok {
t.Fatal("method should exists")
}
payload, err := json.Marshal([]interface{}{1, 2})
if err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(payload))
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Error(rec.Code, rec.Body.String())
}
if r.reached != method {
t.Error("not reached method")
}
var result int
if err := json.Unmarshal(rec.Body.Bytes(), &result); err != nil {
t.Error(err)
}
if result != 3 {
t.Error("calculation miss match")
}
})
t.Run("invalid arguments - not json", func(t *testing.T) {
const method = "Calc"
r := &api{t: t}
index := rpc.Index(r)
handler, ok := index[method]
if !ok {
t.Fatal("method should exists")
}
payload := []byte("abcd,123")
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(payload))
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
t.Error("should be 400 bad request", rec.Code, rec.Body.String())
}
})
t.Run("invalid arguments - different types", func(t *testing.T) {
const method = "Calc"
r := &api{t: t}
index := rpc.Index(r)
handler, ok := index[method]
if !ok {
t.Fatal("method should exists")
}
payload, err := json.Marshal([]interface{}{1, "2"})
if err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(payload))
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
t.Error("should be 400 bad request", rec.Code, rec.Body.String())
}
})
t.Run("invalid arguments - not enough", func(t *testing.T) {
const method = "Calc"
r := &api{t: t}
index := rpc.Index(r)
handler, ok := index[method]
if !ok {
t.Fatal("method should exists")
}
payload, err := json.Marshal([]interface{}{1})
if err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(payload))
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
t.Error("should be 400 bad request", rec.Code, rec.Body.String())
}
})
t.Run("treat error as 500", func(t *testing.T) {
const method = "Fail"
r := &api{t: t}
index := rpc.Index(r)
handler, ok := index[method]
if !ok {
t.Fatal("method should exists")
}
payload, err := json.Marshal([]interface{}{1})
if err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(payload))
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusInternalServerError {
t.Error("should be 500", rec.Code, rec.Body.String())
}
if r.reached != method {
t.Error("not reached method")
}
})
}
func testReach(method string, args ...interface{}) func(t *testing.T) {
return func(t *testing.T) {
r := &api{t: t}
index := rpc.Index(r)
handler, ok := index[method]
if !ok {
t.Fatal("method should exists")
}
payload, err := json.Marshal(args)
if err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(payload))
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Error(rec.Code, rec.Body.String())
}
if r.reached != method {
t.Error("not reached method")
}
}
}
func TestRouter(t *testing.T) {
r := &api{t: t}
router := rpc.New(r)
t.Run("plain call should work", func(t *testing.T) {
payload, err := json.Marshal([]interface{}{1, 2})
if err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/calc", bytes.NewReader(payload))
router.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Error(rec.Code, rec.Body.String())
}
var result int
if err := json.Unmarshal(rec.Body.Bytes(), &result); err != nil {
t.Error(err)
}
if result != 3 {
t.Error("calculation miss match")
}
})
t.Run("non-POST method should be blocked", func(t *testing.T) {
payload, err := json.Marshal([]interface{}{1, 2})
if err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPut, "/Calc", bytes.NewReader(payload))
router.ServeHTTP(rec, req)
if rec.Code != http.StatusMethodNotAllowed {
t.Error("should be not allowed", rec.Code, rec.Body.String())
}
})
}
// semi-realistic example to check concept
type userSession struct {
user string
request int64
}
func (us *userSession) Greet() string {
return fmt.Sprint("Hello, ", us.user, "! Your request is ", us.request)
}
type server struct {
requestID int64
}
func (srv *server) newSession(r *http.Request) (*userSession, error) {
user := r.Header.Get("X-User")
if user == "fail" {
return nil, fmt.Errorf("failed")
}
return &userSession{
user: user,
request: atomic.AddInt64(&srv.requestID, 1),
}, nil
}
func TestBuilder(t *testing.T) {
t.Run("straightforward call should work", func(t *testing.T) {
var srv server
handler := rpc.Builder(srv.newSession)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/Greet", bytes.NewBufferString("[]"))
req.Header.Set("X-User", "reddec")
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Error(rec.Code)
}
if strings.TrimSpace(rec.Body.String()) != `"Hello, reddec! Your request is 1"` {
t.Error(rec.Body.String())
}
})
t.Run("case doesn't matter", func(t *testing.T) {
var srv server
handler := rpc.Builder(srv.newSession)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/gReEt", bytes.NewBufferString("[]"))
req.Header.Set("X-User", "reddec")
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Error(rec.Code)
}
if strings.TrimSpace(rec.Body.String()) != `"Hello, reddec! Your request is 1"` {
t.Error(rec.Body.String())
}
})
t.Run("unknown method should return 404", func(t *testing.T) {
var srv server
handler := rpc.Builder(srv.newSession)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/Unknown", bytes.NewBufferString("[]"))
req.Header.Set("X-User", "reddec")
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusNotFound {
t.Error(rec.Code)
}
})
t.Run("only post is allowed", func(t *testing.T) {
var srv server
handler := rpc.Builder(srv.newSession)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPut, "/Greet", bytes.NewBufferString("[]"))
req.Header.Set("X-User", "reddec")
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusMethodNotAllowed {
t.Error(rec.Code)
}
})
t.Run("return 500 if session failed", func(t *testing.T) {
var srv server
handler := rpc.Builder(srv.newSession)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/Greet", bytes.NewBufferString("[]"))
req.Header.Set("X-User", "fail")
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusInternalServerError {
t.Error(rec.Code)
}
if rec.Body.String() != "failed" {
t.Error(rec.Body.String())
}
})
}