Struct trillium_testing::TestConn[][src]

pub struct TestConn(_);
Expand description

A wrapper around a trillium::Conn for testing

Stability note: this may be replaced by an extension trait at some point.

Implementations

constructs a new TestConn with the provided method, path, and body.

use trillium_testing::{prelude::*, TestConn};
let mut conn = TestConn::build("get", "/", "body");
assert_eq!(conn.method(), Method::Get);
assert_eq!(conn.path(), "/");
assert_eq!(conn.take_request_body_string(), "body");

chainable constructor to append a request header to the TestConn

use trillium_testing::TestConn;
let conn = TestConn::build("get", "/", "body")
    .with_request_header("some-header", "value");
assert_eq!(conn.headers().get_str("some-header"), Some("value"));

chainable constructor to replace the request body. this is useful when chaining with a trillium_testing::methods builder, as they do not provide a way to specify the body.

use trillium_testing::{methods::post, TestConn};
let mut conn = post("/").with_request_body("some body");
assert_eq!(conn.take_request_body_string(), "some body");

let mut conn = TestConn::build("post", "/", "some body")
    .with_request_body("once told me");
assert_eq!(conn.take_request_body_string(), "once told me");

sets the peer ip for this test conn

set the test conn to be secure

blocks on running this conn against a handler and finalizes response headers. also aliased as TestConn::on

use trillium_testing::prelude::*;

async fn handler(conn: Conn) -> Conn {
    conn.ok("hello trillium")
}

let conn = get("/").run(&handler);
assert_ok!(conn, "hello trillium", "content-length" => "14");

blocks on running this conn against a handler and finalizes response headers. also aliased as TestConn::run.

use trillium_testing::prelude::*;

async fn handler(conn: Conn) -> Conn {
    conn.ok("hello trillium")
}

let conn = get("/").on(&handler);
assert_ok!(conn, "hello trillium", "content-length" => "14");

Reads the response body to string and returns it, if set. This is used internally to assert_body which is the preferred interface

Reads the request body to string and returns it

Methods from Deref<Target = Conn>

returns the response status for this conn, if it has been set.

use trillium_testing::prelude::*;
let mut conn = get("/").on(&());
assert!(conn.status().is_none());
conn.set_status(200);
assert_eq!(conn.status().unwrap(), Status::Ok);

assigns a status to this response. see Conn::status for example usage

Sets the response body from any impl Into<Body>. Note that this does not set the response status or halted.

use trillium_testing::prelude::*;
let mut conn = get("/").on(&());
conn.set_body("hello");
assert_eq!(conn.response_len(), Some(5));

Removes the response body from the conn

use trillium_testing::prelude::*;
let mut conn = get("/").on(&());

conn.set_body("hello");
let mut body = conn.take_response_body().unwrap();
assert_eq!(body.len(), Some(5));
assert_eq!(conn.response_len(), None);

Attempts to retrieve a &T from the state set

use trillium_testing::prelude::*;

struct Hello;
let mut conn = get("/").on(&());
assert!(conn.state::<Hello>().is_none());
conn.set_state(Hello);
assert!(conn.state::<Hello>().is_some());

Attempts to retrieve a &mut T from the state set

Puts a new type into the state set. see Conn::state for an example. returns the previous instance of this type, if any

Removes a type from the state set and returns it, if present

Either returns the current &mut T from the state set, or inserts a new one with the provided default function and returns a mutable reference to it

returns a ReceivedBody that references this conn. the conn retains all data and holds the singular transport, but the ReceivedBody provides an interface to read body content

use trillium_testing::prelude::*;
let mut conn = get("/").with_request_body("request body").on(&());

let request_body = conn.request_body().await;
assert_eq!(request_body.content_length(), Some(12));
assert_eq!(request_body.read_string().await.unwrap(), "request body");

Convenience function to read the content of a request body as a String.

use trillium_testing::prelude::*;
let mut conn = get("/").with_request_body("request body").on(&());

assert_eq!(conn.request_body_string().await.unwrap(), "request body");

if there is a response body for this conn and it has a known fixed length, it is returned from this function

use trillium_testing::prelude::*;
let mut conn = get("/").on(&|conn: trillium::Conn| async move {
    conn.with_body("hello")
});

assert_eq!(conn.response_len(), Some(5));

returns the request method for this conn.

use trillium_testing::prelude::*;
let mut conn = get("/").on(&());

assert_eq!(conn.method(), Method::Get);

returns the request headers

stability note: this may become request_headers at some point

returns the mutable response headers

stability note: this may become response_headers at some point

returns the path for this request. note that this may not represent the entire http request path if running nested routers.

returns query part of the request path

use trillium_testing::prelude::*;
let conn = get("/a/b?c&d=e").on(&());
assert_eq!(conn.querystring(), "c&d=e");

let conn = get("/a/b").on(&());
assert_eq!(conn.querystring(), "");

sets the halted attribute of this conn. see Conn::halt.

use trillium_testing::prelude::*;
let mut conn = get("/").on(&());
assert!(!conn.is_halted());
conn.set_halted(true);
assert!(conn.is_halted());

retrieves the halted state of this conn. see Conn::halt.

predicate function to indicate whether the connection is secure. note that this does not necessarily indicate that the transport itself is secure, as it may indicate that trillium_http is behind a trusted reverse proxy that has terminated tls and provided appropriate headers to indicate this.

returns an immutable reference to the inner trillium_http::Conn. please open an issue if you need to do this in application code.

stability note: hopefully this can go away at some point, but for now is an escape hatch in case trillium_http::Conn presents interface that cannot be reached otherwise.

returns a mutable reference to the inner trillium_http::Conn. please open an issue if you need to do this in application code.

stability note: hopefully this can go away at some point, but for now is an escape hatch in case trillium_http::Conn presents interface that cannot be reached otherwise.

for router implementations. pushes a route segment onto the path

for router implementations. removes a route segment onto the path

Trait Implementations

Formats the value using the given formatter. Read more

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

Performs the conversion.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.