Browse Source

Adding in a check for routes with just /

pull/215/head
ShaneSaww 9 years ago
parent
commit
293ebe1493
  1. 10
      mux_test.go
  2. 2
      route.go

10
mux_test.go

@ -1017,6 +1017,7 @@ func TestBuildVarsFunc(t *testing.T) { @@ -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) { @@ -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"),

2
route.go

@ -153,7 +153,7 @@ func (r *Route) addRegexpMatcher(tpl string, matchHost, matchPrefix, matchQuery @@ -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 {

Loading…
Cancel
Save