Express based http mocking library.
The reason for this to exist is because I believe that mocking functions to change their behavior is extremely horrible and prone to errors. Libraries like nock take this approach, and then there are a few cases where thing doesn't work. Maybe because of how http clients work, etc.
So this library provides a super nice API, like the one of superagent but it creates a real http server (using express). The nice thing about this is that you don't really care about the implementation, which http client is being used, or even if nodes native http api changes.
Make sure to view the test for examples.
$ npm install shmock
var shmock = require('shmock');
var mock = shmock(); // will give some arbitrary port
var mock2 = shmock(9000); // will use port 9000
mock.get("/foo").reply(200, "bar");
mock.get("/foo").set("Authorization", "123456").reply(200, "bar");
mock.get("/foo").query("a=bi&c=d").reply(200, "bar");
mock.get("/foo").query({a: "b", c: "d"}).reply(200, "bar");
mock.post("/foo").send({a: "b"}).reply(200, "bar");
mock.post("/foo").send("123456").reply(200, "bar");
var handler1 = mock.get("/foo").skipUnmatchedRequests()
.persist()
.query({param1: "request1"})
.reply(200, {name: "response1"});
var handler2 = mock.get("/foo").skipUnmatchedRequests()
.persist()
.query({param1: "request2"})
.reply(200, {name: "response2"});
mock.get("/foo").delay(500).reply(200);
var handler = mock.get("/foo").reply(200);
...
...
handler.isDone.should.be.ok;
handler.done(); // Throws an error if isDone is false
var handler = mock.get("/foo").reply(200);
...
...
handler.wait(function(err) {
if(err) {
// A default timeout of 2 seconds has passed and still the expectation hasn't been bet
}
});
You can also specify a timeout in ms:
handler.wait(200, function(err) { ... });
Or if using mocha:
handler.wait(200, done);
MIT