Skip to content

Mock responses: answer without a backend

How-to guideThe steps for one task.

The mock-response policy makes the gateway answer an operation itself, with a fixed status, headers and body, and never call a backend. Use it when:

  • The backend is not built yet and the app team wants to code against the contract today.
  • A demo must answer without the real systems behind it.
  • The examples of a Postman collection or an OpenAPI document should be callable as they are.
  • A maintenance page needs a proper status and body rather than a refusal.

Unlike deny, a mock is a normal answer: the policies that ran before it still decorate it (the CORS headers, the rate-limit allowance), and the request inspector shows the answer as the policy's own step.

An API with no backend

An API whose operations are all mocked needs no upstream at all. The body may use the references transformations know, so a mock can echo what was asked:

apiVersion: apinx/v1
kind: API
name: orders
base_path: /orders
strip_prefix: true
policies:
  - type: api-key
  - type: rate-limit
    config: { limit: 100, window: 1m }
  - type: cors
    config:
      origins: ["https://app.example.com"]
operations:
  - name: one
    path: /{id}
    methods: [GET]
    policies:
      - type: mock-response
        config:
          status: 200
          body: |
            {"id": "${path.id}", "customer_id": "c-42", "total": 99.5, "status": "shipped"}
          headers:
            X-Sample: "true"

${path.id}, ${query.page}, ${header.X-Request-Id}, ${claim.sub} and the other references are expanded on every request. content_type defaults to application/json when the body as written is JSON (references inside strings keep it so) and text/plain otherwise; set it yourself for anything else, such as text/html for a maintenance page. An empty body answers with no body.

What the caller sees

GET /orders/o-7 HTTP/1.1
X-API-Key: apk_…

HTTP/1.1 200 OK
Content-Type: application/json
X-Apinx-Mock: 1
X-Sample: true
RateLimit-Limit: 100
RateLimit-Remaining: 99
RateLimit-Reset: 42
Access-Control-Allow-Origin: https://app.example.com

{"id": "o-7", "customer_id": "c-42", "total": 99.5, "status": "shipped"}

X-Apinx-Mock: 1 marks every mocked answer, so a client or a test can tell a stub from the real thing. The access log and analytics count the request as any other, with no backend time.

The policies before it still apply

mock-response sits just before the cache phase, so everything in the access, protection, authentication, authorization and traffic phases runs first. In the example a call without a key is still refused with 401 APX-PL-0001, the 101st call in a minute with 429 APX-PL-0007, and a browser gets the CORS headers it needs, exactly as it will once the real backend is there. Policies of later phases (transform, set-header, service-callout) never see a mocked request; put extra response headers in the mock's own headers.

Stub one operation of a real API

An API with a backend may mock a single operation: keep the API's upstream, bind mock-response on the operation that is not ready, and the other operations forward as usual. When the backend catches up, remove the binding and deploy; nothing else changes. In the console, the operation editor's Backend shows None (answered by a policy) for an operation only a policy answers, and the API's Develop tab shows the backend as answered by a policy.

A slow backend, on purpose

delay holds the answer back, to see what a client does when a call takes long:

- type: mock-response
  config:
    delay: 3s
    status: 504
    body: '{"error": "upstream timeout"}'

The delay is at most 5s. A caller that gives up during the delay gets nothing, as it would from a real backend.

APX-RT-0010

The deploy check refuses an operation that has neither an upstream nor a policy that answers every request (mock-response, or service-callout with respond): must name an upstream, unless a mock-response policy or a service-callout with respond answers the route. If a request on an operation without a backend ever gets through the whole chain unanswered, the gateway answers 502 APX-RT-0010 (This route has no backend and no policy answered the request) rather than hanging. The fix is the same in both cases: give the operation an upstream, or a policy that answers.

Where it runs

On premises, in Apinx Cloud and in file mode alike; a mock keeps no state, so every instance answers the same.