mirror of
https://github.com/labstack/echo.git
synced 2025-01-18 02:58:38 +02:00
parent
ecd3084fa5
commit
f9e97332f3
@ -172,7 +172,10 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
|
|||||||
if h != nil {
|
if h != nil {
|
||||||
cn.addHandler(method, h)
|
cn.addHandler(method, h)
|
||||||
cn.ppath = ppath
|
cn.ppath = ppath
|
||||||
for i, n := range cn.pnames {
|
if len(cn.pnames) == 0 { // Issue #729
|
||||||
|
cn.pnames = pnames
|
||||||
|
}
|
||||||
|
for i, n := range pnames {
|
||||||
// Param name aliases
|
// Param name aliases
|
||||||
if !strings.Contains(n, pnames[i]) {
|
if !strings.Contains(n, pnames[i]) {
|
||||||
cn.pnames[i] += "," + pnames[i]
|
cn.pnames[i] += "," + pnames[i]
|
||||||
|
@ -812,29 +812,42 @@ func TestRouterStaticDynamicConflict(t *testing.T) {
|
|||||||
assert.Equal(t, 3, c.Get("c"))
|
assert.Equal(t, 3, c.Get("c"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRouterAPI(t *testing.T) {
|
func testRouterAPI(t *testing.T, api []Route) {
|
||||||
e := New()
|
e := New()
|
||||||
r := e.router
|
r := e.router
|
||||||
|
|
||||||
for _, route := range gitHubAPI {
|
for _, route := range api {
|
||||||
r.Add(route.Method, route.Path, func(c Context) error {
|
r.Add(route.Method, route.Path, func(c Context) error {
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
c := e.NewContext(nil, nil).(*context)
|
c := e.NewContext(nil, nil).(*context)
|
||||||
for _, route := range gitHubAPI {
|
for _, route := range api {
|
||||||
r.Find(route.Method, route.Path, c)
|
r.Find(route.Method, route.Path, c)
|
||||||
for _, n := range c.pnames {
|
tokens := strings.Split(route.Path[1:], "/")
|
||||||
for _, p := range strings.Split(n, ",") {
|
for _, token := range tokens {
|
||||||
if assert.NotEmpty(t, p) {
|
if token[0] == ':' {
|
||||||
assert.Equal(t, c.Param(p), ":"+p)
|
assert.Equal(t, c.Param(token[1:]), token)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func benchmarkRoutes(b *testing.B, routes []Route) {
|
func TestRouterGitHubAPI(t *testing.T) {
|
||||||
|
testRouterAPI(t, gitHubAPI)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Issue #729
|
||||||
|
func TestRouterParamAlias(t *testing.T) {
|
||||||
|
api := []Route{
|
||||||
|
{GET, "/users/:userID/following", ""},
|
||||||
|
{GET, "/users/:userID/followedBy", ""},
|
||||||
|
{GET, "/users/:userID/follow", ""},
|
||||||
|
}
|
||||||
|
testRouterAPI(t, api)
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkRouterRoutes(b *testing.B, routes []Route) {
|
||||||
e := New()
|
e := New()
|
||||||
r := e.router
|
r := e.router
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
@ -856,20 +869,20 @@ func benchmarkRoutes(b *testing.B, routes []Route) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkStaticRoute(b *testing.B) {
|
func BenchmarkRouterStaticRoutes(b *testing.B) {
|
||||||
benchmarkRoutes(b, staticRoutes)
|
benchmarkRouterRoutes(b, staticRoutes)
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkGitHubAPI(b *testing.B) {
|
func BenchmarkRouterGitHubAPI(b *testing.B) {
|
||||||
benchmarkRoutes(b, gitHubAPI)
|
benchmarkRouterRoutes(b, gitHubAPI)
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkParseAPI(b *testing.B) {
|
func BenchmarkRouterParseAPI(b *testing.B) {
|
||||||
benchmarkRoutes(b, parseAPI)
|
benchmarkRouterRoutes(b, parseAPI)
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkGooglePlusAPI(b *testing.B) {
|
func BenchmarkRouterGooglePlusAPI(b *testing.B) {
|
||||||
benchmarkRoutes(b, googlePlusAPI)
|
benchmarkRouterRoutes(b, googlePlusAPI)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *node) printTree(pfx string, tail bool) {
|
func (n *node) printTree(pfx string, tail bool) {
|
||||||
|
Loading…
Reference in New Issue
Block a user