Skip to content

Commit f9e9733

Browse files
committed
fixed labstack#729
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent ecd3084 commit f9e9733

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

router.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
172172
if h != nil {
173173
cn.addHandler(method, h)
174174
cn.ppath = ppath
175-
for i, n := range cn.pnames {
175+
if len(cn.pnames) == 0 { // Issue #729
176+
cn.pnames = pnames
177+
}
178+
for i, n := range pnames {
176179
// Param name aliases
177180
if !strings.Contains(n, pnames[i]) {
178181
cn.pnames[i] += "," + pnames[i]

router_test.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -812,29 +812,42 @@ func TestRouterStaticDynamicConflict(t *testing.T) {
812812
assert.Equal(t, 3, c.Get("c"))
813813
}
814814

815-
func TestRouterAPI(t *testing.T) {
815+
func testRouterAPI(t *testing.T, api []Route) {
816816
e := New()
817817
r := e.router
818818

819-
for _, route := range gitHubAPI {
819+
for _, route := range api {
820820
r.Add(route.Method, route.Path, func(c Context) error {
821821
return nil
822822
})
823823
}
824824
c := e.NewContext(nil, nil).(*context)
825-
for _, route := range gitHubAPI {
825+
for _, route := range api {
826826
r.Find(route.Method, route.Path, c)
827-
for _, n := range c.pnames {
828-
for _, p := range strings.Split(n, ",") {
829-
if assert.NotEmpty(t, p) {
830-
assert.Equal(t, c.Param(p), ":"+p)
831-
}
827+
tokens := strings.Split(route.Path[1:], "/")
828+
for _, token := range tokens {
829+
if token[0] == ':' {
830+
assert.Equal(t, c.Param(token[1:]), token)
832831
}
833832
}
834833
}
835834
}
836835

837-
func benchmarkRoutes(b *testing.B, routes []Route) {
836+
func TestRouterGitHubAPI(t *testing.T) {
837+
testRouterAPI(t, gitHubAPI)
838+
}
839+
840+
// Issue #729
841+
func TestRouterParamAlias(t *testing.T) {
842+
api := []Route{
843+
{GET, "/users/:userID/following", ""},
844+
{GET, "/users/:userID/followedBy", ""},
845+
{GET, "/users/:userID/follow", ""},
846+
}
847+
testRouterAPI(t, api)
848+
}
849+
850+
func benchmarkRouterRoutes(b *testing.B, routes []Route) {
838851
e := New()
839852
r := e.router
840853
b.ReportAllocs()
@@ -856,20 +869,20 @@ func benchmarkRoutes(b *testing.B, routes []Route) {
856869
}
857870
}
858871

859-
func BenchmarkStaticRoute(b *testing.B) {
860-
benchmarkRoutes(b, staticRoutes)
872+
func BenchmarkRouterStaticRoutes(b *testing.B) {
873+
benchmarkRouterRoutes(b, staticRoutes)
861874
}
862875

863-
func BenchmarkGitHubAPI(b *testing.B) {
864-
benchmarkRoutes(b, gitHubAPI)
876+
func BenchmarkRouterGitHubAPI(b *testing.B) {
877+
benchmarkRouterRoutes(b, gitHubAPI)
865878
}
866879

867-
func BenchmarkParseAPI(b *testing.B) {
868-
benchmarkRoutes(b, parseAPI)
880+
func BenchmarkRouterParseAPI(b *testing.B) {
881+
benchmarkRouterRoutes(b, parseAPI)
869882
}
870883

871-
func BenchmarkGooglePlusAPI(b *testing.B) {
872-
benchmarkRoutes(b, googlePlusAPI)
884+
func BenchmarkRouterGooglePlusAPI(b *testing.B) {
885+
benchmarkRouterRoutes(b, googlePlusAPI)
873886
}
874887

875888
func (n *node) printTree(pfx string, tail bool) {

0 commit comments

Comments
 (0)