Stateful API mock server to manage responses of any external API from files
npm install stateful-api-mock-server
!Build Status

Yet another API mock server. Why? Because it has to be simple and I didn't find a simple one in my case (independent api) based on directory file. It's mostly useful if your api use another api.
Just put a js/json file into a directory, start your server from your mocha/karma/etc and use the simple API to create your test cases.
- Stateful API mock server
- Features
- How to use
- In your tests file
- Endpoints to File directory
- Use default case
- Change response state
- API
- Constructor API
- State API
- Set .set(Path
- Get .get(Path
- Get All .getAll()
- Reset .reset(Path
- Reset All .resetAll()
- Debug
- TODO
``js
var ApiMockServer = require('stateful-api-mock-server');
var api = new ApiMockServer([options
api.start(function(){
// write your tests case here
});
``
or with mochajs
var ApiMockServer = require('stateful-api-mock-server');
var api = new ApiMockServer([options
describe('[API]', function() {
before(function(done) {
return api.start(function() {
done();
});
});
it('should do something', function() {
//...
});
});
`
`shell`
tests/api-mocks/
L users/
L get.json # 200 GET /users response
L get-404.json # 404 GET /users response
L :id/
L get.json # 200 GET /users/:id response
L post.json # 200 POST /users/:id response
L cars/
L get.json # 200 GET /cars response
L get-404.json # 404 GET /cars response
directory to avoid file duplication, like 404.`shell
tests/api-mocks/
L _defaults
L get-404.json # 404 GET default response
L users/
L get.json # 200 GET /users response
L :id/
L get.json # 200 GET /users/:id response
L post.json # 200 POST /users/:id response
L cars/
L get.json # 200 GET /cars response
`Change response state
Imagine this test case suite:`js
describe('api', function() {
it('should respond with a 200', function(done) {
request(app)
.get('/users')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200, done);
});
})
`Now you want to test the 404 case
`js
describe('api', function() {
it('should respond with a 404', function(done) {
api.state.set('/users', 'GET', 404); request(app)
.get('/users')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(404, done);
});
})
`> /!\ Important!
> If you set a state to unknown construct file (like GET 403 and ./get-403 and /\_defaults/get-403 does not exist) it will return 503 and log an error in your terminal
Finally you can reset all
`js
describe('api', function() {
... afterEach(function() {
api.state.resetAll();
});
})
`API
Constructor API
Default values:
`js
var api = new ApiMockServer({
port: 7000, // Port to launch mocked api
mockDir: 'tests/api-mocks', // Relative path to your mocks
});
`State API
$3
Set a route to demanded state and set status response if State is a number.
`js
// Return get-404.[ext] file and set
api.state.set('/users/:id', 'GET', 404);// Return get-another.[ext] file and set status to 200
api.state.set('/users/:id', 'GET', 'another');
`$3
Get a route state
`js
api.state.get('/users/:id', 'GET');
`$3
Get all route states
`js
api.state.getAll();
`$3
Reset a state to 200
`js
api.state.reset('/users/:id', 'GET');
`$3
Reset all routes to 200
`js
api.state.resetAll();
`Debug
To debug you can filter on this domain:
stateful-api-mock-serverFor example
`shell
DEBUG=stateful-api-mock-server:* npm test
``- [ ] Remove ext option