Skip to content

Commit

Permalink
fix http keep-alive test case
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Dec 2, 2015
1 parent 920f9db commit 65f13fb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
6 changes: 2 additions & 4 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ var statuses = require('statuses');
var assert = require('assert');
var Stream = require('stream');
var path = require('path');
var extname = path.extname;
var vary = require('vary');
var extname = path.extname;

/**
* Prototype.
Expand Down Expand Up @@ -165,9 +165,7 @@ module.exports = {
if (val instanceof Stream) {
onFinish(this.res, function(){
// make sure stream's data has been read
if (val.readable) {
val.pipe(new BlackHoleStream());
}
if (val.readable) val.pipe(new BlackHoleStream());
});
ensureErrorHandler(val, this.ctx.onerror);

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
"vary": "^1.0.0"
},
"devDependencies": {
"agentkeepalive": "~2.0.3",
"babel": "^5.0.0",
"freeport": "~1.0.5",
"istanbul": "^0.4.0",
"make-lint": "^1.0.1",
"mocha": "^2.0.1",
Expand Down
62 changes: 41 additions & 21 deletions test/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
'use strict';

var stderr = require('test-console').stderr;
var Agent = require('agentkeepalive');
var request = require('supertest');
var statuses = require('statuses');
var freeport = require('freeport');
var pedding = require('pedding');
var assert = require('assert');
var urllib = require('urllib');
Expand Down Expand Up @@ -896,29 +898,47 @@ describe('app.respond', function(){
})
})

it('should not destroy keepalive connection', function(done){
done = pedding(2, done);
var app = koa();
app.use(function *(){
var remote = yield urllib.request('http://koajs.com', {
streaming: true,
headers: {
connection: 'keep-alive'
}
describe('when .body is a http keepalive IncommingMessage', () => {
var target;
var port;
before(function(done){
var app = koa();
app.use(function *(){
this.body = fs.createReadStream(__filename);
});
var res = remote.res;
this.body = res;
res.once('end', function(){
assert.equal(res.readable, false);
assert.equal(res.socket.destroyed, false);
done();
}.bind(this));
});

var server = app.listen();
request(server)
.head('/')
.expect(200, done);
freeport(function(err, p){
port = p || 12384;
target = app.listen(port, done);
});
})

after(function(){
target.close();
})

it('should not destroy keepalive connection', function(done){
done = pedding(2, done);
var app = koa();
app.use(function *(){
var remote = yield urllib.request('http://127.0.0.1:' + port, {
streaming: true,
agent: new Agent()
});
var res = remote.res;
this.body = res;
res.once('end', function(){
assert.equal(res.readable, false);
assert.equal(res.socket.destroyed, false);
done();
});
});

var server = app.listen();
request(server)
.head('/')
.expect(200, done);
})
})

describe('when .body is an Object', function(){
Expand Down

0 comments on commit 65f13fb

Please sign in to comment.