<!-- https://testapilocally.com/docs/core-features/mocks-openapi/ -->
# Mocks & OpenAPI

Spin up a local mock server from a collection/folder or an OpenAPI/Swagger document. Use it as a smoke target for Runner, CLI, and MCP without leaving the machine.

---

### Start / stop mock from collection or folder

* **Platform Availability:** Full Parity
* **Overview:** Context menus and collection actions can start a mock bound to the selected collection or folder. The mock toolbar shows status, port, and logs; stop returns the port.
* **How It Works (Windows App):**
  1. Right-click a collection or folder → **Mock** (or use collection actions).
  2. Note the listening port from the mock toolbar.
  3. Point requests at `http://127.0.0.1:<port>/...`; stop when finished.

![Windows API main (mock entry)](/docs/assets/screenshots/windows/windows-api-main-active.png)

* **How It Works (VS Code Extension):**
  1. Same mock actions in the shared webview.
  2. Mock process is hosted via extension protocol helpers (OpenAPI mock server module).
  3. Update request `baseUrl` or env to the ephemeral port for local runs.

![VS Code API main](/docs/assets/screenshots/vscode/vscode-api-main-active.png)

* **Code / Configuration Examples:**

```bash
# Point an environment at the mock
baseUrl=http://127.0.0.1:34567
```

* **Edge Cases, Troubleshooting & Pro-Tips:**
  - Port `0` means ephemeral — read the assigned port from logs/toolbar every start.
  - If the collection has a linked OpenAPI spec, the mock can prefer that contract.

---

### OpenAPI / Swagger-driven mock (CLI)

* **Platform Availability:** Full Parity (CLI)
* **Overview:** `npm run mock` serves from `--openapi`, a workspace collection, or a standalone collection file. Ideal for agents and CI smoke targets.
* **How It Works (Windows App):**
  1. Keep the backend package available (`qasignoff-backend`).
  2. Run mock against OpenAPI YAML/JSON or workspace collection.
  3. Hit the printed base URL from API mode or Runner.

![Windows import / OpenAPI context](/docs/assets/screenshots/windows/windows-import-modal.png)

* **How It Works (VS Code Extension):**
  1. Run the same CLI from the integrated terminal.
  2. No need to keep the webview open for CLI mock.
  3. Use MCP `run_request` against mock URLs for agent loops.

![VS Code command palette](/docs/assets/screenshots/vscode/vscode-command-palette.png)

* **Code / Configuration Examples:**

| Flag | Description | Default |
|------|-------------|---------|
| `-w, --workspace <path>` | Absolute Signoff workspace folder | — |
| `-c, --collection <id\|name\|file>` | Collection in workspace, or collection file | — |
| `--folder <id\|name>` | Optional folder when using a workspace | — |
| `-o, --openapi <path>` | OpenAPI/Swagger JSON or YAML | — |
| `-p, --port <n>` | Port (`0` = ephemeral) | `0` |
| `--quiet` | Less request logging | `false` |

```bash
npm run mock -- --openapi ./petstore.yaml --port 0
npm run mock -- --workspace C:/abs/workspace --collection Pets
npm run mock -- --collection ./Pets.signoff.json --port 4010
```

* **Edge Cases, Troubleshooting & Pro-Tips:**
  - You must pass workspace+collection, a collection file, **or** `--openapi`.
  - `--openapi` overrides a linked spec when both are present.
  - Workspace paths must be absolute (no `..` segments) for host safety checks.

---

### Mock toolbar & logs

* **Platform Availability:** Full Parity
* **Overview:** While a UI mock is running, the toolbar exposes stop/status and a log stream of incoming requests for quick verification.
* **How It Works (Windows App):**
  1. Start mock from the tree.
  2. Open mock logs; send a request from API mode.
  3. Confirm method/path/status in the log, then stop.

![Windows utility console](/docs/assets/screenshots/windows/windows-utility-console.png)

* **How It Works (VS Code Extension):**
  1. Same toolbar/logs in the webview.
  2. Correlate with bottom **Console** if script-side errors appear.
  3. Restore panels if the editor layout hid the mock chrome.

![VS Code utility console](/docs/assets/screenshots/vscode/vscode-utility-console.png)

* **Code / Configuration Examples:**

```http
GET http://127.0.0.1:4010/pets/1
Accept: application/json
```

* **Edge Cases, Troubleshooting & Pro-Tips:**
  - 404s usually mean path mismatch vs OpenAPI `paths` or collection routes — compare log path to spec.
  - Parallel mocks need distinct ports; don’t assume a fixed port when using `0`.

---

### OpenAPI import & sync (related)

* **Platform Availability:** Full Parity
* **Overview:** Import OpenAPI 3.x / Swagger 2.0 into collections; sync from collection context menus when the source spec changes. Export OpenAPI 3 from Signoff when sharing contracts.
* **How It Works (Windows App):** Import → choose OpenAPI file → review generated requests → optional Sync OpenAPI later.
* **How It Works (VS Code Extension):** Same import/sync in the shared shell; reveal generated files on disk via **Reveal in File Explorer**.

![Windows import modal](/docs/assets/screenshots/windows/windows-import-modal.png)

![VS Code import modal](/docs/assets/screenshots/vscode/vscode-import-modal.png)

* **Code / Configuration Examples:** See [Import / export](/docs/import-export/).

* **Edge Cases, Troubleshooting & Pro-Tips:**
  - Generated mocks approximate examples/schemas — not a full stateful backend.
  - Prefer contract tests against the real service before release; use mocks for isolation.

---

## Related

- [Import / export](/docs/import-export/)
- [MCP & CLI](/docs/core-features/mcp-and-cli/)
- [Protocols](/docs/core-features/protocols/)
