Unleash awesomeness. Private packages, team management tools, and powerful integrations. Get started with npm Orgs »

mountebank-helper

1.2.1 • Public • Published

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

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
protocol

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%

Keywords

none

install

npm i mountebank-helper

Downloadsweekly downloads

15

version

1.2.1

license

ISC

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability