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.
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§
- SseFrame
- One dispatched Server-Sent Event.
- SseParser
- An incremental
text/event-streamparser: feed it bytes in any chunks, get back the events they completed. - Test
Client - An in-process HTTP test client that dispatches requests directly through an
Applicationwithout opening a TCP socket. - TestDb
- A database for one test; see the module docs.
- Test
DbBuilder - A
TestDbbeing set up: add migration directories, then.awaitit (or callbuild). - Test
Request - A pending test request. Chain builder methods then call
TestRequest::send. - Test
Response - The result of a dispatched test request.
- TestSse
- Server-Sent Events read from a test response; see the module docs.
- Test
WebSocket - The test side of an upgraded connection; see the module docs.
- Test
WebSocket Request - A WebSocket upgrade request being built; see
TestClient::websocket.
Traits§
- Open
ApiSource - Something an OpenAPI document can be read from.
Functions§
- assert_
json_ snapshot assert_openapi_snapshotfor any JSON document (a JSON Schema, an MCPtools/listresult, a response body).- assert_
openapi_ snapshot - Compare an OpenAPI document with the snapshot at
path(see the module docs for normalization andRWS_UPDATE_SNAPSHOTS).sourceis the document’s text (app.openapi_spec(&config)), aTestResponseof it, or aTestClientwhose app serves/openapi.json.