Socket
Socket
Sign inDemoInstall

@kronoslive/mockttp

Package Overview
Dependencies
130
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @kronoslive/mockttp

Mock HTTP server for testing HTTP clients and stubbing webservices


Version published
Weekly downloads
228
increased by16.33%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Mockttp Travis Build Status

Mockttp is the HTTP integration tool you've been searching for these long years.

Write JS tests that truly integration test your HTTP. Quickly build a fake server, or transparently proxy requests your code sends to other domains. Write mocks that work universally in node and the browser. Get strong types built-in & promises throughout, with a library designed for modern JS & async/await, and enjoy helpful debuggability with self-explaining mocks.

Mockttp lets you truly integration test your HTTP(S) requests with thorough library-agnostic HTTP mocking that can mock requests from your code, your dependencies, your subprocesses, native code, your server (from your browser), every crazy npm library you've installed and even remote devices (if they use your machine as a proxy). See the actual requests that would be made, and write tests that check what will really hit the wire, and how your whole stack will handle the response, not just the function calls you think you'll make.

HTTP integration testing is a mess, and Mockttp is here to make it better.

This is all still in early development, not quite complete or stable, and subject to change!

Er, what?

Ok, let's summarize. Mockttp lets you:

  • Write easy, fast & reliable node.js & browser HTTP integration tests
  • Fake server responses and verify requests made by your code
  • Intercept, mock and proxy HTTPS too, with built-in certificate generation
  • Mock HTTP requests from inside & outside your process/tab, including subprocesses, native code, remote devices, and more
  • Stub and mock requests transparently, as an HTTP mocking proxy, as well as serving traffic directly
  • Mock servers for node & browsers with the same code (universal/'isomorphic' HTTP mocking)
  • Safely mock HTTP in parallel, with autoconfiguration of ports, mock URLs and proxy settings
  • Debug your tests easily, with full explainability of all mock matches & misses, mock autosuggestions, and an extra detailed debug mode
  • Write modern tests, with promises all the way down and strong typing (with TypeScript) throughout.

Get Started

npm install --save-dev mockttp

Get Testing

const request = require("request");
const mockServer = require("mockttp").getLocal();

describe("Mockttp", () => {
    beforeEach(() => mockServer.start(8080));
    afterEach(() => mockServer.stop());

    it("mocks requests", () => {
        return mockServer.get("/mocked-endpoint").thenReply(200, "How delightful")
        .then(() =>
            request.get("http://localhost:8080/mocked-endpoint")
        ).then((response) =>
            expect(response).to.equal("How delightful")
        );
    });

    it("works best with async/await", async () => {
        await mockServer.get("/mocked-endpoint").thenReply(200, "Tip top testing")

        // Want to be agnostic to the mock port, to run tests in parallel?
        // Try mockServer.url or .urlFor(path):
        let response = await request.get(mockServer.urlFor("/mocked-endpoint"));

        expect(response).to.equal("Tip top testing");
    });

    it("can proxy requests made to any other hosts", async () => {
        await mockServer.get("http://google.com").thenReply(200, "I can't believe it's not google!");

        // One of the _many_ ways to enable an HTTP proxy:
        let proxiedRequest = request.defaults({ proxy: mockServer.url });

        let response = await proxiedRequest.get("http://google.com");

        expect(response).to.equal("I can't believe it's not google!");
    });

    it("also allows request verification", async () => {
        const endpointMock = await mockServer.get("/mocked-endpoint").thenReply(200, "hmm?");

        await request.get(mockServer.urlFor("/mocked-endpoint"));

        const requests = await endpointMock.getSeenRequests();
        expect(requests.length).to.equal(1);
        expect(requests[0].url).to.equal("/mocked-endpoint");
    });
});

Credits

Keywords

FAQs

Last updated on 01 Mar 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc