Expand description
Testing utilities for trillium applications.
This crate is intended to be used as a development dependency.
use trillium::{Conn, Status, conn_try};
use trillium_testing::TestServer;
async fn handler(mut conn: Conn) -> Conn {
let request_body = conn_try!(conn.request_body_string().await, conn);
conn.with_body(format!("request body was: {}", request_body))
.with_status(418)
.with_response_header("request-id", "special-request")
}
let app = TestServer::new(handler).await;
app.post("/")
.with_body("hello trillium!")
.await
.assert_status(Status::ImATeapot)
.assert_body("request body was: hello trillium!")
.assert_headers([("request-id", "special-request"), ("content-length", "33")]);§Features
To use the same runtime as your application, enable a runtime feature for trillium testing. Without a runtime feature enabled, trillium testing will approximate a runtime through spawning a thread per task and blocking on a future. This is fine for simple testing, but you probably want to enable a server feature.
§Tokio:
[dev-dependencies]
# ...
trillium-testing = { version = "...", features = ["tokio"] }§Async-std:
[dev-dependencies]
# ...
trillium-testing = { version = "...", features = ["async-std"] }§Smol:
[dev-dependencies]
# ...
trillium-testing = { version = "...", features = ["smol"] }Re-exports§
pub use futures_lite;
Modules§
Macros§
- assert_
body - assert that the response body is as specified. this assertion requires mutation of the conn.
- assert_
body_ contains - asserts that the response body matches the specified pattern, using
str::contains - assert_
headers - asserts any number of response headers
- assert_
not_ handled - assert that all of the following are true: the status was not set the body was not set the conn was not halted
- assert_
ok - assert_ok is like
assert_response!except it always asserts a status of 200 Ok. - assert_
response - combines several other assertions. this assertion can be used to assert: just a status code, a status code and a response body, or a status code, a response body, and any number of headers
- assert_
status - assert that the status code of a conn is as specified.
- json
sonic-rs - Construct a
sonic_rs::Valuefrom a JSON literal.
Structs§
- Arced
Connector - An Arced and type-erased
Connector - Conn
Test - Represents both an outbound HTTP request being built and the received HTTP response.
- Runtime
- A type-erased
RuntimeTraitimplementation. Think of this as anArc<dyn RuntimeTrait> - Runtimeless
Client Config - An in-memory Connector to use with RuntimelessServer.
- Runtimeless
Runtime - a runtime that isn’t a runtime
- Runtimeless
Server - A
Serverfor testing that does not depend on any runtime - Server
Connector - a bridge between trillium servers and clients
- Server
Handle - A handle for a spawned trillium server. Returned by
Config::handleandConfig::spawn - Test
Conn - A wrapper around a
trillium::Connfor testing - Test
Server - A testing interface that wraps a trillium handler, providing a high-level API for making requests and asserting on responses.
- Test
Transport - a readable and writable transport for testing
- Url
- A parsed URL record.
- Value
sonic-rs - Represents any valid JSON value.
Enums§
Traits§
- Async
Read - Read bytes asynchronously.
- Async
Read Ext - Extension trait for
AsyncRead. - Async
Write - Write bytes asynchronously.
- Connector
- Interface for runtime and tls adapters for the trillium client
- Runtime
Trait - A trait that covers async runtime behavior.
- Server
- The server trait, for standard network-based server implementations.
- Stream
- A stream of values produced asynchronously.
Functions§
- block_
on - runs the future to completion on the current thread
- client_
config - runtime client config
- config
- runtime server config
- connector
- build a connector from this handler
- from_
json_ str sonic-rs - Deserialize an instance of type
Tfrom a string of JSON text. - harness
- a test harness for use with
test_harness - init
- initialize a handler
- runtime
- smol runtime
- to_
json_ string sonic-rs - Serialize the given data structure as a String of JSON.
- with_
runtime - a harness that includes the runtime
- with_
server - Starts a trillium handler bound to a random available port on localhost, run the async tests provided as the second argument, and then shut down the server. useful for full integration tests that actually exercise the tcp layer.
- with_
transport - open an in-memory connection to this handler and call an async
function with an open
Box<dyn Transport>
Type Aliases§
- Test
Result - a sponge Result
Attribute Macros§
- test
- currently supports #[test_harness::test(harness = path::to::harness_fn)] and #[test] see crate-level docs for usage and examples