]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: add Request.CookiesNamed
authorTimo Furrer <tuxtimo@gmail.com>
Wed, 28 Feb 2024 06:07:16 +0000 (06:07 +0000)
committerEmmanuel Odeke <emmanuel@orijtech.com>
Wed, 20 Mar 2024 16:17:16 +0000 (16:17 +0000)
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: 6ad0094995b45648ebbcd18626f07bb879a3f7cf
GitHub-Pull-Request: golang/go#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>
api/next/61472.txt [new file with mode: 0644]
src/net/http/request.go
src/net/http/request_test.go

diff --git a/api/next/61472.txt b/api/next/61472.txt
new file mode 100644 (file)
index 0000000..2e39c4b
--- /dev/null
@@ -0,0 +1 @@
+pkg net/http, method (*Request) CookiesNamed(string) []*Cookie #61472
index 99fdebcf9bb85932a10e4da2cc13d5317938a435..345ba3d4eb43cf8a136881a2e091d2d5c2444bad 100644 (file)
@@ -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")
 
index 6ce32332e757e1260e966120a157c65eb7360451..8c8116123cb46476a4e84549cd7f89f96534db90 100644 (file)
@@ -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."