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.

Re-exports§

pub use snapshot::assert_json_snapshot;
pub use snapshot::assert_openapi_snapshot;
pub use snapshot::OpenApiSource;
pub use sse::SseFrame;
pub use sse::SseParser;
pub use sse::TestSse;
pub use websocket::TestWebSocket;Non-WebAssembly
pub use websocket::TestWebSocketRequest;Non-WebAssembly
pub use db::TestDb;model-mysql or model-postgres or model-sqlite
pub use db::TestDbBuilder;model-mysql or model-postgres or model-sqlite

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§

TestClient
An in-process HTTP test client that dispatches requests directly through an Application without opening a TCP socket.
TestRequest
A pending test request. Chain builder methods then call TestRequest::send.
TestResponse
The result of a dispatched test request.