Skip to main content

ConnTest

Struct ConnTest 

Source
pub struct ConnTest { /* private fields */ }
Expand description

Represents both an outbound HTTP request being built and the received HTTP response.

Before .await, use the request-building methods to configure the request. After .await, use the accessor and assertion methods to inspect the response.

The response body is read eagerly on .await, so all accessors are synchronous.

Implementations§

Source§

impl ConnTest

Request-building methods (use before .await)

Source

pub fn with_request_header( self, name: impl Into<HeaderName<'static>>, value: impl Into<HeaderValues>, ) -> Self

Inserts a request header, replacing any existing value for that header name.

Source

pub fn with_request_headers<HN, HV, I>(self, headers: I) -> Self
where I: IntoIterator<Item = (HN, HV)> + Send, HN: Into<HeaderName<'static>>, HV: Into<HeaderValues>,

Extends the request headers from an iterable of (name, value) pairs.

Source

pub fn without_request_header( self, name: impl Into<HeaderName<'static>>, ) -> Self

Removes a request header if present.

Source

pub fn with_body(self, body: impl Into<Body>) -> Self

Sets the request body.

Source

pub fn with_json_body(self, body: &impl Serialize) -> Self

Available on crate features sonic-rs or serde_json only.

Sets the request body to the given serializable, as well as setting content-type: application/json if not already set

Source

pub fn with_peer_ip(self, peer_ip: impl Into<IpAddr>) -> Self

Sets a test-double ip that represents the client’s ip, available to the server as peer ip.

Source

pub fn block(self) -> Self

Perform a blocking request

Source§

impl ConnTest

Response accessors and assertions (use after .await)

Source

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

Returns handler state of type T set on the conn during the request, if any.

Source

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

Asserts that handler state of type T was set and equals expected.

Source

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

Asserts that no handler state of type T was set on the conn during the request.

Source

pub fn status(&self) -> Status

Returns the response status code.

Panics if called before the request has been sent (i.e., before .await).

Source

pub fn body(&self) -> &str

Returns the response body as a &str.

Panics if no body was received from the server, or if the body is not a valid utf-8 string.

Source

pub fn body_bytes(&self) -> &[u8]

Returns the response body as a &str.

Panics if no body was received from the server

Source

pub fn response_headers(&self) -> &Headers

Returns the response headers.

Source

pub fn response_trailers(&self) -> Option<&Headers>

Returns the response headers.

Source

pub fn request_trailers(&self) -> Option<&Headers>

Returns the response headers.

Source

pub fn header<'a>(&self, name: impl Into<HeaderName<'a>>) -> Option<&str>

Returns the value of a response header by name, if present.

Source

pub fn trailer<'a>(&self, name: impl Into<HeaderName<'a>>) -> Option<&str>

Returns the value of a response trailer by name, if present.

Source

pub fn assert_status(&self, status: impl TryInto<Status>) -> &Self

Asserts that the response status equals expected.

Source

pub fn assert_ok(&self) -> &Self

Asserts that the response status is 200 OK.

Source

pub fn assert_body(&self, expected: &str) -> &Self

Asserts that the response body is a string that equals expected, ignoring trailing whitespace

Source

pub fn assert_body_contains(&self, pattern: &str) -> &Self

Asserts that the response body contains pattern.

Source

pub fn assert_header<'a, HV, HN>(&self, name: HN, expected: HV) -> &Self
where HeaderValues: PartialEq<HV>, HV: Debug, HN: Into<HeaderName<'a>>,

Asserts that the response has a header name with value value.

Source

pub fn assert_headers<'a, I, HN, HV>(&self, headers: I) -> &Self
where I: IntoIterator<Item = (HN, HV)> + Send, HN: Into<HeaderName<'a>>, HV: Debug, HeaderValues: PartialEq<HV>,

Asserts that the response has a header name with value value.

Source

pub fn assert_no_header(&self, name: &str) -> &Self

Asserts that the response has no header named name.

Source

pub fn assert_header_with<'a, F>( &self, name: impl Into<HeaderName<'a>>, f: F, ) -> &Self
where F: FnOnce(&HeaderValues),

Asserts that a header with the given name exists and runs the provided closure with its value.

Source

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

Asserts that handler state of type T was set and runs the provided closure with it.

Source

pub fn assert_body_with<F>(&self, f: F) -> &Self
where F: FnOnce(&str),

Runs the provided closure with the response body.

Source

pub fn assert_json_body_with<T, F>(&self, f: F) -> &Self
where T: DeserializeOwned, F: FnOnce(&T),

Available on crate features sonic-rs or serde_json only.

Parses the response body as JSON and runs the provided closure with the parsed value.

Source

pub fn assert_json_body<T>(&self, body: &T) -> &Self

Available on crate features sonic-rs or serde_json only.

Parses the response body as JSON and runs the provided closure with the parsed value.

Source

pub fn assert_trailer<'a, HV, HN>(&self, name: HN, expected: HV) -> &Self
where HeaderValues: PartialEq<HV>, HV: Debug, HN: Into<HeaderName<'a>>,

Asserts that the response has a trailer name with value value.

Source

pub fn assert_trailers<'a, I, HN, HV>(&self, trailers: I) -> &Self
where I: IntoIterator<Item = (HN, HV)> + Send, HN: Into<HeaderName<'a>>, HV: Debug, HeaderValues: PartialEq<HV>,

Asserts that the response has a trailer name with value value.

Source

pub fn assert_no_trailer(&self, name: &str) -> &Self

Asserts that the response has no trailer named name.

Source

pub fn assert_trailer_with<'a, F>( &self, name: impl Into<HeaderName<'a>>, f: F, ) -> &Self
where F: FnOnce(&HeaderValues),

Asserts that a trailer with the given name exists and runs the provided closure with its value.

Trait Implementations§

Source§

impl Debug for ConnTest

Source§

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

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

impl IntoFuture for ConnTest

Source§

type IntoFuture = Pin<Box<dyn Future<Output = ConnTest> + Send>>

Which kind of future are we turning this into?
Source§

type Output = ConnTest

The output that the future will produce on completion.
Source§

fn into_future(self) -> Self::IntoFuture

Creates a future from a value. Read more

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