Skip to content

Commit

Permalink
Fixed #207
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <vr@labstack.com>
  • Loading branch information
vishr committed Sep 17, 2015
1 parent eef1574 commit 853e650
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
13 changes: 7 additions & 6 deletions router.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 16 additions & 3 deletions router_test.go
Expand Up @@ -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))
}
}

Expand Down

0 comments on commit 853e650

Please sign in to comment.