Skip to content

Commit 3338ac2

Browse files
authored
radix.node does not check siblings when first child match part of path. (#34)
* BUGFIX: radix.node not check siblings when first child's handler is nil
1 parent 0dbe144 commit 3338ac2

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

radix/node.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ walk:
353353
case child.tsr:
354354
return nil, true
355355
case child.handler == nil:
356-
return nil, false
356+
// try another child
357+
continue
357358
case ctx != nil:
358359
for i, key := range child.paramKeys {
359360
ctx.SetUserValue(key, values[i])

router_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,74 @@ func TestRouterList(t *testing.T) {
958958

959959
}
960960

961+
func TestRouterSamePrefixParamRoute(t *testing.T) {
962+
var id1, id2, id3, pageSize, page, iid string
963+
var routed1, routed2, routed3 bool
964+
965+
r := New()
966+
v1 := r.Group("/v1")
967+
v1.GET("/foo/{id}/{pageSize}/{page}", func(ctx *fasthttp.RequestCtx) {
968+
id1 = ctx.UserValue("id").(string)
969+
pageSize = ctx.UserValue("pageSize").(string)
970+
page = ctx.UserValue("page").(string)
971+
routed1 = true
972+
})
973+
v1.GET("/foo/{id}/{iid}", func(ctx *fasthttp.RequestCtx) {
974+
id2 = ctx.UserValue("id").(string)
975+
iid = ctx.UserValue("iid").(string)
976+
routed2 = true
977+
})
978+
v1.GET("/foo/{id}", func(ctx *fasthttp.RequestCtx) {
979+
id3 = ctx.UserValue("id").(string)
980+
routed3 = true
981+
})
982+
983+
req := new(fasthttp.RequestCtx)
984+
req.Request.SetRequestURI("/v1/foo/1/20/4")
985+
r.Handler(req)
986+
req = new(fasthttp.RequestCtx)
987+
req.Request.SetRequestURI("/v1/foo/2/3")
988+
r.Handler(req)
989+
req = new(fasthttp.RequestCtx)
990+
req.Request.SetRequestURI("/v1/foo/v3")
991+
r.Handler(req)
992+
993+
if !routed1 {
994+
t.Error("/foo/{id}/{pageSize}/{page} not routed.")
995+
}
996+
if !routed2 {
997+
t.Error("/foo/{id}/{iid} not routed")
998+
}
999+
1000+
if !routed3 {
1001+
t.Error("/foo/{id} not routed")
1002+
}
1003+
1004+
if id1 != "1" {
1005+
t.Errorf("/foo/{id}/{pageSize}/{page} id expect: 1 got %s", id1)
1006+
}
1007+
1008+
if pageSize != "20" {
1009+
t.Errorf("/foo/{id}/{pageSize}/{page} pageSize expect: 20 got %s", pageSize)
1010+
}
1011+
1012+
if page != "4" {
1013+
t.Errorf("/foo/{id}/{pageSize}/{page} page expect: 4 got %s", page)
1014+
}
1015+
1016+
if id2 != "2" {
1017+
t.Errorf("/foo/{id}/{iid} id expect: 2 got %s", id2)
1018+
}
1019+
1020+
if iid != "3" {
1021+
t.Errorf("/foo/{id}/{iid} iid expect: 3 got %s", iid)
1022+
}
1023+
1024+
if id3 != "v3" {
1025+
t.Errorf("/foo/{id} id expect: v3 got %s", id3)
1026+
}
1027+
}
1028+
9611029
func BenchmarkAllowed(b *testing.B) {
9621030
handlerFunc := func(_ *fasthttp.RequestCtx) {}
9631031

0 commit comments

Comments
 (0)