Skip to content

Commit

Permalink
Fix issue with middleware from one route leaking into the middleware …
Browse files Browse the repository at this point in the history
…of other routes

Fixed labstack#565

Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
Russ Egan authored and vishr committed Jun 16, 2016
1 parent 1ca76e5 commit 2eb5d97
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
1 change: 0 additions & 1 deletion echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,6 @@ func (e *Echo) add(method, path string, handler HandlerFunc, middleware ...Middl
Handler: name,
}
e.router.routes[method+path] = r
// e.router.routes = append(e.router.routes, r)
}

// Group creates a new router group with prefix and optional group-level middleware.
Expand Down
15 changes: 11 additions & 4 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ func (g *Group) Match(methods []string, path string, handler HandlerFunc, middle
}

// Group creates a new sub-group with prefix and optional sub-group-level middleware.
func (g *Group) Group(prefix string, m ...MiddlewareFunc) *Group {
m = append(g.middleware, m...)
func (g *Group) Group(prefix string, middleware ...MiddlewareFunc) *Group {
m := []MiddlewareFunc{}
m = append(m, g.middleware...)
m = append(m, middleware...)
return g.echo.Group(g.prefix+prefix, m...)
}

Expand All @@ -142,6 +144,11 @@ func (g *Group) File(path, file string) {
}

func (g *Group) add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
middleware = append(g.middleware, middleware...)
g.echo.add(method, g.prefix+path, handler, middleware...)
// Combine into a new slice, to avoid accidentally passing the same
// slice for multiple routes, which would lead to later add() calls overwriting
// the middleware from earlier calls
m := []MiddlewareFunc{}
m = append(m, g.middleware...)
m = append(m, middleware...)
g.echo.add(method, g.prefix+path, handler, m...)
}
25 changes: 24 additions & 1 deletion group_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package echo

import "testing"
import (
"github.com/stretchr/testify/assert"
"testing"
)

// TODO: Fix me
func TestGroup(t *testing.T) {
Expand Down Expand Up @@ -29,3 +32,23 @@ func TestGroup(t *testing.T) {
g.Static("/static", "/tmp")
g.File("/walle", "_fixture/images//walle.png")
}

func TestGroupRouteMiddleware(t *testing.T) {
// Ensure middleware slices are not re-used
e := New()
g := e.Group("/group")
h := func(Context) error { return nil }
m1 := WrapMiddleware(func(c Context) error { return nil })
m2 := WrapMiddleware(func(c Context) error { return nil })
m3 := WrapMiddleware(func(c Context) error { return nil })
m4 := WrapMiddleware(func(c Context) error { return c.NoContent(404) })
m5 := WrapMiddleware(func(c Context) error { return c.NoContent(405) })
g.Use(m1, m2, m3)
g.GET("/404", h, m4)
g.GET("/405", h, m5)

c, _ := request(GET, "/group/404", e)
assert.Equal(t, 404, c)
c, _ = request(GET, "/group/405", e)
assert.Equal(t, 405, c)
}

0 comments on commit 2eb5d97

Please sign in to comment.