-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathgearbox_test.go
361 lines (302 loc) · 9.41 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
package gearbox
import (
"bufio"
"bytes"
"io/ioutil"
"net"
"net/http"
"net/http/httputil"
"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)
}
// startGearbox constructs routing tree and creates server
func startGearbox(gb *gearbox) {
gb.cache = newCache(cacheSizeDefault)
gb.constructRoutingTree()
gb.httpServer = &fasthttp.Server{
Handler: gb.handler,
Logger: nil,
LogAllErrors: false,
}
}
// 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) {}
// unAuthorizedHandler sets status unauthorized in response
var unAuthorizedHandler = func(ctx *Context) {
ctx.RequestCtx.SetStatusCode(StatusUnauthorized)
}
// pingHandler returns string pong in response body
var pingHandler = func(ctx *Context) {
ctx.RequestCtx.Response.SetBodyString("pong")
}
// fallbackHandler returns not found status with custom fallback handler in response body
var fallbackHandler = func(ctx *Context) {
ctx.RequestCtx.SetStatusCode(StatusNotFound)
ctx.RequestCtx.Response.SetBodyString("custom fallback handler")
}
// emptyMiddleware does not stop the request and passes it to the next middleware/handler
var emptyMiddleware = func(ctx *Context) {
ctx.Next()
}
// 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)
default:
gb.Method(method, 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: "/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: emptyHandler},
{method: MethodOptions, path: "/user/204/setting", handler: emptyHandler},
{method: MethodTrace, path: "/users/*", handler: emptyHandler},
{method: MethodTrace, path: "/users/test", handler: emptyHandler},
{method: "CUSTOM", path: "/users/test/private", handler: emptyHandler},
}
// get instance of gearbox
gb := setupGearbox(&Settings{
CaseSensitive: 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
body string
}{
{method: MethodGet, path: "/articles/search", statusCode: StatusOK},
{method: MethodPost, path: "/articles/search", statusCode: StatusNotFound},
{method: MethodGet, path: "/articles/searching", statusCode: StatusNotFound},
{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: StatusNotFound},
{method: MethodPost, path: "/articles/206", statusCode: StatusNotFound},
{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},
{method: MethodOptions, path: "/user/204/setting", statusCode: StatusOK},
{method: MethodTrace, path: "/users/testing", statusCode: StatusOK},
{method: "CUSTOM", path: "/users/test/private", 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 string(body) != tc.body {
t.Fatalf("%s(%s): returned %s expected %s", tc.method, tc.path, body, tc.body)
}
}
}
// TestStart tests start service method
func TestStart(t *testing.T) {
gb := New(&Settings{
DisableStartupMessage: true,
})
go func() {
time.Sleep(1000 * time.Millisecond)
gb.Stop()
}()
gb.Start(":3000")
}
// 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")
}
}
// TestStartConflictHandlers tests start with two handlers for the same path and method
func TestStartConflictHandlers(t *testing.T) {
gb := New()
gb.Get("/test", handler)
gb.Get("/test", handler)
go func() {
time.Sleep(1000 * time.Millisecond)
gb.Stop()
}()
if err := gb.Start(":3001"); 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 TestRegisterFallback(t *testing.T) {
// get instance of gearbox
gb := new(gearbox)
gb.registeredRoutes = make([]*Route, 0)
gb.settings = &Settings{}
// register valid route
gb.Get("/ping", pingHandler)
// register our fallback
gb.Fallback(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)
}
}
}
// Test Use function to try to register middlewares that work before all routes
func Test_Use(t *testing.T) {
// get instance of gearbox
gb := new(gearbox)
gb.registeredRoutes = make([]*Route, 0)
gb.settings = &Settings{}
// 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)
}
}