fix: Status message is not supported on HTTP/2 (#1264)

master
André Cruz 2018-11-09 17:41:21 +00:00 committed by Yiyu He
parent 71aaa29591
commit 99051992a9
2 changed files with 17 additions and 1 deletions

View File

@ -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);

View File

@ -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');
});
});