mirror of
https://github.com/labstack/echo.git
synced 2024-12-22 20:06:21 +02:00
Enhanced router tests
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
feef0c0b8f
commit
30ee5fbb95
289
router_test.go
289
router_test.go
@ -4,7 +4,6 @@ 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"
|
||||||
@ -285,10 +284,8 @@ func TestRouterStatic(t *testing.T) {
|
|||||||
}, e)
|
}, e)
|
||||||
c := NewContext(nil, nil, e)
|
c := NewContext(nil, nil, e)
|
||||||
h, _ := r.Find(GET, path, c)
|
h, _ := r.Find(GET, path, c)
|
||||||
if assert.NotNil(t, h) {
|
h(c)
|
||||||
h(c)
|
assert.Equal(t, path, c.Get("path"))
|
||||||
assert.Equal(t, path, c.Get("path"))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRouterParam(t *testing.T) {
|
func TestRouterParam(t *testing.T) {
|
||||||
@ -298,10 +295,8 @@ func TestRouterParam(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
}, e)
|
}, e)
|
||||||
c := NewContext(nil, nil, e)
|
c := NewContext(nil, nil, e)
|
||||||
h, _ := r.Find(GET, "/users/1", c)
|
r.Find(GET, "/users/1", c)
|
||||||
if assert.NotNil(t, h) {
|
assert.Equal(t, "1", c.P(0))
|
||||||
assert.Equal(t, "1", c.P(0))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRouterTwoParam(t *testing.T) {
|
func TestRouterTwoParam(t *testing.T) {
|
||||||
@ -311,44 +306,39 @@ func TestRouterTwoParam(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
}, e)
|
}, e)
|
||||||
c := NewContext(nil, nil, e)
|
c := NewContext(nil, nil, e)
|
||||||
|
r.Find(GET, "/users/1/files/1", c)
|
||||||
h, _ := r.Find(GET, "/users/1/files/1", c)
|
assert.Equal(t, "1", c.P(0))
|
||||||
if assert.NotNil(t, h) {
|
assert.Equal(t, "1", c.P(1))
|
||||||
assert.Equal(t, "1", c.P(0))
|
|
||||||
assert.Equal(t, "1", c.P(1))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRouterMatchAny(t *testing.T) {
|
func TestRouterMatchAny(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
r := e.router
|
r := e.router
|
||||||
|
|
||||||
// Routes
|
// Add
|
||||||
r.Add(GET, "/", func(*Context) error {
|
r.Add(GET, "/", func(*Context) error {
|
||||||
return nil
|
return nil
|
||||||
}, e)
|
}, e)
|
||||||
|
|
||||||
r.Add(GET, "/*", func(*Context) error {
|
r.Add(GET, "/*", func(*Context) error {
|
||||||
return nil
|
return nil
|
||||||
}, e)
|
}, e)
|
||||||
|
|
||||||
r.Add(GET, "/users/*", func(*Context) error {
|
r.Add(GET, "/users/*", func(*Context) error {
|
||||||
return nil
|
return nil
|
||||||
}, e)
|
}, e)
|
||||||
|
|
||||||
c := NewContext(nil, nil, e)
|
c := NewContext(nil, nil, e)
|
||||||
|
|
||||||
h, _ := r.Find(GET, "/", c)
|
// Find
|
||||||
if assert.NotNil(t, h) {
|
r.Find(GET, "/", c)
|
||||||
assert.Equal(t, "", c.P(0))
|
assert.Equal(t, "", c.P(0))
|
||||||
}
|
|
||||||
|
|
||||||
h, _ = r.Find(GET, "/download", c)
|
r.Find(GET, "/download", c)
|
||||||
if assert.NotNil(t, h) {
|
assert.Equal(t, "download", c.P(0))
|
||||||
assert.Equal(t, "download", c.P(0))
|
|
||||||
}
|
|
||||||
|
|
||||||
h, _ = r.Find(GET, "/users/joe", c)
|
r.Find(GET, "/users/joe", c)
|
||||||
if assert.NotNil(t, h) {
|
assert.Equal(t, "joe", c.P(0))
|
||||||
assert.Equal(t, "joe", c.P(0))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRouterMicroParam(t *testing.T) {
|
func TestRouterMicroParam(t *testing.T) {
|
||||||
@ -358,12 +348,10 @@ func TestRouterMicroParam(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
}, e)
|
}, e)
|
||||||
c := NewContext(nil, nil, e)
|
c := NewContext(nil, nil, e)
|
||||||
h, _ := r.Find(GET, "/1/2/3", c)
|
r.Find(GET, "/1/2/3", c)
|
||||||
if assert.NotNil(t, h) {
|
assert.Equal(t, "1", c.P(0))
|
||||||
assert.Equal(t, "1", c.P(0))
|
assert.Equal(t, "2", c.P(1))
|
||||||
assert.Equal(t, "2", c.P(1))
|
assert.Equal(t, "3", c.P(2))
|
||||||
assert.Equal(t, "3", c.P(2))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRouterMixParamMatchAny(t *testing.T) {
|
func TestRouterMixParamMatchAny(t *testing.T) {
|
||||||
@ -377,40 +365,33 @@ func TestRouterMixParamMatchAny(t *testing.T) {
|
|||||||
c := NewContext(nil, nil, e)
|
c := NewContext(nil, nil, e)
|
||||||
|
|
||||||
h, _ := r.Find(GET, "/users/joe/comments", c)
|
h, _ := r.Find(GET, "/users/joe/comments", c)
|
||||||
if assert.NotNil(t, h) {
|
h(c)
|
||||||
h(c)
|
assert.Equal(t, "joe", c.P(0))
|
||||||
assert.Equal(t, "joe", c.P(0))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRouterMultiRoute(t *testing.T) {
|
func TestRouterMultiRoute(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
r := e.router
|
r := e.router
|
||||||
|
|
||||||
// Routes
|
// Add
|
||||||
r.Add(GET, "/users", func(c *Context) error {
|
r.Add(GET, "/users", func(c *Context) error {
|
||||||
c.Set("path", "/users")
|
c.Set("path", "/users")
|
||||||
return nil
|
return nil
|
||||||
}, e)
|
}, e)
|
||||||
|
|
||||||
r.Add(GET, "/users/:id", func(c *Context) error {
|
r.Add(GET, "/users/:id", func(c *Context) error {
|
||||||
return nil
|
return nil
|
||||||
}, e)
|
}, e)
|
||||||
c := NewContext(nil, nil, e)
|
c := NewContext(nil, nil, e)
|
||||||
|
|
||||||
// Route > /users
|
// Find
|
||||||
h, _ := r.Find(GET, "/users", c)
|
h, _ := r.Find(GET, "/users", c)
|
||||||
if assert.NotNil(t, h) {
|
h(c)
|
||||||
h(c)
|
assert.Equal(t, "/users", c.Get("path"))
|
||||||
assert.Equal(t, "/users", c.Get("path"))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Route > /users/:id
|
|
||||||
h, _ = r.Find(GET, "/users/1", c)
|
h, _ = r.Find(GET, "/users/1", c)
|
||||||
if assert.NotNil(t, h) {
|
assert.Equal(t, "1", c.P(0))
|
||||||
assert.Equal(t, "1", c.P(0))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Route > /user
|
|
||||||
h, _ = r.Find(GET, "/user", c)
|
h, _ = r.Find(GET, "/user", c)
|
||||||
if assert.IsType(t, new(HTTPError), h(c)) {
|
if assert.IsType(t, new(HTTPError), h(c)) {
|
||||||
he := h(c).(*HTTPError)
|
he := h(c).(*HTTPError)
|
||||||
@ -422,167 +403,188 @@ func TestRouterPriority(t *testing.T) {
|
|||||||
e := New()
|
e := New()
|
||||||
r := e.router
|
r := e.router
|
||||||
|
|
||||||
// Routes
|
// Add
|
||||||
r.Add(GET, "/users", func(c *Context) error {
|
r.Add(GET, "/users", func(c *Context) error {
|
||||||
c.Set("a", 1)
|
c.Set("a", 1)
|
||||||
return nil
|
return nil
|
||||||
}, e)
|
}, e)
|
||||||
|
|
||||||
r.Add(GET, "/users/new", func(c *Context) error {
|
r.Add(GET, "/users/new", func(c *Context) error {
|
||||||
c.Set("b", 2)
|
c.Set("b", 2)
|
||||||
return nil
|
return nil
|
||||||
}, e)
|
}, e)
|
||||||
|
|
||||||
r.Add(GET, "/users/:id", func(c *Context) error {
|
r.Add(GET, "/users/:id", func(c *Context) error {
|
||||||
c.Set("c", 3)
|
c.Set("c", 3)
|
||||||
return nil
|
return nil
|
||||||
}, e)
|
}, e)
|
||||||
|
|
||||||
r.Add(GET, "/users/dew", func(c *Context) error {
|
r.Add(GET, "/users/dew", func(c *Context) error {
|
||||||
c.Set("d", 4)
|
c.Set("d", 4)
|
||||||
return nil
|
return nil
|
||||||
}, e)
|
}, e)
|
||||||
|
|
||||||
r.Add(GET, "/users/:id/files", func(c *Context) error {
|
r.Add(GET, "/users/:id/files", func(c *Context) error {
|
||||||
c.Set("e", 5)
|
c.Set("e", 5)
|
||||||
return nil
|
return nil
|
||||||
}, e)
|
}, e)
|
||||||
|
|
||||||
r.Add(GET, "/users/newsee", func(c *Context) error {
|
r.Add(GET, "/users/newsee", func(c *Context) error {
|
||||||
c.Set("f", 6)
|
c.Set("f", 6)
|
||||||
return nil
|
return nil
|
||||||
}, e)
|
}, e)
|
||||||
|
|
||||||
r.Add(GET, "/users/*", func(c *Context) error {
|
r.Add(GET, "/users/*", func(c *Context) error {
|
||||||
c.Set("g", 7)
|
c.Set("g", 7)
|
||||||
return nil
|
return nil
|
||||||
}, e)
|
}, e)
|
||||||
|
|
||||||
c := NewContext(nil, nil, e)
|
c := NewContext(nil, nil, e)
|
||||||
|
|
||||||
// Route > /users
|
// Find
|
||||||
h, _ := r.Find(GET, "/users", c)
|
h, _ := r.Find(GET, "/users", c)
|
||||||
if assert.NotNil(t, h) {
|
h(c)
|
||||||
h(c)
|
assert.Equal(t, 1, c.Get("a"))
|
||||||
assert.Equal(t, 1, c.Get("a"))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Route > /users/new
|
|
||||||
h, _ = r.Find(GET, "/users/new", c)
|
h, _ = r.Find(GET, "/users/new", c)
|
||||||
if assert.NotNil(t, h) {
|
h(c)
|
||||||
h(c)
|
assert.Equal(t, 2, c.Get("b"))
|
||||||
assert.Equal(t, 2, c.Get("b"))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Route > /users/:id
|
|
||||||
h, _ = r.Find(GET, "/users/1", c)
|
h, _ = r.Find(GET, "/users/1", c)
|
||||||
if assert.NotNil(t, h) {
|
h(c)
|
||||||
h(c)
|
assert.Equal(t, 3, c.Get("c"))
|
||||||
assert.Equal(t, 3, c.Get("c"))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Route > /users/dew
|
|
||||||
h, _ = r.Find(GET, "/users/dew", c)
|
h, _ = r.Find(GET, "/users/dew", c)
|
||||||
if assert.NotNil(t, h) {
|
h(c)
|
||||||
h(c)
|
assert.Equal(t, 4, c.Get("d"))
|
||||||
assert.Equal(t, 4, c.Get("d"))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Route > /users/:id/files
|
|
||||||
h, _ = r.Find(GET, "/users/1/files", c)
|
h, _ = r.Find(GET, "/users/1/files", c)
|
||||||
if assert.NotNil(t, h) {
|
h(c)
|
||||||
h(c)
|
assert.Equal(t, 5, c.Get("e"))
|
||||||
assert.Equal(t, 5, c.Get("e"))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Route > /users/:id
|
|
||||||
h, _ = r.Find(GET, "/users/news", c)
|
h, _ = r.Find(GET, "/users/news", c)
|
||||||
if assert.NotNil(t, h) {
|
h(c)
|
||||||
h(c)
|
assert.Equal(t, 3, c.Get("c"))
|
||||||
assert.Equal(t, 3, c.Get("c"))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Route > /users/*
|
|
||||||
h, _ = r.Find(GET, "/users/joe/books", c)
|
h, _ = r.Find(GET, "/users/joe/books", c)
|
||||||
if assert.NotNil(t, h) {
|
h(c)
|
||||||
h(c)
|
assert.Equal(t, 7, c.Get("g"))
|
||||||
assert.Equal(t, 7, c.Get("g"))
|
assert.Equal(t, "joe/books", c.Param("_*"))
|
||||||
assert.Equal(t, "joe/books", c.Param("_*"))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Issue #217, #372
|
// Issue #217
|
||||||
func TestRouterPriorityNext(t *testing.T) {
|
func TestRouterPriorityParamAny(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
r := e.router
|
r := e.router
|
||||||
|
|
||||||
routes := []Route{
|
// Add
|
||||||
{"GET", "/aa", nil},
|
r.Add(GET, "/aa", func(c *Context) error {
|
||||||
{"GET", "/ab", nil},
|
c.Set("a", 1)
|
||||||
{"GET", "/abc", nil},
|
return nil
|
||||||
{"GET", "/abd", nil},
|
}, e)
|
||||||
{"GET", "/a/foo", nil},
|
|
||||||
{"GET", "/a/bar", nil},
|
r.Add(GET, "/ab", func(c *Context) error {
|
||||||
{"GET", "/*", nil},
|
c.Set("b", 2)
|
||||||
}
|
return nil
|
||||||
|
}, e)
|
||||||
|
|
||||||
|
r.Add(GET, "/ab/:name", func(c *Context) error {
|
||||||
|
c.Set("c", 3)
|
||||||
|
return nil
|
||||||
|
}, e)
|
||||||
|
|
||||||
|
r.Add(GET, "/ab/*", func(c *Context) error {
|
||||||
|
c.Set("d", 4)
|
||||||
|
return nil
|
||||||
|
}, e)
|
||||||
|
|
||||||
|
r.Add(GET, "/*", func(c *Context) error {
|
||||||
|
c.Set("e", 5)
|
||||||
|
return nil
|
||||||
|
}, e)
|
||||||
|
|
||||||
for i, route := range routes {
|
|
||||||
a := strconv.Itoa(i)
|
|
||||||
r.Add(route.Method, route.Path, func(c *Context) error {
|
|
||||||
c.Set(a, a)
|
|
||||||
return nil
|
|
||||||
}, e)
|
|
||||||
}
|
|
||||||
c := NewContext(nil, nil, e)
|
c := NewContext(nil, nil, e)
|
||||||
for i, route := range routes {
|
|
||||||
a := strconv.Itoa(i)
|
|
||||||
h, _ := r.Find(route.Method, route.Path, c)
|
|
||||||
if assert.NotNil(t, h) {
|
|
||||||
h(c)
|
|
||||||
assert.Equal(t, a, c.Get(a))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Route > /*
|
// Find
|
||||||
h, _ := r.Find(GET, "/abc.html", c)
|
h, _ := r.Find(GET, "/aa", c)
|
||||||
if assert.NotNil(t, h) {
|
h(c)
|
||||||
h(c)
|
assert.Equal(t, 1, c.Get("a"))
|
||||||
assert.Equal(t, "abc.html", c.P(0))
|
|
||||||
}
|
h, _ = r.Find(GET, "/ab", c)
|
||||||
|
h(c)
|
||||||
|
assert.Equal(t, 2, c.Get("b"))
|
||||||
|
|
||||||
|
h, _ = r.Find(GET, "/ab/joe", c)
|
||||||
|
h(c)
|
||||||
|
assert.Equal(t, 3, c.Get("c"))
|
||||||
|
|
||||||
|
h, _ = r.Find(GET, "/abc.html", c)
|
||||||
|
h(c)
|
||||||
|
assert.Equal(t, 5, c.Get("e"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Issue #372
|
||||||
|
func TestRouterPriorityNotFound(t *testing.T) {
|
||||||
|
e := New()
|
||||||
|
r := e.router
|
||||||
|
c := NewContext(nil, nil, e)
|
||||||
|
|
||||||
|
// Add
|
||||||
|
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
|
||||||
|
h, _ := r.Find(GET, "/a/foo", c)
|
||||||
|
h(c)
|
||||||
|
assert.Equal(t, 1, c.Get("a"))
|
||||||
|
h, _ = r.Find(GET, "/a/bar", c)
|
||||||
|
h(c)
|
||||||
|
assert.Equal(t, 2, c.Get("b"))
|
||||||
|
h, _ = r.Find(GET, "/abc/def", c)
|
||||||
|
he := h(c).(*HTTPError)
|
||||||
|
assert.Equal(t, http.StatusNotFound, he.Code())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRouterParamNames(t *testing.T) {
|
func TestRouterParamNames(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
r := e.router
|
r := e.router
|
||||||
|
|
||||||
// Routes
|
// Add
|
||||||
r.Add(GET, "/users", func(c *Context) error {
|
r.Add(GET, "/users", func(c *Context) error {
|
||||||
c.Set("path", "/users")
|
c.Set("path", "/users")
|
||||||
return nil
|
return nil
|
||||||
}, e)
|
}, e)
|
||||||
|
|
||||||
r.Add(GET, "/users/:id", func(c *Context) error {
|
r.Add(GET, "/users/:id", func(c *Context) error {
|
||||||
return nil
|
return nil
|
||||||
}, e)
|
}, e)
|
||||||
|
|
||||||
r.Add(GET, "/users/:uid/files/:fid", func(c *Context) error {
|
r.Add(GET, "/users/:uid/files/:fid", func(c *Context) error {
|
||||||
return nil
|
return nil
|
||||||
}, e)
|
}, e)
|
||||||
|
|
||||||
c := NewContext(nil, nil, e)
|
c := NewContext(nil, nil, e)
|
||||||
|
|
||||||
// Route > /users
|
// Find
|
||||||
h, _ := r.Find(GET, "/users", c)
|
h, _ := r.Find(GET, "/users", c)
|
||||||
if assert.NotNil(t, h) {
|
h(c)
|
||||||
h(c)
|
assert.Equal(t, "/users", c.Get("path"))
|
||||||
assert.Equal(t, "/users", c.Get("path"))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Route > /users/:id
|
r.Find(GET, "/users/1", c)
|
||||||
h, _ = r.Find(GET, "/users/1", c)
|
assert.Equal(t, "id", c.pnames[0])
|
||||||
if assert.NotNil(t, h) {
|
assert.Equal(t, "1", c.P(0))
|
||||||
assert.Equal(t, "id", c.pnames[0])
|
|
||||||
assert.Equal(t, "1", c.P(0))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Route > /users/:uid/files/:fid
|
r.Find(GET, "/users/1/files/1", c)
|
||||||
h, _ = r.Find(GET, "/users/1/files/1", c)
|
assert.Equal(t, "uid", c.pnames[0])
|
||||||
if assert.NotNil(t, h) {
|
assert.Equal(t, "1", c.P(0))
|
||||||
assert.Equal(t, "uid", c.pnames[0])
|
assert.Equal(t, "fid", c.pnames[1])
|
||||||
assert.Equal(t, "1", c.P(0))
|
assert.Equal(t, "1", c.P(1))
|
||||||
assert.Equal(t, "fid", c.pnames[1])
|
|
||||||
assert.Equal(t, "1", c.P(1))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRouterAPI(t *testing.T) {
|
func TestRouterAPI(t *testing.T) {
|
||||||
@ -596,14 +598,11 @@ func TestRouterAPI(t *testing.T) {
|
|||||||
}
|
}
|
||||||
c := NewContext(nil, nil, e)
|
c := NewContext(nil, nil, e)
|
||||||
for _, route := range api {
|
for _, route := range api {
|
||||||
h, _ := r.Find(route.Method, route.Path, c)
|
r.Find(route.Method, route.Path, c)
|
||||||
if assert.NotNil(t, h) {
|
for i, n := range c.pnames {
|
||||||
for i, n := range c.pnames {
|
if assert.NotEmpty(t, n) {
|
||||||
if assert.NotEmpty(t, n) {
|
assert.Equal(t, ":"+n, c.P(i))
|
||||||
assert.Equal(t, ":"+n, c.P(i))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
h(c)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user