Skip to content

Commit 6dca707

Browse files
timofurrerodeke-em
authored andcommitted
net/http: add Request.CookiesNamed
Implements a new method http.Request.CookiesName, that allows retrieving all cookies that match the given name. Fixes #61472 Change-Id: I405d8771b4195af9ff6b4dfde3cfcd316c23b70c GitHub-Last-Rev: 6ad0094 GitHub-Pull-Request: #61473 Reviewed-on: https://go-review.googlesource.com/c/go/+/511516 Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Carlos Amedee <carlos@golang.org> Reviewed-by: Damien Neil <dneil@google.com>
1 parent db423dd commit 6dca707

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

api/next/61472.txt

+1
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

+9
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,15 @@ 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+
func (r *Request) CookiesNamed(name string) []*Cookie {
437+
if name == "" {
438+
return []*Cookie{}
439+
}
440+
return readCookies(r.Header, name)
441+
}
442+
434443
// ErrNoCookie is returned by Request's Cookie method when a cookie is not found.
435444
var ErrNoCookie = errors.New("http: named cookie not present")
436445

src/net/http/request_test.go

+71
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: []*Cookie{},
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)