pub struct TestServer<H> { /* private fields */ }Expand description
A testing interface that wraps a trillium handler, providing a high-level API for making requests and asserting on responses.
This runs a full request-response cycle against an in-memory virtual transport using
trillium-client. No ports are bound and the tests are fully
parallelizable.
A fluent set of assertions are provided that chain off of a borrow.
use test_harness::test;
use trillium::{Conn, Status};
use trillium_testing::{TestResult, TestServer, harness};
#[test(harness)]
async fn basic_test() {
let app = TestServer::new(|conn: Conn| async move { conn.ok("hello") }).await;
app.get("/").await.assert_ok().assert_body("hello");
// or if you prefer:
let conn = app.post("/").with_body("body").await;
conn.assert_ok();
conn.assert_body("hello");
}
// also an option, but not preferred:
#[test]
fn sync_test() {
let app = TestServer::new_blocking(|conn: Conn| async move { conn.ok("hello") });
app.get("/").block().assert_ok().assert_body("hello");
let conn = app.post("/").with_body("body").block();
conn.assert_ok();
conn.assert_body("hello");
}Implementations§
Source§impl<H: Handler> TestServer<H>
impl<H: Handler> TestServer<H>
Sourcepub async fn new(handler: H) -> Self
pub async fn new(handler: H) -> Self
Creates a new TestServer.
Note that this is async because it initializes the handler with Handler::init.
Sourcepub fn new_blocking(handler: H) -> Self
pub fn new_blocking(handler: H) -> Self
construct a new TestServer and block on initialization
Sourcepub fn build<M: TryInto<Method>>(&self, method: M, path: &str) -> ConnTest
pub fn build<M: TryInto<Method>>(&self, method: M, path: &str) -> ConnTest
Build a new ConnTest
borrow from shared state configured by the handler
assert that a given type is in shared state
assert that a given type is in shared and make further assertions on it
Sourcepub fn with_host(self, host: &str) -> Self
pub fn with_host(self, host: &str) -> Self
Add a default host/authority for this virtual server (eg pretend this server is running at
example.com with .with_host("example.com")
Sourcepub fn set_host(&mut self, host: &str) -> &mut Self
pub fn set_host(&mut self, host: &str) -> &mut Self
Set the default host/authority for this virtual server (eg pretend this server is running at
example.com with .set_host("example.com")
Sourcepub fn with_base(self, base: impl IntoUrl) -> Self
pub fn with_base(self, base: impl IntoUrl) -> Self
Set the url for this virtual server (eg pretend this server is running at
https://example.com with .with_base("https://example.com")
Trait Implementations§
Source§impl<H: Clone> Clone for TestServer<H>
impl<H: Clone> Clone for TestServer<H>
Source§fn clone(&self) -> TestServer<H>
fn clone(&self) -> TestServer<H>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more