mirror of
https://github.com/labstack/echo.git
synced 2026-05-16 09:48:24 +02:00
+11
-11
@@ -18,7 +18,7 @@ func TestBasicAuth(t *testing.T) {
|
||||
}
|
||||
return false
|
||||
}
|
||||
b := BasicAuth(fn)
|
||||
ba := BasicAuth(fn)
|
||||
|
||||
//-------------------
|
||||
// Valid credentials
|
||||
@@ -26,14 +26,14 @@ func TestBasicAuth(t *testing.T) {
|
||||
|
||||
auth := Basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
|
||||
req.Header.Set(echo.Authorization, auth)
|
||||
if b(c) != nil {
|
||||
if ba(c) != nil {
|
||||
t.Error("basic auth should pass")
|
||||
}
|
||||
|
||||
// Case insensitive
|
||||
auth = "basic " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
|
||||
req.Header.Set(echo.Authorization, auth)
|
||||
if b(c) != nil {
|
||||
if ba(c) != nil {
|
||||
t.Error("basic auth should ignore case and pass")
|
||||
}
|
||||
|
||||
@@ -43,31 +43,31 @@ func TestBasicAuth(t *testing.T) {
|
||||
|
||||
auth = Basic + " " + base64.StdEncoding.EncodeToString([]byte(" joe: secret"))
|
||||
req.Header.Set(echo.Authorization, auth)
|
||||
b = BasicAuth(fn)
|
||||
if b(c) == nil {
|
||||
ba = BasicAuth(fn)
|
||||
if ba(c) == nil {
|
||||
t.Error("basic auth should fail")
|
||||
}
|
||||
|
||||
// Invalid header
|
||||
auth = base64.StdEncoding.EncodeToString([]byte(" :secret"))
|
||||
req.Header.Set(echo.Authorization, auth)
|
||||
b = BasicAuth(fn)
|
||||
if b(c) == nil {
|
||||
ba = BasicAuth(fn)
|
||||
if ba(c) == nil {
|
||||
t.Error("basic auth should fail for invalid scheme")
|
||||
}
|
||||
|
||||
// Invalid scheme
|
||||
auth = "Base " + base64.StdEncoding.EncodeToString([]byte(" :secret"))
|
||||
req.Header.Set(echo.Authorization, auth)
|
||||
b = BasicAuth(fn)
|
||||
if b(c) == nil {
|
||||
ba = BasicAuth(fn)
|
||||
if ba(c) == nil {
|
||||
t.Error("basic auth should fail for invalid scheme")
|
||||
}
|
||||
|
||||
// Empty auth header
|
||||
req.Header.Set(echo.Authorization, "")
|
||||
b = BasicAuth(fn)
|
||||
if b(c) == nil {
|
||||
ba = BasicAuth(fn)
|
||||
if ba(c) == nil {
|
||||
t.Error("basic auth should fail for empty auth header")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package middleware
|
||||
|
||||
import "github.com/labstack/echo"
|
||||
|
||||
// StripTrailingSlash removes trailing slash from request path.
|
||||
func StripTrailingSlash() echo.HandlerFunc {
|
||||
return func(c *echo.Context) *echo.HTTPError {
|
||||
p := c.Request.URL.Path
|
||||
l := len(p)
|
||||
if p[l-1] == '/' {
|
||||
c.Request.URL.Path = p[:l-1]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// RedirectToSlash redirects requests without trailing slash path to trailing slash
|
||||
// path with status code.
|
||||
func RedirectToSlash(code int) echo.HandlerFunc {
|
||||
return func(c *echo.Context) (he *echo.HTTPError) {
|
||||
p := c.Request.URL.Path
|
||||
l := len(p)
|
||||
if p[l-1] != '/' {
|
||||
c.Redirect(code, p+"/")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/labstack/echo"
|
||||
)
|
||||
|
||||
func TestStripTrailingSlash(t *testing.T) {
|
||||
req, _ := http.NewRequest(echo.GET, "/users/", nil)
|
||||
res := &echo.Response{Writer: httptest.NewRecorder()}
|
||||
c := echo.NewContext(req, res, echo.New())
|
||||
StripTrailingSlash()(c)
|
||||
if c.Request.URL.Path != "/users" {
|
||||
t.Error("it should strip the trailing slash")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedirectToSlash(t *testing.T) {
|
||||
req, _ := http.NewRequest(echo.GET, "/users", nil)
|
||||
res := &echo.Response{Writer: httptest.NewRecorder()}
|
||||
c := echo.NewContext(req, res, echo.New())
|
||||
RedirectToSlash(301)(c)
|
||||
println(c.Response.Header().Get("Location"))
|
||||
if res.Status() != 301 {
|
||||
t.Errorf("status code should be 301, found %d", res.Status())
|
||||
}
|
||||
if c.Response.Header().Get("Location") != "/users/" {
|
||||
t.Error("Location header should be /users/")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user