Quick Answer
Conditional mock responses return different bodies, status codes, or headers depending on the request. Use them to test error paths (401 when token missing), pagination (page=2 returns second batch), or tenant isolation (different response per subdomain). In Requex.me, configure conditions per response: source (query/header/body/param), operator (equals/contains/regex/exists), and value.
Conditional Mock API Responses
Static mocks only cover the happy path. Real testing needs the mock to adapt — return 429 sometimes, 401 when auth is wrong, different data per tenant. This is what conditional mocking solves.
Why You Need Conditional Mocks
- Test "missing auth header returns 401" without breaking the rest of the suite
- Paginate:
?page=1returns items 1–10,?page=2returns 11–20 - Tenant isolation:
X-Tenant: acmereturns Acme's data, different tenant returns its own - Feature flags:
X-Beta: 1returns new response shape - Validation: POST with missing required field returns 422 with error body
- Rate limit simulation: every request with
X-Simulate: rate-limitreturns 429
How Requex Evaluates Conditions
A Requex mock route can have multiple responses, each with an optional condition set. When a request arrives:
- Requex fetches all responses for the matched route + method, ordered by priority
- It walks the list in order. For each response: if no conditions, it wins. If conditions exist, evaluate them.
- If all (mode=all) or any (mode=any) conditions pass, the response wins
- If conditions fail and a fallback is set, the fallback returns immediately (short-circuits)
- If conditions fail and no fallback, Requex continues to the next response
- If nothing matches, method=ANY responses are tried as final fallback
- If still nothing, 404 returned
Condition Sources and Operators
| Source | Example key | What it reads |
|---|---|---|
| query | page | Query string value for ?page=... |
| header | x-tenant | HTTP header value |
| param | id | Path parameter from route pattern |
| body | user.email | Nested JSON field via dot path |
Operators: eq, neq, contains, startsWith, endsWith, regex, exists, notExists, gt, lt.
Worked Example 1: Pagination
Three responses on GET /items:
Response 1 (priority 1)
condition: query.page eq "1"
body: { "items": [1..10], "nextPage": 2 }
Response 2 (priority 2)
condition: query.page eq "2"
body: { "items": [11..20], "nextPage": null }
Response 3 (priority 3, no condition — default)
body: { "items": [1..10], "nextPage": 2 }Request with ?page=2 gets Response 2. Any other request gets Response 3.
Worked Example 2: Auth + Fallback
Return 200 with data when a valid Bearer token is present; 401 otherwise. Use a condition with a fallback:
Response 1
conditions:
mode: all
rules:
- source: header, key: Authorization, operator: startsWith, value: "Bearer "
fallback:
statusCode: 401
body: '{"error":"Missing or invalid token"}'
headers:
- { key: "WWW-Authenticate", value: "Bearer" }
status: 200
body: { "user": { "id": 1, "name": "Alice" } }Valid token → 200 + user data. No/invalid token → 401 short-circuits via the fallback. One response, two outcomes.
Worked Example 3: Request Body Validation
POST /users — return 422 when body.email is missing:
Response 1 (priority 1, validation fail)
condition: body.email notExists
status: 422
body: { "error": "email required" }
Response 2 (priority 2, default success)
status: 201
body: { "id": "{{body.email}}", "created": true }Best Practices
- Order responses by specificity. Narrow conditions first, generic last.
- Always include a default response. Without one, unexpected requests get 404 — confusing during debugging.
- Use
existsinstead ofeq "". Empty-string equality behaves differently than absent fields. - Test your conditions. Hit the mock with curl first to verify the right response fires for each scenario.
- Name your responses. "Auth fail 401", "Page 1", "Default success" — future you will thank present you.
FAQ
How do conditions differ from route matching?
Route matching picks the route for a request (path + method). Conditions pick the response within that route. You can have one route with five responses, each with different conditions.
Can conditions reference previous responses?
No — Requex mocks are stateless. Each request is evaluated independently. For stateful scenarios (e.g., "return 200 the first time, 409 the second"), you need a stateful mocking tool like WireMock scenarios.
What happens if two responses both have matching conditions?
Priority wins. The response with the lower priority number is evaluated first and wins.
Can I use regex in conditions?
Yes. Use the regex operator. The value is compiled to a RegExp and tested against the source field.