0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-25 08:19:38 +01:00
nodejs/test/parallel/test-http-client-agent.js

49 lines
1.1 KiB
JavaScript
Raw Normal View History

2011-10-16 17:32:13 +02:00
var common = require('../common');
var assert = require('assert');
var http = require('http');
2013-05-23 03:44:24 +02:00
var name = http.globalAgent.getName({ port: common.PORT });
2011-10-16 17:32:13 +02:00
var max = 3;
var count = 0;
var server = http.Server(function(req, res) {
if (req.url === '/0') {
setTimeout(function() {
res.writeHead(200);
res.end('Hello, World!');
}, 100);
} else {
res.writeHead(200);
res.end('Hello, World!');
}
});
server.listen(common.PORT, function() {
for (var i = 0; i < max; ++i) {
request(i);
}
});
function request(i) {
var req = http.get({
port: common.PORT,
path: '/' + i
}, function(res) {
var socket = req.socket;
socket.on('close', function() {
++count;
2011-11-29 12:55:05 +01:00
if (count < max) {
assert.equal(http.globalAgent.sockets[name].indexOf(socket), -1);
} else {
assert(!http.globalAgent.sockets.hasOwnProperty(name));
assert(!http.globalAgent.requests.hasOwnProperty(name));
2011-10-16 17:32:13 +02:00
server.close();
}
});
res.resume();
2011-10-16 17:32:13 +02:00
});
}
process.on('exit', function() {
assert.equal(count, max);
});