Skip to content

Commit 42382b4

Browse files
committed
fix http keep-alive test case
1 parent 920f9db commit 42382b4

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
"vary": "^1.0.0"
4444
},
4545
"devDependencies": {
46+
"agentkeepalive": "~2.0.3",
4647
"babel": "^5.0.0",
48+
"freeport": "~1.0.5",
4749
"istanbul": "^0.4.0",
4850
"make-lint": "^1.0.1",
4951
"mocha": "^2.0.1",

test/application.js

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
'use strict';
33

44
var stderr = require('test-console').stderr;
5+
var Agent = require('agentkeepalive');
56
var request = require('supertest');
67
var statuses = require('statuses');
8+
var freeport = require('freeport');
79
var pedding = require('pedding');
810
var assert = require('assert');
911
var urllib = require('urllib');
@@ -896,29 +898,47 @@ describe('app.respond', function(){
896898
})
897899
})
898900

899-
it('should not destroy keepalive connection', function(done){
900-
done = pedding(2, done);
901-
var app = koa();
902-
app.use(function *(){
903-
var remote = yield urllib.request('http://koajs.com', {
904-
streaming: true,
905-
headers: {
906-
connection: 'keep-alive'
907-
}
901+
describe('when .body is a http keepalive IncommingMessage', () => {
902+
var target;
903+
var port;
904+
before(function(done){
905+
var app = koa();
906+
app.use(function *(){
907+
this.body = fs.createReadStream(__filename);
908908
});
909-
var res = remote.res;
910-
this.body = res;
911-
res.once('end', function(){
912-
assert.equal(res.readable, false);
913-
assert.equal(res.socket.destroyed, false);
914-
done();
915-
}.bind(this));
916-
});
917909

918-
var server = app.listen();
919-
request(server)
920-
.head('/')
921-
.expect(200, done);
910+
freeport(function(err, p){
911+
port = p || 12384;
912+
target = app.listen(port, done);
913+
});
914+
})
915+
916+
after(function(){
917+
target.close();
918+
})
919+
920+
it('should not destroy keepalive connection', function(done){
921+
done = pedding(2, done);
922+
var app = koa();
923+
app.use(function *(){
924+
var remote = yield urllib.request('http://127.0.0.1:' + port, {
925+
streaming: true,
926+
agent: new Agent()
927+
});
928+
var res = remote.res;
929+
this.body = res;
930+
res.once('end', function(){
931+
assert.equal(res.readable, false);
932+
assert.equal(res.socket.destroyed, false);
933+
done();
934+
});
935+
});
936+
937+
var server = app.listen();
938+
request(server)
939+
.head('/')
940+
.expect(200, done);
941+
})
922942
})
923943

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

0 commit comments

Comments
 (0)