Skip to content

Commit 60434e0

Browse files
author
AnikHasibul
committed
🐛 FIX: multiple sub-router group chaining bug
1 parent dcba8b2 commit 60434e0

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

router.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,20 +220,21 @@ func (r *Router) Handle(method, path string, handle fasthttp.RequestHandler) {
220220
if r.beginPath != "/" {
221221
path = r.beginPath + path
222222
}
223-
var route *Router
223+
224+
// Call to the parent recursively until main router to register paths in it
224225
if r.parent != nil {
225-
route = r.parent
226-
} else {
227-
route = r
226+
r.parent.Handle(method, path, handle)
227+
return
228228
}
229-
if route.trees == nil {
230-
route.trees = make(map[string]*node)
229+
230+
if r.trees == nil {
231+
r.trees = make(map[string]*node)
231232
}
232233

233-
root := route.trees[method]
234+
root := r.trees[method]
234235
if root == nil {
235236
root = new(node)
236-
route.trees[method] = root
237+
r.trees[method] = root
237238
}
238239

239240
root.addRoute(path, handle)

router_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ func TestRouterGroup(t *testing.T) {
314314
r2 := r1.Group("/boo")
315315
r3 := r1.Group("/goo")
316316
r4 := r1.Group("/moo")
317+
r5 := r4.Group("/foo")
318+
r6 := r5.Group("/foo")
317319
fooHit := false
318320
r1.POST("/foo", func(ctx *fasthttp.RequestCtx) {
319321
fooHit = true
@@ -333,6 +335,14 @@ func TestRouterGroup(t *testing.T) {
333335
barHit = true
334336
ctx.SetStatusCode(fasthttp.StatusOK)
335337
})
338+
r5.POST("/bar", func(ctx *fasthttp.RequestCtx) {
339+
barHit = true
340+
ctx.SetStatusCode(fasthttp.StatusOK)
341+
})
342+
r6.POST("/bar", func(ctx *fasthttp.RequestCtx) {
343+
barHit = true
344+
ctx.SetStatusCode(fasthttp.StatusOK)
345+
})
336346
s := &fasthttp.Server{
337347
Handler: r1.Handler,
338348
}
@@ -422,6 +432,45 @@ func TestRouterGroup(t *testing.T) {
422432
t.FailNow()
423433
}
424434

435+
rw.r.WriteString("POST /moo/foo/bar HTTP/1.1\r\n\r\n")
436+
go func() {
437+
ch <- s.ServeConn(rw)
438+
}()
439+
select {
440+
case err := <-ch:
441+
if err != nil {
442+
t.Fatalf("return error %s", err)
443+
}
444+
case <-time.After(100 * time.Millisecond):
445+
t.Fatalf("timeout")
446+
}
447+
if err := resp.Read(br); err != nil {
448+
t.Fatalf("Unexpected error when reading response: %s", err)
449+
}
450+
if !(resp.Header.StatusCode() == fasthttp.StatusOK && barHit) {
451+
t.Errorf("Chained routing failed with subrouter grouping.")
452+
t.FailNow()
453+
}
454+
rw.r.WriteString("POST /moo/foo/foo/bar HTTP/1.1\r\n\r\n")
455+
go func() {
456+
ch <- s.ServeConn(rw)
457+
}()
458+
select {
459+
case err := <-ch:
460+
if err != nil {
461+
t.Fatalf("return error %s", err)
462+
}
463+
case <-time.After(100 * time.Millisecond):
464+
t.Fatalf("timeout")
465+
}
466+
if err := resp.Read(br); err != nil {
467+
t.Fatalf("Unexpected error when reading response: %s", err)
468+
}
469+
if !(resp.Header.StatusCode() == fasthttp.StatusOK && barHit) {
470+
t.Errorf("Chained routing failed with subrouter grouping.")
471+
t.FailNow()
472+
}
473+
425474
rw.r.WriteString("POST /qax HTTP/1.1\r\n\r\n")
426475
go func() {
427476
ch <- s.ServeConn(rw)

0 commit comments

Comments
 (0)