From f9e97332f3bebd2c26753f7dc679588a22cb387d Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Tue, 15 Nov 2016 20:09:52 -0800 Subject: [PATCH] fixed #729 Signed-off-by: Vishal Rana --- router.go | 5 ++++- router_test.go | 47 ++++++++++++++++++++++++++++++----------------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/router.go b/router.go index 83895a45..b53edb5a 100644 --- a/router.go +++ b/router.go @@ -172,7 +172,10 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string if h != nil { cn.addHandler(method, h) 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 if !strings.Contains(n, pnames[i]) { cn.pnames[i] += "," + pnames[i] diff --git a/router_test.go b/router_test.go index 968c8dde..ee48f951 100644 --- a/router_test.go +++ b/router_test.go @@ -812,29 +812,42 @@ func TestRouterStaticDynamicConflict(t *testing.T) { assert.Equal(t, 3, c.Get("c")) } -func TestRouterAPI(t *testing.T) { +func testRouterAPI(t *testing.T, api []Route) { e := New() r := e.router - for _, route := range gitHubAPI { + for _, route := range api { r.Add(route.Method, route.Path, func(c Context) error { return nil }) } c := e.NewContext(nil, nil).(*context) - for _, route := range gitHubAPI { + for _, route := range api { r.Find(route.Method, route.Path, c) - for _, n := range c.pnames { - for _, p := range strings.Split(n, ",") { - if assert.NotEmpty(t, p) { - assert.Equal(t, c.Param(p), ":"+p) - } + tokens := strings.Split(route.Path[1:], "/") + for _, token := range tokens { + if token[0] == ':' { + 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() r := e.router b.ReportAllocs() @@ -856,20 +869,20 @@ func benchmarkRoutes(b *testing.B, routes []Route) { } } -func BenchmarkStaticRoute(b *testing.B) { - benchmarkRoutes(b, staticRoutes) +func BenchmarkRouterStaticRoutes(b *testing.B) { + benchmarkRouterRoutes(b, staticRoutes) } -func BenchmarkGitHubAPI(b *testing.B) { - benchmarkRoutes(b, gitHubAPI) +func BenchmarkRouterGitHubAPI(b *testing.B) { + benchmarkRouterRoutes(b, gitHubAPI) } -func BenchmarkParseAPI(b *testing.B) { - benchmarkRoutes(b, parseAPI) +func BenchmarkRouterParseAPI(b *testing.B) { + benchmarkRouterRoutes(b, parseAPI) } -func BenchmarkGooglePlusAPI(b *testing.B) { - benchmarkRoutes(b, googlePlusAPI) +func BenchmarkRouterGooglePlusAPI(b *testing.B) { + benchmarkRouterRoutes(b, googlePlusAPI) } func (n *node) printTree(pfx string, tail bool) {