Skip to content

Commit

Permalink
fix(execute): serialize req.cookies into valid cookie-string
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n committed Jan 6, 2025
1 parent 03c0bba commit 718c700
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
24 changes: 10 additions & 14 deletions src/execute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,20 +299,16 @@ export function buildRequest(options) {
// If the cookie convenience object exists in our request,
// serialize its content and then delete the cookie object.
if (req.cookies && Object.keys(req.cookies).length) {
const cookieString = Object.keys(req.cookies).reduce((prev, cookieName) => {
const cookieValue = req.cookies[cookieName];
const prefix = prev ? '&' : '';
const stringified = serializeCookie([[cookieName, cookieValue]], {
encoders: {
value: cookieValueLenientEncoder,
},
validators: {
name: cookieNameLenientValidator,
value: cookieValueLenientValidator,
},
});
return prev + prefix + stringified;
}, '');
const cookieString = serializeCookie(req.cookies, {
encoders: {
value: cookieValueLenientEncoder,
},
validators: {
name: cookieNameLenientValidator,
value: cookieValueLenientValidator,
},
});

req.headers.Cookie = cookieString;
}

Expand Down
11 changes: 10 additions & 1 deletion test/oas3/execute/authorization.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,11 @@ describe('Authorization - OpenAPI Specification 3.0', () => {
name: 'MyApiKeyCookie',
in: 'cookie',
},
myApiKey1: {
type: 'apiKey',
name: 'MyApiKeyCookie1',
in: 'cookie',
},
},
},
paths: {
Expand All @@ -483,6 +488,7 @@ describe('Authorization - OpenAPI Specification 3.0', () => {
security: [
{
myApiKey: [],
myApiKey1: [],
},
],
},
Expand All @@ -499,6 +505,9 @@ describe('Authorization - OpenAPI Specification 3.0', () => {
myApiKey: {
value: 'MyToken',
},
myApiKey1: {
value: 'MyToken1',
},
},
},
});
Expand All @@ -508,7 +517,7 @@ describe('Authorization - OpenAPI Specification 3.0', () => {
url: '/',
credentials: 'same-origin',
headers: {
Cookie: 'MyApiKeyCookie=MyToken',
Cookie: 'MyApiKeyCookie=MyToken; MyApiKeyCookie1=MyToken1',
},
});
});
Expand Down

0 comments on commit 718c700

Please sign in to comment.