Skip to content

Commit

Permalink
Added slash middleware
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed May 14, 2015
1 parent 6b02099 commit 54cad1f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 18 deletions.
22 changes: 11 additions & 11 deletions middleware/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ func TestBasicAuth(t *testing.T) {
}
return false
}
b := BasicAuth(fn)
ba := BasicAuth(fn)

//-------------------
// Valid credentials
//-------------------

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")
}

Expand All @@ -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 changes: 28 additions & 0 deletions middleware/slash.go
Original file line number Diff line number Diff line change
@@ -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 changes: 33 additions & 0 deletions middleware/slash_test.go
Original file line number Diff line number Diff line change
@@ -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/")
}
}
14 changes: 7 additions & 7 deletions website/docs/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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")
Expand All @@ -203,25 +203,25 @@ 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")
```

### 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")
Expand Down

0 comments on commit 54cad1f

Please sign in to comment.