mirror of
https://github.com/golang/go.git
synced 2024-09-21 10:28:27 +00:00
net/http: add Request.CookiesNamed
This commit implements the new API proposed with https://github.com/golang/go/issues/61472 Implements a new method http.Request.CookiesName, that allows retrieving all cookies that match the given name. Fixes #61472
This commit is contained in:
parent
8db131082d
commit
6ad0094995
1
api/next/61472.txt
Normal file
1
api/next/61472.txt
Normal file
@ -0,0 +1 @@
|
||||
pkg net/http, method (*Request) CookiesNamed(string) []*Cookie #61472
|
@ -431,6 +431,15 @@ 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 {
|
||||
if name == "" {
|
||||
return []*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")
|
||||
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@ -1256,6 +1257,76 @@ func TestRequestCookie(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestCookiesByName(t *testing.T) {
|
||||
tests := []struct {
|
||||
in []*Cookie
|
||||
filter string
|
||||
want []*Cookie
|
||||
}{
|
||||
{
|
||||
in: []*Cookie{
|
||||
{Name: "foo", Value: "foo-1"},
|
||||
{Name: "bar", Value: "bar"},
|
||||
},
|
||||
filter: "foo",
|
||||
want: []*Cookie{{Name: "foo", Value: "foo-1"}},
|
||||
},
|
||||
{
|
||||
in: []*Cookie{
|
||||
{Name: "foo", Value: "foo-1"},
|
||||
{Name: "foo", Value: "foo-2"},
|
||||
{Name: "bar", Value: "bar"},
|
||||
},
|
||||
filter: "foo",
|
||||
want: []*Cookie{
|
||||
{Name: "foo", Value: "foo-1"},
|
||||
{Name: "foo", Value: "foo-2"},
|
||||
},
|
||||
},
|
||||
{
|
||||
in: []*Cookie{
|
||||
{Name: "bar", Value: "bar"},
|
||||
},
|
||||
filter: "foo",
|
||||
want: []*Cookie{},
|
||||
},
|
||||
{
|
||||
in: []*Cookie{
|
||||
{Name: "bar", Value: "bar"},
|
||||
},
|
||||
filter: "",
|
||||
want: []*Cookie{},
|
||||
},
|
||||
{
|
||||
in: []*Cookie{},
|
||||
filter: "foo",
|
||||
want: []*Cookie{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.filter, func(t *testing.T) {
|
||||
req, err := NewRequest("GET", "http://example.com/", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, c := range tt.in {
|
||||
req.AddCookie(c)
|
||||
}
|
||||
|
||||
got := req.CookiesNamed(tt.filter)
|
||||
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
asStr := func(v any) string {
|
||||
blob, _ := json.MarshalIndent(v, "", " ")
|
||||
return string(blob)
|
||||
}
|
||||
t.Fatalf("Result mismatch\n\tGot: %s\n\tWant: %s", asStr(got), asStr(tt.want))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
fileaContents = "This is a test file."
|
||||
filebContents = "Another test file."
|
||||
|
Loading…
Reference in New Issue
Block a user