Skip to main content

Module test_client

Module test_client 

Source
Expand description

In-process testing: TestClient dispatches requests through an Application without opening a socket, and the helpers beside it cover the rest of a test:

  • TestClient::websocket drives a WebSocket route’s upgrade handler over an in-memory duplex (TestWebSocket);
  • TestResponse::sse reads a text/event-stream response, streamed or buffered, and parses its events (TestSse, SseFrame);
  • assert_openapi_snapshot compares the generated OpenAPI document with a committed file (RWS_UPDATE_SNAPSHOTS=1 rewrites it);
  • TestDb (model-* features) gives each test a database that leaves nothing behind, and factory builds #[derive(Factory)] models with default values.

Everything here is for tests: nothing on the request path uses it. It is compiled into every build (like TestClient always was) so that an application’s own tests, in another crate, reach it without a feature of their own.

Modules§

dbmodel-mysql or model-postgres or model-sqlite
A database per test that leaves nothing behind.
factorymodel-mysql or model-postgres or model-sqlite
Factories for models: #[derive(Factory)] (next to #[derive(Model)]) gives a model T::factory(), a ModelFactory that builds instances with a default value for every field, overrides from with, and saves them with create.
snapshot
Snapshot assertions for generated JSON: assert_openapi_snapshot compares an app’s OpenAPI document with a file committed beside the tests, so a change to the API shows up in review as a change to that file.
sse
Server-Sent Events in tests: TestResponse::sse returns a TestSse, which reads the response’s text/event-stream body — streamed (Sse::stream, Response::stream_pipe) as it arrives, or buffered (Sse::into_response) at once — and hands out SseFrames.
websocketNon-WebAssembly
WebSocket routes in tests: TestClient::websocket sends the opening handshake through the application, and when the route answers with WebSocket::upgrade’s 101, TestWebSocketRequest::connect runs its handler on a thread of its own over an in-memory duplex — the same handler the server would run, given the same reader and writer types it expects, with no socket in between. The test side is a TestWebSocket: it masks what it sends, as a browser does (the handler’s recv refuses unmasked frames), answers pings, and joins fragmented messages.

Structs§

SseFrame
One dispatched Server-Sent Event.
SseParser
An incremental text/event-stream parser: feed it bytes in any chunks, get back the events they completed.
TestClient
An in-process HTTP test client that dispatches requests directly through an Application without opening a TCP socket.
TestDb
A database for one test; see the module docs.
TestDbBuilder
A TestDb being set up: add migration directories, then .await it (or call build).
TestRequest
A pending test request. Chain builder methods then call TestRequest::send.
TestResponse
The result of a dispatched test request.
TestSse
Server-Sent Events read from a test response; see the module docs.
TestWebSocket
The test side of an upgraded connection; see the module docs.
TestWebSocketRequest
A WebSocket upgrade request being built; see TestClient::websocket.

Traits§

OpenApiSource
Something an OpenAPI document can be read from.

Functions§

assert_json_snapshot
assert_openapi_snapshot for any JSON document (a JSON Schema, an MCP tools/list result, a response body).
assert_openapi_snapshot
Compare an OpenAPI document with the snapshot at path (see the module docs for normalization and RWS_UPDATE_SNAPSHOTS). source is the document’s text (app.openapi_spec(&config)), a TestResponse of it, or a TestClient whose app serves /openapi.json.