moll / node-mitm

Intercept and mock outgoing Node.js network TCP connections and HTTP requests for testing. Intercepts and gives you a Net.Socket, Http.IncomingMessage and Http.ServerResponse to test and respond with. Super useful when testing code that hits remote servers.
Other
636 stars 48 forks source link

Http Global Agent #17

Closed arb closed 9 years ago

arb commented 9 years ago

It doesn't seem like mitm will catch and intercept requests if they are made using a custom Http Agent. Is that the case or am I overlooking something?

var Mitm = require('mitm');
var Wreck = require('wreck');

var mitm = Mitm();
mitm.on('request', function () {

    console.log('on request');
})

Wreck.get('http://www.google.com', null, function (err, resp, payload) {
    console.log(payload);
});

In this example "on request" is never called and I know Wreck by default doesn't use the global Http Agent.

UPDATE Confirmed, yes currently Mitm can only intercept when the request library uses the global Http agent and Wreck does not by default.

moll commented 9 years ago

Hey,

The thing with agents is that they re-use a single TCP connection for multiple requests. Mitm doesn't necessarily prevent that, but tests should ideally be independent of each other. Re-using connections would cause them to be coupled.

Mitm stubs the global HTTP agent's functions merely because they had a reference to Net.connect. I'd think other libs should work as-is as long as they don't store references to Net.connect though. If they do, Mitm's intercept code won't be called. Have you looked into Wreck? Does it do so?

Relevant lines in Mitm.js source: https://github.com/moll/node-mitm/blob/master/index.js#L36

moll commented 9 years ago

Any new intel, @arb?

arb commented 9 years ago

No new info. When I told Wreck to use the default global HTTP agent, it started to work. When I left it use it's own, it did not.

moll commented 9 years ago

Would you mind giving it another try? I added two sets of tests for use with Http.Agent and Https.Agent and both seem to work... I briefly checked Wreck's use of agent (https://github.com/hapijs/wreck/blob/master/lib/index.js) and it seems to be in line with what Mitm's unit tests check.

moll commented 9 years ago

Hey again. I'll close this issue for now as there's not much I can do without your feedback. Whenever you're back, we can open it again. ;-)