Skip to main content

TestServer

Struct TestServer 

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

Source

pub async fn new(handler: H) -> Self

Creates a new TestServer.

Note that this is async because it initializes the handler with Handler::init.

Source

pub fn new_blocking(handler: H) -> Self

construct a new TestServer and block on initialization

Source

pub fn build<M: TryInto<Method>>(&self, method: M, path: &str) -> ConnTest
where M::Error: Debug,

Build a new ConnTest

Source

pub fn shared_state<T: Send + Sync + 'static>(&self) -> Option<&T>

borrow from shared state configured by the handler

Source

pub fn assert_shared_state<T>(&self, expected: T) -> &Self
where T: Send + Sync + Debug + PartialEq + 'static,

assert that a given type is in shared state

Source

pub fn assert_shared_state_with<T, F>(&self, f: F) -> &Self
where T: Send + Sync + 'static, F: FnOnce(&T),

assert that a given type is in shared and make further assertions on it

Source

pub fn handler(&self) -> &H

Borrow the handler

Source

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")

Source

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")

Source

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")

Source

pub fn set_base(&mut self, base: impl IntoUrl) -> &mut Self

Set the url for this virtual server (eg pretend this server is running at https://example.com with .set_base("https://example.com")

Source

pub fn get(&self, path: &str) -> ConnTest

Builds a GET ConnTest for the given path.

Source

pub fn post(&self, path: &str) -> ConnTest

Builds a POST ConnTest for the given path.

Source

pub fn put(&self, path: &str) -> ConnTest

Builds a PUT ConnTest for the given path.

Source

pub fn delete(&self, path: &str) -> ConnTest

Builds a DELETE ConnTest for the given path.

Source

pub fn patch(&self, path: &str) -> ConnTest

Builds a PATCH ConnTest for the given path.

Trait Implementations§

Source§

impl<H: Clone> Clone for TestServer<H>

Source§

fn clone(&self) -> TestServer<H>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<H: Debug> Debug for TestServer<H>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<H> Freeze for TestServer<H>

§

impl<H> !RefUnwindSafe for TestServer<H>

§

impl<H> Send for TestServer<H>
where H: Sync + Send,

§

impl<H> Sync for TestServer<H>
where H: Sync + Send,

§

impl<H> !Unpin for TestServer<H>

§

impl<H> !UnsafeUnpin for TestServer<H>

§

impl<H> !UnwindSafe for TestServer<H>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.