fix: res.flushHeaders() (#795)

* fix: res.flushHeaders()

* remove arg to flush headers

* fix tests for node v4 and v5
This commit is contained in:
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 * Flush any set headers, and begin the body
*/ */
flushHeaders() { flushHeaders() {
this.res.writeHead(this.res.statusCode); this.res.flushHeaders();
} }
}; };

View file

@ -4,6 +4,7 @@
const request = require('supertest'); const request = require('supertest');
const assert = require('assert'); const assert = require('assert');
const Koa = require('../..'); const Koa = require('../..');
const http = require('http');
describe('ctx.flushHeaders()', () => { describe('ctx.flushHeaders()', () => {
it('should set headersSent', done => { it('should set headersSent', done => {
@ -95,4 +96,43 @@ describe('ctx.flushHeaders()', () => {
done(err); 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();
});
});
}); });