fix(tree): Keep panic infos consistent when wildcard type build faild (#4077)

This commit is contained in:
Xinyu Kuo 2024-10-26 08:28:59 +08:00 committed by GitHub
parent 9d11234efe
commit ea53388e6e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 2 deletions

View File

@ -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

View File

@ -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 + "'")
}

View File

@ -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)
}
}
}