# Mock Sets

> Record responses from captured traffic, a whole domain or a HAR file, and replay them without the network to demo, test or debug an app offline.

Source: https://docs.subcloak.com/cloak-proxy/tools/mock-sets/

A mock set is a snapshot of an API's responses. While it's on, Cloak Proxy answers matching requests from the snapshot instead of the network, so an app behaves exactly as it did when you recorded it. Use mock sets to demo an app without Wi-Fi, to keep testing while a backend is down, or to reproduce a bug from a teammate's HAR file.

## Record a domain and replay it offline

1. Use your app normally so Cloak Proxy captures the screens you need.

2. In the [sidebar](https://docs.subcloak.com/cloak-proxy/inspect/sidebar/), right-click the domain and choose **Mock api.example.com Offline** (the menu item shows the domain's name).

   Cloak Proxy turns every captured response from that host into a mock set named `api.example.com (offline)`, turns Mock Sets on and opens the set.

3. Use the app again. Requests to the host are answered from the set, with a `Mock` badge in the traffic list. Nothing reaches the server.

4. When you're done, clear the set's checkbox or turn Mock Sets off.

*[Screenshot: The Mock Sets tool with one set holding a single 200 response for GET api.example.com/v1/users, Offline mode on and Ignore query strings off. A mock set with one recorded response. With Offline on, nothing reaches api.example.com while the set is on.]*

## Ways to create a mock set

| From | How | The set is named |
|---|---|---|
| Selected requests | Select requests in the traffic list, right-click one of them and choose **Tools › Mock N Responses** (or **Mock This Response** for one). Or search for **Mock Selected Responses** in [Search Everything](https://docs.subcloak.com/cloak-proxy/inspect/search-and-command-palette/). | After the request, or `N requests from <host>` |
| Selected requests, in the Tools window | Click **+** under the list and choose **From Selected Requests**. | `N selected requests` |
| A whole domain | Right-click the domain in the sidebar and choose **Mock … Offline**. | `<host> (offline)` |
| A file | Click **+** and choose **Import HAR or Session…**, or click **Import HAR or Session…** when the list is empty. Accepts `.har`, `.cloaksession`, Charles `.chlsj` and `.json` files. | The file name |
| A command line run | [`cloakctl run --mock FILE`](https://docs.subcloak.com/cloak-proxy/automation/headless-and-ci/) answers requests from a recorded HAR or session. | The file name |

Each way turns Mock Sets on. Only requests with a response are recorded; encrypted tunnels and requests still in progress are skipped.

### What's recorded

For each request, a mock set keeps the method, the full URL, the status code, the headers and the body.

- The body is stored decompressed, so you can read and edit it.
- `Content-Encoding`, `Content-Length`, `Transfer-Encoding`, `Connection` and `Keep-Alive` headers are left out. Cloak Proxy sets the right length when it replays the response.

> **Large bodies:**
> A mock set replays what Cloak Proxy recorded. Bodies larger than the **Max captured body size** in [Network settings](https://docs.subcloak.com/cloak-proxy/capture/network-settings/) were recorded cut short, and are replayed cut short. Raise the limit before you record large downloads.

## Mock set settings

| Setting | Default | What it does |
|---|---|---|
| Name | | Shown in the list and in the offline 404 message. |
| **Offline: answer unrecorded requests to these hosts with 404** | On | Requests to any host in the set that have no recording get a 404 instead of going to the server. The note below the toggle lists the hosts. |
| **Ignore query strings when matching** | Off | Matches `/users?page=2` with a recording of `/users?page=1`. |

With **Offline** off, recorded requests are answered from the set, and anything else goes to the real server.

### The offline 404

An unrecorded request to a host in an **Offline** set gets:

```http title="Offline response"
HTTP/1.1 404 Not Found
Content-Type: application/json
X-Cloak-Proxy: mock-miss

{"error":"No response recorded in mock set “api.example.com (offline)” for GET /v1/settings"}
```

Look for `X-Cloak-Proxy: mock-miss` to find screens you didn't record.

## Edit recorded responses

Select a set to see its responses.

| Column | What it shows |
|---|---|
| **On** | Clear it to skip this response. |
| **Method** | The request method. |
| **URL** | The full recorded URL. |
| **Status** | The status code, red for 400 and above. |
| **Size** | The body size. |

Type in **Filter URLs** to narrow the table. Select a response and click the minus button next to the filter to remove it.

Select a response to edit it below the table:

| Field | Notes |
|---|---|
| **Status code** | Any number; the reason phrase is shown next to it. |
| **Headers** | Edit, **Add** or remove headers. |
| **Body** | Text bodies are editable, with JSON validation, **Format** and **Minify**. Binary bodies show **Binary body, N. It's replayed as recorded.** and can't be edited. |

Changes apply to the next matching request.

## How requests are matched

A request is answered from a set when its method and URL match a recorded response:

- The method must be the same (case doesn't matter).
- The scheme and host are compared without case. Default ports (`:443`, `:80`) and `#fragments` are ignored.
- The path and query values are compared exactly, including case.
- **Query parameter order doesn't matter**: `?b=2&a=1` matches a recording of `?a=1&b=2`.
- With **Ignore query strings when matching** on, the query string is ignored completely.
- If the same request was recorded more than once, the most recent recording wins.
- Responses whose **On** checkbox is cleared are skipped.

(added in Cloak Proxy 0.4.0) GraphQL sends every operation to one URL, so each GraphQL recording also remembers its operation and answers only requests for that operation: `GetUser` gets the `GetUser` recording, `GetCart` the `GetCart` one. An operation with no recording gets the [offline 404](#the-offline-404) when **Offline** is on. Sets recorded before 0.4.0 answer anything at their URL as before.

### Several mock sets

Sets are checked from the top of the list down, and the first set that answers wins (**First matching set wins**). Drag sets to change the order. Clear a set's checkbox to skip it, or right-click it and choose **Delete**.

> **Offline sets answer for their whole host:**
> An **Offline** set answers every request to its hosts: with a recording, or with the 404. A later set never sees those requests, even if it has a recording for one. Put the set with the recordings you want first, or turn **Offline** off on the set above it. In the same way, clearing a response's **On** checkbox in an Offline set makes that request get the 404 rather than reach the server.

## How Mock Sets works with other tools

- [Map Local](https://docs.subcloak.com/cloak-proxy/tools/map-local/) runs first: a matching Map Local rule wins over a mock set.
- Mock Sets match the request after request scripts and the request breakpoint. [Map Remote](https://docs.subcloak.com/cloak-proxy/tools/map-remote/) runs after Mock Sets, so a mocked request is never remapped.
- Mocked responses still go through [No Caching](https://docs.subcloak.com/cloak-proxy/tools/no-caching/), response [scripts](https://docs.subcloak.com/cloak-proxy/tools/scripting/), the response [breakpoint](https://docs.subcloak.com/cloak-proxy/tools/breakpoint/) and the download delay of [Network Conditions](https://docs.subcloak.com/cloak-proxy/tools/network-conditions/). You can script small changes on top of a recording, or test the recording on a slow network.

> **Recordings contain real data:**
> Mock sets hold real responses: personal data, tokens in bodies, cookies in `Set-Cookie` headers. They're saved with your settings on this Mac and included when you choose **Tools › Export Rules…**. Edit or remove sensitive responses before you share an export.

## Related

  - [Demo offline with mock sets](https://docs.subcloak.com/cloak-proxy/guides/demo-offline-with-mock-sets/): A step-by-step guide to an offline demo.
  - [Map Local](https://docs.subcloak.com/cloak-proxy/tools/map-local/): Hand-write a single response instead of recording.
  - [Import sessions](https://docs.subcloak.com/cloak-proxy/sessions/import/): Open HAR, Charles and cURL files.
  - [Headless and CI](https://docs.subcloak.com/cloak-proxy/automation/headless-and-ci/): Run tests against recorded responses with cloakctl run --mock.
