From 8204b18d9c0d9c1a047fc1f43fc5ea8cf4fa8b9a Mon Sep 17 00:00:00 2001 From: Philipp Hempel Date: Wed, 18 Feb 2026 10:53:56 +0100 Subject: [PATCH] removed own broken cookie struct, use http.Cookie instead; see #78185 --- README.md | 12 +++---- pkg/lib/api/request.go | 19 +++------- pkg/lib/api/response.go | 71 ++++++++------------------------------ test/cookies/manifest.json | 15 ++++---- 4 files changed, 34 insertions(+), 83 deletions(-) diff --git a/README.md b/README.md index ed008d2..130b48b 100644 --- a/README.md +++ b/README.md @@ -340,10 +340,10 @@ Expected http status code, if the response has another status code, the test cas "path": "/auth", "domain": "mydomain", "expires": "2021-11-10T10:00:00Z", - "max_age": 86400, + "maxage": 86400, "secure": false, - "http_only": true, - "same_site": 1 + "httponly": true, + "samesite": 1 } ], @@ -407,10 +407,10 @@ Expected http status code, if the response has another status code, the test cas "path": "/auth", "domain": "mydomain", "expires": "2021-11-10T10:00:00Z", - "max_age": 86400, + "maxage": 86400, "secure": false, - "http_only": true, - "same_site": 1 + "httponly": true, + "samesite": 1 } }, diff --git a/pkg/lib/api/request.go b/pkg/lib/api/request.go index f55562e..5feb495 100755 --- a/pkg/lib/api/request.go +++ b/pkg/lib/api/request.go @@ -51,7 +51,7 @@ type Request struct { Headers map[string]any `yaml:"header" json:"header"` HeaderFromStore map[string]string `yaml:"header_from_store" json:"header_from_store"` Cookies map[string]*requestCookie `yaml:"cookies" json:"cookies"` - SetCookies []*cookie `yaml:"header-x-test-set-cookie" json:"header-x-test-set-cookie"` + SetCookies []*http.Cookie `yaml:"header-x-test-set-cookie" json:"header-x-test-set-cookie"` BodyType string `yaml:"body_type" json:"body_type"` BodyFile string `yaml:"body_file" json:"body_file"` Body any `yaml:"body" json:"body"` @@ -257,24 +257,13 @@ func (request Request) buildHttpRequest() (req *http.Request, err error) { } // Add to custom header cookies to set in server - for _, v := range request.SetCookies { - if v == nil { + for _, ck := range request.SetCookies { + if ck == nil { continue } - ck := http.Cookie{ - Name: v.Name, - Value: v.Value, - Path: v.Path, - Domain: v.Domain, - Expires: v.Expires, - MaxAge: v.MaxAge, - Secure: v.Secure, - HttpOnly: v.HttpOnly, - SameSite: v.SameSite, - } ckVal := ck.String() if ckVal == "" { - return nil, fmt.Errorf("Invalid cookie to set server-side: %v", v) + return nil, fmt.Errorf("Invalid cookie to set server-side: %v", ck) } req.Header.Add("X-Test-Set-Cookies", ckVal) } diff --git a/pkg/lib/api/response.go b/pkg/lib/api/response.go index 1ddf84f..907c52b 100755 --- a/pkg/lib/api/response.go +++ b/pkg/lib/api/response.go @@ -40,26 +40,13 @@ func httpHeaderToMap(header http.Header) (headers map[string]any, err error) { return headers, nil } -// cookie definition -type cookie struct { - Name string `json:"name"` - Value string `json:"value"` - Path string `json:"path,omitempty"` - Domain string `json:"domain,omitempty"` - Expires time.Time `json:"expires,omitempty"` - MaxAge int `json:"max_age,omitempty"` - Secure bool `json:"secure,omitempty"` - HttpOnly bool `json:"http_only,omitempty"` - SameSite http.SameSite `json:"same_site,omitempty"` -} - type ResponseSerialization struct { - StatusCode *int `yaml:"statuscode,omitempty" json:"statuscode,omitempty"` - Headers map[string]any `yaml:"header" json:"header,omitempty"` - Cookies map[string]cookie `yaml:"cookie" json:"cookie,omitempty"` - Body any `yaml:"body" json:"body,omitempty"` - BodyControl jsutil.Object `yaml:"body:control" json:"body:control,omitempty"` - Format ResponseFormat `yaml:"format" json:"format,omitempty"` + StatusCode *int `yaml:"statuscode,omitempty" json:"statuscode,omitempty"` + Headers map[string]any `yaml:"header" json:"header,omitempty"` + Cookies map[string]http.Cookie `yaml:"cookie" json:"cookie,omitempty"` + Body any `yaml:"body" json:"body,omitempty"` + BodyControl jsutil.Object `yaml:"body:control" json:"body:control,omitempty"` + Format ResponseFormat `yaml:"format" json:"format"` } type responseSerializationInternal struct { @@ -171,17 +158,7 @@ func NewResponseFromSpec(spec ResponseSerialization) (res Response, err error) { if len(spec.Cookies) > 0 { cookies = make([]*http.Cookie, 0) for _, rck := range spec.Cookies { - cookies = append(cookies, &http.Cookie{ - Name: rck.Name, - Value: rck.Value, - Path: rck.Path, - Domain: rck.Domain, - Expires: rck.Expires, - MaxAge: rck.MaxAge, - Secure: rck.Secure, - HttpOnly: rck.HttpOnly, - SameSite: rck.SameSite, - }) + cookies = append(cookies, &rck) } } @@ -319,21 +296,12 @@ func (response Response) ServerResponseToGenericJSON(responseFormat ResponseForm // Build cookies map from standard bag if len(resp.Cookies) > 0 { - responseJSON.Cookies = make(map[string]cookie) + responseJSON.Cookies = make(map[string]http.Cookie) for _, ck := range resp.Cookies { - if ck != nil { - responseJSON.Cookies[ck.Name] = cookie{ - Name: ck.Name, - Value: ck.Value, - Path: ck.Path, - Domain: ck.Domain, - Expires: ck.Expires, - MaxAge: ck.MaxAge, - Secure: ck.Secure, - HttpOnly: ck.HttpOnly, - SameSite: ck.SameSite, - } + if ck == nil { + continue } + responseJSON.Cookies[ck.Name] = *ck } } @@ -394,21 +362,12 @@ func (response Response) ToGenericJSON() (res any, err error) { // Build cookies map from standard bag if len(response.Cookies) > 0 { - responseJSON.Cookies = make(map[string]cookie) + responseJSON.Cookies = make(map[string]http.Cookie) for _, ck := range response.Cookies { - if ck != nil { - responseJSON.Cookies[ck.Name] = cookie{ - Name: ck.Name, - Value: ck.Value, - Path: ck.Path, - Domain: ck.Domain, - Expires: ck.Expires, - MaxAge: ck.MaxAge, - Secure: ck.Secure, - HttpOnly: ck.HttpOnly, - SameSite: ck.SameSite, - } + if ck == nil { + continue } + responseJSON.Cookies[ck.Name] = *ck } } diff --git a/test/cookies/manifest.json b/test/cookies/manifest.json index 6d725f1..839249d 100644 --- a/test/cookies/manifest.json +++ b/test/cookies/manifest.json @@ -23,10 +23,10 @@ "path": "/mypath", "domain": "mydomain", "expires": "2021-11-10T10:00:00Z", - "max_age": 86400, + "maxage": 86400, "secure": false, - "http_only": false, - "same_site": 2 + "httponly": false, + "samesite": 2 } ], "body": {} @@ -41,19 +41,22 @@ }, "cookie": { "sess": { + "raw": "sess=my_sess", "name": "sess", "value": "my_sess" }, "dummy": { + "raw": "dummy=whatever; Path=/mypath; Domain=mydomain; Expires=Wed, 10 Nov 2021 10:00:00 GMT; Max-Age=86400; SameSite=Lax", "name": "dummy", "value": "whatever", "path": "/mypath", "domain": "mydomain", "expires": "2021-11-10T10:00:00Z", - "max_age": 86400, + "rawexpires": "Wed, 10 Nov 2021 10:00:00 GMT", + "maxage": 86400, "secure": false, - "http_only": false, - "same_site": 2 + "httponly": false, + "samesite": 2 } }, "body": {}