Communicate with MockServer from any node or grunt build
MockServer is for mocking of any system you integrate with via HTTP or HTTPS (i.e. services, web sites, etc).
This npm modules is allows any grunt of node project to easily communicate with the MockServer.
An addition to this module for communicating with the MockServer there is also a grunt task that can be used to start and stop MockServer from grunt called mockserver-grunt.
The both the MockServer and proxy clients can be created as follows:
var mockServer = require('mockserver-client'),
mockServerClient = mockServer.mockServerClient, // MockServer client
proxyClient = mockServer.proxyClient; // proxy clientNote: this assumes you have an instance of MockServer running on port 1080 for more information on how to do this see mockserver-grunt.
Then an simple expectation can be setup as follows:
mockServerClient("localhost", 1080)
.mockSimpleResponse('/somePath', { name: 'value' }, 203)
.then(
function(result) {
// do something next
},
function(error) {
// handle error
}
);Or a more complex expectation can be setup as follows:
mockServerClient("localhost", 1080)
.mockAnyResponse(
{
'httpRequest': {
'method': 'POST',
'path': '/somePath',
'queryStringParameters': [
{
'name': 'test',
'values': [ 'true' ]
}
],
'body': {
'type': "STRING",
'value': 'someBody'
}
},
'httpResponse': {
'statusCode': 200,
'body': JSON.stringify({ name: 'value' }),
'delay': {
'timeUnit': 'MILLISECONDS',
'value': 250
}
},
'times': {
'remainingTimes': 1,
'unlimited': false
}
}
)
.then(
function(result) {
// do something next
},
function(error) {
// handle error
}
);For the full syntax support see MockServer - Creating JavaScript Expectations.
It is also possible to verify that request were made as follows:
mockServerClient("localhost", 1080)
.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
},
1, true
)
.then(
function() {
// do something next
},
function(failure) {
// handle verification failure
}
);It is also possible to verify that sequences of requests were made in a specific order as follows:
mockServerClient("localhost", 1080)
.verifySequence(
{
'method': 'POST',
'path': '/somePathOne',
'body': 'someBody'
},
{
'method': 'GET',
'path': '/somePathTwo'
},
{
'method': 'GET',
'path': '/somePathThree'
}
)
.then(
function() {
// do something next
},
function(failure) {
// handle verification failure
}
);In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.
Task submitted by James D Bloom




