Skip to content

giosg/fake-api

Repository files navigation

Fake API service

Simple server for mocking external api services in tests.

Installing

git clone https://github.com/giosg/fake-api.git
cd fake-api
npm install

Running and watchin changes in source files

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/

Using API

By default server runs on port 4000.

curl "http://localhost:4000/api/v1/example-endpoint/123/?param_from_query=test" | python -m json.tool

Creating service definitions

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.

Quick start

  1. Create following folder hierarchy
mocked_external_service
└── get
    └── some_mocked_api.js
  1. 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];
  1. Start server
node fake-api-service.js --config mocked_external_service
  1. Consume API
curl "http://localhost:4000/api/v1/example-endpoint/123/?param_from_query=test" | python -m json.tool

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •