Skip to content

Commit

Permalink
fix(tree): Keep panic infos consistent when wildcard type build faild (
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcanfish authored Oct 26, 2024
1 parent 9d11234 commit ea53388
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Setup golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.58.1
version: v1.61.0
args: --verbose
test:
needs: lint
Expand Down
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 ea53388

Please sign in to comment.