-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
gearbox_test.go
581 lines (486 loc) · 15.3 KB
/
gearbox_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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
package gearbox
import (
"bufio"
"bytes"
"crypto/tls"
"io/ioutil"
"net"
"net/http"
"net/http/httputil"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/valyala/fasthttp"
)
// Mocked conn interface for testing
// https://golang.org/src/net/net.go#L113
type fakeConn struct {
net.Conn
r bytes.Buffer
w bytes.Buffer
}
// Close closes the connection
func (c *fakeConn) Close() error {
return nil
}
// Read reads data from the connection
func (c *fakeConn) Read(b []byte) (int, error) {
return c.r.Read(b)
}
// Write writes data to the connection
func (c *fakeConn) Write(b []byte) (int, error) {
return c.w.Write(b)
}
// setupGearbox returns instace of gearbox struct
func setupGearbox(settings ...*Settings) *gearbox {
gb := new(gearbox)
gb.registeredRoutes = make([]*Route, 0)
if len(settings) > 0 {
gb.settings = settings[0]
} else {
gb.settings = &Settings{}
}
gb.router = &router{
settings: gb.settings,
cache: make(map[string]*matchResult),
pool: sync.Pool{
New: func() interface{} {
return new(context)
},
},
}
return gb
}
// startGearbox constructs routing tree and creates server
func startGearbox(gb *gearbox) {
gb.setupRouter()
gb.httpServer = &fasthttp.Server{
Handler: gb.router.Handler,
Logger: &customLogger{},
LogAllErrors: false,
}
}
// emptyHandler just an empty handler
var emptyHandler = func(ctx Context) {}
// empty Handlers chain is just an empty array
var emptyHandlersChain = handlersChain{}
var fakeHandlersChain = handlersChain{emptyHandler}
// makeRequest makes an http request to http server and returns response or error
func makeRequest(request *http.Request, gb *gearbox) (*http.Response, error) {
// Dump request to send it
dumpRequest, err := httputil.DumpRequest(request, true)
if err != nil {
return nil, err
}
// Write request to the connection
c := &fakeConn{}
if _, err = c.r.Write(dumpRequest); err != nil {
return nil, err
}
// Handling connection
ch := make(chan error)
go func() {
ch <- gb.httpServer.ServeConn(c)
}()
if err = <-ch; err != nil {
return nil, err
}
// Parse response
buffer := bufio.NewReader(&c.w)
resp, err := http.ReadResponse(buffer, request)
if err != nil {
return nil, err
}
return resp, nil
}
// handler just an empty handler
var handler = func(ctx Context) {}
// errorHandler contains buggy code
var errorHandler = func(ctx Context) {
m := make(map[string]int)
m["a"] = 0
ctx.SendString(string(rune(5 / m["a"])))
}
// headerHandler echos header's value of key "my-header"
var headerHandler = func(ctx Context) {
ctx.Set("custom", ctx.Get("my-header"))
}
// queryHandler answers with query's value of key "name"
var queryHandler = func(ctx Context) {
ctx.SendString(ctx.Query("name"))
}
// bodyHandler answers with request body
var bodyHandler = func(ctx Context) {
ctx.Context().Response.SetBodyString(ctx.Body())
}
// unAuthorizedHandler sets status unauthorized in response
var unAuthorizedHandler = func(ctx Context) {
ctx.Status(StatusUnauthorized)
}
// pingHandler returns string pong in response body
var pingHandler = func(ctx Context) {
ctx.SendString("pong")
}
// fallbackHandler returns not found status with custom fallback handler in response body
var fallbackHandler = func(ctx Context) {
ctx.Status(StatusNotFound).SendString("custom fallback handler")
}
// emptyMiddleware does not stop the request and passes it to the next middleware/handler
var emptyMiddleware = func(ctx Context) {
// Try to set user data
ctx.SetLocal("test-key", "value")
ctx.Next()
}
// emptyMiddlewareHandler just an empty handler
var emptyMiddlewareHandler = func(ctx Context) {
data, ok := ctx.GetLocal("test-key").(string)
if !ok || data != "value" {
panic("test-key value is wrong")
}
}
// registerRoute matches with register route request with available methods and calls it
func registerRoute(gb Gearbox, method, path string, handler func(ctx Context)) {
switch method {
case MethodGet:
gb.Get(path, handler)
case MethodHead:
gb.Head(path, handler)
case MethodPost:
gb.Post(path, handler)
case MethodPut:
gb.Put(path, handler)
case MethodPatch:
gb.Patch(path, handler)
case MethodDelete:
gb.Delete(path, handler)
case MethodConnect:
gb.Connect(path, handler)
case MethodOptions:
gb.Options(path, handler)
case MethodTrace:
gb.Trace(path, handler)
}
}
// TestMethods tests creating gearbox instance, registering routes, making
// requests and getting proper responses
func TestMethods(t *testing.T) {
// testing routes
routes := []struct {
method string
path string
handler func(ctx Context)
}{
{method: MethodGet, path: "/order/get", handler: queryHandler},
{method: MethodPost, path: "/order/add", handler: bodyHandler},
{method: MethodGet, path: "/books/find", handler: emptyHandler},
{method: MethodGet, path: "/articles/search", handler: emptyHandler},
{method: MethodPut, path: "/articles/search", handler: emptyHandler},
{method: MethodHead, path: "/articles/test", handler: emptyHandler},
{method: MethodPost, path: "/articles/204", handler: emptyHandler},
{method: MethodPost, path: "/articles/205", handler: unAuthorizedHandler},
{method: MethodGet, path: "/ping", handler: pingHandler},
{method: MethodPut, path: "/posts", handler: emptyHandler},
{method: MethodPatch, path: "/post/502", handler: emptyHandler},
{method: MethodDelete, path: "/post/a23011a", handler: emptyHandler},
{method: MethodConnect, path: "/user/204", handler: headerHandler},
{method: MethodOptions, path: "/user/204/setting", handler: errorHandler},
{method: MethodTrace, path: "/users/*", handler: emptyHandler},
}
// get instance of gearbox
gb := setupGearbox(&Settings{
CaseInSensitive: true,
AutoRecover: true,
HandleOPTIONS: true,
HandleMethodNotAllowed: true,
})
// register routes according to method
for _, r := range routes {
registerRoute(gb, r.method, r.path, r.handler)
}
// start serving
startGearbox(gb)
// Requests that will be tested
testCases := []struct {
method string
path string
statusCode int
requestBody string
body string
headers map[string]string
}{
{method: MethodGet, path: "/order/get?name=art123", statusCode: StatusOK, body: "art123"},
{method: MethodPost, path: "/order/add", requestBody: "testOrder", statusCode: StatusOK, body: "testOrder"},
{method: MethodPost, path: "/books/find", statusCode: StatusMethodNotAllowed, body: "Method Not Allowed", headers: map[string]string{"Allow": "GET, OPTIONS"}},
{method: MethodGet, path: "/articles/search", statusCode: StatusOK},
{method: MethodGet, path: "/articles/search", statusCode: StatusOK},
{method: MethodGet, path: "/Articles/search", statusCode: StatusOK},
{method: MethodOptions, path: "/articles/search", statusCode: StatusOK},
{method: MethodOptions, path: "*", statusCode: StatusOK},
{method: MethodOptions, path: "/*", statusCode: StatusOK},
{method: MethodGet, path: "/articles/searching", statusCode: StatusNotFound, body: "Not Found"},
{method: MethodHead, path: "/articles/test", statusCode: StatusOK},
{method: MethodPost, path: "/articles/204", statusCode: StatusOK},
{method: MethodPost, path: "/articles/205", statusCode: StatusUnauthorized},
{method: MethodPost, path: "/Articles/205", statusCode: StatusUnauthorized},
{method: MethodPost, path: "/articles/206", statusCode: StatusNotFound, body: "Not Found"},
{method: MethodGet, path: "/ping", statusCode: StatusOK, body: "pong"},
{method: MethodPut, path: "/posts", statusCode: StatusOK},
{method: MethodPatch, path: "/post/502", statusCode: StatusOK},
{method: MethodDelete, path: "/post/a23011a", statusCode: StatusOK},
{method: MethodConnect, path: "/user/204", statusCode: StatusOK, headers: map[string]string{"custom": "testing"}},
{method: MethodOptions, path: "/user/204/setting", statusCode: StatusInternalServerError, body: "Internal Server Error"},
{method: MethodTrace, path: "/users/testing", statusCode: StatusOK},
}
for _, tc := range testCases {
// create and make http request
req, _ := http.NewRequest(tc.method, tc.path, strings.NewReader(tc.requestBody))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(tc.requestBody)))
req.Header.Set("my-header", "testing")
response, err := makeRequest(req, gb)
if err != nil {
t.Fatalf("%s(%s): %s", tc.method, tc.path, err.Error())
}
// check status code
if response.StatusCode != tc.statusCode {
t.Fatalf("%s(%s): returned %d expected %d", tc.method, tc.path, response.StatusCode, tc.statusCode)
}
// read body from response
body, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatalf("%s(%s): %s", tc.method, tc.path, err.Error())
}
// check response body
if string(body) != tc.body {
t.Fatalf("%s(%s): returned %s expected %s", tc.method, tc.path, body, tc.body)
}
for expectedKey, expectedValue := range tc.headers {
actualValue := response.Header.Get(expectedKey)
if actualValue != expectedValue {
t.Errorf(" mismatch for route '%s' parameter '%s' actual '%s', expected '%s'",
tc.path, expectedKey, actualValue, expectedValue)
}
}
}
}
func TestStatic(t *testing.T) {
// get instance of gearbox
gb := setupGearbox(&Settings{
CaseInSensitive: true,
AutoRecover: true,
HandleOPTIONS: true,
HandleMethodNotAllowed: true,
})
gb.Static("/static/", "./assets/")
// start serving
startGearbox(gb)
// Requests that will be tested
testCases := []struct {
method string
path string
statusCode int
body string
}{
{method: MethodGet, path: "/static/gearbox.png", statusCode: StatusOK},
}
for _, tc := range testCases {
// create and make http request
req, _ := http.NewRequest(tc.method, tc.path, nil)
response, err := makeRequest(req, gb)
if err != nil {
t.Fatalf("%s(%s): %s", tc.method, tc.path, err.Error())
}
// check status code
if response.StatusCode != tc.statusCode {
t.Fatalf("%s(%s): returned %d expected %d", tc.method, tc.path, response.StatusCode, tc.statusCode)
}
// read body from response
body, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatalf("%s(%s): %s", tc.method, tc.path, err.Error())
}
// check response body
if tc.body != "" && string(body) != tc.body {
t.Fatalf("%s(%s): returned %s expected %s", tc.method, tc.path, body, tc.body)
}
}
}
// TestStartWithPrefork tests start service method
func TestStartWithPrefork(t *testing.T) {
gb := New(&Settings{
Prefork: true,
})
go func() {
time.Sleep(1000 * time.Millisecond)
gb.Stop()
}()
gb.Start(":3000")
}
// TestStart tests start service method
func TestStart(t *testing.T) {
gb := New()
go func() {
time.Sleep(1000 * time.Millisecond)
gb.Stop()
}()
gb.Start(":3010")
}
// TestStartWithTLS tests start service method
func TestStartWithTLS(t *testing.T) {
gb := New(&Settings{
TLSKeyPath: "./assets/ssl-cert-snakeoil.key",
TLSCertPath: "./assets/ssl-cert-snakeoil.crt",
TLSEnabled: true,
})
// use a channel to hand off the error ( if any )
errs := make(chan error, 1)
go func() {
_, err := tls.DialWithDialer(
&net.Dialer{
Timeout: time.Second * 3,
},
"tcp",
"localhost:3050",
&tls.Config{
InsecureSkipVerify: true,
})
errs <- err
gb.Stop()
}()
gb.Start(":3050")
// wait for an error
err := <-errs
if err != nil {
t.Fatalf("StartWithSSL failed to connect with TLS error: %s", err)
}
}
// TestStartInvalidListener tests start with invalid listener
func TestStartInvalidListener(t *testing.T) {
gb := New()
go func() {
time.Sleep(1000 * time.Millisecond)
gb.Stop()
}()
if err := gb.Start("invalid listener"); err == nil {
t.Fatalf("invalid listener passed")
}
}
// TestStop tests stop service method
func TestStop(t *testing.T) {
gb := New()
go func() {
time.Sleep(1000 * time.Millisecond)
gb.Stop()
}()
gb.Start("")
}
// TestRegisterFallback tests router fallback handler
func TestNotFound(t *testing.T) {
// get instance of gearbox
gb := setupGearbox()
// register valid route
gb.Get("/ping", pingHandler)
// register not found handlers
gb.NotFound(fallbackHandler)
// start serving
startGearbox(gb)
// One valid request, one invalid
testCases := []struct {
method string
path string
statusCode int
body string
}{
{method: MethodGet, path: "/ping", statusCode: StatusOK, body: "pong"},
{method: MethodGet, path: "/error", statusCode: StatusNotFound, body: "custom fallback handler"},
}
for _, tc := range testCases {
// create and make http request
req, _ := http.NewRequest(tc.method, tc.path, nil)
response, err := makeRequest(req, gb)
if err != nil {
t.Fatalf("%s(%s): %s", tc.method, tc.path, err.Error())
}
// check status code
if response.StatusCode != tc.statusCode {
t.Fatalf("%s(%s): returned %d expected %d", tc.method, tc.path, response.StatusCode, tc.statusCode)
}
// read body from response
body, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatalf("%s(%s): %s", tc.method, tc.path, err.Error())
}
// check response body
if string(body) != tc.body {
t.Fatalf("%s(%s): returned %s expected %s", tc.method, tc.path, body, tc.body)
}
}
}
// TestGroupRouting tests that you can do group routing
func TestGroupRouting(t *testing.T) {
// create gearbox instance
gb := setupGearbox()
routes := []*Route{
gb.Get("/id", emptyHandler),
gb.Post("/abc", emptyHandler),
gb.Post("/abcd", emptyHandler),
}
gb.Group("/account", gb.Group("/api", routes))
// start serving
startGearbox(gb)
// One valid request, one invalid
testCases := []struct {
method string
path string
statusCode int
body string
}{
{method: MethodGet, path: "/account/api/id", statusCode: StatusOK},
{method: MethodPost, path: "/account/api/abc", statusCode: StatusOK},
{method: MethodPost, path: "/account/api/abcd", statusCode: StatusOK},
{method: MethodGet, path: "/id", statusCode: StatusNotFound, body: "Not Found"},
}
for _, tc := range testCases {
// create and make http request
req, _ := http.NewRequest(tc.method, tc.path, nil)
response, err := makeRequest(req, gb)
if err != nil {
t.Fatalf("%s(%s): %s", tc.method, tc.path, err.Error())
}
// check status code
if response.StatusCode != tc.statusCode {
t.Fatalf("%s(%s): returned %d expected %d", tc.method, tc.path, response.StatusCode, tc.statusCode)
}
// read body from response
body, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatalf("%s(%s): %s", tc.method, tc.path, err.Error())
}
// check response body
if string(body) != tc.body {
t.Fatalf("%s(%s): returned %s expected %s", tc.method, tc.path, body, tc.body)
}
}
}
// TestUse tries to register middlewares that work before all routes
func TestUse(t *testing.T) {
// get instance of gearbox
gb := setupGearbox()
// register valid route
gb.Get("/ping", pingHandler)
// Use authorized middleware for all the application
gb.Use(unAuthorizedHandler)
// start serving
startGearbox(gb)
req, _ := http.NewRequest(MethodGet, "/ping", nil)
response, err := makeRequest(req, gb)
if err != nil {
t.Fatalf("%s(%s): %s", MethodGet, "/ping", err.Error())
}
// check status code
if response.StatusCode != StatusUnauthorized {
t.Fatalf("%s(%s): returned %d expected %d", MethodGet, "/ping", response.StatusCode, StatusUnauthorized)
}
}