From 9276fe846b983f4f870c2b3eb97e2c254bd5ff5a Mon Sep 17 00:00:00 2001 From: Gao Yang Date: Mon, 20 Jul 2020 20:34:56 +0800 Subject: [PATCH] fix: status 204 when user not set status (#533) --- packages/serverless-fc-starter/src/runtime.ts | 10 ++++++---- .../test/fixtures/http-302/index.ts | 15 +++++++++++++++ .../serverless-fc-starter/test/index.test.ts | 19 +++++++++++++++++++ .../serverless-scf-starter/src/runtime.ts | 10 ++++++---- .../serverless-scf-starter/test/index.test.ts | 12 ++++++++++++ 5 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 packages/serverless-fc-starter/test/fixtures/http-302/index.ts diff --git a/packages/serverless-fc-starter/src/runtime.ts b/packages/serverless-fc-starter/src/runtime.ts index aec78b723848..d38ed30746a4 100644 --- a/packages/serverless-fc-starter/src/runtime.ts +++ b/packages/serverless-fc-starter/src/runtime.ts @@ -97,10 +97,12 @@ export class FCRuntime extends ServerlessLightRuntime { ctx.body = result; } - if (ctx.body === null || ctx.body === 'undefined') { - ctx.body = ''; - ctx.type = 'text'; - ctx.status = 204; + if (!ctx.response.explicitStatus) { + if (ctx.body === null || ctx.body === 'undefined') { + ctx.body = ''; + ctx.type = 'text'; + ctx.status = 204; + } } let encoded = false; diff --git a/packages/serverless-fc-starter/test/fixtures/http-302/index.ts b/packages/serverless-fc-starter/test/fixtures/http-302/index.ts new file mode 100644 index 000000000000..ecb670e0704a --- /dev/null +++ b/packages/serverless-fc-starter/test/fixtures/http-302/index.ts @@ -0,0 +1,15 @@ +import { asyncWrapper, start } from '../../../src'; + +let runtime; +let inited; + +exports.handler = asyncWrapper(async (...args) => { + if (!inited) { + inited = true; + runtime = await start(); + } + return runtime.asyncEvent(async function (ctx) { + ctx.status = 302; + ctx.set('Location', 'https://github.com/midwayjs/midway'); + })(...args); +}); diff --git a/packages/serverless-fc-starter/test/index.test.ts b/packages/serverless-fc-starter/test/index.test.ts index bca06668c205..67301bfa40ae 100644 --- a/packages/serverless-fc-starter/test/index.test.ts +++ b/packages/serverless-fc-starter/test/index.test.ts @@ -423,6 +423,25 @@ describe('/test/index.test.ts', () => { await runtime.close(); }); + it('should invoke 302', async () => { + const runtime = createRuntime({ + functionDir: join(__dirname, './fixtures/http-302'), + }); + await runtime.start(); + const result = await runtime.invoke( + new HTTPTrigger({ + path: '/help', + method: 'GET', + }) + ); + await runtime.close(); + assert.equal(result.statusCode, 302); + assert.equal( + result.headers.location, + 'https://github.com/midwayjs/midway' + ); + }); + it('should invoke normal code', async () => { const runtime = createRuntime({ functionDir: join(__dirname, './fixtures/http-json'), diff --git a/packages/serverless-scf-starter/src/runtime.ts b/packages/serverless-scf-starter/src/runtime.ts index af63ae123bc5..c94db5ae0bd3 100644 --- a/packages/serverless-scf-starter/src/runtime.ts +++ b/packages/serverless-scf-starter/src/runtime.ts @@ -62,10 +62,12 @@ export class SCFRuntime extends ServerlessLightRuntime { ctx.body = result; } - if (ctx.body === null || ctx.body === 'undefined') { - ctx.body = ''; - ctx.type = 'text'; - ctx.status = 204; + if (!ctx.response.explicitStatus) { + if (ctx.body === null || ctx.body === 'undefined') { + ctx.body = ''; + ctx.type = 'text'; + ctx.status = 204; + } } const setContentType = (type: string) => { diff --git a/packages/serverless-scf-starter/test/index.test.ts b/packages/serverless-scf-starter/test/index.test.ts index dad75b306fe8..22dad6c0ab93 100644 --- a/packages/serverless-scf-starter/test/index.test.ts +++ b/packages/serverless-scf-starter/test/index.test.ts @@ -89,6 +89,18 @@ describe('/test/index.test.ts', () => { assert.equal(res.body, '{"ok":true}'); }); + it('should 302', async () => { + const runtime = await start(); + const handle = asyncWrapper(async (...args) => { + return runtime.asyncEvent(async ctx => { + ctx.status = 302; + ctx.set('Location', 'https://github.com/midwayjs/midway'); + })(...args); + }); + const res = await test(handle).runHttp(require('../resource/event'), {}); + assert.equal(res.statusCode, 302); + }); + it('should ok with raw json', async () => { const runtime = await start(); const handle = asyncWrapper(async (...args) => {