diff --git a/gin.go b/gin.go index 1965a429..fabd3095 100644 --- a/gin.go +++ b/gin.go @@ -755,6 +755,7 @@ func redirectTrailingSlash(c *Context) { p = prefix + "/" + req.URL.Path } req.URL.Path = p + "/" + p = regexp.MustCompile("^/{2,}").ReplaceAllString(p, "/") if length := len(p); length > 1 && p[length-1] == '/' { req.URL.Path = p[:length-1] } diff --git a/routes_test.go b/routes_test.go index 1cae3fce..8b8f615d 100644 --- a/routes_test.go +++ b/routes_test.go @@ -150,8 +150,13 @@ func TestRouteRedirectTrailingSlash(t *testing.T) { router.GET("/path2/", func(c *Context) {}) router.POST("/path3", 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, http.StatusMovedPermanently, w.Code)