Skip to content

Commit 2da44ed

Browse files
committed
net/http: add Request.CookiesNamed
This commit implements the new API proposed with #61472 Iimplements a new method http.Request.CookiesName, that allows retrieving all cookies that match the given name. Fixes #61472
1 parent 8db1310 commit 2da44ed

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

api/next/61472.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pkg net/http, method (*Request) CookiesNamed(string) []*Cookie #61472

src/net/http/request.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,16 @@ func (r *Request) Cookies() []*Cookie {
431431
return readCookies(r.Header, "")
432432
}
433433

434+
// CookiesNamed parses and returns the named HTTP cookies sent with the request
435+
// or an empty slice if none matched.
436+
// If name is empty nil returned.
437+
func (r *Request) CookiesNamed(name string) []*Cookie {
438+
if name == "" {
439+
return nil
440+
}
441+
return readCookies(r.Header, name)
442+
}
443+
434444
// ErrNoCookie is returned by Request's Cookie method when a cookie is not found.
435445
var ErrNoCookie = errors.New("http: named cookie not present")
436446

src/net/http/request_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"context"
1111
"crypto/rand"
1212
"encoding/base64"
13+
"encoding/json"
1314
"errors"
1415
"fmt"
1516
"io"
@@ -1256,6 +1257,76 @@ func TestRequestCookie(t *testing.T) {
12561257
}
12571258
}
12581259

1260+
func TestRequestCookiesByName(t *testing.T) {
1261+
tests := []struct {
1262+
in []*Cookie
1263+
filter string
1264+
want []*Cookie
1265+
}{
1266+
{
1267+
in: []*Cookie{
1268+
{Name: "foo", Value: "foo-1"},
1269+
{Name: "bar", Value: "bar"},
1270+
},
1271+
filter: "foo",
1272+
want: []*Cookie{{Name: "foo", Value: "foo-1"}},
1273+
},
1274+
{
1275+
in: []*Cookie{
1276+
{Name: "foo", Value: "foo-1"},
1277+
{Name: "foo", Value: "foo-2"},
1278+
{Name: "bar", Value: "bar"},
1279+
},
1280+
filter: "foo",
1281+
want: []*Cookie{
1282+
{Name: "foo", Value: "foo-1"},
1283+
{Name: "foo", Value: "foo-2"},
1284+
},
1285+
},
1286+
{
1287+
in: []*Cookie{
1288+
{Name: "bar", Value: "bar"},
1289+
},
1290+
filter: "foo",
1291+
want: []*Cookie{},
1292+
},
1293+
{
1294+
in: []*Cookie{
1295+
{Name: "bar", Value: "bar"},
1296+
},
1297+
filter: "",
1298+
want: nil,
1299+
},
1300+
{
1301+
in: []*Cookie{},
1302+
filter: "foo",
1303+
want: []*Cookie{},
1304+
},
1305+
}
1306+
1307+
for _, tt := range tests {
1308+
t.Run(tt.filter, func(t *testing.T) {
1309+
req, err := NewRequest("GET", "http://example.com/", nil)
1310+
if err != nil {
1311+
t.Fatal(err)
1312+
}
1313+
for _, c := range tt.in {
1314+
req.AddCookie(c)
1315+
}
1316+
1317+
got := req.CookiesNamed(tt.filter)
1318+
1319+
if !reflect.DeepEqual(got, tt.want) {
1320+
asStr := func(v any) string {
1321+
blob, _ := json.MarshalIndent(v, "", " ")
1322+
return string(blob)
1323+
}
1324+
t.Fatalf("Result mismatch\n\tGot: %s\n\tWant: %s", asStr(got), asStr(tt.want))
1325+
}
1326+
})
1327+
}
1328+
}
1329+
12591330
const (
12601331
fileaContents = "This is a test file."
12611332
filebContents = "Another test file."

0 commit comments

Comments
 (0)