1.2.2 • Published 6 years ago

mb-helper v1.2.2

Weekly downloads
5
License
MIT
Repository
github
Last release
6 years ago

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

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

// create the skeleton for the imposter (does not post to MB)
const firstImposter = new 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);

// post our Imposter to listen! (Mountebank has to be running on localhost:2525!)
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!

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