diff --git a/lib/unexpectedMitm.js b/lib/unexpectedMitm.js index aba9a6b..3a7dae3 100644 --- a/lib/unexpectedMitm.js +++ b/lib/unexpectedMitm.js @@ -614,23 +614,20 @@ module.exports = { recordedExchanges = []; return expect.promise(function (resolve, reject) { - var bypassNextConnect = false, - lastHijackedSocket, - lastHijackedSocketOptions; + var bypassNextConnect = false; mitm.on('connect', function (socket, opts) { if (bypassNextConnect) { socket.bypass(); bypassNextConnect = false; - } else { - lastHijackedSocket = socket; - lastHijackedSocketOptions = opts; } }).on('request', createSerializedRequestHandler(function (req, res) { + var clientSocket = req.connection._mitm.client; + var clientSocketOptions = req.connection._mitm.opts; var metadata = _.extend( {}, - _.pick(lastHijackedSocketOptions.agent && lastHijackedSocketOptions.agent.options, metadataPropertyNames), - _.pick(lastHijackedSocketOptions, metadataPropertyNames) + _.pick(clientSocketOptions.agent && clientSocketOptions.agent.options, metadataPropertyNames), + _.pick(clientSocketOptions, metadataPropertyNames) ), recordedExchange = { request: _.extend({ @@ -688,7 +685,7 @@ module.exports = { }); }).caught(function (err) { recordedExchange.response = err; - lastHijackedSocket.emit('error', err); + clientSocket.emit('error', err); }); }); })); @@ -757,29 +754,26 @@ module.exports = { var assertionPromise = expect.promise(function (resolve, reject) { var httpConversation = new messy.HttpConversation(), - httpConversationSatisfySpec = {exchanges: []}, - lastHijackedSocket, - lastHijackedSocketOptions; + httpConversationSatisfySpec = {exchanges: []}; __lastError = null; - mitm.on('connect', function (socket, opts) { - lastHijackedSocket = socket; - lastHijackedSocketOptions = opts; - if (typeof lastHijackedSocketOptions.port === 'string') { + mitm.on('request', createSerializedRequestHandler(function (req, res) { + var clientSocket = req.connection._mitm.client; + var clientSocketOptions = req.connection._mitm.opts; + if (typeof clientSocketOptions.port === 'string') { // The port could have been defined as a string in a 3rdparty library doing the http(s) call, and that seems to be valid use of the http(s) module - lastHijackedSocketOptions = _.defaults({ - port: parseInt(lastHijackedSocketOptions.port, 10) - }, lastHijackedSocketOptions); + clientSocketOptions = _.defaults({ + port: parseInt(clientSocketOptions.port, 10) + }, clientSocketOptions); } - }).on('request', createSerializedRequestHandler(function (req, res) { var currentDescription = requestDescriptions.shift(), hasRequestDescription = !!currentDescription, metadata = _.defaults( { encrypted: Boolean(res.connection.encrypted) }, - _.pick(lastHijackedSocketOptions, messy.HttpRequest.metadataPropertyNames), - _.pick(lastHijackedSocketOptions && lastHijackedSocketOptions.agent && lastHijackedSocketOptions.agent.options, messy.HttpRequest.metadataPropertyNames) + _.pick(clientSocketOptions, messy.HttpRequest.metadataPropertyNames), + _.pick(clientSocketOptions && clientSocketOptions.agent && clientSocketOptions.agent.options, messy.HttpRequest.metadataPropertyNames) ), requestDescription = currentDescription, responseProperties = requestDescription && requestDescription.response, @@ -845,7 +839,7 @@ module.exports = { } if (mockResponseError) { setImmediate(function () { - lastHijackedSocket.emit('error', mockResponseError); + clientSocket.emit('error', mockResponseError); assertMockResponse(mockResponse, mockResponseError); }); } else { @@ -875,7 +869,7 @@ module.exports = { */ // cancel the delegated assertion - lastHijackedSocket.emit('error', new Error('unexpected-mitm: Saw unexpected requests.')); + clientSocket.emit('error', new Error('unexpected-mitm: Saw unexpected requests.')); // continue with current assertion resolve([null, httpConversation, httpConversationSatisfySpec]); } @@ -934,7 +928,7 @@ module.exports = { // record the error __lastError = e; // cancel the delegated assertion - lastHijackedSocket.emit('error', e); + clientSocket.emit('error', e); }); }); } @@ -946,7 +940,7 @@ module.exports = { * will still be pending and must be completed. We * do this by signalling the error on the socket. */ - lastHijackedSocket.emit('error', e); + clientSocket.emit('error', e); reject(e); }); }));