# FetchTest (https://effetch.pages.dev/docs/reference/fetch-test)

`@monarcode/effetch/test` exports one name, the `FetchTest` namespace. Its members:

```ts
import { FetchTest } from "@monarcode/effetch/test";
```

## Fake backend

Guide: [Test with a fake backend](https://effetch.pages.dev/docs/testing).

### FetchTest.fail

Makes a handler that answers every request to `endpoint` with the error status `status`, for [`layer`](https://effetch.pages.dev/docs/reference/fetch-test#layer). To answer some requests with an error and others with data, return a [`reply`](https://effetch.pages.dev/docs/reference/fetch-test#reply) from [`respond`](https://effetch.pages.dev/docs/reference/fetch-test#respond) instead.

### FetchTest.layer

Makes a fake backend: an `HttpClient` layer that answers requests with `handlers`, without a network. Provide it where `FetchHttpClient.layer` goes, under a real client with its plugins, so a test runs the same code as production, schemas included.

- **Matching.** A request is answered by the handler whose route matches its method and path. Without `options.baseUrl`, a route matches the end of the path, so `GET /todos/:id` answers `https://api.test/v1/todos/1`. When several routes match, the longest wins, then the one with the most literal segments (`/todos/count` before `/todos/:id`), then the one listed first. A route with an absolute URL matches that URL only.
- **No match.** A request no handler matches is a defect. Its message lists the routes the backend has, and the ones that match the path with another method.
- **Recording.** The layer also provides the [`Recorder`](https://effetch.pages.dev/docs/reference/fetch-test#recorder), with a new record each time it's built. Read it with [`requests`](https://effetch.pages.dev/docs/reference/fetch-test#requests).
- **Services.** The layer needs the services its handlers need. A handler runs with the services the layer was built with, and those of the call it answers, which win when both have one.

### FetchTest.reply

Makes a response that a [`respond`](https://effetch.pages.dev/docs/reference/fetch-test#respond) handler sends as it is, for any status: a `201` with a `location` header, a `404` for a missing item, a `500`. The body isn't checked against the endpoint's schemas.

### FetchTest.respond

Makes a handler that answers `endpoint`'s requests with `handler`, for [`layer`](https://effetch.pages.dev/docs/reference/fetch-test#layer).

### FetchTest.FailBody

The body [`fail`](https://effetch.pages.dev/docs/reference/fetch-test#fail) takes for endpoint `EP` and status `S`: a value of the `statusErrors` schemas for `S`, or of the `error` schema.

- On a bound endpoint, `statusErrors` has the endpoint's and the client's schemas, but not their statuses, so the body may be a value of any of them. Its `error` is always set, `Schema.Unknown` by default, which takes anything.
- On a definition without `error`, a status without `statusErrors` takes any body, as the client's `defaultError` isn't known.

### FetchTest.FailServices

The services a [`fail`](https://effetch.pages.dev/docs/reference/fetch-test#fail) handler for endpoint `EP` needs: those of encoding its `statusErrors` and `error` schemas.

### FetchTest.Handler

One route of the fake backend, made by [`respond`](https://effetch.pages.dev/docs/reference/fetch-test#respond) or [`fail`](https://effetch.pages.dev/docs/reference/fetch-test#fail). Pass a list of them to [`layer`](https://effetch.pages.dev/docs/reference/fetch-test#layer).

- `R`: the services it needs to run, which the layer then needs.

### FetchTest.HandlerServices

The services handler `H` needs to run: `R` for a `Handler<R>`. For a union of handlers, the union of their services, which is what [`layer`](https://effetch.pages.dev/docs/reference/fetch-test#layer) needs.

### FetchTest.LayerOptions

Options for [`layer`](https://effetch.pages.dev/docs/reference/fetch-test#layer).

| Prop       | Type     | Description                                                            |
| ---------- | -------- | ---------------------------------------------------------------------- |
| `baseUrl?` | `string` | Where the routes start. Without it, a route matches the end of a path. |

### FetchTest.QueryParams

The query parameters of a request, as they were in the URL (decoded): a `string` per key, or a list of strings for a key that was repeated.

### FetchTest.Reply

A response sent as it is, made by [`reply`](https://effetch.pages.dev/docs/reference/fetch-test#reply). A [`respond`](https://effetch.pages.dev/docs/reference/fetch-test#respond) handler returns one to answer with another status, such as `201` or `404`.

### FetchTest.ReplyOptions

Options for a [`reply`](https://effetch.pages.dev/docs/reference/fetch-test#reply) or a [`fail`](https://effetch.pages.dev/docs/reference/fetch-test#fail) handler.

| Prop       | Type    | Description                                    |
| ---------- | ------- | ---------------------------------------------- |
| `headers?` | `union` | Sent with the response, over its content type. |

### FetchTest.RespondOptions

Options for the responses of a [`respond`](https://effetch.pages.dev/docs/reference/fetch-test#respond) handler that aren't a [`Reply`](https://effetch.pages.dev/docs/reference/fetch-test#reply-type).

| Prop       | Type     | Description                                                              |
| ---------- | -------- | ------------------------------------------------------------------------ |
| `status?`  | `number` | The status, from 200 to 599. Defaults to `200`, or `204` without a body. |
| `headers?` | `union`  | Sent with the response, over its content type.                           |

### FetchTest.RespondRequest

What a [`respond`](https://effetch.pages.dev/docs/reference/fetch-test#respond) handler is called with, for one request to route `R`, whose endpoint has the `input` schema `I` and the query schema `QS`.

| Prop      | Type                | Description                                                                  |
| --------- | ------------------- | ---------------------------------------------------------------------------- |
| `params`  | `RouteParams<R>`    | The path params, as strings (see RouteParams).                               |
| `query`   | `RequestQuery<QS>`  | The query, decoded with the query schema if there is one (see RequestQuery). |
| `body`    | `RequestBody<R, I>` | The body, decoded with `input` if there is one (see RequestBody).            |
| `headers` | `object`            | The request's headers, with lowercase names.                                 |
| `url`     | `string`            | The URL the request was sent to.                                             |
| `request` | `object`            | The request itself, for anything else.                                       |

### FetchTest.RespondResult

What a [`respond`](https://effetch.pages.dev/docs/reference/fetch-test#respond) handler succeeds with, for an endpoint with the `output` schema `O` and the stream format `St`, or a [`Reply`](https://effetch.pages.dev/docs/reference/fetch-test#reply-type):

- without `St`: a value of `output`'s `Type`, encoded with `output` before it's sent. Without `output`, the value is sent by its runtime type (see [`reply`](https://effetch.pages.dev/docs/reference/fetch-test#reply)).
- `"sse"`: a list of [`TestEvent`](https://effetch.pages.dev/docs/reference/fetch-test#testevent)s, sent as Server-Sent Events. An `event` or `id` with a line break can't be sent, and the call dies.
- `"ndjson"`: a list of values of `output`'s `Type` (anything that has a JSON form without `output`), sent one per line. With `Fetch.text()`, a value with a line break, or a blank one, can't be sent as one line, and the call dies.

### FetchTest.RespondServices

The services a [`respond`](https://effetch.pages.dev/docs/reference/fetch-test#respond) handler for endpoint `EP` needs, besides its own: those of encoding `output`, and of decoding `input` and the query schema.

### FetchTest.RouteParams

The path params a handler gets for route `R`: one key per `:param`, each a `string`, as it was in the URL (decoded).

### FetchTest.TestEndpoint

An endpoint the fake backend can answer for: one made by `Fetch.endpoint`, `client.endpoint` or `client.bind`. The fake backend reads its route and its schemas.

### FetchTest.TestEvent

One event a [`respond`](https://effetch.pages.dev/docs/reference/fetch-test#respond) handler sends to an SSE endpoint (one with `stream: "sse"`): its `data`, of `output`'s `Type` (a `string` without `output`), and optionally its `event` name and `id`.

| Prop     | Type     | Description                                                             |
| -------- | -------- | ----------------------------------------------------------------------- |
| `event?` | `string` | The event's name, on one line. Without it, the client sees `"message"`. |
| `id?`    | `string` | The event's ID, on one line.                                            |
| `data`   | `A`      | The event's data, encoded with `output`.                                |

## Recorded requests

Guide: [Check what was sent](https://effetch.pages.dev/docs/testing/requests).

### FetchTest.Recorder

The service that keeps the requests the fake backend received, in the order it received them. [`layer`](https://effetch.pages.dev/docs/reference/fetch-test#layer) provides it, with a new, empty record each time the layer is built. Read it with [`requests`](https://effetch.pages.dev/docs/reference/fetch-test#requests).

### FetchTest.requests

The requests the fake backend received so far, oldest first.

### FetchTest.RequestBody

The body a handler gets for route `R`, whose endpoint has the `input` schema `I`:

- `undefined` for a route whose method takes no body, such as `GET`;
- the decoded body, typed by `I`, with an `input` schema;
- `unknown` otherwise: the JSON value for a JSON body, the text for a text or form body, the bytes for other bytes, the `FormData` or stream as it was sent, and `undefined` without a body.

### FetchTest.RequestQuery

The query a handler gets: decoded with the endpoint's query schema `QS` when it has one, as the call gave it, and the raw [`QueryParams`](https://effetch.pages.dev/docs/reference/fetch-test#queryparams) otherwise.

### FetchTest.SentRequest

A request the fake backend received, as the [`Recorder`](https://effetch.pages.dev/docs/reference/fetch-test#recorder) keeps it.

| Prop      | Type      | Description                                                                                                                                                                     |
| --------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `method`  | `string`  | The HTTP method, such as `"GET"`.                                                                                                                                               |
| `url`     | `string`  | The URL it was sent to, with its query string.                                                                                                                                  |
| `route`   | `union`   | The route of the handler that answered it, or `undefined` when none did.                                                                                                        |
| `params`  | `object`  | The path params the route matched, as strings. Empty when none did.                                                                                                             |
| `query`   | `object`  | The query parameters, as they were in the URL (see QueryParams).                                                                                                                |
| `headers` | `object`  | The headers, with lowercase names.                                                                                                                                              |
| `body`    | `unknown` | The body: the JSON value for a JSON body, the text for a text or form body, the bytes for other bytes, the `FormData` or stream as it was sent, and `undefined` without a body. |