Skip to content

Commit 8068c69

Browse files
committed
net/http: add Request.CookiesNamed
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
1 parent 5fe3f0a commit 8068c69

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-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) CookiesNamed(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

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

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

src/net/http/request_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,60 @@ 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+
requestCookies: []*Cookie{
1236+
{Name: "bar", Value: "bar"},
1237+
},
1238+
filterForCookie: "",
1239+
expectedCookies: 0,
1240+
},
1241+
} {
1242+
req, err := NewRequest("GET", "http://example.com/", nil)
1243+
if err != nil {
1244+
t.Fatal(err)
1245+
}
1246+
for _, c := range tt.requestCookies {
1247+
req.AddCookie(c)
1248+
}
1249+
1250+
cs := req.CookiesNamed(tt.filterForCookie)
1251+
1252+
if len(cs) != tt.expectedCookies {
1253+
t.Errorf("got %d cookies, want %d", len(cs), tt.expectedCookies)
1254+
}
1255+
}
1256+
}
1257+
12041258
const (
12051259
fileaContents = "This is a test file."
12061260
filebContents = "Another test file."

0 commit comments

Comments
 (0)