Skip to content

Commit

Permalink
fix(api-gateway-v2): not returning correctly the cookies when they ca…
Browse files Browse the repository at this point in the history
…me with set-cookies

fix #8
  • Loading branch information
H4ad committed Apr 12, 2022
1 parent 6e95fb3 commit 3f58cdd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
11 changes: 5 additions & 6 deletions src/adapters/aws/api-gateway-v2.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getDefaultIfUndefined,
getEventBodyAsBuffer,
getFlattenedHeadersMap,
getMultiValueHeadersMap,
getPathWithQueryStringParams,
} from '../../core';

Expand Down Expand Up @@ -127,6 +128,7 @@ export class ApiGatewayV2Adapter
response,
}: GetResponseAdapterProps<APIGatewayProxyEventV2>): APIGatewayProxyStructuredResultV2 {
const headers = getFlattenedHeadersMap(responseHeaders);
const multiValueHeaders = getMultiValueHeadersMap(responseHeaders);

const transferEncodingHeader: string | undefined =
headers['transfer-encoding'];
Expand All @@ -146,19 +148,16 @@ export class ApiGatewayV2Adapter
);
}

const cookies = headers['set-cookie'];
const cookies = multiValueHeaders['set-cookie'];

if (cookies) {
headers.cookies = cookies;

delete headers['set-cookie'];
}
if (headers) delete headers['set-cookie'];

return {
statusCode,
body,
headers,
isBase64Encoded,
cookies,
};
}

Expand Down
51 changes: 47 additions & 4 deletions test/adapters/aws/api-gateway-v2.adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe(ApiGatewayV2Adapter.name, () => {
const requestBody = { name: 'H4ad Collaborator V2' };
const queryParams = { page: '2' };
const cookies = ['batata', 'joga10'];
const resultCookies = 'batata; joga10; h4ad';
const resultCookies = 'batata; joga10';

const resultBody = '{"success":true}';
const resultStatusCode = 200;
Expand All @@ -155,22 +155,65 @@ describe(ApiGatewayV2Adapter.name, () => {
);
const resultHeaders = getFlattenedHeadersMap(event.headers);

resultHeaders['set-cookie'] = resultCookies;
const result = adapter.getResponse({
event,
log: {} as ILogger,
body: resultBody,
isBase64Encoded: resultIsBase64Encoded,
statusCode: resultStatusCode,
headers: {
...resultHeaders,
'set-cookie': resultCookies,
},
});

expect(result).toHaveProperty('statusCode', 200);
expect(result).toHaveProperty('body', resultBody);
expect(result).toHaveProperty('headers');
expect(result.headers).toHaveProperty('set-cookie', undefined);
expect(result).toHaveProperty('cookies', [resultCookies]);
expect(result).toHaveProperty('isBase64Encoded', resultIsBase64Encoded);
});

it('should return the correct mapping for the response when set-cookie is array', () => {
const method = 'PUT';
const path = '/collaborators';
const requestBody = { name: 'H4ad Collaborator V2' };
const queryParams = { page: '2' };
const cookies = ['batata', 'joga10'];
const resultCookies = ['batata', 'joga10'];

const resultBody = '{"success":true}';
const resultStatusCode = 200;
const resultIsBase64Encoded = false;

const event = createApiGatewayV2(
method,
path,
requestBody,
{},
queryParams,
cookies,
);
const resultHeaders = getFlattenedHeadersMap(event.headers);

const result = adapter.getResponse({
event,
log: {} as ILogger,
body: resultBody,
isBase64Encoded: resultIsBase64Encoded,
statusCode: resultStatusCode,
headers: resultHeaders,
headers: {
...resultHeaders,
'set-cookie': resultCookies,
},
});

expect(result).toHaveProperty('statusCode', 200);
expect(result).toHaveProperty('body', resultBody);
expect(result).toHaveProperty('headers');
expect(result.headers).toHaveProperty('set-cookie', undefined);
expect(result.headers).toHaveProperty('cookies', resultCookies);
expect(result).toHaveProperty('cookies', resultCookies);
expect(result).toHaveProperty('isBase64Encoded', resultIsBase64Encoded);
});

Expand Down

0 comments on commit 3f58cdd

Please sign in to comment.