mirror of
https://github.com/labstack/echo.git
synced 2025-02-09 13:36:57 +02:00
commit
18dcc4bcca
@ -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")
|
||||
}
|
||||
}
|
||||
|
28
middleware/slash.go
Normal file
28
middleware/slash.go
Normal file
@ -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
|
||||
}
|
||||
}
|
33
middleware/slash_test.go
Normal file
33
middleware/slash_test.go
Normal file
@ -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/")
|
||||
}
|
||||
}
|
@ -87,7 +87,7 @@ types of handlers.
|
||||
|
||||
### Path parameter
|
||||
|
||||
URL path parameters can be extracted either by name `echo.Context.Param(name string) string`
|
||||
Request path parameters can be extracted either by name `echo.Context.Param(name string) string`
|
||||
or by index `echo.Context.P(i uint8) string`. Getting parameter by index gives a
|
||||
slightly better performance.
|
||||
|
||||
@ -194,7 +194,7 @@ Sends an HTML HTTP response with status code.
|
||||
### Static files
|
||||
|
||||
`echo.Static(path, root string)` serves static files. For example, code below serves
|
||||
files from directory `public/scripts` for any URL path starting with `/scripts/`.
|
||||
files from directory `public/scripts` for any request path starting with `/scripts/`.
|
||||
|
||||
```go
|
||||
e.Static("/scripts/", "public/scripts")
|
||||
@ -203,7 +203,7 @@ e.Static("/scripts/", "public/scripts")
|
||||
### Serving a file
|
||||
|
||||
`echo.ServeFile(path, file string)` serves a file. For example, code below serves
|
||||
file `welcome.html` for URL path `/welcome`.
|
||||
file `welcome.html` for request path `/welcome`.
|
||||
|
||||
```go
|
||||
e.ServeFile("/welcome", "welcome.html")
|
||||
@ -211,17 +211,17 @@ e.ServeFile("/welcome", "welcome.html")
|
||||
|
||||
### Serving an index file
|
||||
|
||||
`echo.Index(file string)` serves an index file. For example, code below serves file
|
||||
`index.html`.
|
||||
`echo.Index(file string)` serves root index page - `GET /`. For example, code below
|
||||
serves root index page from file `public/index.html`.
|
||||
|
||||
```go
|
||||
e.Index("index.html")
|
||||
e.Index("public/index.html")
|
||||
```
|
||||
|
||||
### Serving favicon
|
||||
|
||||
`echo.Favicon(file string)` serves default favicon - `GET /favicon.ico`. For example,
|
||||
code below serves file `favicon.ico`.
|
||||
code below serves favicon from file `public/favicon.ico`.
|
||||
|
||||
```go
|
||||
e.Index("public/favicon.ico")
|
||||
|
Loading…
x
Reference in New Issue
Block a user