1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-12 01:22:21 +02:00

Making Echo#Pre actually work.

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-03-17 13:24:52 -07:00
parent 6f0a7372fb
commit e159f4d7ef
4 changed files with 38 additions and 47 deletions

25
echo.go
View File

@ -192,15 +192,11 @@ func New() (e *Echo) {
}
e.router = NewRouter(e)
e.middleware = []Middleware{e.router}
e.head = e.router.Handle(HandlerFunc(func(c Context) error {
e.head = HandlerFunc(func(c Context) error {
return c.Handle(c)
}))
})
e.pristineHead = e.head
// e.head = HandlerFunc(func(c Context) error {
// return c.Handle(c)
// })
// e.router.Handle(e.head)
e.chainMiddleware()
//----------
// Defaults
@ -294,21 +290,18 @@ func (e *Echo) Debug() bool {
// Pre adds middleware to the chain which is run before router.
func (e *Echo) Pre(middleware ...Middleware) {
e.middleware = append(e.middleware, middleware...)
e.head = e.pristineHead
// Chain middleware
for i := len(e.middleware) - 1; i >= 0; i-- {
e.head = e.middleware[i].Handle(e.head)
}
e.middleware = append(middleware, e.middleware...)
e.chainMiddleware()
}
// Use adds middleware to the chain which is run after router.
func (e *Echo) Use(middleware ...Middleware) {
e.middleware = append(e.middleware, middleware...)
e.head = e.pristineHead
e.chainMiddleware()
}
// Chain middleware
func (e *Echo) chainMiddleware() {
e.head = e.pristineHead
for i := len(e.middleware) - 1; i >= 0; i-- {
e.head = e.middleware[i].Handle(e.head)
}

View File

@ -83,26 +83,26 @@ func TestEchoMiddleware(t *testing.T) {
e := New()
buf := new(bytes.Buffer)
e.Use(MiddlewareFunc(func(h Handler) Handler {
return HandlerFunc(func(c Context) error {
buf.WriteString("a")
return h.Handle(c)
})
}))
e.Pre(WrapMiddleware(HandlerFunc(func(c Context) error {
assert.Empty(t, c.Path())
buf.WriteString("-1")
return nil
})))
e.Use(MiddlewareFunc(func(h Handler) Handler {
return HandlerFunc(func(c Context) error {
buf.WriteString("b")
return h.Handle(c)
})
}))
e.Use(WrapMiddleware(HandlerFunc(func(c Context) error {
buf.WriteString("1")
return nil
})))
e.Use(MiddlewareFunc(func(h Handler) Handler {
return HandlerFunc(func(c Context) error {
buf.WriteString("c")
return h.Handle(c)
})
}))
e.Use(WrapMiddleware(HandlerFunc(func(c Context) error {
buf.WriteString("2")
return nil
})))
e.Use(WrapMiddleware(HandlerFunc(func(c Context) error {
buf.WriteString("3")
return nil
})))
// Route
e.Get("/", HandlerFunc(func(c Context) error {
@ -110,17 +110,18 @@ func TestEchoMiddleware(t *testing.T) {
}))
c, b := request(GET, "/", e)
assert.Equal(t, "abc", buf.String())
assert.Equal(t, "-1123", buf.String())
assert.Equal(t, http.StatusOK, c)
assert.Equal(t, "OK", b)
}
// Error
e.Use(MiddlewareFunc(func(Handler) Handler {
return HandlerFunc(func(c Context) error {
return errors.New("error")
})
}))
c, b = request(GET, "/", e)
func TestEchoMiddlewareError(t *testing.T) {
e := New()
e.Use(WrapMiddleware(HandlerFunc(func(c Context) error {
return errors.New("error")
})))
e.Get("/", notFoundHandler)
c, _ := request(GET, "/", e)
assert.Equal(t, http.StatusInternalServerError, c)
}

View File

@ -54,10 +54,7 @@ func GzipFromConfig(config GzipConfig) echo.MiddlewareFunc {
c.Response().Header().Set(echo.ContentEncoding, scheme)
c.Response().SetWriter(g)
}
if err := next.Handle(c); err != nil {
c.Error(err)
}
return nil
return next.Handle(c)
})
}
}

View File

@ -39,7 +39,7 @@ func LoggerFromConfig(config LoggerConfig) echo.MiddlewareFunc {
start := time.Now()
if err := next.Handle(c); err != nil {
c.Error(err)
return err
}
stop := time.Now()
method := req.Method()