Skip to content

Commit ee32e3e

Browse files
Jeffrey Reuling (TI)vishr
authored andcommitted
fix issue #1086
1 parent d79727c commit ee32e3e

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

echo.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,20 +558,24 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
558558
c.Reset(r, w)
559559

560560
method := r.Method
561-
path := r.URL.RawPath
562-
if path == "" {
563-
path = r.URL.Path
564-
}
565561
h := NotFoundHandler
566562

567563
if e.premiddleware == nil {
564+
path := r.URL.RawPath
565+
if path == "" {
566+
path = r.URL.Path
567+
}
568568
e.router.Find(method, path, c)
569569
h = c.Handler()
570570
for i := len(e.middleware) - 1; i >= 0; i-- {
571571
h = e.middleware[i](h)
572572
}
573573
} else {
574574
h = func(c Context) error {
575+
path := r.URL.RawPath
576+
if path == "" {
577+
path = r.URL.Path
578+
}
575579
e.router.Find(method, path, c)
576580
h := c.Handler()
577581
for i := len(e.middleware) - 1; i >= 0; i-- {

middleware/rewrite_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,28 @@ func TestRewrite(t *testing.T) {
3333
e.ServeHTTP(rec, req)
3434
assert.Equal(t, "/user/jack/order/1", req.URL.Path)
3535
}
36+
37+
//Issue #1086
38+
func TestEchoRewritePreMiddleware(t *testing.T) {
39+
e := echo.New()
40+
r := e.Router()
41+
42+
// Rewrite old url to new one
43+
e.Pre(RewriteWithConfig(RewriteConfig{
44+
Rules: map[string]string{
45+
"/old": "/new",
46+
},
47+
}))
48+
49+
// Route
50+
r.Add(echo.GET, "/new", func(c echo.Context) error {
51+
return c.NoContent(200)
52+
return nil
53+
})
54+
55+
req := httptest.NewRequest(echo.GET, "/old", nil)
56+
rec := httptest.NewRecorder()
57+
e.ServeHTTP(rec, req)
58+
assert.Equal(t, "/new", req.URL.Path)
59+
assert.Equal(t, 200, rec.Code)
60+
}

0 commit comments

Comments
 (0)