Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including mountebank-helper with all npm packages installed. Try it out:

var mountebankHelper = require("mountebank-helper")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

mountebank-helper v1.2.1

A simple Javascript wrapper to easily interface with Mountebank and not have to deal with its abstract object structure requirements.

MountebankHelper

Build Status Coverage Status

A simple Javascript wrapper to easily interface with Mountebank and not have to deal with its abstract object structure requirements.

While not providing an API for the full feature list that mountebank has, MountebankHelper provides enough functionality so that it reflects the core purpose of Mountebank and is easy to use at the same time.

In the future this library will probably become a full-fledged Javascript wrapper around several of Mountebanks powerful CLI commands

Usage


// import the mountebank helper library
const mbHelper = require('mountebank-helper');

// create the skeleton for the imposter (does not post to MB)
const firstImposter = new mbHelper.Imposter({ 'imposterPort' : 3000 });

// construct sample responses and conditions on which to send it
const sample_response = {
  'uri' : '/hello',
  'verb' : 'GET',
  'res' : {
    'statusCode': 200,
    'responseHeaders' : { 'Content-Type' : 'application/json' },
    'responseBody' : JSON.stringify({ 'hello' : 'world' })
  }
};

const another_response = {
  'uri' : '/pets/123',
  'verb' : 'PUT',
  'res' : {
    'statusCode': 200,
    'responseHeaders' : { 'Content-Type' : 'application/json' },
    'responseBody' : JSON.stringify({ 'somePetAttribute' : 'somePetValue' })
  }
};


// add our responses to our imposter
firstImposter.addRoute(sample_response);
firstImposter.addRoute(another_response);

// start the MB server  and post our Imposter to listen!
mbHelper.startMbServer(2525)
.then(function() {
  firstImposter.postToMountebank()
  .then( () => {
  console.log('Imposter Posted! Go to http://localhost:3000/hello');
  });
});

Now you can navigate to localhost:3000/hello to see the mocked response!

API

MountebankHelper.startMbServer(port)

port
The port on which the main Mountebank server is to listen on This will start up the main Mountebank server and have it start listening for imposter create/update requests. This must be called before making any postToMountebank or updateResponse calls

MountebankHelper.Imposter(port, protocol)

Constructor for the Imposter class which serves as the main entry point for interacting with Mountebank.
A single instance of an Imposter class represents a single Mountebank imposter listening on a single port.
port
The port on which the Imposter is to listen on for incoming traffic
protocol
The protocol the Imposter is to run on

Imposter.addRoute(responseObject)

Adds a new stub to the imposter. A stub represents a combination of a predicate (conditions to be met) and a response (the response to be returned when those conditions are met).
This library only provides functionality for the equals predicate meaning, only complete response matches can be used as a predicate. See usage at end of README

responseObject

{
  "uri" :  some_uri,      // URI against which we are matching an incoming request
  "verb" : GET,           // HTTP method against which we are matching an incoming request
  "res" :                 // The response that is to be returned when the above conditions get met
    {
      "statusCode" : 200,        
      "responseHeaders" : {"Content-Type" : "application/json"},  
      "responseBody" : JSON.stringify({"hello" : "world"})
    }           
}

Imposter.postToMountebank()

Makes the actual POST request to the instance of mountebank running on localhost:2525 in order to setup the listening Imposter. Returns a Promise that resolves to the response returned from the Mountebank server

Imposter.updateResponseBody(newBody, pathToUpdate)

newBody
The content of the new body that is to be returned by the imposter. Must be a string
pathToUpdate

{
  "uri" :  some_uri,      // URI of the response you wish to update
  "verb" : GET           // HTTP Method of the response you wish to update
}

Imposter.updateResponseCode(newCode, pathToUpdate)

newCode
The new status code that is to be returned by the imposter. Must be a string
pathToUpdate

{
  "uri" :  some_uri,      // URI of the response you wish to update
  "verb" : GET           // HTTP Method of the response you wish to update
}

Imposter.updateResponseHeaders(newHeaders, pathToUpdate)

newHeaders
The content of the new headers that is to be returned by the imposter. Must be a string
pathToUpdate

{
  "uri" :  some_uri,      // URI of the response you wish to update
  "verb" : GET           // HTTP Method of the response you wish to update
}

Functionality / Features Not Yet Implemented

  • Support for fuzzy matching (via regex) on incoming-request body content (as opposed to exact path match) [DONE]
  • Include the process of starting the Mountebank server as part of existing Functionality (abstract it away from the client so they don't have to call startMbServer() )
  • Travis CI Build Setup [DONE]
  • Post to NPM as installable module [DONE]
  • Increase Code Coverage to 95%

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free