mirror of
https://github.com/labstack/echo.git
synced 2025-01-12 01:22:21 +02:00
Middleware with options
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
58ed17d35e
commit
f5a385b547
@ -34,7 +34,7 @@ func TestEchoIndex(t *testing.T) {
|
|||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
r, _ := http.NewRequest(GET, "/", nil)
|
r, _ := http.NewRequest(GET, "/", nil)
|
||||||
e.ServeHTTP(w, r)
|
e.ServeHTTP(w, r)
|
||||||
if w.Code != 200 {
|
if w.Code != http.StatusOK {
|
||||||
t.Errorf("status code should be 200, found %d", w.Code)
|
t.Errorf("status code should be 200, found %d", w.Code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -45,7 +45,7 @@ func TestEchoFavicon(t *testing.T) {
|
|||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
r, _ := http.NewRequest(GET, "/favicon.ico", nil)
|
r, _ := http.NewRequest(GET, "/favicon.ico", nil)
|
||||||
e.ServeHTTP(w, r)
|
e.ServeHTTP(w, r)
|
||||||
if w.Code != 200 {
|
if w.Code != http.StatusOK {
|
||||||
t.Errorf("status code should be 200, found %d", w.Code)
|
t.Errorf("status code should be 200, found %d", w.Code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -56,7 +56,7 @@ func TestEchoStatic(t *testing.T) {
|
|||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
r, _ := http.NewRequest(GET, "/scripts/main.js", nil)
|
r, _ := http.NewRequest(GET, "/scripts/main.js", nil)
|
||||||
e.ServeHTTP(w, r)
|
e.ServeHTTP(w, r)
|
||||||
if w.Code != 200 {
|
if w.Code != http.StatusOK {
|
||||||
t.Errorf("status code should be 200, found %d", w.Code)
|
t.Errorf("status code should be 200, found %d", w.Code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -227,7 +227,7 @@ func TestEchoGroup(t *testing.T) {
|
|||||||
w = httptest.NewRecorder()
|
w = httptest.NewRecorder()
|
||||||
r, _ = http.NewRequest(GET, "/group3/group4/home", nil)
|
r, _ = http.NewRequest(GET, "/group3/group4/home", nil)
|
||||||
e.ServeHTTP(w, r)
|
e.ServeHTTP(w, r)
|
||||||
if w.Code != 200 {
|
if w.Code != http.StatusOK {
|
||||||
t.Errorf("status code should be 200, found %d", w.Code)
|
t.Errorf("status code should be 200, found %d", w.Code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,15 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import "github.com/labstack/echo"
|
import (
|
||||||
|
"github.com/labstack/echo"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
RedirectToSlashOptions struct {
|
||||||
|
Code int
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
// StripTrailingSlash removes trailing slash from request path.
|
// StripTrailingSlash removes trailing slash from request path.
|
||||||
func StripTrailingSlash() echo.HandlerFunc {
|
func StripTrailingSlash() echo.HandlerFunc {
|
||||||
@ -15,8 +24,16 @@ func StripTrailingSlash() echo.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RedirectToSlash redirects requests without trailing slash path to trailing slash
|
// RedirectToSlash redirects requests without trailing slash path to trailing slash
|
||||||
// path, with status code.
|
// path, with .
|
||||||
func RedirectToSlash(code int) echo.HandlerFunc {
|
func RedirectToSlash(opts ...RedirectToSlashOptions) echo.HandlerFunc {
|
||||||
|
code := http.StatusMovedPermanently
|
||||||
|
|
||||||
|
for _, o := range opts {
|
||||||
|
if o.Code != 0 {
|
||||||
|
code = o.Code
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return func(c *echo.Context) (he *echo.HTTPError) {
|
return func(c *echo.Context) (he *echo.HTTPError) {
|
||||||
p := c.Request.URL.Path
|
p := c.Request.URL.Path
|
||||||
l := len(p)
|
l := len(p)
|
||||||
|
@ -22,10 +22,9 @@ func TestRedirectToSlash(t *testing.T) {
|
|||||||
req, _ := http.NewRequest(echo.GET, "/users", nil)
|
req, _ := http.NewRequest(echo.GET, "/users", nil)
|
||||||
res := &echo.Response{Writer: httptest.NewRecorder()}
|
res := &echo.Response{Writer: httptest.NewRecorder()}
|
||||||
c := echo.NewContext(req, res, echo.New())
|
c := echo.NewContext(req, res, echo.New())
|
||||||
RedirectToSlash(301)(c)
|
RedirectToSlash(RedirectToSlashOptions{Code: http.StatusTemporaryRedirect})(c)
|
||||||
println(c.Response.Header().Get("Location"))
|
if res.Status() != http.StatusTemporaryRedirect {
|
||||||
if res.Status() != 301 {
|
t.Errorf("status code should be 307, found %d", res.Status())
|
||||||
t.Errorf("status code should be 301, found %d", res.Status())
|
|
||||||
}
|
}
|
||||||
if c.Response.Header().Get("Location") != "/users/" {
|
if c.Response.Header().Get("Location") != "/users/" {
|
||||||
t.Error("Location header should be /users/")
|
t.Error("Location header should be /users/")
|
||||||
|
Loading…
Reference in New Issue
Block a user