pub struct UnexpectedStatusError(/* private fields */);
Expand description

An unexpected http status code was received. Transform this back into the conn with From::from/Into::into.

Currently only returned by Conn::success

Methods from Deref<Target = Conn>§

source

pub fn request_headers(&mut self) -> &mut Headers

retrieves a mutable borrow of the request headers, suitable for appending a header. generally, prefer using chainable methods on Conn

use trillium_testing::ClientConfig;
use trillium_client::Client;

let handler = |conn: trillium::Conn| async move {
    let header = conn.headers().get_str("some-request-header").unwrap_or_default();
    let response = format!("some-request-header was {}", header);
    conn.ok(response)
};

let client = Client::new(ClientConfig::new());

trillium_testing::with_server(handler, move |url| async move {
    let mut conn = client.get(url);

    conn.request_headers() //<-
        .insert("some-request-header", "header-value");

    (&mut conn).await?;

    assert_eq!(
        conn.response_body().read_string().await?,
        "some-request-header was header-value"
    );
    Ok(())
})
source

pub fn response_headers(&self) -> &Headers

let handler = |conn: trillium::Conn| async move {
    conn.with_header("some-header", "some-value")
        .with_status(200)
};

use trillium_client::Client;
use trillium_testing::ClientConfig;

trillium_testing::with_server(handler, move |url| async move {
    let client = Client::new(ClientConfig::new());
    let conn = client.get(url).await?;

    let headers = conn.response_headers(); //<-

    assert_eq!(headers.get_str("some-header"), Some("some-value"));
    Ok(())
})
source

pub fn response_headers_mut(&mut self) -> &mut Headers

get a mutable borrow of the response headers

source

pub fn set_request_body(&mut self, body: impl Into<Body>)

sets the request body on a mutable reference. prefer the chainable Conn::with_body wherever possible

env_logger::init();
use trillium_client::Client;
use trillium_testing::ClientConfig;


let handler = |mut conn: trillium::Conn| async move {
    let body = conn.request_body_string().await.unwrap();
    conn.ok(format!("request body was: {}", body))
};

trillium_testing::with_server(handler, move |url| async move {
    let client = Client::new(ClientConfig::new());
    let mut conn = client.post(url);

    conn.set_request_body("body"); //<-

    (&mut conn).await?;

    assert_eq!(conn.response_body().read_string().await?, "request body was: body");
    Ok(())
});
source

pub fn url(&self) -> &Url

retrieves the url for this conn.

use trillium_testing::ClientConfig;
use trillium_client::Client;
let client = Client::from(ClientConfig::new());
let conn = client.get("http://localhost:9080");

let url = conn.url(); //<-

assert_eq!(url.host_str().unwrap(), "localhost");
source

pub fn method(&self) -> Method

retrieves the url for this conn.

use trillium_testing::ClientConfig;
use trillium_client::Client;

use trillium_testing::prelude::*;

let client = Client::from(ClientConfig::new());
let conn = client.get("http://localhost:9080");

let method = conn.method(); //<-

assert_eq!(method, Method::Get);
source

pub fn response_body(&mut self) -> ReceivedBody<'_, BoxedTransport>

returns a [ReceivedBody] that borrows the connection inside this conn.

env_logger::init();
use trillium_testing::ClientConfig;
use trillium_client::Client;



let handler = |mut conn: trillium::Conn| async move {
    conn.ok("hello from trillium")
};

trillium_testing::with_server(handler, |url| async move {
    let client = Client::from(ClientConfig::new());
    let mut conn = client.get(url).await?;

    let response_body = conn.response_body(); //<-

    assert_eq!(19, response_body.content_length().unwrap());
    let string = response_body.read_string().await?;
    assert_eq!("hello from trillium", string);
    Ok(())
});
source

pub fn status(&self) -> Option<Status>

returns the status code for this conn. if the conn has not yet been sent, this will be None.

use trillium_testing::ClientConfig;
use trillium_client::Client;
use trillium_testing::prelude::*;

async fn handler(conn: trillium::Conn) -> trillium::Conn {
    conn.with_status(418)
}

trillium_testing::with_server(handler, |url| async move {
    let client = Client::new(ClientConfig::new());
    let conn = client.get(url).await?;
    assert_eq!(Status::ImATeapot, conn.status().unwrap());
    Ok(())
});
source

pub fn peer_addr(&self) -> Option<SocketAddr>

attempts to retrieve the connected peer address

Trait Implementations§

source§

impl Debug for UnexpectedStatusError

source§

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

Formats the value using the given formatter. Read more
source§

impl Deref for UnexpectedStatusError

§

type Target = Conn

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl DerefMut for UnexpectedStatusError

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl Display for UnexpectedStatusError

source§

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

Formats the value using the given formatter. Read more
source§

impl Error for UnexpectedStatusError

1.30.0 · source§

fn source(&self) -> Option<&(dyn Error + 'static)>

The lower-level source of this error, if any. Read more
1.0.0 · source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type based access to context intended for error reports. Read more
source§

impl From<Conn> for UnexpectedStatusError

source§

fn from(value: Conn) -> Self

Converts to this type from the input type.
source§

impl From<UnexpectedStatusError> for Conn

source§

fn from(value: UnexpectedStatusError) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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> 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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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

§

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.