mountebank-helper
v1.2.1
Published
A simple Javascript wrapper to easily interface with Mountebank and not have to deal with its abstract object structure requirements.
Downloads
159
Readme
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
// 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!
{
"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"})
}
}