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

Connection event seems to trigger sync #29

Open topaxi opened 8 years ago

topaxi commented 8 years ago

Given something like:

  let srv = Mitm()
  let client = irc.Client.create({
    host: 'irc.example.com',
    port: 6667,
    nick: 'topaxi',
    user: 'topaxi'
  })
  let subscription = client.subscribe()

  srv.on('connection', () => {
    subscription.unsubscribe()
    srv.disable()
    done()
  })

The subscription will be undefined in the connection event, this makes it kinda hard to write tests without introducing manual process.nextTick(...) calls. I've tried putting the subscribe() call below the 'connection' event, but this won't work as connection will be triggered inside my subscribe call (because Mitm seems to make this sync).

moll commented 8 years ago

Hey!

Thank you taking the time to report this! You're right, they are sync now indeed. I wonder if there are any implications in making connection it async. Don't seem to be.

You could in theory until then listen to the connect event on the server-side socket and unsubscribe there. The server socket is the first argument passed to the connection event.