1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-10 00:28:23 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-02-24 08:55:08 -08:00
parent edfed6497e
commit feef0c0b8f
2 changed files with 37 additions and 57 deletions

View File

@ -375,12 +375,15 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
// Any node // Any node
Any: Any:
if cn = cn.findChildByKind(akind); cn == nil { if cn = cn.findChildByKind(akind); cn == nil {
cn = nn if nn != nil {
search = ns cn = nn
if nk == pkind { nn = nil // Next
goto Param search = ns
} else if nk == akind { if nk == pkind {
goto Any goto Param
} else if nk == akind {
goto Any
}
} }
// Not found // Not found
return return

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"strconv"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -503,67 +504,43 @@ func TestRouterPriority(t *testing.T) {
} }
} }
// Issue #217 // Issue #217, #372
func TestRouterPriorityNext(t *testing.T) { func TestRouterPriorityNext(t *testing.T) {
e := New() e := New()
r := e.router r := e.router
// Routes routes := []Route{
r.Add(GET, "/aa", func(c *Context) error { {"GET", "/aa", nil},
c.Set("a", 1) {"GET", "/ab", nil},
return nil {"GET", "/abc", nil},
}, e) {"GET", "/abd", nil},
r.Add(GET, "/ab", func(c *Context) error { {"GET", "/a/foo", nil},
c.Set("b", 2) {"GET", "/a/bar", nil},
return nil {"GET", "/*", nil},
}, e) }
r.Add(GET, "/abc", func(c *Context) error {
c.Set("c", 3) for i, route := range routes {
return nil a := strconv.Itoa(i)
}, e) r.Add(route.Method, route.Path, func(c *Context) error {
r.Add(GET, "/abd", func(c *Context) error { c.Set(a, a)
c.Set("d", 4) return nil
return nil }, e)
}, e) }
r.Add(GET, "/*", func(c *Context) error {
c.Set("e", 5)
return nil
}, e)
c := NewContext(nil, nil, e) c := NewContext(nil, nil, e)
for i, route := range routes {
// Route > /aa a := strconv.Itoa(i)
h, _ := r.Find(GET, "/aa", c) h, _ := r.Find(route.Method, route.Path, c)
if assert.NotNil(t, h) { if assert.NotNil(t, h) {
h(c) h(c)
assert.Equal(t, 1, c.Get("a")) assert.Equal(t, a, c.Get(a))
} }
// Route > /ab
h, _ = r.Find(GET, "/ab", c)
if assert.NotNil(t, h) {
h(c)
assert.Equal(t, 2, c.Get("b"))
}
// Route > /abc
h, _ = r.Find(GET, "/abc", c)
if assert.NotNil(t, h) {
h(c)
assert.Equal(t, 3, c.Get("c"))
}
// Route > /abd
h, _ = r.Find(GET, "/abd", c)
if assert.NotNil(t, h) {
h(c)
assert.Equal(t, 4, c.Get("d"))
} }
// Route > /* // Route > /*
h, _ = r.Find(GET, "/abc.html", c) h, _ := r.Find(GET, "/abc.html", c)
if assert.NotNil(t, h) { if assert.NotNil(t, h) {
h(c) h(c)
assert.Equal(t, 5, c.Get("e")) assert.Equal(t, "abc.html", c.P(0))
} }
} }