Browse Source

Merge pull request #108 from burrbd/master

fix for empty query
pull/83/merge
Kamil Kisiel 11 years ago
parent
commit
fe156cb197
  1. 18
      mux_test.go
  2. 8
      regexp.go

18
mux_test.go

@ -588,6 +588,24 @@ func TestQueries(t *testing.T) { @@ -588,6 +588,24 @@ func TestQueries(t *testing.T) {
path: "",
shouldMatch: false,
},
{
title: "Queries route with empty value, should match",
route: new(Route).Queries("foo", ""),
request: newRequest("GET", "http://localhost?foo=bar"),
vars: map[string]string{},
host: "",
path: "",
shouldMatch: true,
},
{
title: "Queries route with overlapping value, should not match",
route: new(Route).Queries("foo", "bar"),
request: newRequest("GET", "http://localhost?foo=barfoo"),
vars: map[string]string{},
host: "",
path: "",
shouldMatch: false,
},
}
for _, test := range tests {

8
regexp.go

@ -34,7 +34,7 @@ func newRouteRegexp(tpl string, matchHost, matchPrefix, matchQuery, strictSlash @@ -34,7 +34,7 @@ func newRouteRegexp(tpl string, matchHost, matchPrefix, matchQuery, strictSlash
// Now let's parse it.
defaultPattern := "[^/]+"
if matchQuery {
defaultPattern = "[^?&]+"
defaultPattern = "[^?&]*"
} else if matchHost {
defaultPattern = "[^.]+"
matchPrefix = false
@ -89,6 +89,12 @@ func newRouteRegexp(tpl string, matchHost, matchPrefix, matchQuery, strictSlash @@ -89,6 +89,12 @@ func newRouteRegexp(tpl string, matchHost, matchPrefix, matchQuery, strictSlash
if strictSlash {
pattern.WriteString("[/]?")
}
if matchQuery {
// Add the default pattern if the query value is empty
if queryVal := strings.SplitN(template, "=", 2)[1]; queryVal == "" {
pattern.WriteString(defaultPattern)
}
}
if !matchPrefix {
pattern.WriteByte('$')
}

Loading…
Cancel
Save