Skip to content

Commit

Permalink
fix: koa response 204 (#733)
Browse files Browse the repository at this point in the history
* fix: koa response 204

* fix: response 204
  • Loading branch information
echosoar authored Nov 28, 2020
1 parent 2e430f3 commit 2463d77
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/context/midwayContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const DEFAULT_IGNORE_PATTERN = [
'**/view/**',
'**/views/**',
'**/app/extend/**',
'**/node_modules/**'
'**/node_modules/**',
];

const globalDebugLogger = util.debuglog('midway:container');
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ export {
} from '@midwayjs/decorator';
export * from './interface';
export { ContainerLoader } from './loader';
export { MidwayContainer, clearContainerCache } from './context/midwayContainer';
export {
MidwayContainer,
clearContainerCache,
} from './context/midwayContainer';
export { MidwayRequestContainer } from './context/requestContainer';
export { BaseFramework } from './baseFramework';
export * from './context/providerWrapper';
Expand Down
9 changes: 4 additions & 5 deletions packages/web-express/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ export class MidwayExpressFramework extends BaseFramework<
const controller = await req.requestContext.getAsync(controllerId);
// eslint-disable-next-line prefer-spread
const result = await controller[methodName].apply(controller, args);

if (res.statusCode === 200 && (result === null || result === undefined)) {
res.status(204);
}
// implement response decorator
if (Array.isArray(routerResponseData) && routerResponseData.length) {
for (const routerRes of routerResponseData) {
Expand All @@ -152,10 +154,7 @@ export class MidwayExpressFramework extends BaseFramework<
}
}
}

if (result) {
res.send(result);
}
res.send(result);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export class APIController {
return 'hello world,' + name;
}

@Get('/204')
async status204() {
// empty
}

@Get('/login')
@Redirect('/')
async redirect() {
Expand Down
6 changes: 6 additions & 0 deletions packages/web-express/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ describe('/test/feature.test.ts', () => {
const result = await createHttpRequest(app).get('/login');
expect(result.status).toBe(302);
});

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

});
5 changes: 1 addition & 4 deletions packages/web-koa/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ 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) {
ctx.body = result;
}
ctx.body = await controller[methodName].apply(controller, args);

// implement response decorator
if (Array.isArray(routerResponseData) && routerResponseData.length) {
Expand Down
5 changes: 5 additions & 0 deletions packages/web-koa/test/fixtures/base-app/src/controller/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export class APIController {
return 'hello world,' + name + age;
}

@Get('/204')
async status204() {
// empty
}

@Get('/login')
@Redirect('/')
async redirect() {}
Expand Down
5 changes: 5 additions & 0 deletions packages/web-koa/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ describe('/test/feature.test.ts', () => {
const result = await createHttpRequest(app).get('/login');
expect(result.status).toBe(302);
});

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

});
5 changes: 5 additions & 0 deletions packages/web/test/feature.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ describe('/test/feature.test.ts', () => {
expect(result.headers['ccc']).toEqual('ddd');
});

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

it('test get method with return value', async () => {
const result = await createHttpRequest(app)
.get('/')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ export class APIController {
async redirect() {
}

@Get('/204')
async r204() {
//
}

}

0 comments on commit 2463d77

Please sign in to comment.