A simple Flask-based mock API service using SQLite for storage.
Install dependencies:
pip install -r requirements.txtRun the application:
python -m mockapi.appThis will start the server on http://127.0.0.1:5000. If you use
localhost and your system prefers IPv6, you might hit another service
listening on port 5000. Using the IPv4 address avoids that issue.
Opening http://127.0.0.1:5000/ in your browser loads a Bootstrap styled UI
for managing endpoints. It lets you register, deregister, clear, export and
import endpoints and shows a table of all current registrations.
Send a POST request to /register with a JSON body:
{
"path": "customer/123",
"methods": ["GET"],
"response_type": "json",
"response_body": "{\"id\":123,\"name\":\"John Doe\",\"email\":\"john@doe.com\"}",
"status_code": 200
}Send a POST request to /deregister with a JSON body containing the path:
{ "path": "customer/123" }Call GET /endpoints to retrieve a JSON array of all registered endpoints.
Each entry contains the path, allowed methods and status code. For example:
[
{
"path": "customer/123",
"methods": ["GET"],
"status_code": 200
}
]Once registered, you can query the mocked endpoint via the /api/ prefix.
For example, if you registered customer/123 you would call
GET http://127.0.0.1:5000/api/customer/123 to retrieve the document.
You can also register endpoints that return HTML with custom status codes. For example, to register an endpoint that returns a welcome page and another that returns a 500 error:
curl -X POST http://127.0.0.1:5000/register -H 'Content-Type: application/json' \
-d '{"path":"welcome","methods":["GET"],"response_type":"html","response_body":"<h1>Welcome</h1>","status_code":200}'
curl -X POST http://127.0.0.1:5000/register -H 'Content-Type: application/json' \
-d '{"path":"error","methods":["GET"],"response_type":"html","response_body":"<h1>Internal Server Error</h1>","status_code":500}'After registering, GET http://127.0.0.1:5000/api/welcome returns the HTML
welcome page with status 200, while
GET http://127.0.0.1:5000/api/error returns the error page with status 500.
You can export all registered endpoints with:
curl http://127.0.0.1:5000/exportThis returns a JSON array with the full definitions including response bodies.
To import multiple endpoints in one call, POST the JSON back to /import:
curl -X POST http://127.0.0.1:5000/import -H 'Content-Type: application/json' \
-d '[{"path":"foo","methods":["GET"],"response_type":"json","response_body":"{}","status_code":200}]'Each entry is added just like calling /register and the response includes the
number of endpoints created.
This project is licensed under the MIT License — see LICENSE.
Created with ❤️ by Jon Arnar Jonsson.