From e159f4d7efbad621d88ccf89a727808b9faad4a6 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Thu, 17 Mar 2016 13:24:52 -0700 Subject: [PATCH] Making Echo#Pre actually work. Signed-off-by: Vishal Rana --- echo.go | 25 +++++++------------- echo_test.go | 53 +++++++++++++++++++++--------------------- middleware/compress.go | 5 +--- middleware/logger.go | 2 +- 4 files changed, 38 insertions(+), 47 deletions(-) diff --git a/echo.go b/echo.go index a8eb13c6..49ad6bf7 100644 --- a/echo.go +++ b/echo.go @@ -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) } diff --git a/echo_test.go b/echo_test.go index 30676931..d4fd3d1e 100644 --- a/echo_test.go +++ b/echo_test.go @@ -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) } diff --git a/middleware/compress.go b/middleware/compress.go index 71e59063..7f4aea6a 100644 --- a/middleware/compress.go +++ b/middleware/compress.go @@ -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) }) } } diff --git a/middleware/logger.go b/middleware/logger.go index 58f78e55..d13ebcf8 100644 --- a/middleware/logger.go +++ b/middleware/logger.go @@ -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()