Skip to content

Commit a322b2c

Browse files
nmiyakekisielk
authored andcommitted
Update Walk to match all subrouters
Matches all routes instead of just routes with paths. Fixes #261
1 parent bcd8bc7 commit a322b2c

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

mux.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,6 @@ type WalkFunc func(route *Route, router *Router, ancestors []*Route) error
299299

300300
func (r *Router) walk(walkFn WalkFunc, ancestors []*Route) error {
301301
for _, t := range r.routes {
302-
if t.regexp == nil || t.regexp.path == nil || t.regexp.path.template == "" {
303-
continue
304-
}
305-
306302
err := walkFn(t, r, ancestors)
307303
if err == SkipRouter {
308304
continue

mux_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,34 @@ func TestWalkNested(t *testing.T) {
14011401
}
14021402
}
14031403

1404+
func TestWalkSubrouters(t *testing.T) {
1405+
router := NewRouter()
1406+
1407+
g := router.Path("/g").Subrouter()
1408+
o := g.PathPrefix("/o").Subrouter()
1409+
o.Methods("GET")
1410+
o.Methods("PUT")
1411+
1412+
// all 4 routes should be matched, but final 2 routes do not have path templates
1413+
paths := []string{"/g", "/g/o", "", ""}
1414+
idx := 0
1415+
err := router.Walk(func(route *Route, router *Router, ancestors []*Route) error {
1416+
path := paths[idx]
1417+
tpl, _ := route.GetPathTemplate()
1418+
if tpl != path {
1419+
t.Errorf(`Expected %s got %s`, path, tpl)
1420+
}
1421+
idx++
1422+
return nil
1423+
})
1424+
if err != nil {
1425+
panic(err)
1426+
}
1427+
if idx != len(paths) {
1428+
t.Errorf("Expected %d routes, found %d", len(paths), idx)
1429+
}
1430+
}
1431+
14041432
func TestWalkErrorRoute(t *testing.T) {
14051433
router := NewRouter()
14061434
router.Path("/g")

0 commit comments

Comments
 (0)