Skip to content

Commit

Permalink
Use req.connection._mitm.{client,opts} instead of trying to keep trac…
Browse files Browse the repository at this point in the history
…k of the last hijacked socket and options. Fixes failing test.
  • Loading branch information
papandreou committed Aug 3, 2016
1 parent e56d302 commit c411666
Showing 1 changed file with 20 additions and 26 deletions.
46 changes: 20 additions & 26 deletions lib/unexpectedMitm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -688,7 +685,7 @@ module.exports = {
});
}).caught(function (err) {
recordedExchange.response = err;
lastHijackedSocket.emit('error', err);
clientSocket.emit('error', err);
});
});
}));
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -845,7 +839,7 @@ module.exports = {
}
if (mockResponseError) {
setImmediate(function () {
lastHijackedSocket.emit('error', mockResponseError);
clientSocket.emit('error', mockResponseError);
assertMockResponse(mockResponse, mockResponseError);
});
} else {
Expand Down Expand Up @@ -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]);
}
Expand Down Expand Up @@ -934,7 +928,7 @@ module.exports = {
// record the error
__lastError = e;
// cancel the delegated assertion
lastHijackedSocket.emit('error', e);
clientSocket.emit('error', e);
});
});
}
Expand All @@ -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);
});
}));
Expand Down

0 comments on commit c411666

Please sign in to comment.