Rule Syntax & Mocking Guide

Every mock is a rule — a JSON object stored in chrome.storage.local that tells MockPro when to intercept a request and what to return.

Rule Shape

{
  "id": "rule_abc123",
  "enabled": true,
  "type": "rest",
  "urlPattern": "https://api.example.com/users/*",
  "method": "GET",
  "conditions": {
    "headers": { "x-api-version": "2" },
    "bodyContains": "search_term"
  },
  "response": {
    "status": 200,
    "headers": { "Content-Type": "application/json" },
    "body": "{ \"id\": 1, \"name\": \"Alice\" }"
  },
  "delay": 300
}

type is one of "rest", "sse", or "websocket". Fields marked ? are optional.


URL Matching

MockPro evaluates urlPattern against the full request URL using one of three modes, tried in order:

Regex mode

Wrap the pattern in forward slashes with optional flags: /pattern/flags.

/\/api\/v[0-9]+\/users/i

Matches any URL whose path contains /api/v1/users, /api/v2/users, etc. (case-insensitive).

Wildcard mode

A bare pattern (no leading /) is treated as a wildcard:

  • * expands to .*
  • All other regex metacharacters are escaped
  • The result is anchored: ^...$
https://api.example.com/users/*

Becomes ^https://api\.example\.com/users/.*$ — matches any user path on that origin.

Fallback

If the pattern cannot be compiled as a regex (e.g. malformed syntax), MockPro falls back to a plain url.includes(pattern) check and logs a console warning.


Method Matching

Set method to an HTTP verb ("GET", "POST", …) for an exact match. Use "*", "ANY", or omit the field entirely to match all methods.


Conditions

conditions narrows a URL match further. Both fields are optional.

conditions.headers

Key/value map. Lookup is case-insensitive — "Content-Type" and "content-type" are equivalent.

"conditions": {
  "headers": { "authorization": "Bearer test-token" }
}

Only requests that carry this header value will be intercepted.

conditions.bodyContains

Plain substring check against the serialized request body (up to 10 KB is read).

"conditions": {
  "bodyContains": "\"role\":\"admin\""
}

Works with JSON, form-encoded, and plain-text bodies. ReadableStream bodies that have already been consumed return null and skip the check.


REST Response

"response": {
  "status": 201,
  "headers": {
    "Content-Type": "application/json",
    "X-Mock": "true"
  },
  "body": "{ \"created\": true }"
},
"delay": 1200
  • status — HTTP status code returned to the page (default 200).
  • headers — response headers merged into the mocked Response.
  • body — the response body. Strings are returned as-is (JSON strings are minified); plain objects are JSON-serialized automatically.
  • delay — milliseconds to wait before resolving. Useful for testing loading states and timeout handling.

SSE (Server-Sent Events)

Set type: "sse" to intercept EventSource connections.

{
  "type": "sse",
  "urlPattern": "https://api.example.com/stream",
  "response": {
    "events": [
      { "event": "update", "data": "{ \"progress\": 25 }", "delay": 500 },
      { "event": "update", "data": "{ \"progress\": 75 }", "delay": 1000 },
      { "event": "done",   "data": "{ \"progress\": 100 }", "delay": 500 }
    ],
    "repeat": true
  }
}
  • events — ordered array of SSE frames. Each frame has an optional event name, a data string, and a delay in ms before it fires.
  • repeat — when true, the sequence loops indefinitely after the last event, simulating a live feed.

WebSocket

Set type: "websocket" to intercept WebSocket connections.

{
  "type": "websocket",
  "urlPattern": "wss://chat.example.com/socket",
  "response": {
    "onConnect": [
      { "data": "{ \"type\": \"connected\", \"id\": \"sess_001\" }", "delay": 100 }
    ],
    "onMessage": [
      {
        "match": "ping",
        "data": "pong",
        "delay": 50
      },
      {
        "match": "echo",
        "data": "{{message}}"
      }
    ]
  }
}
  • onConnect — messages sent to the page immediately after the connection opens (simulates server greeting). Each entry has a data string and an optional delay in ms.
  • onMessage — match/reply pairs evaluated against each outbound message from the page; the first match wins.
    • match — pattern to test against the outgoing message. Tried as a regex first, falling back to a substring check; "*" matches every message.
    • data — string to send back. Use {{message}} to echo the original message verbatim.
    • delay — ms before the reply is delivered.

Import / Export

Rules can be exported as a JSON array from the Settings tab in the dashboard and imported back on any machine. This is the recommended way to share a mock set with your team:

[
  {
    "id": "rule_abc123",
    "enabled": true,
    "type": "rest",
    "urlPattern": "https://api.example.com/*",
    "method": "*",
    "response": { "status": 200, "body": "{ \"ok\": true }" }
  }
]

Import validates each entry and skips malformed rules, reporting them in the dashboard.


Back to Basics

Not sure where to start? Return to Getting Started for a step-by-step walkthrough.