-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroute_overwrite_extensive_test.go
More file actions
472 lines (382 loc) · 13.5 KB
/
route_overwrite_extensive_test.go
File metadata and controls
472 lines (382 loc) · 13.5 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
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
package ming
import (
"fmt"
"testing"
"github.com/valyala/fasthttp"
)
// TestExtensiveRouteOverwrite performs a comprehensive test of route overwriting behavior
func TestExtensiveRouteOverwrite(t *testing.T) {
// Create a router instance
router := New()
// ==========================================
// Test 1: Static route overwrite
// ==========================================
t.Run("StaticRouteOverwrite", func(t *testing.T) {
var handlerCalled string
// Register first handler
firstHandler := func(ctx *fasthttp.RequestCtx) {
handlerCalled = "first"
}
// Register second handler with same path
secondHandler := func(ctx *fasthttp.RequestCtx) {
handlerCalled = "second"
}
// Set both handlers
router.Get("/api/products", firstHandler)
router.Get("/api/products", secondHandler)
// Simulate request
ctx := &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod("GET")
ctx.Request.SetRequestURI("/api/products")
router.Handler(ctx)
// Check result
if handlerCalled != "second" {
t.Errorf("Expected second handler to be called, got %s", handlerCalled)
}
// Print debug info
fmt.Println("[StaticRouteOverwrite] Handler called:", handlerCalled)
})
// ==========================================
// Test 2: Parameter route overwrite
// ==========================================
t.Run("ParameterRouteOverwrite", func(t *testing.T) {
var handlerCalled string
var paramValue string
// Register first handler
firstHandler := func(ctx *fasthttp.RequestCtx) {
handlerCalled = "first"
paramValue = Param(ctx, "id")
}
// Register second handler with same path
secondHandler := func(ctx *fasthttp.RequestCtx) {
handlerCalled = "second"
paramValue = Param(ctx, "id")
}
// Set both handlers
router.Post("/api/users/{id}", firstHandler)
router.Post("/api/users/{id}", secondHandler)
// Simulate request
ctx := &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod("POST")
ctx.Request.SetRequestURI("/api/users/42")
router.Handler(ctx)
// Check result
if handlerCalled != "second" {
t.Errorf("Expected second handler to be called, got %s", handlerCalled)
}
if paramValue != "42" {
t.Errorf("Expected param value '42', got '%s'", paramValue)
}
// Print debug info
fmt.Println("[ParameterRouteOverwrite] Handler called:", handlerCalled, "Param:", paramValue)
})
// ==========================================
// Test 3: Regex parameter route overwrite
// ==========================================
t.Run("RegexParameterRouteOverwrite", func(t *testing.T) {
var handlerCalled string
var paramValue string
// Register first handler
firstHandler := func(ctx *fasthttp.RequestCtx) {
handlerCalled = "first"
paramValue = Param(ctx, "id")
}
// Register second handler with same path
secondHandler := func(ctx *fasthttp.RequestCtx) {
handlerCalled = "second"
paramValue = Param(ctx, "id")
}
// Set both handlers with regex constraint
router.Put("/api/posts/{id:[0-9]+}", firstHandler)
router.Put("/api/posts/{id:[0-9]+}", secondHandler)
// Simulate request
ctx := &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod("PUT")
ctx.Request.SetRequestURI("/api/posts/123")
router.Handler(ctx)
// Check result
if handlerCalled != "second" {
t.Errorf("Expected second handler to be called, got %s", handlerCalled)
}
if paramValue != "123" {
t.Errorf("Expected param value '123', got '%s'", paramValue)
}
// Print debug info
fmt.Println("[RegexParameterRouteOverwrite] Handler called:", handlerCalled, "Param:", paramValue)
})
// ==========================================
// Test 4: Mixed route overwrite (one with regex, one without)
// ==========================================
t.Run("MixedParameterRouteOverwrite", func(t *testing.T) {
var handlerCalled string
var paramValue string
// Register first handler without regex constraint
firstHandler := func(ctx *fasthttp.RequestCtx) {
handlerCalled = "first"
paramValue = Param(ctx, "id")
}
// Register second handler with regex constraint
secondHandler := func(ctx *fasthttp.RequestCtx) {
handlerCalled = "second"
paramValue = Param(ctx, "id")
}
// Set both handlers
router.Delete("/products/{id}", firstHandler)
router.Delete("/products/{id:[0-9]+}", secondHandler)
// Simulate request
ctx := &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod("DELETE")
ctx.Request.SetRequestURI("/products/777")
router.Handler(ctx)
// Check result - the second handler should be used
if handlerCalled != "second" {
t.Errorf("Expected second handler to be called, got %s", handlerCalled)
}
if paramValue != "777" {
t.Errorf("Expected param value '777', got '%s'", paramValue)
}
// Print debug info
fmt.Println("[MixedParameterRouteOverwrite] Handler called:", handlerCalled, "Param:", paramValue)
})
// ==========================================
// Test 5: Multiple parameters route overwrite
// ==========================================
t.Run("MultipleParametersRouteOverwrite", func(t *testing.T) {
var handlerCalled string
var userID, postID string
// Register first handler
firstHandler := func(ctx *fasthttp.RequestCtx) {
handlerCalled = "first"
userID = Param(ctx, "userId")
postID = Param(ctx, "postId")
}
// Register second handler with same path
secondHandler := func(ctx *fasthttp.RequestCtx) {
handlerCalled = "second"
userID = Param(ctx, "userId")
postID = Param(ctx, "postId")
}
// Set both handlers
router.Get("/users/{userId}/posts/{postId}", firstHandler)
router.Get("/users/{userId}/posts/{postId}", secondHandler)
// Simulate request
ctx := &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod("GET")
ctx.Request.SetRequestURI("/users/123/posts/456")
router.Handler(ctx)
// Check result
if handlerCalled != "second" {
t.Errorf("Expected second handler to be called, got %s", handlerCalled)
}
if userID != "123" || postID != "456" {
t.Errorf("Expected params userId='123', postId='456', got '%s', '%s'", userID, postID)
}
// Print debug info
fmt.Println("[MultipleParametersRouteOverwrite] Handler called:", handlerCalled,
"UserID:", userID, "PostID:", postID)
})
// ==========================================
// Test 6: Optional parameter route overwrite
// ==========================================
t.Run("OptionalParameterRouteOverwrite", func(t *testing.T) {
var handlerCalled string
var format string
// Skip this test if optional parameters are not working correctly
handler, _, _ := router.trees["GET"].getValue("/api/", "GET")
if handler == nil {
t.Skip("Optional parameters not working yet, skipping test")
}
// Register first handler
firstHandler := func(ctx *fasthttp.RequestCtx) {
handlerCalled = "first"
format = Param(ctx, "format")
if format == "" {
format = "default"
}
}
// Register second handler with same path
secondHandler := func(ctx *fasthttp.RequestCtx) {
handlerCalled = "second"
format = Param(ctx, "format")
if format == "" {
format = "default"
}
}
// Set both handlers
router.Get("/api/report{format?}", firstHandler)
router.Get("/api/report{format?}", secondHandler)
// Simulate request with format
ctx := &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod("GET")
ctx.Request.SetRequestURI("/api/report.json")
router.Handler(ctx)
// Check result
if handlerCalled != "second" {
t.Errorf("Expected second handler to be called with format, got %s", handlerCalled)
}
if format != ".json" {
t.Errorf("Expected format '.json', got '%s'", format)
}
// Reset for next test
handlerCalled = ""
format = ""
// Simulate request without format
ctx = &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod("GET")
ctx.Request.SetRequestURI("/api/report")
router.Handler(ctx)
// Check result
if handlerCalled != "second" {
t.Errorf("Expected second handler to be called without format, got %s", handlerCalled)
}
if format != "default" {
t.Errorf("Expected default format, got '%s'", format)
}
// Print debug info
fmt.Println("[OptionalParameterRouteOverwrite] Handler called:", handlerCalled, "Format:", format)
})
// ==========================================
// Test 7: Catch-all parameter route overwrite
// ==========================================
t.Run("CatchAllParameterRouteOverwrite", func(t *testing.T) {
var handlerCalled string
var path string
// Skip this test if catch-all parameters are not working correctly
handler, _, _ := router.trees["GET"].getValue("/static/test.txt", "GET")
if handler == nil {
t.Skip("Catch-all parameters not working yet, skipping test")
}
// Register first handler
firstHandler := func(ctx *fasthttp.RequestCtx) {
handlerCalled = "first"
path = Param(ctx, "filepath")
}
// Register second handler with same path
secondHandler := func(ctx *fasthttp.RequestCtx) {
handlerCalled = "second"
path = Param(ctx, "filepath")
}
// Set both handlers
router.Get("/static/{filepath:*}", firstHandler)
router.Get("/static/{filepath:*}", secondHandler)
// Simulate request
ctx := &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod("GET")
ctx.Request.SetRequestURI("/static/css/style.css")
router.Handler(ctx)
// Check result
if handlerCalled != "second" {
t.Errorf("Expected second handler to be called, got %s", handlerCalled)
}
if path != "css/style.css" {
t.Errorf("Expected path 'css/style.css', got '%s'", path)
}
// Print debug info
fmt.Println("[CatchAllParameterRouteOverwrite] Handler called:", handlerCalled, "Path:", path)
})
// ==========================================
// Test 8: ALL method overwrite with specific method
// ==========================================
t.Run("AllMethodOverwriteWithSpecific", func(t *testing.T) {
var handlerCalled string
// Register ALL handler first
allHandler := func(ctx *fasthttp.RequestCtx) {
handlerCalled = "all"
}
// Register specific method handler second
getHandler := func(ctx *fasthttp.RequestCtx) {
handlerCalled = "get"
}
// Set both handlers
router.All("/ping", allHandler)
router.Get("/ping", getHandler)
// Simulate GET request
ctx := &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod("GET")
ctx.Request.SetRequestURI("/ping")
router.Handler(ctx)
// Check GET result
if handlerCalled != "get" {
t.Errorf("Expected get handler to be called, got %s", handlerCalled)
}
// Reset for POST test
handlerCalled = ""
// Simulate POST request
ctx = &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod("POST")
ctx.Request.SetRequestURI("/ping")
router.Handler(ctx)
// Check POST result - should use ALL handler
if handlerCalled != "all" {
t.Errorf("Expected all handler to be called for POST, got %s", handlerCalled)
}
// Print debug info
fmt.Println("[AllMethodOverwriteWithSpecific] Handler called:", handlerCalled)
})
// ==========================================
// Test 9: Specific method overwrite with ALL method
// ==========================================
t.Run("SpecificMethodOverwriteWithAll", func(t *testing.T) {
var handlerCalled string
// Register specific method handler first
getHandler := func(ctx *fasthttp.RequestCtx) {
handlerCalled = "get"
}
// Register ALL handler second
allHandler := func(ctx *fasthttp.RequestCtx) {
handlerCalled = "all"
}
// Set both handlers
router.Get("/health", getHandler)
router.All("/health", allHandler)
// Simulate GET request
ctx := &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod("GET")
ctx.Request.SetRequestURI("/health")
router.Handler(ctx)
// Check GET result - should use GET handler despite ALL being registered later
if handlerCalled != "get" {
t.Errorf("Expected get handler to be called, got %s", handlerCalled)
}
// Reset for POST test
handlerCalled = ""
// Simulate POST request
ctx = &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod("POST")
ctx.Request.SetRequestURI("/health")
router.Handler(ctx)
// Check POST result - should use ALL handler
if handlerCalled != "all" {
t.Errorf("Expected all handler to be called for POST, got %s", handlerCalled)
}
// Print debug info
fmt.Println("[SpecificMethodOverwriteWithAll] Handler called:", handlerCalled)
})
// ==========================================
// Test 10: Multiple overwrites in sequence
// ==========================================
t.Run("MultipleOverwritesInSequence", func(t *testing.T) {
var handlerCalled string
// Register multiple handlers in sequence
handler1 := func(ctx *fasthttp.RequestCtx) { handlerCalled = "first" }
handler2 := func(ctx *fasthttp.RequestCtx) { handlerCalled = "second" }
handler3 := func(ctx *fasthttp.RequestCtx) { handlerCalled = "third" }
handler4 := func(ctx *fasthttp.RequestCtx) { handlerCalled = "fourth" }
// Set handlers in sequence
router.Get("/sequence", handler1)
router.Get("/sequence", handler2)
router.Get("/sequence", handler3)
router.Get("/sequence", handler4)
// Simulate request
ctx := &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod("GET")
ctx.Request.SetRequestURI("/sequence")
router.Handler(ctx)
// Check result - should use last registered handler
if handlerCalled != "fourth" {
t.Errorf("Expected fourth handler to be called, got %s", handlerCalled)
}
// Print debug info
fmt.Println("[MultipleOverwritesInSequence] Handler called:", handlerCalled)
})
}