From 2b32409792406cc61512ca3aa35706bb9ce8c7dd Mon Sep 17 00:00:00 2001 From: Bay Dodd Date: Thu, 16 Jul 2015 10:52:01 +0100 Subject: [PATCH 1/2] fix for empty query --- mux_test.go | 9 +++++++++ regexp.go | 5 ++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/mux_test.go b/mux_test.go index 8118aab..0f61da8 100644 --- a/mux_test.go +++ b/mux_test.go @@ -588,6 +588,15 @@ 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, + }, } for _, test := range tests { diff --git a/regexp.go b/regexp.go index d98575e..8ecf270 100644 --- a/regexp.go +++ b/regexp.go @@ -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,9 @@ func newRouteRegexp(tpl string, matchHost, matchPrefix, matchQuery, strictSlash if strictSlash { pattern.WriteString("[/]?") } + if matchQuery && len(idxs) == 0 { + pattern.WriteString(defaultPattern) + } if !matchPrefix { pattern.WriteByte('$') } From 19f0a91c4e299b8253f142001ad546110f3c1e84 Mon Sep 17 00:00:00 2001 From: Bay Dodd Date: Thu, 16 Jul 2015 12:45:51 +0100 Subject: [PATCH 2/2] adding test and updating condition --- mux_test.go | 9 +++++++++ regexp.go | 7 +++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/mux_test.go b/mux_test.go index 0f61da8..9bed205 100644 --- a/mux_test.go +++ b/mux_test.go @@ -597,6 +597,15 @@ func TestQueries(t *testing.T) { 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 { diff --git a/regexp.go b/regexp.go index 8ecf270..a1f6eb6 100644 --- a/regexp.go +++ b/regexp.go @@ -89,8 +89,11 @@ func newRouteRegexp(tpl string, matchHost, matchPrefix, matchQuery, strictSlash if strictSlash { pattern.WriteString("[/]?") } - if matchQuery && len(idxs) == 0 { - pattern.WriteString(defaultPattern) + 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('$')