mirror of
https://github.com/labstack/echo.git
synced 2025-07-15 01:34:53 +02:00
Removed RedirectToSlash middleware
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
@ -1,16 +1,6 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import "github.com/labstack/echo"
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/labstack/echo"
|
|
||||||
)
|
|
||||||
|
|
||||||
type (
|
|
||||||
RedirectToSlashOptions struct {
|
|
||||||
Code int
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// StripTrailingSlash returns a middleware which removes trailing slash from request
|
// StripTrailingSlash returns a middleware which removes trailing slash from request
|
||||||
// path.
|
// path.
|
||||||
@ -24,23 +14,3 @@ func StripTrailingSlash() echo.HandlerFunc {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// RedirectToSlash returns a middleware which redirects requests without trailing
|
|
||||||
// slash path to trailing slash path.
|
|
||||||
func RedirectToSlash(opts ...RedirectToSlashOptions) echo.HandlerFunc {
|
|
||||||
code := http.StatusMovedPermanently
|
|
||||||
|
|
||||||
if len(opts) > 0 {
|
|
||||||
o := opts[0]
|
|
||||||
code = o.Code
|
|
||||||
}
|
|
||||||
|
|
||||||
return func(c *echo.Context) error {
|
|
||||||
p := c.Request().URL.Path
|
|
||||||
l := len(p)
|
|
||||||
if p[l-1] != '/' {
|
|
||||||
c.Redirect(code, p+"/")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -16,12 +16,3 @@ func TestStripTrailingSlash(t *testing.T) {
|
|||||||
StripTrailingSlash()(c)
|
StripTrailingSlash()(c)
|
||||||
assert.Equal(t, "/users", c.Request().URL.Path)
|
assert.Equal(t, "/users", c.Request().URL.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRedirectToSlash(t *testing.T) {
|
|
||||||
req, _ := http.NewRequest(echo.GET, "/users", nil)
|
|
||||||
rec := httptest.NewRecorder()
|
|
||||||
c := echo.NewContext(req, echo.NewResponse(rec), echo.New())
|
|
||||||
RedirectToSlash(RedirectToSlashOptions{Code: http.StatusTemporaryRedirect})(c)
|
|
||||||
assert.Equal(t, http.StatusTemporaryRedirect, rec.Code)
|
|
||||||
assert.Equal(t, "/users/", c.Response().Header().Get("Location"))
|
|
||||||
}
|
|
||||||
|
@ -220,25 +220,6 @@ StripTrailingSlash middleware removes the trailing slash from request path.
|
|||||||
e.Use(mw.StripTrailingSlash())
|
e.Use(mw.StripTrailingSlash())
|
||||||
```
|
```
|
||||||
|
|
||||||
### RedirectToSlash
|
|
||||||
RedirectToSlash middleware redirects requests without trailing slash path to trailing
|
|
||||||
slash path.
|
|
||||||
|
|
||||||
*Options*
|
|
||||||
```go
|
|
||||||
RedirectToSlashOptions struct {
|
|
||||||
Code int
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
*Example*
|
|
||||||
|
|
||||||
```go
|
|
||||||
e.Use(mw.RedirectToSlash())
|
|
||||||
```
|
|
||||||
|
|
||||||
> StripTrailingSlash and RedirectToSlash middleware should not be used together.
|
|
||||||
|
|
||||||
[Examples](https://github.com/labstack/echo/tree/master/examples/middleware)
|
[Examples](https://github.com/labstack/echo/tree/master/examples/middleware)
|
||||||
|
|
||||||
## Request
|
## Request
|
||||||
|
Reference in New Issue
Block a user