# Check what was sent (https://effetch.monarcode.dev/docs/testing/requests)

Sometimes what matters in a test isn't what comes back but what your code sent:
that a delete went to the right URL, or that a form carried the right
fields. The [fake backend](https://effetch.monarcode.dev/docs/testing) records every request it
receives, so you can assert on them. This page shows how to read those
records, and how the fake backend picks a handler for each request.

## Read the recorded requests

`FetchTest.requests` gives you the requests the fake backend received,
oldest first:

```ts
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";

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

class Api extends Fetch.Service<Api>()("Api", {
  baseUrl: "https://api.test",
}) {}

const deleteTodo = Fetch.endpoint("DELETE /todos/:id");

const Backend = FetchTest.layer([
  FetchTest.respond(deleteTodo, () => Effect.void),
]);

export const test = Effect.gen(function* () {
  const api = yield* Api;
  yield* api.bind(deleteTodo)({ params: { id: 1 } });
  const [sent] = yield* FetchTest.requests;
  // sent.method: "DELETE", sent.url: "https://api.test/todos/1",
  // sent.route: "DELETE /todos/:id", sent.params: { id: "1" }
  return sent;
}).pipe(Effect.provide(Api.layer.pipe(Layer.provideMerge(Backend))));
```

The records live in `FetchTest.Recorder`, a service that the fake backend's
layer provides, with a new, empty record each time the layer is built.
Notice `Layer.provideMerge` in the last line. Under a client's layer,
`Layer.provide` would hide the recorder from your test, while
`Layer.provideMerge` keeps it available.

Each recorded request has the following properties:

- `method`, such as `"DELETE"`;
- `url`, with its query string;
- `route` and `params`, the route and the path params it matched, as
  strings;
- `query`, the raw query parameters;
- `headers`, with lowercase names;
- `body`, read back: the value of a JSON body, the text of a text or form
  body, the bytes of other bytes, a `FormData` or stream as it was sent, or
  `undefined` without one.

A request that no route matched is recorded too, with `route` set to
`undefined` and `params` set to `{}`.

## How requests are matched

The fake backend picks a handler for each request by these rules:

- **Method and path.** A route matches a request with the same method, whose
  path ends with the route's path. So `GET /todos/:id` answers
  `https://api.test/v1/todos/1`, whatever the client's `baseUrl`. To match
  the whole URL instead, pass the `baseUrl` to the layer:
  `FetchTest.layer(handlers, { baseUrl: "https://api.test/v1" })`.
- **The root route,** `GET /`, matches the path `/` alone, since every path
  with a trailing slash ends with it. Under a `baseUrl` with a path, pass
  that `baseUrl` to the layer.
- **The best match wins.** When several routes match, the one with the most
  segments wins, then the one with the most literal segments, so
  `GET /todos/count` wins over `GET /todos/:id`. After that, the one listed
  first wins.
- **Absolute routes,** such as `POST https://uploads.example.com/files`,
  match that URL only.
- **No match is a defect,** whose message lists the backend's routes, and the
  ones that match the path with another method:

  ```text
  FetchTest: no handler for POST https://api.test/todos/1.
  GET /todos/:id matches its path, but not its method.
  The handlers are:
    - GET /todos/:id
  ```

## What's next

- [Test errors and statuses](https://effetch.monarcode.dev/docs/testing/errors): make the fake backend
  fail, or answer with another status.
- [Use a hand-written HttpClient](https://effetch.monarcode.dev/docs/testing/http-client): fake what the
  fake backend doesn't do, such as a network failure.
- [FetchTest](https://effetch.monarcode.dev/docs/reference/fetch-test): look up every member of the
  `FetchTest` namespace.