Skip to content

Commit b1685f4

Browse files
authored
Call mux.NewRouter() in pat.New() (#31)
Initializing mux.Router with a zero value breaks route naming: the `mux.Router.namedRoutes` map remains nil and an attempt to assign a route name panics. This fix simply delegates mux.Router creation to its constructor, which does the right thing.
1 parent 2ef8c2c commit b1685f4

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pat.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import (
1616

1717
// New returns a new router.
1818
func New() *Router {
19-
return &Router{}
19+
return &Router{
20+
Router: *mux.NewRouter(),
21+
}
2022
}
2123

2224
// Router is a request router that implements a pat-like API.

pat_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,21 @@ func TestPatMatch(t *testing.T) {
6262
testMatch(t, "GET", "/foo/x{name}", "/foo/xbar/baz", true, map[string]string{":name": "bar"})
6363
testMatch(t, "PATCH", "/foo/x{name}", "/foo/xbar/baz", true, map[string]string{":name": "bar"})
6464
}
65+
66+
func TestNamedRoutes(t *testing.T) {
67+
router := New()
68+
route := router.Get("/", nil)
69+
name := "foo"
70+
route.Name(name)
71+
if route.GetError() != nil {
72+
t.Errorf("Route name assign: %v", route.GetError())
73+
}
74+
gotRoute := router.Router.Get(name)
75+
if gotRoute == nil {
76+
t.Errorf("mux.Router.Get by name returned nil")
77+
}
78+
gotName := gotRoute.GetName()
79+
if gotName != name {
80+
t.Errorf("Unexpected route name: got=%q, want=%q", gotName, name)
81+
}
82+
}

0 commit comments

Comments
 (0)