Skip to content

Commit ec7cc31

Browse files
committed
net/http: introduce ReadCookies() API for Request
This commit implements the new API proposed with #61472 This change set implements the ReadCookies 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. Closes #61472
1 parent 5fe3f0a commit ec7cc31

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

api/go1.21.txt

+1
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ pkg maps, func Keys[$0 interface{ ~map[$1]$2 }, $1 comparable, $2 interface{}]($
348348
pkg maps, func Values[$0 interface{ ~map[$1]$2 }, $1 comparable, $2 interface{}]($0) []$2 #57436
349349
pkg math/big, method (*Int) Float64() (float64, Accuracy) #56984
350350
pkg net/http, method (*ProtocolError) Is(error) bool #41198
351+
pkg net/http, method (*Request) ReadCookies(string) []*Cookie #61472
351352
pkg net/http, method (*ResponseController) EnableFullDuplex() error #57786
352353
pkg net/http, var ErrSchemeMismatch error #44855
353354
pkg net, method (*Dialer) MultipathTCP() bool #56539

src/net/http/request.go

+6
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,12 @@ func (r *Request) Cookies() []*Cookie {
416416
return readCookies(r.Header, "")
417417
}
418418

419+
// ReadCookies parses and returns the named HTTP cookies sent with the request
420+
// or an empty slice if none matched.
421+
func (r *Request) ReadCookies(name string) []*Cookie {
422+
return readCookies(r.Header, name)
423+
}
424+
419425
// ErrNoCookie is returned by Request's Cookie method when a cookie is not found.
420426
var ErrNoCookie = errors.New("http: named cookie not present")
421427

src/net/http/request_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,53 @@ func TestRequestCookie(t *testing.T) {
12011201
}
12021202
}
12031203

1204+
func TestRequestFilterCookiesByName(t *testing.T) {
1205+
for _, tt := range []struct {
1206+
requestCookies []*Cookie
1207+
filterForCookie string
1208+
expectedCookies int
1209+
}{
1210+
{
1211+
requestCookies: []*Cookie{
1212+
{Name: "foo", Value: "foo-1"},
1213+
{Name: "bar", Value: "bar"},
1214+
},
1215+
filterForCookie: "foo",
1216+
expectedCookies: 1,
1217+
},
1218+
{
1219+
requestCookies: []*Cookie{
1220+
{Name: "foo", Value: "foo-1"},
1221+
{Name: "foo", Value: "foo-2"},
1222+
{Name: "bar", Value: "bar"},
1223+
},
1224+
filterForCookie: "foo",
1225+
expectedCookies: 2,
1226+
},
1227+
{
1228+
requestCookies: []*Cookie{
1229+
{Name: "bar", Value: "bar"},
1230+
},
1231+
filterForCookie: "foo",
1232+
expectedCookies: 0,
1233+
},
1234+
} {
1235+
req, err := NewRequest("GET", "http://example.com/", nil)
1236+
if err != nil {
1237+
t.Fatal(err)
1238+
}
1239+
for _, c := range tt.requestCookies {
1240+
req.AddCookie(c)
1241+
}
1242+
1243+
cs := req.ReadCookies(tt.filterForCookie)
1244+
1245+
if len(cs) != tt.expectedCookies {
1246+
t.Errorf("got %d cookies, want %d", len(cs), tt.expectedCookies)
1247+
}
1248+
}
1249+
}
1250+
12041251
const (
12051252
fileaContents = "This is a test file."
12061253
filebContents = "Another test file."

0 commit comments

Comments
 (0)