Skip to content

Commit

Permalink
fix(tree): Keep panic information consistent when wildcard type build…
Browse files Browse the repository at this point in the history
… failed
  • Loading branch information
kingcanfish committed Oct 25, 2024
1 parent 647311a commit 3af8f5f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ func (n *node) insertChild(path string, fullPath string, handlers HandlersChain)

// currently fixed width 1 for '/'
i--
if path[i] != '/' {
if i < 0 || path[i] != '/' {
panic("no / before catch-all in path '" + fullPath + "'")
}

Expand Down
25 changes: 25 additions & 0 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -993,3 +993,28 @@ func TestTreeInvalidEscape(t *testing.T) {
}
}
}

func TestWildcardInvalidSlash(t *testing.T) {
const panicMsgPrefix = "no / before catch-all in path"

routes := map[string]bool{
"/foo/bar": true,
"/foo/x*zy": false,
"/foo/b*r": false,
}

for route, valid := range routes {
tree := &node{}
recv := catchPanic(func() {
tree.addRoute(route, nil)
})

if recv == nil != valid {
t.Fatalf("%s should be %t but got %v", route, valid, recv)
}

if rs, ok := recv.(string); recv != nil && (!ok || !strings.HasPrefix(rs, panicMsgPrefix)) {
t.Fatalf(`"Expected panic "%s" for route '%s', got "%v"`, panicMsgPrefix, route, recv)
}
}
}

0 comments on commit 3af8f5f

Please sign in to comment.