Skip to content

Commit

Permalink
fix: status 204 when user not set status (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
echosoar committed Jul 20, 2020
1 parent 4fa679d commit 9276fe8
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
10 changes: 6 additions & 4 deletions packages/serverless-fc-starter/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions packages/serverless-fc-starter/test/fixtures/http-302/index.ts
Original file line number Diff line number Diff line change
@@ -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);
});
19 changes: 19 additions & 0 deletions packages/serverless-fc-starter/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
10 changes: 6 additions & 4 deletions packages/serverless-scf-starter/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
12 changes: 12 additions & 0 deletions packages/serverless-scf-starter/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 9276fe8

Please sign in to comment.