diff --git a/router.go b/router.go index 318f2a22..a63af596 100644 --- a/router.go +++ b/router.go @@ -347,12 +347,13 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo } if search == "" { - // TODO: Needs improvement - if cn.findChildWithType(mtype) == nil { - continue + if cn.handler == nil { + // Look up for match-any, might have an empty value for *, e.g. + // serving a directory. Issue #207 + cn = cn.findChildWithType(mtype) + ctx.pvalues[len(ctx.pvalues)-1] = "" } - // Empty value - goto MatchAny + continue } // Static node @@ -390,7 +391,7 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo // Match-any node MatchAny: - // c = cn.getChild() + // c = cn.getChild() c = cn.findChildWithType(mtype) if c != nil { cn = c diff --git a/router_test.go b/router_test.go index 3e9abebd..e1d38df0 100644 --- a/router_test.go +++ b/router_test.go @@ -321,19 +321,32 @@ func TestRouterTwoParam(t *testing.T) { func TestRouterMatchAny(t *testing.T) { e := New() r := e.router + + // Routes + r.Add(GET, "/", func(*Context) error { + return nil + }, e) + r.Add(GET, "/*", func(*Context) error { + return nil + }, e) r.Add(GET, "/users/*", func(*Context) error { return nil }, e) c := NewContext(nil, nil, e) - h, _ := r.Find(GET, "/users/", c) + h, _ := r.Find(GET, "/", c) if assert.NotNil(t, h) { assert.Equal(t, "", c.P(0)) } - h, _ = r.Find(GET, "/users/1", c) + h, _ = r.Find(GET, "/download", c) if assert.NotNil(t, h) { - assert.Equal(t, "1", c.P(0)) + assert.Equal(t, "download", c.P(0)) + } + + h, _ = r.Find(GET, "/users/joe", c) + if assert.NotNil(t, h) { + assert.Equal(t, "joe", c.P(0)) } }