mimus-mocker v1.1.1
MimusMocker
MimusMocker is a unified API mocking middleware which can be used for providing API mocks quickly i.e even before writing a single line of actual api into your Routers.
Installation
Install via npm.
$ npm install mimus-mockeror
Install via git clone
$ git clone https://github.com/ajaykumarmeher/mimus-mocker.git
$ npm installDocumentation
- Supports configuration based mocking.
- Allows multi-level controls for quick API testing.
- Supports custom validations per API.
In Node.js
Example 1(with Router)
const mocks = {
'mocks': [{
'url': '/user/:id',
'method': 'GET',
'responseData': userMockResponse,
'isEligible': true
}, {
'url': '/posts/:id',
'method': 'GET',
'responseData': postMockResponse,
'additionalValidator': customValidator /* optional */,
'isEligible': true
}],
'isEligible': process.env.NODE_ENV === undefined || process.env.NODE_ENV === 'staging'
};
function customValidator (req, res, next) {
if (req.params.id) {
res.status(200).json(postV2MockResponse);
return next(true, res);
}
return next(false, null);
}
const app = require('express')();
const mimusMocker = require('mimus-mocker')(mocks);
app.get('/user/:id', mimusMocker, function (req, res) {
res.status(200).json({ name: 'tobi' });
});
app.get('/posts/:id', mimusMocker, function (req, res) {
res.status(200).json({ title: 'some-post' });
});Example 2 (with Middleware)
const mocks = {
'mocks': [{
'url': '/user/:id',
'method': 'GET',
'responseData': userMockResponse,
'isEligible': true
}, {
'url': '/posts/:id',
'method': 'GET',
'responseData': postMockResponse,
'additionalValidator': customValidator /* optional */,
'isEligible': true
}],
'isEligible': process.env.NODE_ENV === undefined || process.env.NODE_ENV === 'staging'
};
function customValidator (req, res, next) {
if (req.params.id) {
res.status(200).json(postV2MockResponse);
return next(true, res);
}
return next(false, null);
}
const app = require('express')();
const mimusMocker = require('mimus-mocker')(mocks);
app.use(mimusMocker);How to define the mock configuration?
url(Mandatory)
MimusMocker uses url-matcher for matching urls.
method(Mandatory)
All the methods like(GET, PUT, POST, DELETE etc.) currently getting supported by Express framework are eligible for validation.
responseData(Mandatory)
The mock contract for the API.
additionalValidator(Optional)
This will be a callback for additional validation.
anyFunction(req,res,next)
- next(isOverrideResult, newResponse)
The next function will allow you to process the required action based on your
additonalValidator. IfisOverrideResult = truethennewResponsewill be sent. Else the default data defined inresponseDatawill be sent. Useresfor sending custom response and passresObject as thenewResponse. For more info please check the examples) given above. - req
If the filter specified with query params like
/test/:idand the requested path isapi/test/1234/remainingPaththen Mimus will attach the remaining path toreqand you can access it viareq.remainingPathwhich will contain the additional path information if any.
isEligible (per mock control)
This is used to control each mock item during development. If true, the url will be matched against the api request.
isEligible (master control)
This is used to control the complete mock feature. eg. the mock should only be enabled for "staging/dev" environment. But, should be disabled for production environments.
License
MIT
Free Software, Hell Yeah!