1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

router changes

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-10-21 20:36:49 -07:00
parent be7d6962f0
commit 36251e88e2
4 changed files with 43 additions and 43 deletions

View File

@ -229,12 +229,12 @@ func TestContextPath(t *testing.T) {
e := New()
r := e.Router()
r.Add(GET, "/users/:id", nil, e)
r.Add(GET, "/users/:id", nil)
c := e.NewContext(nil, nil)
r.Find(GET, "/users/1", c)
assert.Equal(t, "/users/:id", c.Path())
r.Add(GET, "/users/:uid/files/:fid", nil, e)
r.Add(GET, "/users/:uid/files/:fid", nil)
c = e.NewContext(nil, nil)
r.Find(GET, "/users/1/files/1", c)
assert.Equal(t, "/users/:uid/files/:fid", c.Path())
@ -388,7 +388,7 @@ func TestContextHandler(t *testing.T) {
r.Add(GET, "/handler", func(Context) error {
_, err := b.Write([]byte("handler"))
return err
}, e)
})
c := e.NewContext(nil, nil)
r.Find(GET, "/handler", c)
c.Handler()(c)

View File

@ -405,7 +405,7 @@ func (e *Echo) add(method, path string, handler HandlerFunc, middleware ...Middl
h = middleware[i](h)
}
return h(c)
}, e)
})
r := Route{
Method: method,
Path: path,

View File

@ -51,7 +51,7 @@ func NewRouter(e *Echo) *Router {
}
// Add registers a new route for method and path with matching handler.
func (r *Router) Add(method, path string, h HandlerFunc, e *Echo) {
func (r *Router) Add(method, path string, h HandlerFunc) {
// Validate path
if path == "" {
panic("echo: path cannot be empty")
@ -66,7 +66,7 @@ func (r *Router) Add(method, path string, h HandlerFunc, e *Echo) {
if path[i] == ':' {
j := i + 1
r.insert(method, path[:i], nil, skind, "", nil, e)
r.insert(method, path[:i], nil, skind, "", nil)
for ; i < l && path[i] != '/'; i++ {
}
@ -75,26 +75,26 @@ func (r *Router) Add(method, path string, h HandlerFunc, e *Echo) {
i, l = j, len(path)
if i == l {
r.insert(method, path[:i], h, pkind, ppath, pnames, e)
r.insert(method, path[:i], h, pkind, ppath, pnames)
return
}
r.insert(method, path[:i], nil, pkind, ppath, pnames, e)
r.insert(method, path[:i], nil, pkind, ppath, pnames)
} else if path[i] == '*' {
r.insert(method, path[:i], nil, skind, "", nil, e)
r.insert(method, path[:i], nil, skind, "", nil)
pnames = append(pnames, "*")
r.insert(method, path[:i+1], h, akind, ppath, pnames, e)
r.insert(method, path[:i+1], h, akind, ppath, pnames)
return
}
}
r.insert(method, path, h, skind, ppath, pnames, e)
r.insert(method, path, h, skind, ppath, pnames)
}
func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string, pnames []string, e *Echo) {
func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string, pnames []string) {
// Adjust max param
l := len(pnames)
if *e.maxParam < l {
*e.maxParam = l
if *r.echo.maxParam < l {
*r.echo.maxParam = l
}
cn := r.tree // Current node as root

View File

@ -280,7 +280,7 @@ func TestRouterStatic(t *testing.T) {
r.Add(GET, path, func(c Context) error {
c.Set("path", path)
return nil
}, e)
})
c := e.NewContext(nil, nil).(*context)
r.Find(GET, path, c)
c.handler(c)
@ -292,7 +292,7 @@ func TestRouterParam(t *testing.T) {
r := e.router
r.Add(GET, "/users/:id", func(c Context) error {
return nil
}, e)
})
c := e.NewContext(nil, nil).(*context)
r.Find(GET, "/users/1", c)
assert.Equal(t, "1", c.Param("id"))
@ -303,7 +303,7 @@ func TestRouterTwoParam(t *testing.T) {
r := e.router
r.Add(GET, "/users/:uid/files/:fid", func(Context) error {
return nil
}, e)
})
c := e.NewContext(nil, nil).(*context)
r.Find(GET, "/users/1/files/1", c)
@ -318,11 +318,11 @@ func TestRouterParamWithSlash(t *testing.T) {
r.Add(GET, "/a/:b/c/d/:e", func(c Context) error {
return nil
}, e)
})
r.Add(GET, "/a/:b/c/:d/:f", func(c Context) error {
return nil
}, e)
})
c := e.NewContext(nil, nil).(*context)
assert.NotPanics(t, func() {
@ -337,13 +337,13 @@ func TestRouterMatchAny(t *testing.T) {
// 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 := e.NewContext(nil, nil).(*context)
r.Find(GET, "/", c)
@ -361,7 +361,7 @@ func TestRouterMicroParam(t *testing.T) {
r := e.router
r.Add(GET, "/:a/:b/:c", func(c Context) error {
return nil
}, e)
})
c := e.NewContext(nil, nil).(*context)
r.Find(GET, "/1/2/3", c)
assert.Equal(t, "1", c.Param("a"))
@ -376,7 +376,7 @@ func TestRouterMixParamMatchAny(t *testing.T) {
// Route
r.Add(GET, "/users/:id/*", func(c Context) error {
return nil
}, e)
})
c := e.NewContext(nil, nil).(*context)
r.Find(GET, "/users/joe/comments", c)
@ -392,10 +392,10 @@ func TestRouterMultiRoute(t *testing.T) {
r.Add(GET, "/users", func(c Context) error {
c.Set("path", "/users")
return nil
}, e)
})
r.Add(GET, "/users/:id", func(c Context) error {
return nil
}, e)
})
c := e.NewContext(nil, nil).(*context)
// Route > /users
@ -422,31 +422,31 @@ func TestRouterPriority(t *testing.T) {
r.Add(GET, "/users", func(c Context) error {
c.Set("a", 1)
return nil
}, e)
})
r.Add(GET, "/users/new", func(c Context) error {
c.Set("b", 2)
return nil
}, e)
})
r.Add(GET, "/users/:id", func(c Context) error {
c.Set("c", 3)
return nil
}, e)
})
r.Add(GET, "/users/dew", func(c Context) error {
c.Set("d", 4)
return nil
}, e)
})
r.Add(GET, "/users/:id/files", func(c Context) error {
c.Set("e", 5)
return nil
}, e)
})
r.Add(GET, "/users/newsee", func(c Context) error {
c.Set("f", 6)
return nil
}, e)
})
r.Add(GET, "/users/*", func(c Context) error {
c.Set("g", 7)
return nil
}, e)
})
c := e.NewContext(nil, nil).(*context)
// Route > /users
@ -496,11 +496,11 @@ func TestRouterPriorityNotFound(t *testing.T) {
r.Add(GET, "/a/foo", func(c Context) error {
c.Set("a", 1)
return nil
}, e)
})
r.Add(GET, "/a/bar", func(c Context) error {
c.Set("b", 2)
return nil
}, e)
})
// Find
r.Find(GET, "/a/foo", c)
@ -525,13 +525,13 @@ func TestRouterParamNames(t *testing.T) {
r.Add(GET, "/users", func(c Context) error {
c.Set("path", "/users")
return nil
}, e)
})
r.Add(GET, "/users/:id", func(c Context) error {
return nil
}, e)
})
r.Add(GET, "/users/:uid/files/:fid", func(c Context) error {
return nil
}, e)
})
c := e.NewContext(nil, nil).(*context)
// Route > /users
@ -561,15 +561,15 @@ func TestRouterStaticDynamicConflict(t *testing.T) {
r.Add(GET, "/dictionary/skills", func(c Context) error {
c.Set("a", 1)
return nil
}, e)
})
r.Add(GET, "/dictionary/:name", func(c Context) error {
c.Set("b", 2)
return nil
}, e)
})
r.Add(GET, "/server", func(c Context) error {
c.Set("c", 3)
return nil
}, e)
})
r.Find(GET, "/dictionary/skills", c)
c.Handler()(c)
@ -591,7 +591,7 @@ func TestRouterAPI(t *testing.T) {
for _, route := range api {
r.Add(route.Method, route.Path, func(c Context) error {
return nil
}, e)
})
}
c := e.NewContext(nil, nil).(*context)
for _, route := range api {
@ -613,7 +613,7 @@ func BenchmarkRouterGitHubAPI(b *testing.B) {
for _, route := range api {
r.Add(route.Method, route.Path, func(c Context) error {
return nil
}, e)
})
}
// Find routes