Skip to content

Rules

Rules are the core of mockd’s request handling. Each rule combines conditions, a response (with optional sequencing), and actions — all in a single, composable unit. Rules are evaluated top-to-bottom by priority; the first rule whose conditions match determines the response.

When a request arrives, mockd walks the endpoint’s active rules in priority order:

  1. Evaluate conditions — if the rule has conditions and they don’t all match, skip to the next rule
  2. Execute actions — if the rule has actions, run them (mutations are visible to subsequent rules)
  3. Resolve response — if the rule has a response definition, use it and stop evaluating further rules

If no rule matches or no rule defines a response, the endpoint’s default response is returned.

From the endpoint detail page, click Add Rule. Each rule has:

  • Name — a descriptive label
  • Priority — evaluation order (lower numbers first)
  • Active toggle — enable/disable without deleting
  • Conditions — when this rule applies (optional — matches all requests if empty)
  • Response — what to return, with optional multi-step sequencing
  • Actions — server-side logic to execute when matched

Conditions control when a rule fires. All conditions on a rule use AND logic — every condition must match.

TargetKeyDescription
MethodHTTP method (GET, POST, etc.)
PathRequest path
HeaderHeader nameValue of a specific request header
QueryParameter nameValue of a query string parameter
Body (raw)Raw request body content
Body (JSON path)Dot-notation pathJSON field value (e.g., user.role, items.0.name)
RandomProbabilistic matching (see below)
OperatorDescription
equalsExact string match
not_equalsDoes not equal
containsSubstring match
not_containsDoes not contain substring
matches_regexRegular expression match
existsValue is present
not_existsValue is absent
less_thanNumeric comparison (also used with random target)

Use the random target with less_than to create probabilistic rules. For example, a condition with target random and value 30 matches ~30% of requests. This is useful for simulating flaky behavior directly within a rule.

A rule’s response defines what to return when matched. Each response has one or more steps and a sequence mode.

For a simple conditional response, add a single step:

FieldDescription
Status CodeHTTP status (100–599)
BodyResponse content (supports template variables)
HeadersResponse headers (key-value pairs)
Delay (ms)Minimum response delay
Delay Max (ms)Maximum delay for random jitter

Add multiple steps to cycle through different responses on successive requests. Each step also has a Count field — how many times to return this step before advancing (default: 1).

ModeBehavior
LoopAfter the last step, cycle back to the first
Hold LastAfter the last step, keep returning it indefinitely

Click Reset Sequence on a rule to restart its position back to step 0.

Simulate a resource being created then retrieved:

StepStatusBodyCount
1201{"id": "abc", "status": "creating"}1
2200{"id": "abc", "status": "ready"}1

Mode: Hold Last — first request gets 201, all subsequent get 200.

Simulate a flaky API:

StepStatusBodyCount
1500{"error": "Internal Server Error"}2
2200{"data": "success"}1

Mode: Loop — fails twice, succeeds once, then repeats.

Actions execute server-side logic when a rule’s conditions match. They run before the response is resolved, so actions can modify the response.

Sets a variable for the current request’s response processing. Access with {{vars.name}}.

ConfigDescription
NameVariable name
ValueVariable value (supports template variables)

Persists a variable in the endpoint’s storage across requests. Access with {{endpoint.name}}.

ConfigDescription
NameVariable name
ValueVariable value (supports template variables)

Makes an outbound HTTP request. Useful for webhooks or callbacks.

ConfigDescription
URLTarget URL
MethodHTTP method (default: GET)
HeadersRequest headers (JSON object)
BodyRequest body
Apply ResponseIf true, use the HTTP response as the mock response

Private IP addresses are blocked for security (10.x, 172.16.x, 192.168.x, 127.x).

Each action specifies an error behavior:

OptionBehavior
ContinueIf the action fails, proceed with remaining actions
StopIf the action fails, stop executing further actions
  • Each action has a 5-second timeout by default
  • The entire action pipeline has a 10-second timeout
  • If an action times out, processing continues with remaining actions
FeatureFreePro
Rules per endpoint315

All plans support conditions, response steps, and actions within rules.

Return different responses based on query params:

A GET /api/users endpoint with two rules:

  1. Rule: condition query.status equals active → return 200 with active users
  2. Rule: condition query.status equals inactive → return 200 with inactive users
  3. Default: return 200 with all users

Webhook on POST with conditional response:

A POST /api/orders endpoint with a rule that:

  • Condition: method equals POST
  • Action: HTTP Request to https://hooks.example.com/new-order with body {"orderId": "{{$uuid}}"}
  • Response: 201 with {"id": "{{$uuid}}", "status": "created"}

Simulate auth flow with sequences:

A POST /api/auth/token endpoint with a rule that returns different tokens:

StepStatusBodyCount
1200{"token": "valid-token-1", "expiresIn": 3600}3
2401{"error": "token_expired"}1

Mode: Loop — three successful auths, then one expiry, repeating.