mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
Removed RedirectToSlash middleware
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
9b47516c50
commit
b4da73313f
@ -1,16 +1,6 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo"
|
||||
)
|
||||
|
||||
type (
|
||||
RedirectToSlashOptions struct {
|
||||
Code int
|
||||
}
|
||||
)
|
||||
import "github.com/labstack/echo"
|
||||
|
||||
// StripTrailingSlash returns a middleware which removes trailing slash from request
|
||||
// path.
|
||||
@ -24,23 +14,3 @@ func StripTrailingSlash() echo.HandlerFunc {
|
||||
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)
|
||||
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())
|
||||
```
|
||||
|
||||
### 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)
|
||||
|
||||
## Request
|
||||
|
Loading…
Reference in New Issue
Block a user