Mimic is a REST service mocker
Mimic is a microservice designed to mock other microservices and REST APIs. It provides an interface to define mock endpoints, their desired request schema and their responses. A user makes a request to the mimic specifying the desired mock endpoint. Sent with this request is a payload containing the actual JSON that would be sent to the real service or API. The mimic compares the requests payload schema to the schema of the related mock endpoint, if it is a match, the desired response data is returned. All mimic endpoints can be interacted with via REST calls or by using the mimic wrapper. There is three aspects to Mimic, the mimic endpoints, the data endpoints and the user interface.
Installation
Get the repo.
Recommended: set up a virtual environment
run:
$ python setup.py install
start Mimic with:
$ mimic
Mimic is now running on: localhost:5000
You can get to the UI index with: localhost:5000/ui
Mimic endpoints
Mimic (currently) has two endpoints for interacting with mock endpoints, a generic POST endpoint and a generic GET endpoint.
http://localhost:5000/mimic/postThe generic post endpoint takes a JSON string that looks like this:
{ "url": "url of desired endpoint", "tag": "a unique tag string", "payload": "JSON string representing the real request body" }Mimic checks the database for an enpoint that matches the url and tag and if it finds a match returns it's schema and response payload. The request payload is compared to the endpoints schema and if it matches, the response payload is returned.
In this way, you can post a request to the Mimic specifying the mock endpoint, have it validate your request payload and return your desired response payload.
http://localhost:5000/mimic/getThe generic get endpoint is actually a post endpoint that takes JSON string that looks like this:
{ "url": "url of desired endpoint", "tag": "a unique tag string", "query": "optional query string" }It is called a get endpoint because it is mocking get endpoints. The endpoint with the desired tag and url is returned from the database and the request query string is compared to the endpoints schema, if it matches, the response payload is returned. If no query string is in the request then no comparison is made and the response is returned.
Data endpoints
These endpoints are concerned with the CRUDing of your mock endpoints. The endpoint database looks like this:
id verb service url tag schema payload 1 POST some service /some/url/tail unique tag {"key1": "value1", "key2": "value2"} {"some big": "load of JSON data"} 2 GET some service /some/url/tail unique tag ?key1=value1&key2=value2 {"some big": "load of JSON data"}
http://localhost/insert_endpointThis endpoint inserts a new mock endpoint and takes a JSON string that looks like this:
{ "verb": "VERB", "service": "service name", "url": "url of endpoint", "tag": "unique tag", "schema": "expected request schema", "payload": "response payload to return", }
http://localhost/update_endpointThis endpoint updates a current mock endpoint and takes a JSON string that looks like this:
{ "id": "id of endpoint", "verb": "VERB", "service": "service name", "url": "url of endpoint", "tag": "unique tag", "schema": "expected request schema", "payload": "response payload to return", }
http://localhost/delete_endpoint/{id}This endpoint deletes a current mock endpoint and takes an endpoint id in the url
User Interface
Mimic provides a user interface to CRUD your endpoints and test your endpoints, and also this docs page! The interfaces make AJAX calls to the endpoints and no endpoint requires the interface to be used.
http://localhost/ui/This endpoint returns an index page. It contains a table of all mock endpoints. Each endpoint row has a link for updating and deleting that endpoint.
http://localhost/ui/inser_endpointThis endpoint returns an insert page. It contains a form that allows you to define new mock endpoints.
http://localhost/ui/update_endpoint/{id}This endpoint returns an update page. It contains a pre populated form that allows you to update the existing mock endpoint. It is accessed from the endpoint table in the index page.
http://localhost/ui/testThis endpoint returns an endpoint test page. This page allows you to test a particular mock endpoint. You are provided with a form that allow you to enter the tag, url and payload (or query) for an endpoint and query it to see the response.
http://localhost/ui/docsThis endpoint returns this very Docs page!.
The Mimic wrapper
A wrapper is included to allow you to easily replace service wrapper calls with Mimic calls. A Mimic specific to a service can be instanciated with the service name as an argument. Alternatively a generic Mimic can be instanciated with no service name, although a service name will have to be passed to each call.
from wrapper.mimic_wrapper import Mimic""" POST """ mimic = Mimic('service1') url = '/service1/url' tag = 'my service1 call' payload = '{"key1": "value1", "key2": "value2"}' response = mimic.post(url=url, tag=tag, payload=payload) print response
""" GET, query """ mimic = Mimic('service2') url = '/service2/url' tag = 'my service2 call' query = '?name=alarm' response = mimic.get(url=url, tag=tag, query=query) print response
""" GET, generic mimic, no query """ mimic = Mimic() url = '/service3/url' tag = 'my service3 call' response = mimic.get(service='service3', url=url, tag=tag) print response url = '/service4/url' tag = 'my service4 call' response = mimic.get(service='service4', url=url, tag=tag) print response