From 3abe661e7265a67d54766ae099b802759e0fff55 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Wed, 16 Mar 2016 20:15:38 -0700 Subject: [PATCH] Fixed nested group middleware Signed-off-by: Vishal Rana --- echo.go | 10 +++++++--- echo_test.go | 25 ++++++++++++++++--------- engine/engine.go | 21 ++++++++++++++++++++- group.go | 7 +++++-- 4 files changed, 48 insertions(+), 15 deletions(-) diff --git a/echo.go b/echo.go index 906ed351..a8eb13c6 100644 --- a/echo.go +++ b/echo.go @@ -391,9 +391,13 @@ func (e *Echo) add(method, path string, handler Handler, middleware ...Middlewar name := handlerName(handler) e.router.Add(method, path, HandlerFunc(func(c Context) error { h := handler - for _, m := range middleware { - h = m.Handle(h) + // Chain middleware + for i := len(middleware) - 1; i >= 0; i-- { + h = middleware[i].Handle(h) } + // for _, m := range middleware { + // h = m.Handle(h) + // } return h.Handle(c) }), e) r := Route{ @@ -404,7 +408,7 @@ func (e *Echo) add(method, path string, handler Handler, middleware ...Middlewar e.router.routes = append(e.router.routes, r) } -// Group creates a new sub-router with prefix. +// Group creates a new router group with prefix. func (e *Echo) Group(prefix string, m ...Middleware) (g *Group) { g = &Group{prefix: prefix, echo: e} g.Use(m...) diff --git a/echo_test.go b/echo_test.go index 355ebbf8..30676931 100644 --- a/echo_test.go +++ b/echo_test.go @@ -255,17 +255,24 @@ func TestEchoGroup(t *testing.T) { // Group g1 := e.Group("/group1") - g1.Use(MiddlewareFunc(func(h Handler) Handler { - return HandlerFunc(func(c Context) error { - buf.WriteString("1") - return h.Handle(c) - }) - })) + g1.Use(WrapMiddleware(HandlerFunc(func(c Context) error { + buf.WriteString("1") + return h.Handle(c) + + }))) g1.Get("", h) - // Nested groups + // Nested groups with middleware g2 := e.Group("/group2") + g2.Use(WrapMiddleware(HandlerFunc(func(c Context) error { + buf.WriteString("2") + return nil + }))) g3 := g2.Group("/group3") + g3.Use(WrapMiddleware(HandlerFunc(func(c Context) error { + buf.WriteString("3") + return nil + }))) g3.Get("", h) request(GET, "/users", e) @@ -276,8 +283,8 @@ func TestEchoGroup(t *testing.T) { assert.Equal(t, "01", buf.String()) buf.Reset() - c, _ := request(GET, "/group2/group3", e) - assert.Equal(t, http.StatusOK, c) + request(GET, "/group2/group3", e) + assert.Equal(t, "023", buf.String()) } func TestEchoNotFound(t *testing.T) { diff --git a/engine/engine.go b/engine/engine.go index 2157f6c5..b95b7274 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -18,20 +18,39 @@ type ( // Request defines the interface for HTTP request. Request interface { - // TLS returns true if connection is TLS otherwise false. + // TLS returns true if HTTP connection is TLS otherwise false. TLS() bool + // Scheme returns the HTTP protocol scheme, `http` or `https`. Scheme() string + + // Host returns HTTP request host. // For server requests Host specifies the host on which the + // URL is sought. Per RFC 2616, this is either the value of + // the "Host" header or the host name given in the URL itself. Host() string + + // URI returns the unmodified `Request-URI` sent by the client. URI() string + + // URL returns `engine.URL`. URL() URL + + // Header returns `engine.Header`. Header() Header + // Proto() string // ProtoMajor() int // ProtoMinor() int + + // UserAgent returns the client's `User-Agent`. UserAgent() string + + // RemoteAddress returns the client's network address. RemoteAddress() string + + // Method returns the HTTP method of the request. Method() string + SetMethod(string) Body() io.ReadCloser FormValue(string) string diff --git a/group.go b/group.go index c81cc58d..6a6f852f 100644 --- a/group.go +++ b/group.go @@ -60,7 +60,9 @@ func (g *Group) Match(methods []string, path string, handler Handler, middleware } } +// Group creates a new sub-group with prefix. func (g *Group) Group(prefix string, m ...Middleware) *Group { + m = append(g.middleware, m...) return g.echo.Group(g.prefix+prefix, m...) } @@ -71,8 +73,9 @@ func (g *Group) add(method, path string, handler Handler, middleware ...Middlewa g.echo.router.Add(method, path, HandlerFunc(func(c Context) error { h := handler - for _, m := range middleware { - h = m.Handle(h) + // Chain middleware + for i := len(middleware) - 1; i >= 0; i-- { + h = middleware[i].Handle(h) } return h.Handle(c) }), g.echo)