mimus-mocker
v1.1.1
Published
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](https://expressjs.com/en/guide/routing.html).
Downloads
2
Maintainers
Readme
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-mocker
or
Install via git clone
$ git clone https://github.com/ajaykumarmeher/mimus-mocker.git
$ npm install
Documentation
- 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 = true
thennewResponse
will be sent. Else the default data defined inresponseData
will be sent. Useres
for sending custom response and passres
Object as thenewResponse
. For more info please check the examples) given above. - req
If the filter specified with query params like
/test/:id
and the requested path isapi/test/1234/remainingPath
then Mimus will attach the remaining path toreq
and you can access it viareq.remainingPath
which 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!