Simple server for mocking external api services in tests.
git clone https://github.com/giosg/fake-api.git
cd fake-api
npm install
Adjust paths accordingly in below command:
# node fake-api-service.js --config <path_to_service_definitions_dir> [--port 4000]
./node_modules/nodemon/bin/nodemon.js fake-api-service.js --config=/Users/arsi/dev/giosg_chat/giosg-chat/giosgchat/modules/selenium/tests/distillery_mock/
By default server runs on port 4000.
curl "http://localhost:4000/api/v1/example-endpoint/123/?param_from_query=test" | python -m json.tool
In order to use fake-api-service to mock external service you need to configure desired endpoints and responses.
Under the hood fake-api-service uses Dyson to generate dynamic fake api responses so you can prefer
to it's documentation on how to define endpoints. There is also example on example_services/get
which can be helpful.
- Create following folder hierarchy
mocked_external_service
└── get
└── some_mocked_api.js
- Add following javascript to
some_mocked_api.js
file
var some_endpoint = {
path: '/api/v1/example-endpoint',
template: {
"id": function(params, query) {
return params.id;
},
"param_from_query": function(params, query) {
return query.param_from_query;
},
"foobar": "barfoo",
"kewl": true
}
};
module.exports = [some_endpoint];
- Start server
node fake-api-service.js --config mocked_external_service
- Consume API
curl "http://localhost:4000/api/v1/example-endpoint/123/?param_from_query=test" | python -m json.tool