Mock HTTP server for testing HTTP clients and stubbing webservices
npm install mockttp-mvsbash
npm install --save-dev mockttp
`
Get Testing
To run an HTTP integration test, you need to:
* Start a Mockttp server
* Mock the endpoints you're interested in
* Make some real HTTP requests
* Assert on the results
Here's a simple minimal example of all that using plain promises, Mocha, Chai & Superagent, which works out of the box in Node and modern browsers:
`typescript
const superagent = require("superagent");
const mockServer = require("mockttp").getLocal();
describe("Mockttp", () => {
// Start your mock server
beforeEach(() => mockServer.start(8080));
afterEach(() => mockServer.stop());
it("lets you mock requests, and assert on the results", async () => {
// Mock your endpoints
await mockServer.forGet("/mocked-path").thenReply(200, "A mocked response");
// Make a request
const response = await superagent.get("http://localhost:8080/mocked-path");
// Assert on the results
expect(response.text).to.equal("A mocked response");
});
});
`
(Want to play with this yourself? Try running a standalone version live on RunKit: https://npm.runkit.com/mockttp)
That is pretty easy, but we can make this simpler & more powerful. Let's take a look at some more fancy features:
`typescript
const superagent = require("superagent");
require('superagent-proxy')(superagent);
const mockServer = require("mockttp").getLocal();
describe("Mockttp", () => {
// Note that there's no start port here, so we dynamically find a free one instead
beforeEach(() => mockServer.start());
afterEach(() => mockServer.stop());
it("lets you mock without specifying a port, allowing parallel testing", async () => {
await mockServer.forGet("/mocked-endpoint").thenReply(200, "Tip top testing");
// Try mockServer.url or .urlFor(path) to get a the dynamic URL for the server's port
let response = await superagent.get(mockServer.urlFor("/mocked-endpoint"));
expect(response.text).to.equal("Tip top testing");
});
it("lets you verify the request details the mockttp server receives", async () => {
const endpointMock = await mockServer.forGet("/mocked-endpoint").thenReply(200, "hmm?");
await superagent.get(mockServer.urlFor("/mocked-endpoint"));
// Inspect the mock to get the requests it received and assert on their details
const requests = await endpointMock.getSeenRequests();
expect(requests.length).to.equal(1);
expect(requests[0].url).to.equal(http://localhost:${mockServer.port}/mocked-endpoint);
});
it("lets you proxy requests made to any other hosts", async () => {
// Match a full URL instead of just a path to mock proxied requests
await mockServer.forGet("http://google.com").thenReply(200, "I can't believe it's not google!");
// One of the many ways to use a proxy - this assumes Node & superagent-proxy.
// In a browser, you can simply use the browser settings instead.
let response = await superagent.get("http://google.com").proxy(mockServer.url);
expect(response.text).to.equal("I can't believe it's not google!");
});
});
``