Skip to content

Commit

Permalink
🐛 FIX: multiple sub-router group chaining bug
Browse files Browse the repository at this point in the history
  • Loading branch information
AnikHasibul committed Jul 15, 2019
1 parent dcba8b2 commit 60434e0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
17 changes: 9 additions & 8 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,20 +220,21 @@ func (r *Router) Handle(method, path string, handle fasthttp.RequestHandler) {
if r.beginPath != "/" {
path = r.beginPath + path
}
var route *Router

// Call to the parent recursively until main router to register paths in it
if r.parent != nil {
route = r.parent
} else {
route = r
r.parent.Handle(method, path, handle)
return
}
if route.trees == nil {
route.trees = make(map[string]*node)

if r.trees == nil {
r.trees = make(map[string]*node)
}

root := route.trees[method]
root := r.trees[method]
if root == nil {
root = new(node)
route.trees[method] = root
r.trees[method] = root
}

root.addRoute(path, handle)
Expand Down
49 changes: 49 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ func TestRouterGroup(t *testing.T) {
r2 := r1.Group("/boo")
r3 := r1.Group("/goo")
r4 := r1.Group("/moo")
r5 := r4.Group("/foo")
r6 := r5.Group("/foo")
fooHit := false
r1.POST("/foo", func(ctx *fasthttp.RequestCtx) {
fooHit = true
Expand All @@ -333,6 +335,14 @@ func TestRouterGroup(t *testing.T) {
barHit = true
ctx.SetStatusCode(fasthttp.StatusOK)
})
r5.POST("/bar", func(ctx *fasthttp.RequestCtx) {
barHit = true
ctx.SetStatusCode(fasthttp.StatusOK)
})
r6.POST("/bar", func(ctx *fasthttp.RequestCtx) {
barHit = true
ctx.SetStatusCode(fasthttp.StatusOK)
})
s := &fasthttp.Server{
Handler: r1.Handler,
}
Expand Down Expand Up @@ -422,6 +432,45 @@ func TestRouterGroup(t *testing.T) {
t.FailNow()
}

rw.r.WriteString("POST /moo/foo/bar HTTP/1.1\r\n\r\n")
go func() {
ch <- s.ServeConn(rw)
}()
select {
case err := <-ch:
if err != nil {
t.Fatalf("return error %s", err)
}
case <-time.After(100 * time.Millisecond):
t.Fatalf("timeout")
}
if err := resp.Read(br); err != nil {
t.Fatalf("Unexpected error when reading response: %s", err)
}
if !(resp.Header.StatusCode() == fasthttp.StatusOK && barHit) {
t.Errorf("Chained routing failed with subrouter grouping.")
t.FailNow()
}
rw.r.WriteString("POST /moo/foo/foo/bar HTTP/1.1\r\n\r\n")
go func() {
ch <- s.ServeConn(rw)
}()
select {
case err := <-ch:
if err != nil {
t.Fatalf("return error %s", err)
}
case <-time.After(100 * time.Millisecond):
t.Fatalf("timeout")
}
if err := resp.Read(br); err != nil {
t.Fatalf("Unexpected error when reading response: %s", err)
}
if !(resp.Header.StatusCode() == fasthttp.StatusOK && barHit) {
t.Errorf("Chained routing failed with subrouter grouping.")
t.FailNow()
}

rw.r.WriteString("POST /qax HTTP/1.1\r\n\r\n")
go func() {
ch <- s.ServeConn(rw)
Expand Down

0 comments on commit 60434e0

Please sign in to comment.