Skip to main content

Crate trillium_testing

Crate trillium_testing 

Source
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§

methods
TestConn builders for http methods
prelude
useful stuff for testing trillium apps

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.
jsonsonic-rs
Construct a sonic_rs::Value from a JSON literal.

Structs§

ArcedConnector
An Arced and type-erased Connector
ConnTest
Represents both an outbound HTTP request being built and the received HTTP response.
Runtime
A type-erased RuntimeTrait implementation. Think of this as an Arc<dyn RuntimeTrait>
RuntimelessClientConfig
An in-memory Connector to use with RuntimelessServer.
RuntimelessRuntime
a runtime that isn’t a runtime
RuntimelessServer
A Server for testing that does not depend on any runtime
ServerConnector
a bridge between trillium servers and clients
ServerHandle
A handle for a spawned trillium server. Returned by Config::handle and Config::spawn
TestConn
A wrapper around a trillium::Conn for testing
TestServer
A testing interface that wraps a trillium handler, providing a high-level API for making requests and asserting on responses.
TestTransport
a readable and writable transport for testing
Url
A parsed URL record.
Valuesonic-rs
Represents any valid JSON value.

Enums§

Method
HTTP request methods.
Status
HTTP response status codes.

Traits§

AsyncRead
Read bytes asynchronously.
AsyncReadExt
Extension trait for AsyncRead.
AsyncWrite
Write bytes asynchronously.
Connector
Interface for runtime and tls adapters for the trillium client
RuntimeTrait
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_strsonic-rs
Deserialize an instance of type T from a string of JSON text.
harness
a test harness for use with test_harness
init
initialize a handler
runtime
smol runtime
to_json_stringsonic-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§

TestResult
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