1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Merge pull request #191 from labstack/issue-190

StripTrailingSlash is now an option
This commit is contained in:
Vishal Rana 2015-09-01 08:08:21 -07:00
commit e7b1358de5
8 changed files with 57 additions and 87 deletions

13
echo.go
View File

@ -34,6 +34,7 @@ type (
renderer Renderer
pool sync.Pool
debug bool
stripTrailingSlash bool
router *Router
}
@ -248,14 +249,14 @@ func (e *Echo) SetRenderer(r Renderer) {
e.renderer = r
}
// SetDebug sets debug mode.
func (e *Echo) SetDebug(on bool) {
e.debug = on
// Debug enables debug mode.
func (e *Echo) Debug() {
e.debug = true
}
// Debug returns debug mode.
func (e *Echo) Debug() bool {
return e.debug
// StripTrailingSlash enables removing trailing slash from the request path.
func (e *Echo) StripTrailingSlash() {
e.stripTrailingSlash = true
}
// Use adds handler to the middleware chain.

View File

@ -33,8 +33,8 @@ func TestEcho(t *testing.T) {
assert.NotNil(t, e.Router())
// Debug
e.SetDebug(true)
assert.True(t, e.Debug())
e.Debug()
assert.True(t, e.debug)
// DefaultHTTPErrorHandler
e.DefaultHTTPErrorHandler(errors.New("error"), c)
@ -403,6 +403,15 @@ func TestEchoServer(t *testing.T) {
assert.IsType(t, &http.Server{}, s)
}
func TestStripTrailingSlash(t *testing.T) {
e := New()
e.StripTrailingSlash()
r, _ := http.NewRequest(GET, "/users/", nil)
w := httptest.NewRecorder()
e.ServeHTTP(w, r)
assert.Equal(t, http.StatusNotFound, w.Code)
}
func testMethod(t *testing.T, method, path string, e *Echo) {
m := fmt.Sprintf("%c%s", method[0], strings.ToLower(method[1:]))
p := reflect.ValueOf(path)

View File

@ -17,7 +17,7 @@ func main() {
e := echo.New()
// Debug mode
e.SetDebug(true)
e.Debug()
//------------
// Middleware
@ -37,16 +37,6 @@ func main() {
return false
}))
//-------
// Slash
//-------
e.Use(mw.StripTrailingSlash())
// or
// e.Use(mw.RedirectToSlash())
// Gzip
e.Use(mw.Gzip())

View File

@ -11,7 +11,7 @@ import (
func TestRecover(t *testing.T) {
e := echo.New()
e.SetDebug(true)
e.Debug()
req, _ := http.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := echo.NewContext(req, echo.NewResponse(rec), e)

View File

@ -1,16 +0,0 @@
package middleware
import "github.com/labstack/echo"
// StripTrailingSlash returns a middleware which removes trailing slash from request
// path.
func StripTrailingSlash() echo.HandlerFunc {
return func(c *echo.Context) error {
p := c.Request().URL.Path
l := len(p)
if p[l-1] == '/' {
c.Request().URL.Path = p[:l-1]
}
return nil
}
}

View File

@ -1,18 +0,0 @@
package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo"
"github.com/stretchr/testify/assert"
)
func TestStripTrailingSlash(t *testing.T) {
req, _ := http.NewRequest(echo.GET, "/users/", nil)
rec := httptest.NewRecorder()
c := echo.NewContext(req, echo.NewResponse(rec), echo.New())
StripTrailingSlash()(c)
assert.Equal(t, "/users", c.Request().URL.Path)
}

View File

@ -282,9 +282,17 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
h = badRequestHandler
return
}
search := path
// Strip trailing slash
if r.echo.stripTrailingSlash {
l := len(path)
if path[l - 1] == '/' {
path = path[:l - 1]
}
}
var (
search = path
c *node // Child node
n int // Param counter
nt ntype // Next type

View File

@ -50,6 +50,12 @@ Enables debug mode.
`Echo.DisableColoredLog()`
### StripTrailingSlash
StripTrailingSlash enables removing trailing slash from the request path.
`e.StripTrailingSlash()`
## Routing
Echo's router is [fast, optimized](https://github.com/labstack/echo#benchmark) and
@ -210,16 +216,6 @@ to the centralized [HTTPErrorHandler](#error-handling).
e.Use(mw.Recover())
```
### StripTrailingSlash
StripTrailingSlash middleware removes the trailing slash from request path.
*Example*
```go
e.Use(mw.StripTrailingSlash())
```
[Examples](https://github.com/labstack/echo/tree/master/examples/middleware)
## Request