Skip to content

Commit

Permalink
net/http: add Request.CookiesNamed
Browse files Browse the repository at this point in the history
This commit implements the new API proposed with
golang#61472

This change set implements the CookiesNamed function
on the net/http Request so that it's conveniently possible
to retrieve all cookies from the Cookie header that match
a given name.

It does so by just wrapping the already existing readCookies
function.

Fixes golang#61472
  • Loading branch information
timofurrer committed Jul 21, 2023
1 parent 5fe3f0a commit 68535d4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/go1.21.txt
Expand Up @@ -348,6 +348,7 @@ pkg maps, func Keys[$0 interface{ ~map[$1]$2 }, $1 comparable, $2 interface{}]($
pkg maps, func Values[$0 interface{ ~map[$1]$2 }, $1 comparable, $2 interface{}]($0) []$2 #57436
pkg math/big, method (*Int) Float64() (float64, Accuracy) #56984
pkg net/http, method (*ProtocolError) Is(error) bool #41198
pkg net/http, method (*Request) CookiesNamed(string) []*Cookie #61472
pkg net/http, method (*ResponseController) EnableFullDuplex() error #57786
pkg net/http, var ErrSchemeMismatch error #44855
pkg net, method (*Dialer) MultipathTCP() bool #56539
Expand Down
6 changes: 6 additions & 0 deletions src/net/http/request.go
Expand Up @@ -416,6 +416,12 @@ func (r *Request) Cookies() []*Cookie {
return readCookies(r.Header, "")
}

// CookiesNamed parses and returns the named HTTP cookies sent with the request
// or an empty slice if none matched.
func (r *Request) CookiesNamed(name string) []*Cookie {
return readCookies(r.Header, name)
}

// ErrNoCookie is returned by Request's Cookie method when a cookie is not found.
var ErrNoCookie = errors.New("http: named cookie not present")

Expand Down
47 changes: 47 additions & 0 deletions src/net/http/request_test.go
Expand Up @@ -1201,6 +1201,53 @@ func TestRequestCookie(t *testing.T) {
}
}

func TestRequestFilterCookiesByName(t *testing.T) {
for _, tt := range []struct {
requestCookies []*Cookie
filterForCookie string
expectedCookies int
}{
{
requestCookies: []*Cookie{
{Name: "foo", Value: "foo-1"},
{Name: "bar", Value: "bar"},
},
filterForCookie: "foo",
expectedCookies: 1,
},
{
requestCookies: []*Cookie{
{Name: "foo", Value: "foo-1"},
{Name: "foo", Value: "foo-2"},
{Name: "bar", Value: "bar"},
},
filterForCookie: "foo",
expectedCookies: 2,
},
{
requestCookies: []*Cookie{
{Name: "bar", Value: "bar"},
},
filterForCookie: "foo",
expectedCookies: 0,
},
} {
req, err := NewRequest("GET", "http://example.com/", nil)
if err != nil {
t.Fatal(err)
}
for _, c := range tt.requestCookies {
req.AddCookie(c)
}

cs := req.CookiesNamed(tt.filterForCookie)

if len(cs) != tt.expectedCookies {
t.Errorf("got %d cookies, want %d", len(cs), tt.expectedCookies)
}
}
}

const (
fileaContents = "This is a test file."
filebContents = "Another test file."
Expand Down

0 comments on commit 68535d4

Please sign in to comment.