From fe40f0d05612a52a42bbbcd9d28cb81ebe4e6d36 Mon Sep 17 00:00:00 2001 From: Bay Dodd Date: Fri, 17 Jul 2015 08:40:28 +0100 Subject: [PATCH 1/2] updating query match string --- mux_test.go | 9 +++++++++ regexp.go | 10 +++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/mux_test.go b/mux_test.go index 9bed205..79d1651 100644 --- a/mux_test.go +++ b/mux_test.go @@ -606,6 +606,15 @@ func TestQueries(t *testing.T) { path: "", shouldMatch: false, }, + { + title: "Queries route with no parameter in request , should not match", + route: new(Route).Queries("foo", "{bar}"), + request: newRequest("GET", "http://localhost"), + vars: map[string]string{}, + host: "", + path: "", + shouldMatch: false, + }, } for _, test := range tests { diff --git a/regexp.go b/regexp.go index a1f6eb6..b300730 100644 --- a/regexp.go +++ b/regexp.go @@ -186,9 +186,13 @@ func (r *routeRegexp) getUrlQuery(req *http.Request) string { if !r.matchQuery { return "" } - key := strings.Split(r.template, "=")[0] - val := req.URL.Query().Get(key) - return key + "=" + val + templateKey := strings.Split(r.template, "=")[0] + for key, vals := range req.URL.Query() { + if key == templateKey && len(vals) > 0 { + return key + "=" + vals[0] + } + } + return "" } func (r *routeRegexp) matchQueryString(req *http.Request) bool { From 3339267a853e3a855d6e415b96d6f3447b60a824 Mon Sep 17 00:00:00 2001 From: Bay Dodd Date: Fri, 17 Jul 2015 10:52:37 +0100 Subject: [PATCH 2/2] adding tests --- mux_test.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/mux_test.go b/mux_test.go index 79d1651..1a66ad5 100644 --- a/mux_test.go +++ b/mux_test.go @@ -597,6 +597,24 @@ func TestQueries(t *testing.T) { path: "", shouldMatch: true, }, + { + title: "Queries route with empty value and no parameter in request, should not match", + route: new(Route).Queries("foo", ""), + request: newRequest("GET", "http://localhost"), + vars: map[string]string{}, + host: "", + path: "", + shouldMatch: false, + }, + { + title: "Queries route with empty value and empty parameter in request, should match", + route: new(Route).Queries("foo", ""), + request: newRequest("GET", "http://localhost?foo="), + vars: map[string]string{}, + host: "", + path: "", + shouldMatch: true, + }, { title: "Queries route with overlapping value, should not match", route: new(Route).Queries("foo", "bar"), @@ -607,7 +625,7 @@ func TestQueries(t *testing.T) { shouldMatch: false, }, { - title: "Queries route with no parameter in request , should not match", + title: "Queries route with no parameter in request, should not match", route: new(Route).Queries("foo", "{bar}"), request: newRequest("GET", "http://localhost"), vars: map[string]string{}, @@ -615,6 +633,15 @@ func TestQueries(t *testing.T) { path: "", shouldMatch: false, }, + { + title: "Queries route with empty parameter in request, should match", + route: new(Route).Queries("foo", "{bar}"), + request: newRequest("GET", "http://localhost?foo="), + vars: map[string]string{"foo": ""}, + host: "", + path: "", + shouldMatch: true, + }, } for _, test := range tests {