mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 12:12:12 +08:00
Merge b7dd5120ef35e1ed60c497d770495aa3054e6ab1 into c3d1092b3b48addf6f9cd00fe274ec3bd14650eb
This commit is contained in:
commit
29dffa8096
1
gin.go
1
gin.go
@ -755,6 +755,7 @@ func redirectTrailingSlash(c *Context) {
|
|||||||
p = prefix + "/" + req.URL.Path
|
p = prefix + "/" + req.URL.Path
|
||||||
}
|
}
|
||||||
req.URL.Path = p + "/"
|
req.URL.Path = p + "/"
|
||||||
|
p = regexp.MustCompile("^/{2,}").ReplaceAllString(p, "/")
|
||||||
if length := len(p); length > 1 && p[length-1] == '/' {
|
if length := len(p); length > 1 && p[length-1] == '/' {
|
||||||
req.URL.Path = p[:length-1]
|
req.URL.Path = p[:length-1]
|
||||||
}
|
}
|
||||||
|
@ -150,8 +150,13 @@ func TestRouteRedirectTrailingSlash(t *testing.T) {
|
|||||||
router.GET("/path2/", func(c *Context) {})
|
router.GET("/path2/", func(c *Context) {})
|
||||||
router.POST("/path3", func(c *Context) {})
|
router.POST("/path3", func(c *Context) {})
|
||||||
router.PUT("/path4/", func(c *Context) {})
|
router.PUT("/path4/", func(c *Context) {})
|
||||||
|
router.GET("/:param1/:param2", func(c *Context) {})
|
||||||
|
|
||||||
w := PerformRequest(router, http.MethodGet, "/path/")
|
w := PerformRequest(router, http.MethodGet, "//path/")
|
||||||
|
assert.Equal(t, "/path", w.Header().Get("Location"))
|
||||||
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||||
|
|
||||||
|
w = PerformRequest(router, http.MethodGet, "/path/")
|
||||||
assert.Equal(t, "/path", w.Header().Get("Location"))
|
assert.Equal(t, "/path", w.Header().Get("Location"))
|
||||||
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user