Skip to content

Commit

Permalink
fix: param router doesn't match param (#978)
Browse files Browse the repository at this point in the history
  • Loading branch information
Duslia committed Nov 14, 2023
1 parent 837aa65 commit 67ca073
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
4 changes: 1 addition & 3 deletions pkg/route/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,7 @@ func (r *router) insert(path string, h app.HandlersChain, t kind, ppath string,
if h != nil {
currentNode.handlers = h
currentNode.ppath = ppath
if len(currentNode.pnames) == 0 {
currentNode.pnames = pnames
}
currentNode.pnames = pnames
}
}
return
Expand Down
29 changes: 29 additions & 0 deletions pkg/route/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,3 +706,32 @@ func TestTreeFindCaseInsensitivePath(t *testing.T) {
}
}
}

func TestTreeParamNotOptimize(t *testing.T) {
tree := &router{method: "GET", root: &node{}, hasTsrHandler: make(map[string]bool)}
routes := [...]string{
"/:parama/start",
"/:paramb",
}
for _, route := range routes {
tree.addRoute(route, fakeHandler(route))
}
checkRequests(t, tree, testRequests{
{"/1", false, "/:paramb", param.Params{param.Param{Key: "paramb", Value: "1"}}},
{"/1/start", false, "/:parama/start", param.Params{param.Param{Key: "parama", Value: "1"}}},
})

// other sequence
tree = &router{method: "GET", root: &node{}, hasTsrHandler: make(map[string]bool)}
routes = [...]string{
"/:paramb",
"/:parama/start",
}
for _, route := range routes {
tree.addRoute(route, fakeHandler(route))
}
checkRequests(t, tree, testRequests{
{"/1/start", false, "/:parama/start", param.Params{param.Param{Key: "parama", Value: "1"}}},
{"/1", false, "/:paramb", param.Params{param.Param{Key: "paramb", Value: "1"}}},
})
}

0 comments on commit 67ca073

Please sign in to comment.