var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
if (req.listeners(eventName).length) {
req.upgradeOrConnect = true;
- req.emit(eventName, res, socket, bodyHead);
+
+ // detach the socket
socket.emit('agentRemove');
+ socket.removeListener('close', closeListener);
+ socket.removeListener('error', errorListener);
+
+ req.emit(eventName, res, socket, bodyHead);
+ req.emit('close');
} else {
// Got Upgrade header or CONNECT method, but have no handler.
socket.destroy();
}
if (cb) { cb(); }
} else {
- self.socket.on('connect', function() {
+ self.socket.once('connect', function() {
if (method) {
self.socket[method].apply(self.socket, arguments_);
}
}, function(res) {
assert(false);
});
+
+ var clientRequestClosed = false;
+ req.on('close', function() {
+ clientRequestClosed = true;
+ });
+
req.on('connect', function(res, socket, firstBodyChunk) {
common.debug('Client got CONNECT request');
clientGotConnect = true;
+ // Make sure this request got removed from the pool.
+ var name = 'localhost:' + common.PORT;
+ assert(!http.globalAgent.sockets.hasOwnProperty(name));
+ assert(!http.globalAgent.requests.hasOwnProperty(name));
+
+ // Make sure this socket has detached.
+ assert(!socket.ondata);
+ assert(!socket.onend);
+ assert.equal(socket.listeners('connect').length, 0);
+ assert.equal(socket.listeners('data').length, 0);
+ assert.equal(socket.listeners('end').length, 0);
+ assert.equal(socket.listeners('free').length, 0);
+ assert.equal(socket.listeners('close').length, 0);
+ assert.equal(socket.listeners('error').length, 0);
+ assert.equal(socket.listeners('agentRemove').length, 0);
+
var data = firstBodyChunk.toString();
socket.on('data', function(buf) {
data += buf.toString();
});
socket.on('end', function() {
assert.equal(data, 'HeadBody');
+ assert(clientRequestClosed);
server.close();
});
socket.write('Body');
process.on('exit', function() {
assert.ok(serverGotConnect);
assert.ok(clientGotConnect);
-
- // Make sure this request got removed from the pool.
- var name = 'localhost:' + common.PORT;
- assert(!http.globalAgent.sockets.hasOwnProperty(name));
- assert(!http.globalAgent.requests.hasOwnProperty(name));
});
'connection': 'upgrade',
'upgrade': 'websocket' };
assert.deepEqual(expectedHeaders, res.headers);
- assert.equal(http.globalAgent.sockets[name].length, 1);
- process.nextTick(function() {
- // Make sure this request got removed from the pool.
- assert(!http.globalAgent.sockets.hasOwnProperty(name));
+ // Make sure this request got removed from the pool.
+ assert(!http.globalAgent.sockets.hasOwnProperty(name));
+
+ req.on('close', function() {
socket.end();
srv.close();