Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore set body after user set status #741

Merged
merged 1 commit into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});
});
});

});