Skip to content

trillium-testing-v0.8.0

Choose a tag to compare

@jbr jbr released this 02 May 22:38
· 191 commits to main since this release
ca48261

Changed

  • Compatible with trillium 1.0
  • init(&mut handler) is now async and returns Arc<HttpContext>: init(&mut handler).await; capture the returned value if you need to pass it to TestConn::with_context()
  • ClientConfig struct removed; use the client_config() function or RuntimelessClientConfig directly
  • SpawnHandle<F> removed; background task handles are now DroppableFuture from trillium-server-common

Added

  • Introduce new testing approach described at TestHandler:
use trillium::{Conn, Status, conn_try};
use trillium_testing::TestServer;

async fn handler(mut conn: Conn) -> Conn {
    let Ok(request_body) = conn.request_body_string().await else {
        return conn.with_status(500).halt();
    };

    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")
    ]);
  • The assertion macros (assert_ok!, assert_status!, assert_not_handled!, etc.) and request builders are unchanged
  • Zero-dependency testing: when no runtime feature is enabled, RuntimelessRuntime, RuntimelessServer, and RuntimelessClientConfig provide fully in-memory test infrastructure without requiring tokio, smol, or async-std
  • with_runtime(|runtime| async { ... }) — test harness that injects a Runtime into the test closure, also usable as a test harness
  • TestConn::with_context(Arc<HttpContext>) — pass a server config (including shared state initialized by init) to a test connection

Added

  • (api) [breaking] make IoErrors respond with BadRequest
  • deprecate set_state for insert_state

Other

  • release