Write a mock server for an API I depend on
Writes a small mock server that imitates an API you depend on, with success, error and slow responses for local tests.
Task:
Write a small mock server in [language and framework] that imitates the API described in the material, so my tests do not call the real one.
Context:
My app calls [number of endpoints] endpoints of this API: [endpoint list]. I run the mock locally and in my test pipeline. I have a sample of real responses with personal data removed.
Rules:
- Return realistic responses based on the sample, with made-up values, for the success case of each endpoint.
- Add a way to switch an endpoint to an error response (at least 400, 401, 404 and 500), a slow response of [delay] seconds and a malformed body.
- Check required request headers and fields, and answer with the same error shape as the real API where the sample shows it.
- Listen only on localhost, and use a fake token that is obviously not real.
- Tell me how to keep the mock in step with the real API, for example by a contract check.
Result:
The complete server code with comments, then a table of the switches (endpoint, mode, how to turn it on) with at most 6 rows.
Material:
[paste the API description or sample responses here]