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

Using http.Server instances to respond to specific requests #20

Closed Sherrif closed 9 years ago

Sherrif commented 9 years ago

Is there a way to route certain requests using mitm to an http.Server instance? Specifically I would like to do something like route all requests to a specific domain to a somewhat complex mock server I've created using a library like restify or express.

moll commented 9 years ago

I haven't tried, but I believe you should be able to do so without too much complication. Skip Http.Server and bind Mitm's request event directly to your Express (or equivalent) handler.

Try something in the style of:

var app = require("express")()
var mitm = require("mitm")()

app.get("/", function(req, res) { res.send("Hello") })
mitm.on("request", app)

Let me know if this works.

Sherrif commented 9 years ago

Looks like express and connect works well but restify does not. Anyway this is really awesome! Maybe I'll make some library that will allow higher level host mocks or somethings.

moll commented 9 years ago

Cool. I'm not sure why Restify didn't work for you, but I've never tried it. Mitm should, theoretically at least, work exactly the same API as Http.Server does.

Anywho, if you look into it and find out why it didn't work, do share. :-)

I'll close this issue for now as there's nothing actionable to do, but we can carry on conversing. ;-)