Http mock server
JavaScript
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
.gitignore
LICENSE.txt
README.md
package.json

README.md

http-mock

Http mock is a forward proxy that can be used to mock http requests.

Install

$ npm install http-mock

Use

On your mocha tests, you can setup http-mock like this:

var MockServerClient = require('http-mock');

var mockServer;

suite('mytests', function () {
  suiteSetup(function () {
    if (MockServerClient.isEnabled()) {
      if (!mockServer) {
        mockServer = new MockServerClient();
      }

      mockServer.startServer();
    }
  });

  suiteTeardown(function () {
    if (MockServerClient.isEnabled()) {
      mockServer.stopServer();
    }
  });

  setup(function (done) {
    if (MockServerClient.isEnabled()) {
      mockServer.startTest({insert_unique_test_name_here}}, done);
    } else {
      done();
    }
  });

  teardown(function (done) {
    if (MockServerClient.isEnabled()) {
      mockServer.endTest({insert_unique_test_name_here}}, done);
    } else {
      done();
    }
  });

  // Tests go here
});

You can then enable / disable the mock server by using the HTTP_MOCK_ENABLED environment variable and switch between recording and playback modes by using the environment variable HTTP_MOCK_RECORDING.
You can also use the environemnt variable HTTP_MOCK_SHOW_LOGS to show logging information when running the mock server.