Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bb945cf

Browse files
citizen233thinkerou
authored andcommittedNov 24, 2021
fix the misplacement of adding slashes (#2847)
1 parent a3f0872 commit bb945cf

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed
 

‎routes_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,21 @@ func TestRouterNotFound(t *testing.T) {
479479
router.GET("/a", func(c *Context) {})
480480
w = performRequest(router, http.MethodGet, "/")
481481
assert.Equal(t, http.StatusNotFound, w.Code)
482+
483+
// Reproduction test for the bug of issue #2843
484+
router = New()
485+
router.NoRoute(func(c *Context) {
486+
if c.Request.RequestURI == "/login" {
487+
c.String(200, "login")
488+
}
489+
})
490+
router.GET("/logout", func(c *Context) {
491+
c.String(200, "logout")
492+
})
493+
w = performRequest(router, http.MethodGet, "/login")
494+
assert.Equal(t, "login", w.Body.String())
495+
w = performRequest(router, http.MethodGet, "/logout")
496+
assert.Equal(t, "logout", w.Body.String())
482497
}
483498

484499
func TestRouterStaticFSNotFound(t *testing.T) {

‎tree.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,8 @@ walk: // Outer loop for walking the tree
635635
// Nothing found. We can recommend to redirect to the same URL with an
636636
// extra trailing slash if a leaf exists for that path
637637
value.tsr = path == "/" ||
638-
(len(prefix) == len(path)+1 && n.handlers != nil)
638+
(len(prefix) == len(path)+1 && prefix[len(path)] == '/' &&
639+
path == prefix[:len(prefix)-1] && n.handlers != nil)
639640
return
640641
}
641642
}

0 commit comments

Comments
 (0)
Please sign in to comment.