Skip to content

net/url: add Values.Has #45835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/net/url/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions src/net/url/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down