Skip to content

Commit 8d52105

Browse files
ejose19Eladio Mora
and
Eladio Mora
authored
feat: allow bodyless responses for non empty status codes (#1447)
Co-authored-by: Eladio Mora <[email protected]>
1 parent 6b6b0dd commit 8d52105

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

lib/application.js

+5
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,11 @@ function respond(ctx) {
238238

239239
// status body
240240
if (null == body) {
241+
if (ctx.response._explicitNullBody) {
242+
ctx.response.remove('Content-Type');
243+
ctx.response.remove('Transfer-Encoding');
244+
return res.end();
245+
}
241246
if (ctx.req.httpVersionMajor >= 2) {
242247
body = String(code);
243248
} else {

lib/response.js

+1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ module.exports = {
139139
// no content
140140
if (null == val) {
141141
if (!statuses.empty[this.status]) this.status = 204;
142+
if (val === null) this._explicitNullBody = true;
142143
this.remove('Content-Type');
143144
this.remove('Content-Length');
144145
this.remove('Transfer-Encoding');

test/application/respond.js

+37
Original file line numberDiff line numberDiff line change
@@ -810,4 +810,41 @@ describe('app.respond', () => {
810810
assert.equal(res.headers.hasOwnProperty('content-type'), false);
811811
});
812812
});
813+
814+
describe('with explicit null body', () => {
815+
it('should preserve given status', async() => {
816+
const app = new Koa();
817+
818+
app.use(ctx => {
819+
ctx.body = null;
820+
ctx.status = 404;
821+
});
822+
823+
const server = app.listen();
824+
825+
return request(server)
826+
.get('/')
827+
.expect(404)
828+
.expect('')
829+
.expect({});
830+
});
831+
it('should respond with correct headers', async() => {
832+
const app = new Koa();
833+
834+
app.use(ctx => {
835+
ctx.body = null;
836+
ctx.status = 401;
837+
});
838+
839+
const server = app.listen();
840+
841+
const res = await request(server)
842+
.get('/')
843+
.expect(401)
844+
.expect('')
845+
.expect({});
846+
847+
assert.equal(res.headers.hasOwnProperty('content-type'), false);
848+
});
849+
});
813850
});

test/application/response.js

+28
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ describe('app.response', () => {
1010
app1.response.msg = 'hello';
1111
const app2 = new Koa();
1212
const app3 = new Koa();
13+
const app4 = new Koa();
14+
const app5 = new Koa();
1315

1416
it('should merge properties', () => {
1517
app1.use((ctx, next) => {
@@ -43,4 +45,30 @@ describe('app.response', () => {
4345
.expect(404);
4446
assert.equal(response.text, '404');
4547
});
48+
49+
it('should set ._explicitNullBody correctly', async() => {
50+
app4.use((ctx, next) => {
51+
ctx.body = null;
52+
assert.strictEqual(ctx.response._explicitNullBody, true);
53+
});
54+
55+
return request(app4.listen())
56+
.get('/')
57+
.expect(204);
58+
});
59+
60+
it('should not set ._explicitNullBody incorrectly', async() => {
61+
app5.use((ctx, next) => {
62+
ctx.body = undefined;
63+
assert.strictEqual(ctx.response._explicitNullBody, undefined);
64+
ctx.body = '';
65+
assert.strictEqual(ctx.response._explicitNullBody, undefined);
66+
ctx.body = false;
67+
assert.strictEqual(ctx.response._explicitNullBody, undefined);
68+
});
69+
70+
return request(app5.listen())
71+
.get('/')
72+
.expect(204);
73+
});
4674
});

0 commit comments

Comments
 (0)