Skip to content

Commit 293ebe1

Browse files
committed
Adding in a check for routes with just /
1 parent 757bef9 commit 293ebe1

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

mux_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,7 @@ func TestBuildVarsFunc(t *testing.T) {
10171017
func TestSubRouter(t *testing.T) {
10181018
subrouter1 := new(Route).Host("{v1:[a-z]+}.google.com").Subrouter()
10191019
subrouter2 := new(Route).PathPrefix("/foo/{v1}").Subrouter()
1020+
subrouter3 := new(Route).PathPrefix("/foo").Subrouter()
10201021

10211022
tests := []routeTest{
10221023
{
@@ -1048,6 +1049,15 @@ func TestSubRouter(t *testing.T) {
10481049
pathTemplate: `/foo/{v1}/baz/{v2}`,
10491050
shouldMatch: true,
10501051
},
1052+
{
1053+
route: subrouter3.Path("/"),
1054+
request: newRequest("GET", "http://localhost/foo/"),
1055+
vars: map[string]string{},
1056+
host: "",
1057+
path: "/foo/",
1058+
pathTemplate: `/foo/`,
1059+
shouldMatch: true,
1060+
},
10511061
{
10521062
route: subrouter2.Path("/baz/{v2}"),
10531063
request: newRequest("GET", "http://localhost/foo/bar"),

route.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func (r *Route) addRegexpMatcher(tpl string, matchHost, matchPrefix, matchQuery
153153
}
154154
r.regexp = r.getRegexpGroup()
155155
if !matchHost && !matchQuery {
156-
if len(tpl) == 0 || tpl[0] != '/' {
156+
if tpl == "/" && (len(tpl) == 0 || tpl[0] != '/') {
157157
return fmt.Errorf("mux: path must start with a slash, got %q", tpl)
158158
}
159159
if r.regexp.path != nil {

0 commit comments

Comments
 (0)