mocking
Answers requests with a mocked response instead of an upstream.
mockingPriority 10900What it does
mocking answers the requests a route takes itself, with a made-up response, and the request never reaches a backend. The answer carries the Response status and the Content type you set. Its body is written here under A body written here, or built for each request from a JSON Schema under A body generated from a schema.
The route needs no upstream at all: a route that carries only its path and mocking answers 200 with the body you wrote. Once the real backend is ready, take mocking off the route and give the route its upstream.
When to use it
- The mobile app team builds against the orders API while its backend is still being written, and needs an order to come back, for example
{"id": 1042, "status": "shipped"}. - A partner needs a sandbox route that answers the same way every time, without reaching the real backend.
- A client should be tested against a slow answer, held back under Delay, or against an error status it rarely sees.
- An answer should take its shape from a JSON Schema the teams already agreed on, with example values where the schema carries them.
Not for changing the real answer of a backend, its status, headers or body — see response-rewrite (Rewrites the response status, headers and body) or aborting or delaying some requests on their way to a real backend — see fault-injection (Injects delays or fixed responses for testing).
Guides
Pages that walk through a job with mocking, step by step.
- Answer with a mock while the backend is builtPut mocking on a route so it answers a made-up order while the real backend is still being written.
Set it up
Answer with a fixed JSON body while the real service is being built
Leave Response status at 200, set Content type to application/json, choose A body written here and set Response example to {"id": 1042, "status": "shipped"}.
{
"response_status": 200,
"content_type": "application/json",
"response_example": "{\"id\": 1042, \"status\": \"shipped\"}"
}
What you will see
- A request is answered
200by the gateway itself with the body{"id": 1042, "status": "shipped"}; no backend is called.
More scenarios
Answer with a body generated from a schema
Set Content type to application/json, choose A body generated from a schema and fill Response schema as in the JSON below.
{
"content_type": "application/json",
"response_schema": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"status": {
"type": "string",
"example": "shipped"
}
}
}
}
What you will see
- A request is answered
200by the gateway itself with a JSON body generated from the schema, whosestatuscarries the example valueshipped; no backend is called.
Things to know
- Response example — The body is sent as written: with
application/jsonand the example's body, a request is answered200with{"id": 1042, "status": "shipped"}. - Response example — A $name or ${name} is replaced by that request variable, and a name the request does not carry by nothing: $uri sends the request path. A backslash before the dollar stops the replacement but stays in the value.
- Response example — Response example and Response schema never go together: a configuration that carries both is refused when you save.
- Response schema — A property that carries an example is answered with it:
statuswith the exampleshippedcomes back as"status":"shipped". - Response schema — Give every property a type: one without it makes every request answer 500. Under Content type text/html the generated body is empty.
- Response status — A status below
200,199for example, is refused when you save. - Response headers — A $name or ${name} is replaced by that request variable, and a name the request does not carry by nothing: $uri sends the request path. A backslash before the dollar stops the replacement but stays in the value.
- Response headers — A name with a colon travels percent-encoded:
X-A:Breaches the client asx-a%3ab. - AdviceOn a sandbox route that partners call, turn off With mock header.
Settings
The fields as the console shows them, section by section.
response_statusintegerdefault 200The status the mocked answer carries.
content_typestringdefault application/json;charset=utf8One of application/json, application/xml, text/plain, text/html or text/xml, optionally with one parameter, as in text/plain; charset=utf-8.
pattern
^(application/json|application/xml|text/plain|text/html|text/xml)(;[^;]*)?$Choose one:
A body written here
response_examplestringRequiredThe body of the mocked answer, such as {"ok":true}, read by the client as the Content type above says.
A body generated from a schema
response_schemaobjectRequiredEach property is generated from its type, and from its example where it carries one.
{"type":"object","properties":{"id":{"type":"integer"},"name":{"type":"string","example":"Ada"}}}response_headersobjectHeaders added to the mocked answer, one name and value per row. A name with a colon travels percent-encoded: X-A:B reaches the client as x-a%3ab. An object whose keys are a name without a colon, each with a string or number as its value.
delayintegerdefault 0How long the answer is held back before it is sent.
with_mock_headerbooleandefault trueAdds x-mock-by, naming the gateway and its version, to every mocked answer.