Skip to content
This repository has been archived by the owner on Jul 2, 2020. It is now read-only.

Commit

Permalink
fix: fix cookie in fc apigw
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed May 2, 2020
1 parent 4438758 commit e15decb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
17 changes: 15 additions & 2 deletions packages/serverless-fc-starter/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as getRawBody from 'raw-body';
import { FAAS_ARGS_KEY, ServerlessLightRuntime, } from '@midwayjs/runtime-engine';
import {
FAAS_ARGS_KEY,
ServerlessLightRuntime,
} from '@midwayjs/runtime-engine';
import { Context } from '@midwayjs/serverless-http-parser';
import * as util from 'util';

Expand Down Expand Up @@ -105,7 +108,17 @@ export class FCRuntime extends ServerlessLightRuntime {
for (const key in ctx.res.headers) {
// The length after base64 is wrong.
if (!['content-length'].includes(key)) {
newHeader[key] = ctx.res.headers[key];
if ('set-cookie' === key && !isHTTPMode) {
// unsupport multiple cookie when use apiGateway
newHeader[key] = ctx.res.headers[key][0];
if (ctx.res.headers[key].length > 1) {
ctx.logger.warn(
`[fc-starter]: unsupport multiple cookie when use apiGateway`
);
}
} else {
newHeader[key] = ctx.res.headers[key];
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/serverless-fc-starter/test/fixtures/apigw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ exports.handler = asyncWrapper(async (...args) => {
deepStrictEqual(event.path, '/test');
deepStrictEqual(event.httpMethod, 'POST');
ctx.status = 200;
ctx.set('set-cookie', [
'bbbb=123; path=/; httponly',
'ccc=321; path=/; httponly',
]);
ctx.body = {
headers: ctx.headers,
method: ctx.method,
Expand Down
2 changes: 2 additions & 0 deletions packages/serverless-fc-starter/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@ describe('/test/index.test.ts', () => {
assert(result.isBase64Encoded === false);
assert(result.statusCode === 200);
assert(result.headers);
assert(result.headers['set-cookie'] === 'bbbb=123; path=/; httponly');
result.headers;
assert.equal(typeof result.body, 'string');
const body = JSON.parse(result.body);
assert.equal(body.method, 'POST');
Expand Down

0 comments on commit e15decb

Please sign in to comment.