fix: res.flushHeaders() (#795)

* fix: res.flushHeaders()

* remove arg to flush headers

* fix tests for node v4 and v5
master
jongleberry 2016-08-10 12:15:48 -07:00 committed by GitHub
parent ce75a9c872
commit 2abed6ec75
2 changed files with 41 additions and 1 deletions

View File

@ -534,6 +534,6 @@ module.exports = {
* Flush any set headers, and begin the body
*/
flushHeaders() {
this.res.writeHead(this.res.statusCode);
this.res.flushHeaders();
}
};

View File

@ -4,6 +4,7 @@
const request = require('supertest');
const assert = require('assert');
const Koa = require('../..');
const http = require('http');
describe('ctx.flushHeaders()', () => {
it('should set headersSent', done => {
@ -95,4 +96,43 @@ describe('ctx.flushHeaders()', () => {
done(err);
});
});
it('should flush headers first and delay to send data', done => {
const PassThrough = require('stream').PassThrough;
const app = new Koa();
app.use(ctx => {
ctx.type = 'json';
ctx.status = 200;
ctx.headers['Link'] = '</css/mycss.css>; as=style; rel=preload, <https://img.craftflair.com>; rel=preconnect; crossorigin';
const stream = ctx.body = new PassThrough();
ctx.flushHeaders();
setTimeout(() => {
stream.end(JSON.stringify({ message: 'hello!' }));
}, 10000);
});
app.listen(function(err){
if (err) return done(err);
const port = this.address().port;
http.request({
port
})
.on('response', res => {
const onData = () => done(new Error('boom'));
res.on('data', onData);
// shouldn't receive any data for a while
setTimeout(() => {
res.removeListener('data', onData);
done();
}, 1000);
})
.on('error', done)
.end();
});
});
});