Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
],

Expand Down Expand Up @@ -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
}
},

Expand Down
19 changes: 4 additions & 15 deletions pkg/lib/api/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
}
Expand Down
71 changes: 15 additions & 56 deletions pkg/lib/api/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}
}

Expand Down
15 changes: 9 additions & 6 deletions test/cookies/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {}
Expand All @@ -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": {}
Expand Down
Loading