Adding in a check for routes with just /

This commit is contained in:
ShaneSaww
2017-01-15 21:47:00 -08:00
parent 757bef944d
commit 293ebe1493
2 changed files with 11 additions and 1 deletions

View File

@@ -1017,6 +1017,7 @@ func TestBuildVarsFunc(t *testing.T) {
func TestSubRouter(t *testing.T) {
subrouter1 := new(Route).Host("{v1:[a-z]+}.google.com").Subrouter()
subrouter2 := new(Route).PathPrefix("/foo/{v1}").Subrouter()
subrouter3 := new(Route).PathPrefix("/foo").Subrouter()
tests := []routeTest{
{
@@ -1048,6 +1049,15 @@ func TestSubRouter(t *testing.T) {
pathTemplate: `/foo/{v1}/baz/{v2}`,
shouldMatch: true,
},
{
route: subrouter3.Path("/"),
request: newRequest("GET", "http://localhost/foo/"),
vars: map[string]string{},
host: "",
path: "/foo/",
pathTemplate: `/foo/`,
shouldMatch: true,
},
{
route: subrouter2.Path("/baz/{v2}"),
request: newRequest("GET", "http://localhost/foo/bar"),

View File

@@ -153,7 +153,7 @@ func (r *Route) addRegexpMatcher(tpl string, matchHost, matchPrefix, matchQuery
}
r.regexp = r.getRegexpGroup()
if !matchHost && !matchQuery {
if len(tpl) == 0 || tpl[0] != '/' {
if tpl == "/" && (len(tpl) == 0 || tpl[0] != '/') {
return fmt.Errorf("mux: path must start with a slash, got %q", tpl)
}
if r.regexp.path != nil {