-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrouter_test.go
More file actions
398 lines (348 loc) · 11.1 KB
/
router_test.go
File metadata and controls
398 lines (348 loc) · 11.1 KB
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
package celeris
import (
"testing"
)
func TestRouterStaticRoutes(t *testing.T) {
r := newRouter()
called := ""
r.addRoute("GET", "/", []HandlerFunc{func(_ *Context) error { called = "/"; return nil }})
r.addRoute("GET", "/hello", []HandlerFunc{func(_ *Context) error { called = "/hello"; return nil }})
r.addRoute("GET", "/hello/world", []HandlerFunc{func(_ *Context) error { called = "/hello/world"; return nil }})
var params Params
handlers, fp := r.find("GET", "/", ¶ms)
if handlers == nil {
t.Fatal("expected handlers for /")
}
_ = handlers[0](nil)
if called != "/" {
t.Fatalf("expected /, got %s", called)
}
if fp != "/" {
t.Fatalf("expected fullPath /, got %s", fp)
}
params = params[:0]
handlers, fp = r.find("GET", "/hello", ¶ms)
if handlers == nil {
t.Fatal("expected handlers for /hello")
}
_ = handlers[0](nil)
if called != "/hello" {
t.Fatalf("expected /hello, got %s", called)
}
if fp != "/hello" {
t.Fatalf("expected fullPath /hello, got %s", fp)
}
params = params[:0]
handlers, fp = r.find("GET", "/hello/world", ¶ms)
if handlers == nil {
t.Fatal("expected handlers for /hello/world")
}
_ = handlers[0](nil)
if called != "/hello/world" {
t.Fatalf("expected /hello/world, got %s", called)
}
if fp != "/hello/world" {
t.Fatalf("expected fullPath /hello/world, got %s", fp)
}
}
func TestRouterParamRoutes(t *testing.T) {
r := newRouter()
r.addRoute("GET", "/users/:id", []HandlerFunc{func(_ *Context) error { return nil }})
r.addRoute("GET", "/users/:id/posts/:pid", []HandlerFunc{func(_ *Context) error { return nil }})
var params Params
handlers, fp := r.find("GET", "/users/42", ¶ms)
if handlers == nil {
t.Fatal("expected handlers for /users/42")
}
if len(params) != 1 || params[0].Key != "id" || params[0].Value != "42" {
t.Fatalf("expected id=42, got %+v", params)
}
if fp != "/users/:id" {
t.Fatalf("expected fullPath /users/:id, got %s", fp)
}
params = params[:0]
handlers, fp = r.find("GET", "/users/7/posts/99", ¶ms)
if handlers == nil {
t.Fatal("expected handlers for /users/7/posts/99")
}
if len(params) != 2 {
t.Fatalf("expected 2 params, got %d: %+v", len(params), params)
}
if params[0].Key != "id" || params[0].Value != "7" {
t.Fatalf("expected id=7, got %+v", params[0])
}
if params[1].Key != "pid" || params[1].Value != "99" {
t.Fatalf("expected pid=99, got %+v", params[1])
}
if fp != "/users/:id/posts/:pid" {
t.Fatalf("expected fullPath /users/:id/posts/:pid, got %s", fp)
}
}
func TestRouterCatchAll(t *testing.T) {
r := newRouter()
r.addRoute("GET", "/files/*filepath", []HandlerFunc{func(_ *Context) error { return nil }})
var params Params
handlers, fp := r.find("GET", "/files/css/style.css", ¶ms)
if handlers == nil {
t.Fatal("expected handlers for /files/css/style.css")
}
if len(params) != 1 || params[0].Key != "filepath" {
t.Fatalf("expected filepath param, got %+v", params)
}
if fp != "/files/*filepath" {
t.Fatalf("expected fullPath /files/*filepath, got %s", fp)
}
}
func TestRouterNotFound(t *testing.T) {
r := newRouter()
r.addRoute("GET", "/hello", []HandlerFunc{func(_ *Context) error { return nil }})
var params Params
handlers, _ := r.find("GET", "/notfound", ¶ms)
if handlers != nil {
t.Fatal("expected nil handlers for /notfound")
}
handlers, _ = r.find("POST", "/hello", ¶ms)
if handlers != nil {
t.Fatal("expected nil handlers for POST /hello")
}
}
func TestRouterMethodSeparation(t *testing.T) {
r := newRouter()
getCalled := false
postCalled := false
r.addRoute("GET", "/test", []HandlerFunc{func(_ *Context) error { getCalled = true; return nil }})
r.addRoute("POST", "/test", []HandlerFunc{func(_ *Context) error { postCalled = true; return nil }})
var params Params
handlers, _ := r.find("GET", "/test", ¶ms)
if handlers == nil {
t.Fatal("expected GET handler")
}
_ = handlers[0](nil)
if !getCalled {
t.Fatal("GET handler not called")
}
handlers, _ = r.find("POST", "/test", ¶ms)
if handlers == nil {
t.Fatal("expected POST handler")
}
_ = handlers[0](nil)
if !postCalled {
t.Fatal("POST handler not called")
}
}
func TestRouterAllowedMethods(t *testing.T) {
r := newRouter()
r.addRoute("GET", "/resource", []HandlerFunc{func(_ *Context) error { return nil }})
r.addRoute("POST", "/resource", []HandlerFunc{func(_ *Context) error { return nil }})
r.addRoute("PUT", "/resource", []HandlerFunc{func(_ *Context) error { return nil }})
allowed := r.allowedMethods("/resource", "DELETE")
if len(allowed) != 3 {
t.Fatalf("expected 3 allowed methods, got %d: %v", len(allowed), allowed)
}
// Exclude GET — should return POST, PUT.
allowed = r.allowedMethods("/resource", "GET")
if len(allowed) != 2 {
t.Fatalf("expected 2 allowed methods, got %d: %v", len(allowed), allowed)
}
// Unknown path — no methods.
allowed = r.allowedMethods("/missing", "GET")
if len(allowed) != 0 {
t.Fatalf("expected 0 allowed methods, got %d: %v", len(allowed), allowed)
}
}
func TestRouterOverlappingPaths(t *testing.T) {
r := newRouter()
r.addRoute("GET", "/api/users", []HandlerFunc{func(_ *Context) error { return nil }})
r.addRoute("GET", "/api/users/:id", []HandlerFunc{func(_ *Context) error { return nil }})
r.addRoute("GET", "/api/posts", []HandlerFunc{func(_ *Context) error { return nil }})
var params Params
handlers, _ := r.find("GET", "/api/users", ¶ms)
if handlers == nil {
t.Fatal("expected handlers for /api/users")
}
params = params[:0]
handlers, _ = r.find("GET", "/api/users/123", ¶ms)
if handlers == nil {
t.Fatal("expected handlers for /api/users/123")
}
if len(params) != 1 || params[0].Value != "123" {
t.Fatalf("expected id=123, got %+v", params)
}
params = params[:0]
handlers, _ = r.find("GET", "/api/posts", ¶ms)
if handlers == nil {
t.Fatal("expected handlers for /api/posts")
}
}
func TestRouterFullPath(t *testing.T) {
r := newRouter()
r.addRoute("GET", "/", []HandlerFunc{func(_ *Context) error { return nil }})
r.addRoute("GET", "/users/:id", []HandlerFunc{func(_ *Context) error { return nil }})
r.addRoute("GET", "/files/*path", []HandlerFunc{func(_ *Context) error { return nil }})
r.addRoute("GET", "/static/about", []HandlerFunc{func(_ *Context) error { return nil }})
tests := []struct {
path string
wantFP string
wantNil bool
}{
{"/", "/", false},
{"/users/42", "/users/:id", false},
{"/files/a/b/c", "/files/*path", false},
{"/static/about", "/static/about", false},
{"/missing", "", true},
}
for _, tt := range tests {
var params Params
handlers, fp := r.find("GET", tt.path, ¶ms)
if tt.wantNil {
if handlers != nil {
t.Fatalf("path %s: expected nil handlers", tt.path)
}
continue
}
if handlers == nil {
t.Fatalf("path %s: expected handlers", tt.path)
}
if fp != tt.wantFP {
t.Fatalf("path %s: expected fullPath %q, got %q", tt.path, tt.wantFP, fp)
}
}
}
func TestRouteName(t *testing.T) {
r := newRouter()
route := r.addRoute("GET", "/users/:id", []HandlerFunc{func(_ *Context) error { return nil }})
named := route.Name("user-by-id")
if named != route {
t.Fatal("expected Name to return same Route")
}
if route.name != "user-by-id" {
t.Fatalf("expected name 'user-by-id', got %q", route.name)
}
}
func TestRouterStaticPriorityOverParam(t *testing.T) {
r := newRouter()
paramCalled := false
staticCalled := false
// Register param BEFORE static — static must still win.
r.addRoute("GET", "/users/:id", []HandlerFunc{func(_ *Context) error { paramCalled = true; return nil }})
r.addRoute("GET", "/users/new", []HandlerFunc{func(_ *Context) error { staticCalled = true; return nil }})
var params Params
// /users/new should match the static route.
handlers, _ := r.find("GET", "/users/new", ¶ms)
if handlers == nil {
t.Fatal("expected handlers for /users/new")
}
_ = handlers[0](nil)
if !staticCalled {
t.Fatal("expected static handler to be called for /users/new")
}
if paramCalled {
t.Fatal("param handler should not be called for /users/new")
}
// /users/42 should still match the param route.
paramCalled = false
staticCalled = false
params = params[:0]
handlers, _ = r.find("GET", "/users/42", ¶ms)
if handlers == nil {
t.Fatal("expected handlers for /users/42")
}
_ = handlers[0](nil)
if !paramCalled {
t.Fatal("expected param handler to be called for /users/42")
}
if len(params) != 1 || params[0].Value != "42" {
t.Fatalf("expected id=42, got %+v", params)
}
}
func TestCatchAllDedup(t *testing.T) {
r := newRouter()
h1 := []HandlerFunc{func(_ *Context) error { return nil }}
h2 := []HandlerFunc{func(_ *Context) error { return nil }}
r.addRoute("GET", "/files/*filepath", h1)
r.addRoute("GET", "/files/*path", h2)
var params Params
handlers, _ := r.find("GET", "/files/a.txt", ¶ms)
if handlers == nil {
t.Fatal("expected handlers for /files/a.txt")
}
// Both registrations should share the same catchall node.
// The first key ("filepath") wins because it created the node.
if len(params) != 1 || params[0].Key != "filepath" {
t.Fatalf("expected param key 'filepath', got %+v", params)
}
}
func TestTrailingSlashSeparateRoutes(t *testing.T) {
r := newRouter()
withSlash := false
withoutSlash := false
r.addRoute("GET", "/users", []HandlerFunc{func(_ *Context) error { withoutSlash = true; return nil }})
r.addRoute("GET", "/users/", []HandlerFunc{func(_ *Context) error { withSlash = true; return nil }})
var params Params
handlers, _ := r.find("GET", "/users", ¶ms)
if handlers == nil {
t.Fatal("expected handlers for /users")
}
_ = handlers[0](nil)
if !withoutSlash {
t.Fatal("expected /users handler")
}
handlers, _ = r.find("GET", "/users/", ¶ms)
if handlers == nil {
t.Fatal("expected handlers for /users/")
}
_ = handlers[0](nil)
if !withSlash {
t.Fatal("expected /users/ handler")
}
}
func TestParamAtRoot(t *testing.T) {
r := newRouter()
r.addRoute("GET", "/:id", []HandlerFunc{func(_ *Context) error { return nil }})
var params Params
handlers, fp := r.find("GET", "/42", ¶ms)
if handlers == nil {
t.Fatal("expected handlers for /42")
}
if len(params) != 1 || params[0].Key != "id" || params[0].Value != "42" {
t.Fatalf("expected id=42, got %+v", params)
}
if fp != "/:id" {
t.Fatalf("expected fullPath /:id, got %s", fp)
}
}
func TestCatchAllAtRoot(t *testing.T) {
r := newRouter()
r.addRoute("GET", "/*filepath", []HandlerFunc{func(_ *Context) error { return nil }})
var params Params
handlers, fp := r.find("GET", "/a/b/c", ¶ms)
if handlers == nil {
t.Fatal("expected handlers for /a/b/c")
}
if len(params) != 1 || params[0].Key != "filepath" {
t.Fatalf("expected filepath param, got %+v", params)
}
if fp != "/*filepath" {
t.Fatalf("expected fullPath /*filepath, got %s", fp)
}
}
func TestParamsGet(t *testing.T) {
params := Params{
{Key: "id", Value: "42"},
{Key: "name", Value: "alice"},
}
// Found case.
v, ok := params.Get("id")
if !ok || v != "42" {
t.Fatalf("expected (42, true), got (%s, %v)", v, ok)
}
v, ok = params.Get("name")
if !ok || v != "alice" {
t.Fatalf("expected (alice, true), got (%s, %v)", v, ok)
}
// Not-found case.
v, ok = params.Get("missing")
if ok || v != "" {
t.Fatalf("expected ('', false), got (%s, %v)", v, ok)
}
}