-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope_test.go
337 lines (319 loc) · 7.8 KB
/
scope_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
package celerity
import (
"fmt"
"net/http"
"testing"
"github.com/spf13/afero"
)
func emptyHandler(c Context) Response {
return c.Respond(nil)
}
func TestScopeRoute(t *testing.T) {
{
req, _ := http.NewRequest(GET, "/users", nil)
scope := newScope("/")
scope.Route(GET, "/users", emptyHandler)
if !scope.Match(req, req.URL.Path) {
t.Error("scope did not match valid path")
}
}
{
req, _ := http.NewRequest(GET, "/bad", nil)
scope := newScope("/")
scope.Route(GET, "/users", emptyHandler)
if scope.Match(req, req.URL.Path) {
t.Error("scope did match invalid path")
}
}
{
scope := newScope("/")
sub := scope.Scope("/users")
sub.Route(GET, "/:id", emptyHandler)
{
req, _ := http.NewRequest(GET, "/users/1", nil)
if !scope.Match(req, req.URL.Path) {
t.Error("scope did not match valid path")
}
}
{
req, _ := http.NewRequest(GET, "/users/1/abd", nil)
if scope.Match(req, req.URL.Path) {
t.Error("scope did match invalid path")
}
}
}
}
func TestScopeHandle(t *testing.T) {
{
scope := newScope("/")
scope.Route("GET", "/users", func(c Context) Response {
return c.R("test")
})
req, _ := http.NewRequest(GET, "/users", nil)
c := RequestContext(req)
r := scope.Handle(c)
v, ok := r.Data.(string)
if !ok {
t.Error("Data result incorrect type")
return
} else if v != "test" {
t.Error("Data result not correct")
}
}
{
scope := newScope("/")
scope.Route("GET", "/users", func(c Context) Response {
return c.R("test")
})
req, _ := http.NewRequest(GET, "/notfound", nil)
c := RequestContext(req)
r := scope.Handle(c)
if r.StatusCode != 404 {
t.Error("status code should be 404 for not found path")
}
}
}
func TestMethodRouting(t *testing.T) {
{
scope := newScope("/")
scope.Route(GET, "/users", func(c Context) Response {
return c.R("test")
})
req, _ := http.NewRequest(GET, "/users", nil)
c := RequestContext(req)
r := scope.Handle(c)
if r.StatusCode != 200 {
t.Error("Non 200 response code for valid method/path")
}
}
{
scope := newScope("/")
scope.Route(POST, "/users", func(c Context) Response {
return c.R("test")
})
req, _ := http.NewRequest(GET, "/users", nil)
c := RequestContext(req)
r := scope.Handle(c)
if r.StatusCode != 404 {
t.Error("Non 404 response code for invalid method/path")
}
}
}
func TestMethodAliases(t *testing.T) {
t.Run("GET", func(t *testing.T) {
scope := newScope("/")
scope.GET("/get", func(c Context) Response {
return c.R("test")
})
req, _ := http.NewRequest(GET, "/get", nil)
c := RequestContext(req)
r := scope.Handle(c)
if r.StatusCode != 200 {
t.Error("Non 200 response code for valid method/path")
}
})
t.Run("PUT", func(t *testing.T) {
scope := newScope("/")
scope.PUT("/put", func(c Context) Response {
return c.R("test")
})
req, _ := http.NewRequest(PUT, "/put", nil)
c := RequestContext(req)
r := scope.Handle(c)
if r.StatusCode != 200 {
t.Error("Non 200 response code for valid method/path")
}
})
t.Run("DELETE", func(t *testing.T) {
scope := newScope("/")
scope.DELETE("/delete", func(c Context) Response {
return c.R("test")
})
req, _ := http.NewRequest(DELETE, "/delete", nil)
c := RequestContext(req)
r := scope.Handle(c)
if r.StatusCode != 200 {
t.Error("Non 200 response code for valid method/path")
}
})
t.Run("PATCH", func(t *testing.T) {
scope := newScope("/")
scope.PATCH("/patch", func(c Context) Response {
return c.R("test")
})
req, _ := http.NewRequest(PATCH, "/patch", nil)
c := RequestContext(req)
r := scope.Handle(c)
if r.StatusCode != 200 {
t.Error("Non 200 response code for valid method/path")
}
})
t.Run("POST", func(t *testing.T) {
scope := newScope("/")
scope.POST("/post", func(c Context) Response {
return c.R("test")
})
req, _ := http.NewRequest(POST, "/post", nil)
c := RequestContext(req)
r := scope.Handle(c)
if r.StatusCode != 200 {
t.Error("Non 200 response code for valid method/path")
}
})
}
func BenchmarkScopeRoute(b *testing.B) {
root := newScope("/")
scope := root
for n := 0; n < 10; n++ {
scope = root.Scope(fmt.Sprintf("/%d", n))
}
scope.Route("GET", "ep", emptyHandler)
for n := 0; n < b.N; n++ {
req, _ := http.NewRequest(GET, "/0/1/2/3/4/5/6/7/8/9/ep", nil)
scope.Match(req, req.URL.Path)
}
}
func TestScopeCollision(t *testing.T) {
root := newScope("/")
ss1 := root.Scope("/")
ss2 := root.Scope("/")
users := ss1.Scope("/users")
ss2.Scope("/users")
users.Route(GET, "/get", func(c Context) Response {
return c.R("test")
})
req, _ := http.NewRequest(GET, "/users/get", nil)
c := RequestContext(req)
r := root.Handle(c)
v, ok := r.Data.(string)
if !ok {
t.Error("Data result incorrect type")
return
} else if v != "test" {
t.Error("Data result not correct")
}
}
func TestMiddleware(t *testing.T) {
root := newScope("/")
root.Use(func(next RouteHandler) RouteHandler {
return func(c Context) Response {
c.Set("prop", "123")
return next(c)
}
})
t.Run("root middleware", func(t *testing.T) {
root.Route(GET, "/middleware", func(c Context) Response {
if v, ok := c.Get("prop").(string); ok {
return c.R(v)
}
return c.R("bad")
})
req, _ := http.NewRequest(GET, "/middleware", nil)
c := RequestContext(req)
r := root.Handle(c)
v, ok := r.Data.(string)
if !ok {
t.Error("Data result incorrect type")
return
} else if v != "123" {
t.Error("Data result not correct")
}
})
t.Run("middelware inheritance", func(t *testing.T) {
sub := root.Scope("/sub")
sub.Route(GET, "/middleware", func(c Context) Response {
if v, ok := c.Get("prop").(string); ok {
return c.R(v)
}
return c.R("bad")
})
req, _ := http.NewRequest(GET, "/sub/middleware", nil)
c := RequestContext(req)
r := root.Handle(c)
v, ok := r.Data.(string)
if !ok {
t.Error("Data result incorrect type")
return
} else if v != "123" {
t.Errorf("Data result not correct: %s", v)
}
})
}
func TestPre(t *testing.T) {
s := newScope("/")
mw := func(next RouteHandler) RouteHandler {
return func(c Context) Response {
return next(c)
}
}
s.Pre(mw)
if len(s.PreMiddleware) != 1 {
t.Error("middleware not added to collection")
}
}
func TestUse(t *testing.T) {
s := newScope("/")
mw := func(next RouteHandler) RouteHandler {
return func(c Context) Response {
return next(c)
}
}
s.Use(mw)
if len(s.Middleware) != 1 {
t.Error("middleware not added to collection")
}
}
func TestPanicRecovery(t *testing.T) {
s := newScope("/")
s.GET("/foo", func(c Context) Response {
panic("uh oh")
})
req, err := http.NewRequest("GET", "http://example.com/foo", nil)
if err != nil {
t.Fatal(err.Error())
}
c := RequestContext(req)
r := s.Handle(c)
if r.StatusCode != 500 {
t.Errorf("status code should be 500: %d", r.StatusCode)
}
}
func TestServePath(t *testing.T) {
s := newScope("/")
adapter := NewMEMAdapter()
FSAdapter = adapter
afero.WriteFile(adapter.MEMFS, "/public/test.txt", []byte("public file"), 0755)
s.ServePath("/test", "/public")
t.Run("root without file", func(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com/test/test.txt", nil)
if s.Match(req, "/") {
t.Error("should not match non existing file at root")
}
})
}
func TestFixPath(t *testing.T) {
p := "test"
if fixPath(p) != "/test" {
t.Errorf("path not prepended with slash: %s", fixPath(p))
}
}
func TestDeepestRoutePriority(t *testing.T) {
s := newScope("/")
s.GET("/*", func(c Context) Response {
return c.R("catch")
})
ss := s.Scope("/api")
ss.GET("/endpoint", func(c Context) Response {
return c.R("api")
})
req, err := http.NewRequest("GET", "http://example.com/api/endpoint", nil)
if err != nil {
t.Fatal(err.Error())
}
c := RequestContext(req)
r := s.Handle(c)
rStr := r.Data.(string)
if rStr != "api" {
t.Errorf("should get api response, got %s", rStr)
}
}