Skip to content

Commit

Permalink
fix the misplacement of adding slashes (#2847)
Browse files Browse the repository at this point in the history
  • Loading branch information
citizen233 authored and thinkerou committed Nov 24, 2021
1 parent a3f0872 commit bb945cf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
15 changes: 15 additions & 0 deletions routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,21 @@ func TestRouterNotFound(t *testing.T) {
router.GET("/a", func(c *Context) {})
w = performRequest(router, http.MethodGet, "/")
assert.Equal(t, http.StatusNotFound, w.Code)

// Reproduction test for the bug of issue #2843
router = New()
router.NoRoute(func(c *Context) {
if c.Request.RequestURI == "/login" {
c.String(200, "login")
}
})
router.GET("/logout", func(c *Context) {
c.String(200, "logout")
})
w = performRequest(router, http.MethodGet, "/login")
assert.Equal(t, "login", w.Body.String())
w = performRequest(router, http.MethodGet, "/logout")
assert.Equal(t, "logout", w.Body.String())
}

func TestRouterStaticFSNotFound(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,8 @@ walk: // Outer loop for walking the tree
// Nothing found. We can recommend to redirect to the same URL with an
// extra trailing slash if a leaf exists for that path
value.tsr = path == "/" ||
(len(prefix) == len(path)+1 && n.handlers != nil)
(len(prefix) == len(path)+1 && prefix[len(path)] == '/' &&
path == prefix[:len(prefix)-1] && n.handlers != nil)
return
}
}
Expand Down

0 comments on commit bb945cf

Please sign in to comment.