From 19d91b9b951a7b898182057ae4993c4181e10524 Mon Sep 17 00:00:00 2001 From: Ian Mckay Date: Thu, 29 Apr 2021 09:50:30 +1000 Subject: [PATCH 1/2] net/url: add Values.Has Adds a method within Values of detecting whether a query parameter without a value is set. Fixes #45100 --- src/net/url/url.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/net/url/url.go b/src/net/url/url.go index e138082d229c0a..a4d7c03a87d820 100644 --- a/src/net/url/url.go +++ b/src/net/url/url.go @@ -909,6 +909,12 @@ func (v Values) Del(key string) { delete(v, key) } +// Has checks whether a given key is set. +func (v Values) Has(key string) bool { + _, ok := v[key] + return ok +} + // ParseQuery parses the URL-encoded query string and returns // a map listing the values specified for each key. // ParseQuery always returns a non-nil map containing all the From 0b27cdab9024b93bad1eab9941aff8928a29fa76 Mon Sep 17 00:00:00 2001 From: Ian Mckay Date: Thu, 29 Apr 2021 16:28:49 +1000 Subject: [PATCH 2/2] net/url: add tests for Values.Has --- src/net/url/url_test.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/net/url/url_test.go b/src/net/url/url_test.go index f02e4650d87ecf..55348c4a7dad40 100644 --- a/src/net/url/url_test.go +++ b/src/net/url/url_test.go @@ -1295,10 +1295,10 @@ func TestResolveReference(t *testing.T) { } func TestQueryValues(t *testing.T) { - u, _ := Parse("http://x.com?foo=bar&bar=1&bar=2") + u, _ := Parse("http://x.com?foo=bar&bar=1&bar=2&baz") v := u.Query() - if len(v) != 2 { - t.Errorf("got %d keys in Query values, want 2", len(v)) + if len(v) != 3 { + t.Errorf("got %d keys in Query values, want 3", len(v)) } if g, e := v.Get("foo"), "bar"; g != e { t.Errorf("Get(foo) = %q, want %q", g, e) @@ -1313,6 +1313,18 @@ func TestQueryValues(t *testing.T) { if g, e := v.Get("baz"), ""; g != e { t.Errorf("Get(baz) = %q, want %q", g, e) } + if h, e := v.Has("foo"), true; h != e { + t.Errorf("Has(foo) = %t, want %t", h, e) + } + if h, e := v.Has("bar"), true; h != e { + t.Errorf("Has(bar) = %t, want %t", h, e) + } + if h, e := v.Has("baz"), true; h != e { + t.Errorf("Has(baz) = %t, want %t", h, e) + } + if h, e := v.Has("noexist"), false; h != e { + t.Errorf("Has(noexist) = %t, want %t", h, e) + } v.Del("bar") if g, e := v.Get("bar"), ""; g != e { t.Errorf("second Get(bar) = %q, want %q", g, e)