Skip to content

Commit

Permalink
fix: ignore set body after user set status (#741)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Dec 4, 2020
1 parent d40de78 commit 4fdb2a6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/web-koa/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ export abstract class MidwayKoaBaseFramework<
const controller = await ctx.requestContext.getAsync(controllerId);
// eslint-disable-next-line prefer-spread
const result = await controller[methodName].apply(controller, args);
if (result) {
if (result !== undefined) {
ctx.body = result;
}

if (!ctx.body) {
if (ctx.body === undefined && !(ctx.response as any)._explicitStatus) {
ctx.body = undefined;
}

Expand Down
41 changes: 41 additions & 0 deletions packages/web-koa/test/fixtures/base-app/src/controller/case.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Get, Provide, Controller, Inject } from '@midwayjs/decorator';

@Provide()
@Controller('/case')
export class CaseController {

@Inject()
ctx;

@Get('/500')
async status500() {
this.ctx.status = 500;
}

@Get('/500_1')
async status500_1() {
this.ctx.status = 500;
return '';
}

@Get('/204')
async status204() {
this.ctx.status = 204;
}

@Get('/204_1')
async status204_1() {
this.ctx.status = 204;
return '';
}

@Get('/204_2')
async status204_2() {
}

@Get('/204_3')
async status204_3() {
this.ctx.status = 500;
this.ctx.body = undefined;
}
}
30 changes: 30 additions & 0 deletions packages/web-koa/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,36 @@ describe('/test/feature.test.ts', () => {
const result = await createHttpRequest(app).get('/ctx-body');
expect(result.text).toEqual('ctx-body');
});

describe('test 500', function () {
it('test status 500', async () => {
const result = await createHttpRequest(app).get('/case/500');
expect(result.status).toBe(500);
});

it('test status 500_1', async () => {
const result = await createHttpRequest(app).get('/case/500');
expect(result.status).toBe(500);
});

});

describe('test 204', function () {
it('test status 204', async () => {
const result = await createHttpRequest(app).get('/case/204');
expect(result.status).toBe(204);
});

it('test status 204_1', async () => {
const result = await createHttpRequest(app).get('/case/204_1');
expect(result.status).toBe(204);
});

it('test status 204_2', async () => {
const result = await createHttpRequest(app).get('/case/204_2');
expect(result.status).toBe(204);
});
});
});

});

0 comments on commit 4fdb2a6

Please sign in to comment.