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::websocketdrives a WebSocket route’s upgrade handler over an in-memory duplex (TestWebSocket);TestResponse::ssereads atext/event-streamresponse, streamed or buffered, and parses its events (TestSse,SseFrame);assert_openapi_snapshotcompares the generated OpenAPI document with a committed file (RWS_UPDATE_SNAPSHOTS=1rewrites it);TestDb(model-*features) gives each test a database that leaves nothing behind, andfactorybuilds#[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-mysqlormodel-postgresormodel-sqlitepub use db::TestDbBuilder;model-mysqlormodel-postgresormodel-sqlite
Modules§
- db
model-mysqlormodel-postgresormodel-sqlite - A database per test that leaves nothing behind.
- factory
model-mysqlormodel-postgresormodel-sqlite - Factories for models:
#[derive(Factory)](next to#[derive(Model)]) gives a modelT::factory(), aModelFactorythat builds instances with a default value for every field, overrides fromwith, and saves them withcreate. - snapshot
- Snapshot assertions for generated JSON:
assert_openapi_snapshotcompares 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::ssereturns aTestSse, which reads the response’stext/event-streambody — streamed (Sse::stream,Response::stream_pipe) as it arrives, or buffered (Sse::into_response) at once — and hands outSseFrames. - websocket
Non-WebAssembly - WebSocket routes in tests:
TestClient::websocketsends the opening handshake through the application, and when the route answers withWebSocket::upgrade’s101,TestWebSocketRequest::connectruns 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 aTestWebSocket: it masks what it sends, as a browser does (the handler’srecvrefuses unmasked frames), answers pings, and joins fragmented messages.
Structs§
- Test
Client - An in-process HTTP test client that dispatches requests directly through an
Applicationwithout opening a TCP socket. - Test
Request - A pending test request. Chain builder methods then call
TestRequest::send. - Test
Response - The result of a dispatched test request.