mirror of
https://github.com/labstack/echo.git
synced 2025-06-06 23:46:16 +02:00
fix issue #1086
This commit is contained in:
parent
d79727cf89
commit
ee32e3e7fb
10
echo.go
10
echo.go
@ -558,13 +558,13 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
c.Reset(r, w)
|
c.Reset(r, w)
|
||||||
|
|
||||||
method := r.Method
|
method := r.Method
|
||||||
|
h := NotFoundHandler
|
||||||
|
|
||||||
|
if e.premiddleware == nil {
|
||||||
path := r.URL.RawPath
|
path := r.URL.RawPath
|
||||||
if path == "" {
|
if path == "" {
|
||||||
path = r.URL.Path
|
path = r.URL.Path
|
||||||
}
|
}
|
||||||
h := NotFoundHandler
|
|
||||||
|
|
||||||
if e.premiddleware == nil {
|
|
||||||
e.router.Find(method, path, c)
|
e.router.Find(method, path, c)
|
||||||
h = c.Handler()
|
h = c.Handler()
|
||||||
for i := len(e.middleware) - 1; i >= 0; i-- {
|
for i := len(e.middleware) - 1; i >= 0; i-- {
|
||||||
@ -572,6 +572,10 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
h = func(c Context) error {
|
h = func(c Context) error {
|
||||||
|
path := r.URL.RawPath
|
||||||
|
if path == "" {
|
||||||
|
path = r.URL.Path
|
||||||
|
}
|
||||||
e.router.Find(method, path, c)
|
e.router.Find(method, path, c)
|
||||||
h := c.Handler()
|
h := c.Handler()
|
||||||
for i := len(e.middleware) - 1; i >= 0; i-- {
|
for i := len(e.middleware) - 1; i >= 0; i-- {
|
||||||
|
@ -33,3 +33,28 @@ func TestRewrite(t *testing.T) {
|
|||||||
e.ServeHTTP(rec, req)
|
e.ServeHTTP(rec, req)
|
||||||
assert.Equal(t, "/user/jack/order/1", req.URL.Path)
|
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)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user