Skip to content

Commit

Permalink
radix.node does not check siblings when first child match part of pat…
Browse files Browse the repository at this point in the history
…h. (#34)

* BUGFIX: radix.node not check siblings when first child's handler is nil
  • Loading branch information
zxfishhack committed May 28, 2020
1 parent 0dbe144 commit 3338ac2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
3 changes: 2 additions & 1 deletion radix/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ walk:
case child.tsr:
return nil, true
case child.handler == nil:
return nil, false
// try another child
continue
case ctx != nil:
for i, key := range child.paramKeys {
ctx.SetUserValue(key, values[i])
Expand Down
68 changes: 68 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,74 @@ func TestRouterList(t *testing.T) {

}

func TestRouterSamePrefixParamRoute(t *testing.T) {
var id1, id2, id3, pageSize, page, iid string
var routed1, routed2, routed3 bool

r := New()
v1 := r.Group("/v1")
v1.GET("/foo/{id}/{pageSize}/{page}", func(ctx *fasthttp.RequestCtx) {
id1 = ctx.UserValue("id").(string)
pageSize = ctx.UserValue("pageSize").(string)
page = ctx.UserValue("page").(string)
routed1 = true
})
v1.GET("/foo/{id}/{iid}", func(ctx *fasthttp.RequestCtx) {
id2 = ctx.UserValue("id").(string)
iid = ctx.UserValue("iid").(string)
routed2 = true
})
v1.GET("/foo/{id}", func(ctx *fasthttp.RequestCtx) {
id3 = ctx.UserValue("id").(string)
routed3 = true
})

req := new(fasthttp.RequestCtx)
req.Request.SetRequestURI("/v1/foo/1/20/4")
r.Handler(req)
req = new(fasthttp.RequestCtx)
req.Request.SetRequestURI("/v1/foo/2/3")
r.Handler(req)
req = new(fasthttp.RequestCtx)
req.Request.SetRequestURI("/v1/foo/v3")
r.Handler(req)

if !routed1 {
t.Error("/foo/{id}/{pageSize}/{page} not routed.")
}
if !routed2 {
t.Error("/foo/{id}/{iid} not routed")
}

if !routed3 {
t.Error("/foo/{id} not routed")
}

if id1 != "1" {
t.Errorf("/foo/{id}/{pageSize}/{page} id expect: 1 got %s", id1)
}

if pageSize != "20" {
t.Errorf("/foo/{id}/{pageSize}/{page} pageSize expect: 20 got %s", pageSize)
}

if page != "4" {
t.Errorf("/foo/{id}/{pageSize}/{page} page expect: 4 got %s", page)
}

if id2 != "2" {
t.Errorf("/foo/{id}/{iid} id expect: 2 got %s", id2)
}

if iid != "3" {
t.Errorf("/foo/{id}/{iid} iid expect: 3 got %s", iid)
}

if id3 != "v3" {
t.Errorf("/foo/{id} id expect: v3 got %s", id3)
}
}

func BenchmarkAllowed(b *testing.B) {
handlerFunc := func(_ *fasthttp.RequestCtx) {}

Expand Down

0 comments on commit 3338ac2

Please sign in to comment.