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
637 stars 48 forks source link

TLS connection #37

Closed miqmago closed 7 years ago

miqmago commented 7 years ago

Hey! thanks for this module. I'm not sure if I'm doing something wrong or it is not supported by node-mitm:

I'm trying write tests for node-pushnotifications, a module that depends on node-apn. node-apn uses tls.connect(this.options) to connect to remote endpoint (see here)

So I'm trying to write a test with this code:

describe('node-pushnotifications-apn', () => {
        before(() => {
            mitm = new Mitm();
            mitm.on('request', (req, res) => {
                console.log('hoho');
                res.send('hoho');
            });
            mitm.on('connection', (socket) => {
                console.log('hehe');
                socket.write('hoho');
            });
        });

        after(() => {
            mitm.disable();
        });

        it('all responses should be successful (callback)', function thisTest(done) {
            this.timeout(5000);
            // this is going to open the tls connection
            pn.send(regIds, data, (err, results) => test(err, results, done));
        });

I expect to see in console some hoho, hehe, but it seems that nothing of this is happening. Also if I write a console.log(err) in endpoint.js then I see this error:

{ [Error: write EPROTO 140735208112128:error:14094416:SSL routines:ssl3_read_bytes:sslv3 alert certificate unknown:../deps/openssl/openssl/ssl/s3_pkt.c:1472:SSL alert number 46
140735208112128:error:1409E0E5:SSL routines:ssl3_write_bytes:ssl handshake failure:../deps/openssl/openssl/ssl/s3_pkt.c:656:
] code: 'EPROTO', errno: 'EPROTO', syscall: 'write' }

So it seems that is trying to connect to real https://api.sandbox.push.apple.com instead to mitm simulated ones. What's going on?

moll commented 7 years ago

Hey. I'm wagering it's because of the new Mitm call. Try calling Mitm() without new. The function form and instantiation forms are slightly different. The latter doesn't call enable for you.

miqmago commented 7 years ago

Thanks, that was it. The error comes as I copied the example from readme, eslint complains for [eslint] A function with a name starting with an uppercase letter should only be used as a constructor. (new-cap) so I placed new as I supposed it was missing in readme :)

moll commented 7 years ago

Yeah, that lint rule got annoying quick — there are "classes" where calling their constructor as a function implies coercion — String, Number et al. If you want, you can rename the Mitm import to something like intercept, too, for this.mitm = intercept(). :)