aboutsummaryrefslogtreecommitdiff
path: root/src/net
diff options
context:
space:
mode:
authorIan Mckay <iann0036@gmail.com>2021-04-29 06:30:44 +0000
committerIan Lance Taylor <iant@golang.org>2021-04-30 16:48:56 +0000
commita893682d83760a9f9bb6f5d543b907ee6467cc6e (patch)
tree29b9e3ece387f4af2e84c30551b13d3b5eee15a1 /src/net
parent3366556d1c97a7c7925432e19f2c0bb1ecb69ecd (diff)
downloadgo-a893682d83760a9f9bb6f5d543b907ee6467cc6e.tar.gz
go-a893682d83760a9f9bb6f5d543b907ee6467cc6e.zip
net/url: add Values.Has
Adds a method within Values for detecting whether a query parameter is set. Fixes #45100 Change-Id: I6bb49417e8547e11cc7e8d55c5211d24ee436ec1 GitHub-Last-Rev: 0b27cdab9024b93bad1eab9941aff8928a29fa76 GitHub-Pull-Request: golang/go#45835 Reviewed-on: https://go-review.googlesource.com/c/go/+/314850 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Cherry Zhang <cherryyz@google.com>
Diffstat (limited to 'src/net')
-rw-r--r--src/net/url/url.go6
-rw-r--r--src/net/url/url_test.go18
2 files changed, 21 insertions, 3 deletions
diff --git a/src/net/url/url.go b/src/net/url/url.go
index e138082d22..a4d7c03a87 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
diff --git a/src/net/url/url_test.go b/src/net/url/url_test.go
index f02e4650d8..55348c4a7d 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)