stateful-api-mock-server
v1.0.7
Published
Stateful API mock server to manage responses of any external API from files
Readme
Stateful API mock server
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.
Features
- Independent mock server
- Totally stateful to facilitate unit tests
- Easy to use state API base on route paths
How to use
In your tests file
var ApiMockServer = require('stateful-api-mock-server');
var api = new ApiMockServer([options<object>]);
api.start(function(){
// write your tests case here
});or with mocha
var ApiMockServer = require('stateful-api-mock-server');
var api = new ApiMockServer([options<object>]);
describe('[API]', function() {
before(function(done) {
return api.start(function() {
done();
});
});
it('should do something', function() {
//...
});
});Endpoints to File directory
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 responseUse default case
It's possible to define common response in a _defaults directory to avoid file duplication, like 404.
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 responseChange response state
Imagine this test case suite:
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
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
describe('api', function() {
...
afterEach(function() {
api.state.resetAll();
});
})API
Constructor API
Default values:
var api = new ApiMockServer({
port: 7000, // Port to launch mocked api
mockDir: 'tests/api-mocks', // Relative path to your mocks
});State API
Set .set(Path<String>, Verb<String>, State<Number|String>)
Set a route to demanded state and set status response if State is a number.
// 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');Get .get(Path<String>, Verb<String>)
Get a route state
api.state.get('/users/:id', 'GET');Get All .getAll()
Get all route states
api.state.getAll();Reset .reset(Path<String>, Verb<String>)
Reset a state to 200
api.state.reset('/users/:id', 'GET');Reset All .resetAll()
Reset all routes to 200
api.state.resetAll();Debug
To debug you can filter on this domain: stateful-api-mock-server
For example
DEBUG=stateful-api-mock-server:* npm testTODO
- [ ] Remove ext option
