Browse Source

Merge pull request #215 from ShaneSaww/fix_for_subroutes_with_pathPrefix

Adding in a check for routes with just /
pull/218/head
Kamil Kisiel 9 years ago committed by GitHub
parent
commit
34dda716af
  1. 58
      mux_test.go
  2. 2
      route.go

58
mux_test.go

@ -1017,6 +1017,9 @@ func TestBuildVarsFunc(t *testing.T) {
func TestSubRouter(t *testing.T) { func TestSubRouter(t *testing.T) {
subrouter1 := new(Route).Host("{v1:[a-z]+}.google.com").Subrouter() subrouter1 := new(Route).Host("{v1:[a-z]+}.google.com").Subrouter()
subrouter2 := new(Route).PathPrefix("/foo/{v1}").Subrouter() subrouter2 := new(Route).PathPrefix("/foo/{v1}").Subrouter()
subrouter3 := new(Route).PathPrefix("/foo").Subrouter()
subrouter4 := new(Route).PathPrefix("/foo/bar").Subrouter()
subrouter5 := new(Route).PathPrefix("/{category}").Subrouter()
tests := []routeTest{ tests := []routeTest{
{ {
@ -1057,6 +1060,61 @@ func TestSubRouter(t *testing.T) {
pathTemplate: `/foo/{v1}/baz/{v2}`, pathTemplate: `/foo/{v1}/baz/{v2}`,
shouldMatch: false, shouldMatch: false,
}, },
{
route: subrouter3.Path("/"),
request: newRequest("GET", "http://localhost/foo/"),
vars: map[string]string{},
host: "",
path: "/foo/",
pathTemplate: `/foo/`,
shouldMatch: true,
},
{
route: subrouter3.Path(""),
request: newRequest("GET", "http://localhost/foo"),
vars: map[string]string{},
host: "",
path: "/foo",
pathTemplate: `/foo`,
shouldMatch: true,
},
{
route: subrouter4.Path("/"),
request: newRequest("GET", "http://localhost/foo/bar/"),
vars: map[string]string{},
host: "",
path: "/foo/bar/",
pathTemplate: `/foo/bar/`,
shouldMatch: true,
},
{
route: subrouter4.Path(""),
request: newRequest("GET", "http://localhost/foo/bar"),
vars: map[string]string{},
host: "",
path: "/foo/bar",
pathTemplate: `/foo/bar`,
shouldMatch: true,
},
{
route: subrouter5.Path("/"),
request: newRequest("GET", "http://localhost/baz/"),
vars: map[string]string{"category": "baz"},
host: "",
path: "/baz/",
pathTemplate: `/{category}/`,
shouldMatch: true,
},
{
route: subrouter5.Path(""),
request: newRequest("GET", "http://localhost/baz"),
vars: map[string]string{"category": "baz"},
host: "",
path: "/baz",
pathTemplate: `/{category}`,
shouldMatch: true,
},
} }
for _, test := range tests { for _, test := range tests {

2
route.go

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

Loading…
Cancel
Save