diff --git a/lib/application.js b/lib/application.js index 84107cb..9919d99 100644 --- a/lib/application.js +++ b/lib/application.js @@ -222,7 +222,11 @@ function respond(ctx) { // status body if (null == body) { - body = ctx.message || String(code); + if (ctx.req.httpVersionMajor >= 2) { + body = String(code); + } else { + body = ctx.message || String(code); + } if (!res.headersSent) { ctx.type = 'text'; ctx.length = Buffer.byteLength(body); diff --git a/test/application/response.js b/test/application/response.js index 432fb26..b1ef016 100644 --- a/test/application/response.js +++ b/test/application/response.js @@ -9,6 +9,7 @@ describe('app.response', () => { const app1 = new Koa(); app1.response.msg = 'hello'; const app2 = new Koa(); + const app3 = new Koa(); it('should merge properties', () => { app1.use((ctx, next) => { @@ -31,4 +32,15 @@ describe('app.response', () => { .get('/') .expect(204); }); + + it('should not include status message in body for http2', async () => { + app3.use((ctx, next) => { + ctx.req.httpVersionMajor = 2; + ctx.status = 404; + }); + const response = await request(app3.listen()) + .get('/') + .expect(404); + assert.equal(response.text, '404'); + }); });