1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-28 08:38:39 +02:00
This commit is contained in:
Jeffrey Reuling (TI) 2018-03-16 15:05:22 +01:00 committed by Vishal Rana
parent d79727cf89
commit ee32e3e7fb
2 changed files with 33 additions and 4 deletions

12
echo.go
View File

@ -558,13 +558,13 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c.Reset(r, w)
method := r.Method
path := r.URL.RawPath
if path == "" {
path = r.URL.Path
}
h := NotFoundHandler
if e.premiddleware == nil {
path := r.URL.RawPath
if path == "" {
path = r.URL.Path
}
e.router.Find(method, path, c)
h = c.Handler()
for i := len(e.middleware) - 1; i >= 0; i-- {
@ -572,6 +572,10 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
} else {
h = func(c Context) error {
path := r.URL.RawPath
if path == "" {
path = r.URL.Path
}
e.router.Find(method, path, c)
h := c.Handler()
for i := len(e.middleware) - 1; i >= 0; i-- {

View File

@ -33,3 +33,28 @@ func TestRewrite(t *testing.T) {
e.ServeHTTP(rec, req)
assert.Equal(t, "/user/jack/order/1", req.URL.Path)
}
//Issue #1086
func TestEchoRewritePreMiddleware(t *testing.T) {
e := echo.New()
r := e.Router()
// Rewrite old url to new one
e.Pre(RewriteWithConfig(RewriteConfig{
Rules: map[string]string{
"/old": "/new",
},
}))
// Route
r.Add(echo.GET, "/new", func(c echo.Context) error {
return c.NoContent(200)
return nil
})
req := httptest.NewRequest(echo.GET, "/old", nil)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
assert.Equal(t, "/new", req.URL.Path)
assert.Equal(t, 200, rec.Code)
}