Skip to content

Do not handle special trailing slash case for partial prefix #1741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,14 @@ func (r *Router) Find(method, path string, c Context) {
if search == "" && (nn == nil || cn.parent == nil || cn.ppath != "") {
break
}
// Handle special case of trailing slash route with existing any route (see #1526)
if search == "" && path[len(path)-1] == '/' && cn.anyChildren != nil {
goto Any
}
}

// Attempt to go back up the tree on no matching prefix or no remaining search
if l != pl || search == "" {
// Handle special case of trailing slash route with existing any route (see #1526)
if path[len(path)-1] == '/' && cn.anyChildren != nil {
goto Any
}
if nn == nil { // Issue #1348
return // Not found
}
Expand Down
41 changes: 41 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,47 @@ func TestRouterMatchAny(t *testing.T) {
assert.Equal(t, "joe", c.Param("*"))
}

// Issue #1739
func TestRouterMatchAnyPrefixIssue(t *testing.T) {
e := New()
r := e.router

// Routes
r.Add(http.MethodGet, "/*", func(c Context) error {
c.Set("path", c.Path())
return nil
})
r.Add(http.MethodGet, "/users/*", func(c Context) error {
c.Set("path", c.Path())
return nil
})
c := e.NewContext(nil, nil).(*context)
r.Find(http.MethodGet, "/", c)
c.handler(c)
assert.Equal(t, "/*", c.Get("path"))
assert.Equal(t, "", c.Param("*"))

r.Find(http.MethodGet, "/users", c)
c.handler(c)
assert.Equal(t, "/*", c.Get("path"))
assert.Equal(t, "users", c.Param("*"))

r.Find(http.MethodGet, "/users/", c)
c.handler(c)
assert.Equal(t, "/users/*", c.Get("path"))
assert.Equal(t, "", c.Param("*"))

r.Find(http.MethodGet, "/users_prefix", c)
c.handler(c)
assert.Equal(t, "/*", c.Get("path"))
assert.Equal(t, "users_prefix", c.Param("*"))

r.Find(http.MethodGet, "/users_prefix/", c)
c.handler(c)
assert.Equal(t, "/*", c.Get("path"))
assert.Equal(t, "users_prefix/", c.Param("*"))
}

// TestRouterMatchAnySlash shall verify finding the best route
// for any routes with trailing slash requests
func TestRouterMatchAnySlash(t *testing.T) {
Expand Down