-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver_test.go
386 lines (309 loc) · 11 KB
/
server_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
package jrpc
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestServerPrimitiveTypes(t *testing.T) {
s := NewServer("/v1/cmd")
type respData struct {
Res1 string
Res2 bool
}
s.Add("test", func(id uint64, params json.RawMessage) Response {
var args []interface{}
if err := json.Unmarshal(params, &args); err != nil {
return Response{Error: err.Error()}
}
t.Logf("%+v", args)
assert.Equal(t, 3, len(args))
assert.Equal(t, "blah", args[0].(string))
assert.Equal(t, 42., args[1].(float64))
assert.Equal(t, true, args[2].(bool))
return EncodeResponse(id, respData{"res blah", true}, nil)
})
go func() { _ = s.Run(9091) }()
defer func() { assert.NoError(t, s.Shutdown()) }()
time.Sleep(10 * time.Millisecond)
// check with direct http call
clientReq := Request{Method: "test", Params: []interface{}{"blah", 42, true}, ID: 123}
b := bytes.Buffer{}
require.NoError(t, json.NewEncoder(&b).Encode(clientReq))
resp, err := http.Post("http://127.0.0.1:9091/v1/cmd", "application/json", &b)
require.NoError(t, err)
defer func() { _ = resp.Body.Close() }()
assert.Equal(t, 200, resp.StatusCode)
data, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, `{"result":{"Res1":"res blah","Res2":true},"id":123}`+"\n", string(data))
// check with client call
c := Client{API: "http://127.0.0.1:9091/v1/cmd", Client: http.Client{}}
r, err := c.Call("test", "blah", 42, true)
assert.NoError(t, err)
assert.Equal(t, "", r.Error)
res := respData{}
err = json.Unmarshal(*r.Result, &res)
assert.NoError(t, err)
assert.Equal(t, respData{Res1: "res blah", Res2: true}, res)
assert.Equal(t, uint64(1), r.ID)
}
func TestServerWithObject(t *testing.T) {
s := NewServer("/v1/cmd")
type respData struct {
Res1 string
Res2 bool
}
type reqData struct {
Time time.Time
F1 string
F2 time.Duration
}
s.Add("test", func(id uint64, params json.RawMessage) Response {
arg := reqData{}
if err := json.Unmarshal(params, &arg); err != nil {
return Response{Error: err.Error()}
}
t.Logf("%+v", arg)
return EncodeResponse(id, respData{"res blah", true}, nil)
})
go func() { _ = s.Run(9091) }()
defer func() { assert.NoError(t, s.Shutdown()) }()
time.Sleep(10 * time.Millisecond)
c := Client{API: "http://127.0.0.1:9091/v1/cmd", Client: http.Client{}}
r, err := c.Call("test", reqData{Time: time.Now(), F1: "sawert", F2: time.Minute})
assert.NoError(t, err)
assert.Equal(t, "", r.Error)
res := respData{}
err = json.Unmarshal(*r.Result, &res)
assert.NoError(t, err)
assert.Equal(t, respData{Res1: "res blah", Res2: true}, res)
}
func TestServerMethodNotImplemented(t *testing.T) {
s := NewServer("")
ts := httptest.NewServer(http.HandlerFunc(s.handler))
defer ts.Close()
s.Add("test", func(_ uint64, params json.RawMessage) Response {
return Response{}
})
r := Request{Method: "blah"}
buf := bytes.Buffer{}
assert.NoError(t, json.NewEncoder(&buf).Encode(r))
resp, err := http.Post(ts.URL, "application/json", &buf)
require.NoError(t, err)
require.NoError(t, resp.Body.Close())
assert.Equal(t, http.StatusNotImplemented, resp.StatusCode)
assert.EqualError(t, s.Shutdown(), "http server is not running")
}
func TestServerWithAuth(t *testing.T) {
s := NewServer("/v1/cmd", Auth("user", "passwd"))
s.Add("test", func(id uint64, params json.RawMessage) Response {
var args []interface{}
if err := json.Unmarshal(params, &args); err != nil {
return Response{Error: err.Error()}
}
t.Logf("%+v", args)
assert.Equal(t, 3, len(args))
assert.Equal(t, "blah", args[0].(string))
assert.Equal(t, 42., args[1].(float64))
assert.Equal(t, true, args[2].(bool))
return EncodeResponse(id, "res blah", nil)
})
go func() { _ = s.Run(9091) }()
time.Sleep(10 * time.Millisecond)
defer func() { assert.NoError(t, s.Shutdown()) }()
c := Client{API: "http://127.0.0.1:9091/v1/cmd", Client: http.Client{}, AuthUser: "user", AuthPasswd: "passwd"}
r, err := c.Call("test", "blah", 42, true)
assert.NoError(t, err)
assert.Equal(t, "", r.Error)
val := ""
err = json.Unmarshal(*r.Result, &val)
assert.NoError(t, err)
assert.Equal(t, "res blah", val)
c = Client{API: "http://127.0.0.1:9091/v1/cmd", Client: http.Client{}}
_, err = c.Call("test", "blah", 42, true)
assert.EqualError(t, err, "bad status 401 Unauthorized for test")
}
func TestServerErrReturn(t *testing.T) {
s := NewServer("/v1/cmd", Auth("user", "passwd"))
s.Add("test", func(id uint64, params json.RawMessage) Response {
var args []interface{}
if err := json.Unmarshal(params, &args); err != nil {
return Response{Error: err.Error()}
}
t.Logf("%+v", args)
assert.Equal(t, 3, len(args))
assert.Equal(t, "blah", args[0].(string))
assert.Equal(t, 42., args[1].(float64))
assert.Equal(t, true, args[2].(bool))
return EncodeResponse(id, "res blah", fmt.Errorf("some error"))
})
go func() { _ = s.Run(9091) }()
defer func() { assert.NoError(t, s.Shutdown()) }()
time.Sleep(10 * time.Millisecond)
c := Client{API: "http://127.0.0.1:9091/v1/cmd", Client: http.Client{}, AuthUser: "user", AuthPasswd: "passwd"}
_, err := c.Call("test", "blah", 42, true)
assert.EqualError(t, err, "some error")
}
func TestServerGroup(t *testing.T) {
s := NewServer("/v1/cmd")
s.Group("pre", HandlersGroup{
"fn1": func(uint64, json.RawMessage) Response {
return Response{}
},
"fn2": func(uint64, json.RawMessage) Response {
return Response{}
},
})
go func() { _ = s.Run(9091) }()
defer func() { assert.NoError(t, s.Shutdown()) }()
time.Sleep(10 * time.Millisecond)
c := Client{API: "http://127.0.0.1:9091/v1/cmd", Client: http.Client{}}
_, err := c.Call("fn1")
assert.EqualError(t, err, "bad status 501 Not Implemented for fn1")
_, err = c.Call("pre.fn1")
assert.NoError(t, err)
_, err = c.Call("pre.fn2")
assert.NoError(t, err)
}
func TestServerAddLate(t *testing.T) {
s := NewServer("/v1/cmd")
s.Add("fn1", func(id uint64, params json.RawMessage) Response {
return Response{}
})
go func() { _ = s.Run(9091) }()
defer func() { assert.NoError(t, s.Shutdown()) }()
time.Sleep(10 * time.Millisecond)
// too late, ignored after run
s.Add("fn2", func(id uint64, params json.RawMessage) Response {
return Response{}
})
c := Client{API: "http://127.0.0.1:9091/v1/cmd", Client: http.Client{}}
_, err := c.Call("fn1")
assert.NoError(t, err)
_, err = c.Call("fn2")
assert.EqualError(t, err, "bad status 501 Not Implemented for fn2")
}
func TestServerNoHandlers(t *testing.T) {
s := NewServer("/v1/cmd", Auth("user", "passwd"))
assert.EqualError(t, s.Run(9091), "nothing mapped for dispatch, Add has to be called prior to Run")
}
func TestServer_getDefaultTimeouts(t *testing.T) {
s := NewServer("")
s.timeouts = getDefaultTimeouts()
assert.Equal(t, Timeouts{ReadHeaderTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, IdleTimeout: 5 * time.Second}, s.timeouts)
// test with custom timeouts
s = NewServer("",
WithTimeouts(Timeouts{ReadHeaderTimeout: 1 * time.Second, WriteTimeout: 4 * time.Second, IdleTimeout: 2 * time.Second}))
assert.Equal(t, Timeouts{ReadHeaderTimeout: 1 * time.Second, WriteTimeout: 4 * time.Second, IdleTimeout: 2 * time.Second}, s.timeouts)
}
func TestServer_WithLimits(t *testing.T) {
s := NewServer("")
assert.Equal(t, limits{}, s.limits)
// test with custom limit
s = NewServer("",
WithLimits(100))
assert.Equal(t, limits{clientLimit: 100}, s.limits)
}
func TestServer_WithThrottler(t *testing.T) {
s := NewServer("")
assert.Equal(t, limits{}, s.limits)
// test with custom limit
s = NewServer("",
WithThrottler(200))
assert.Equal(t, limits{serverThrottle: 200}, s.limits)
}
func TestServer_WithSignature(t *testing.T) {
checkSignatureMiddlewareFn := func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "", w.Header().Get("App-Name"))
assert.Equal(t, "", w.Header().Get("App-Version"))
assert.Equal(t, "", w.Header().Get("Author"))
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
s := NewServer("/v1/cmd", WithMiddlewares(checkSignatureMiddlewareFn))
s.Add("fn1", func(id uint64, params json.RawMessage) Response {
return Response{}
})
go func() { _ = s.Run(9091) }()
time.Sleep(10 * time.Millisecond)
c := Client{API: "http://127.0.0.1:9091/v1/cmd", Client: http.Client{}}
_, err := c.Call("fn1")
assert.NoError(t, err)
assert.NoError(t, s.Shutdown())
time.Sleep(10 * time.Millisecond)
// checking signature with server response
checkSignatureMiddlewareFn = func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "testApp", w.Header().Get("App-Name"))
assert.Equal(t, "testAuthor", w.Header().Get("Author"))
assert.Equal(t, "0.1.0", w.Header().Get("App-Version"))
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
s = NewServer("/v1/cmd", WithSignature("testApp", "testAuthor", "0.1.0"), WithMiddlewares(checkSignatureMiddlewareFn))
assert.Equal(t, signaturePayload{"testApp", "testAuthor", "0.1.0"}, s.signature)
s.Add("fn1", func(id uint64, params json.RawMessage) Response {
return Response{}
})
go func() { _ = s.Run(9091) }()
defer func() { assert.NoError(t, s.Shutdown()) }()
time.Sleep(10 * time.Millisecond)
c = Client{API: "http://127.0.0.1:9091/v1/cmd", Client: http.Client{}}
_, err = c.Call("fn1")
assert.NoError(t, err)
}
func TestServerCustomMiddlewares(t *testing.T) {
testMiddlewareGetPathFn := func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, "POST")
assert.Equal(t, r.URL.Path, "/v1/cmd")
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
testMiddlewareGetValueFn := func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, "POST")
assert.Equal(t, r.URL.Path, "/v1/cmd")
value, isValue := r.URL.Query()["value"]
assert.True(t, isValue)
assert.Equal(t, value[0], "test")
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
s := NewServer("/v1/cmd", WithMiddlewares(testMiddlewareGetPathFn, testMiddlewareGetValueFn))
s.Add("fn1", func(id uint64, params json.RawMessage) Response {
return Response{}
})
go func() { _ = s.Run(9091) }()
time.Sleep(10 * time.Millisecond)
defer func() { assert.NoError(t, s.Shutdown()) }()
c := Client{API: "http://127.0.0.1:9091/v1/cmd?value=test", Client: http.Client{}}
_, err := c.Call("fn1")
assert.NoError(t, err)
time.Sleep(10 * time.Millisecond)
}
func TestServer_WithLogger(t *testing.T) {
s := NewServer("")
assert.Equal(t, reflect.TypeOf(s.logger), reflect.TypeOf(NoOpLogger))
s = NewServer("", WithLogger(testLogger{}))
assert.Equal(t, reflect.TypeOf(s.logger), reflect.TypeOf(testLogger{}))
}
type testLogger struct{}
func (l testLogger) Logf(format string, args ...interface{}) {}