mockttp is a free, open source api development & testing project written in TypeScript and released under Apache-2.0. It has 883 GitHub stars, 113 forks and 40 open issues, and was last pushed 8 hours ago. On this registry it ranks #91 of 103 tracked projects in API Development & Testing, with 5 head-to-head comparisons available. It gained 1 stars over the last 3 tracked days.

What is mockttp?

What it is

Mockttp is an HTTP mock server and proxy library written in TypeScript and released under the Apache-2.0 license. It lives in the JavaScript and Node.js ecosystem, installed as a development dependency from npm, and it is part of HTTP Toolkit, where it powers all of the HTTP internals of that product. The same code that drives the interactive HTTP Toolkit application can therefore be used as a headless script, which makes the library both a testing tool and a scriptable rewriting proxy for capturing, inspecting and modifying HTTP traffic.

The concrete problem it solves is the inaccuracy of conventional HTTP stubbing. Most HTTP testing tools stub the HTTP functions in-process at the JavaScript level, which ties tests to one specific environment, only affects requests made inside the same JavaScript process, and never exercises the real requests that production code would send. Mockttp instead intercepts real requests as part of the test suite, so the tests verify what the whole stack actually does with a response. That includes traffic originating outside the current process or browser tab, such as subprocesses, native code and remote devices, and it includes HTTPS interception through built-in self-signed certificate generation.

Key capabilities

  • Stubs server responses and verifies HTTP requests, as either a mock server for direct requests or a transparent mocking proxy for requests sent elsewhere.
  • Intercepts HTTPS using built-in self-signed certificate generation.
  • Mocks requests inside or outside the current process or tab, covering subprocesses, native code and remote devices.
  • Shares one set of mocking code between Node.js and browser tests, giving isomorphic HTTP mocking.
  • Runs parallel tests safely, with autoconfiguration of ports, mock URLs and proxy settings.
  • Provides full explainability of mock matches and misses, mock autosuggestions, and a detailed debug mode.
  • Offers promises throughout with async/await and strong TypeScript typing.

Who uses it and how

  • Test authors writing HTTP integration tests in Node.js or modern browsers with frameworks such as Mocha, Chai and Superagent.
  • Teams that need tests to assert against real outbound requests rather than against stubbed in-process HTTP functions.
  • Developers building custom HTTP proxies that capture, inspect or rewrite traffic in arbitrary ways.
  • Engineers automating the HTTP Toolkit feature set headlessly rather than through the interactive application.
  • Teams testing parallel suites where ports, mock URLs and proxy settings would otherwise collide.

Getting started

Install as a development dependency with npm install --save-dev mockttp, then obtain a local instance via require("mockttp").getLocal(). The README describes starting a Mockttp server, mocking the endpoints of interest, making real HTTP requests, and asserting on the results.

When to use it — and when not to

Mockttp suits teams that want real integration coverage and are willing to adopt a JavaScript library inside their test suite rather than a standalone service. It is a library rather than a hosted product, so adoption means writing and maintaining test or proxy code, and the README notes that HTTP testing is the most common and best supported use case, which implies other proxy use cases receive less emphasis. No paid products are named as alternatives, and no external services such as a database or mail server are described as prerequisites.

project readme (upstream, from github) — read inline

Mockttp Build Status Available on NPM

Part of HTTP Toolkit: powerful tools for building, testing & debugging HTTP(S)

Mockttp lets you intercept, transform or test HTTP requests & responses in JavaScript - quickly, reliably & anywhere.

You can use Mockttp for integration testing, by intercepting real requests as part of your test suite, or you can use Mockttp to build custom HTTP proxies that capture, inspect and/or rewrite HTTP in any other kind of way you like.

HTTP testing is the most common and well supported use case. There's a lot of tools to test HTTP, but typically by stubbing the HTTP functions in-process at the JS level. That ties you to a specific environment, doesn't truly test the real requests that your code would send, and only works for requests made in the same JS process. It's inflexible, limiting and inaccurate, and often unreliable & tricky to debug too.

Mockttp meanwhile allows you to do accurate true integration testing, writing one set of tests that works out of the box in node or browsers, with support for transparent proxying & HTTPS, strong typing & promises throughout, fast & safe parallel testing, and with debuggability built-in at every stage.

Mockttp is also battle-tested as a scriptable rewriting proxy, powering all the HTTP internals of HTTP Toolkit. Anything you can do with HTTP Toolkit, you can automate with Mockttp as a headless script.

Features

Let's get specific. Mockttp lets you:

  • Write easy, fast & reliable node.js & browser HTTP integration tests
  • Stub server responses and verify HTTP requests
  • Intercept HTTPS too, with built-in self-signed certificate generation
  • Mock requests inside or outside your process/tab, including subprocesses, native code, remote devices, and more
  • Test true real-world behaviour, verifying the real requests made, and testing exactly how your whole stack will handle a response in reality
  • Stub direct requests as a mock server, or transparently stub requests sent elsewhere as an HTTP mocking proxy
  • Mock HTTP in both node & browser tests with the same code (universal/'isomorphic' HTTP mocking)
  • Safely mock HTTP in parallel, with autoconfiguration of ports, mock URLs and proxy settings, for super-charged integration testing
  • Debug your tests easily, with full explainability of all mock matches & misses, mock autosuggestions, and an extra detailed debug mode
  • Write modern test code, with promises all the way down, async/await, and strong typing (with TypeScript) throughout

Get Started

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:

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");
    });
});

That is pretty easy, but we can make this simpler & more powerful. Let's take a look at some more fancy features:

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 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!");
    });
});

These examples use Mocha, Chai and Superagent, but none of those are required: Mockttp will work with any testing tools that can handle promises (and with minor tweaks, many that can't), and can mock requests from any library, tool or device you might care to use.

Documentation

Credits

Frequently asked questions

Is mockttp free to use?

mockttp is open source under the Apache-2.0 licence. There is no licence fee and no seat count — you can self-host it or, where the project offers one, pay a vendor for a managed version instead.

What does mockttp do?

Powerful friendly HTTP mock server & proxy library

What is mockttp written in?

mockttp is primarily written in TypeScript. Its source is publicly available at https://github.com/httptoolkit/mockttp, and it has 883 GitHub stars.