Skip to main content

mocking

Answers requests with a mocked response instead of an upstream.

Category TransformationAdd it to Route, Service, Consumer, Consumer group, Global rule, Plugin configEngine plugin mockingPriority 10900

What 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.

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"}.

The console's Add plugin panel, filled with this example.
mocking · as the console saves it
{
"response_status": 200,
"content_type": "application/json",
"response_example": "{\"id\": 1042, \"status\": \"shipped\"}"
}

What you will see​

  • A request is answered 200 by the gateway itself with the body {"id": 1042, "status": "shipped"}; no backend is called.

More scenarios​

Answer with the mock only after the route's limit and address check

Priority 0 puts mocking after the other plugins of the route, so limit-count counts a request and ip-restriction checks its address before the mocked answer is sent. At its own priority mocking answers first, and neither of them sees the request.

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"}. Under Execution set Priority to 0.

mocking · as the console saves it
{
"_meta": {
"priority": 0
},
"response_status": 200,
"content_type": "application/json",
"response_example": "{\"id\": 1042, \"status\": \"shipped\"}"
}

What you will see​

  • With limit-count on the same route, a first request is answered 200 with the mocked body and the next ones 429.
  • With ip-restriction on the same route, a request from an address outside its list is answered 403.
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.

mocking · as the console saves it
{
"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 200 by the gateway itself with a JSON body generated from the schema, whose status carries the example value shipped; no backend is called.

Things to know​

  • Response example — The body is sent as written: with application/json and the example's body, a request is answered 200 with {"id": 1042, "status": "shipped"}.
  • Response example — A $name or ${name} is replaced by that request variable, or by nothing if the request lacks it: $uri sends the request path.
  • 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: status with the example shipped comes 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, 199 for example, is refused when you save.
  • Response headers — A $name or ${name} is replaced by that request variable, or by nothing if the request lacks it: $uri sends the request path.
  • Response headers — A name with a colon travels percent-encoded: X-A:B reaches the client as x-a%3ab.
  • A request that mocking answers goes no further, so a plugin of the same route that would act after mocking never sees it. limit-count beside it does not count: against a limit of 1 request a minute, three requests in a row are all answered 200.
  • ip-restriction beside mocking on the same route lets every address through: a request from an address outside its list is answered 200 like any other.
  • key-auth on the same route still acts before mocking: a request it refuses, one without a key for example, is answered 401 and never reaches mocking.
  • To have the route's other plugins act first, give mocking a Priority below theirs, 0 for example. limit-count then counts and refuses a request over the limit, with 429 in the example; ip-restriction answers 403 to an address outside its list. How G8 handles a request explains phases and priority.
  • AdviceOn a sandbox route that partners call, turn off With mock header.

Settings​

The fields as the console shows them, section by section.

Response statusresponse_statusintegerdefault 200

The status the mocked answer carries.

at least 200
Content typecontent_typestringdefault application/json;charset=utf8

One 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 hereThe body you write is sent for every request, whatever the request asked for. A $name in it is replaced by the request variable of that name.
  • A body generated from a schemaBuilt for every request from the properties a JSON Schema names, using the example each one carries.

A body written here​

Response exampleresponse_examplestringRequired

The 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 schemaresponse_schemaobjectRequired

Each property is generated from its type, and from its example where it carries one.

example {"type":"object","properties":{"id":{"type":"integer"},"name":{"type":"string","example":"Ada"}}}
Response headersresponse_headersobject

Headers 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.

at least 1 key
Delaydelayintegerdefault 0

How long the answer is held back before it is sent.

in seconds
With mock headerwith_mock_headerbooleandefault true

Adds x-mock-by, naming the gateway and its version, to every mocked answer.

Every plugin also has Enabled, Priority, Filter, Error response and Pre-function: see Plugin execution settings.

G8 v0.11.55 · Apache APISIX® 3.19.0
Apache, Apache APISIX and APISIX are trademarks of The Apache Software Foundation.
Facts
Category
Transformation
Add it to
Route, Service, Consumer, Consumer group, Global rule, Plugin config
Engine plugin
mocking
Priority
10900