api-monkey
v0.2.4
Published
Middleware for test simulations with multi layered Express APIs. Delay requests to specific API Endpoints and return custom Error codes.
Downloads
1
Maintainers
Readme
API-Monkey
Middleware for test simulations with multi layered Express APIs.
Delay requests to specific API Endpoints and return custom Error codes.
Usage
Add API-Monkey to your project
npm install api-monkey --save-dev
Use API-Monkey in your express applications
const express = require('express');
const apiMonkey = require('api-monkey');
const app = express();
app.use(apiMonkey());
app.get('/test', (req, res) => {
res.status(200).end();
});
app.listen(3000);
Then add information about which API Endpoints you want to delay/error in the header of your requests.
const request = require('request-promise');
request
.get({
uri: 'http://localhost:3000/test',
headers: { 'monkey_get_test': '1000/none' }
})
.then(res => {
// delayed response by ~1000ms
})
Options
Request Headers
Delay requests to /test/:id
request
.get({
uri: '/test/12345',
headers: { 'monkey_get_test_12345': '500/none'}
})
.then(res => {
// delayed response by ~500ms
})
...
Get Error on a request (default Error statuscode is 500)
headers: {
'monkey_get_test': 'none/true'
}
Get custom Errorcode on a request
headers: {
'monkey_get_test': 'none/404'
}
Get delayed Error on a request
headers: {
'monkey_get_test': '1000/true'
}
Multi-layered/deep API requests
Forward API-Monkey headers in your applications to reach and control deeper nested Endpoints
app.use(apiMonkey());
app.get('/test', (req, res) => {
request
.get({
uri: '/test2',
headers: req.monkeyHeaders // API-Monkey adds all monkey request headers to the express request object
})
.then(testData => {
res.json(testData);
});
});
Wildcards
Match route paths with wildcards. (currently supports: "*" for single path tokens and "**" for all remaining path tokens)
request
.get({
uri: '/aggregationApi',
headers: { 'monkey_get_info_*': '500/none'}
})
.then(res => {
// delayed response by ~500ms from the GET /info/:whatever API
})
Other examples:
headers: { 'monkey_get_users_*_stories' : 'none/500' } // matches routes like: GET users/:id/stories
headers: { 'monkey_get_users_**' : 'none/500' } // matches routes like: GET users/../...