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)
impl ConnTest
Request-building methods (use before .await)
Sourcepub fn with_request_header(
self,
name: impl Into<HeaderName<'static>>,
value: impl Into<HeaderValues>,
) -> Self
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.
Sourcepub fn with_request_headers<HN, HV, I>(self, headers: I) -> Selfwhere
I: IntoIterator<Item = (HN, HV)> + Send,
HN: Into<HeaderName<'static>>,
HV: Into<HeaderValues>,
pub fn with_request_headers<HN, HV, I>(self, headers: I) -> Selfwhere
I: IntoIterator<Item = (HN, HV)> + Send,
HN: Into<HeaderName<'static>>,
HV: Into<HeaderValues>,
Extends the request headers from an iterable of (name, value) pairs.
Sourcepub fn without_request_header(
self,
name: impl Into<HeaderName<'static>>,
) -> Self
pub fn without_request_header( self, name: impl Into<HeaderName<'static>>, ) -> Self
Removes a request header if present.
Sourcepub fn with_json_body(self, body: &impl Serialize) -> Self
Available on crate features sonic-rs or serde_json only.
pub fn with_json_body(self, body: &impl Serialize) -> Self
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
Sourcepub fn with_peer_ip(self, peer_ip: impl Into<IpAddr>) -> Self
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§impl ConnTest
Response accessors and assertions (use after .await)
impl ConnTest
Response accessors and assertions (use after .await)
Sourcepub fn state<T: Send + Sync + 'static>(&self) -> Option<&T>
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.
Sourcepub fn assert_state<T>(&self, expected: T) -> &Self
pub fn assert_state<T>(&self, expected: T) -> &Self
Asserts that handler state of type T was set and equals expected.
Sourcepub fn assert_no_state<T>(&self) -> &Self
pub fn assert_no_state<T>(&self) -> &Self
Asserts that no handler state of type T was set on the conn during the request.
Sourcepub fn status(&self) -> Status
pub fn status(&self) -> Status
Returns the response status code.
Panics if called before the request has been sent (i.e., before .await).
Sourcepub fn body(&self) -> &str
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.
Sourcepub fn body_bytes(&self) -> &[u8] ⓘ
pub fn body_bytes(&self) -> &[u8] ⓘ
Returns the response body as a &str.
Panics if no body was received from the server
Sourcepub fn response_headers(&self) -> &Headers
pub fn response_headers(&self) -> &Headers
Returns the response headers.
Sourcepub fn response_trailers(&self) -> Option<&Headers>
pub fn response_trailers(&self) -> Option<&Headers>
Returns the response headers.
Sourcepub fn request_trailers(&self) -> Option<&Headers>
pub fn request_trailers(&self) -> Option<&Headers>
Returns the response headers.
Sourcepub fn header<'a>(&self, name: impl Into<HeaderName<'a>>) -> Option<&str>
pub fn header<'a>(&self, name: impl Into<HeaderName<'a>>) -> Option<&str>
Returns the value of a response header by name, if present.
Sourcepub fn trailer<'a>(&self, name: impl Into<HeaderName<'a>>) -> Option<&str>
pub fn trailer<'a>(&self, name: impl Into<HeaderName<'a>>) -> Option<&str>
Returns the value of a response trailer by name, if present.
Sourcepub fn assert_status(&self, status: impl TryInto<Status>) -> &Self
pub fn assert_status(&self, status: impl TryInto<Status>) -> &Self
Asserts that the response status equals expected.
Sourcepub fn assert_body(&self, expected: &str) -> &Self
pub fn assert_body(&self, expected: &str) -> &Self
Asserts that the response body is a string that equals expected, ignoring trailing
whitespace
Sourcepub fn assert_body_contains(&self, pattern: &str) -> &Self
pub fn assert_body_contains(&self, pattern: &str) -> &Self
Asserts that the response body contains pattern.
Sourcepub fn assert_header<'a, HV, HN>(&self, name: HN, expected: HV) -> &Self
pub fn assert_header<'a, HV, HN>(&self, name: HN, expected: HV) -> &Self
Asserts that the response has a header name with value value.
Sourcepub fn assert_headers<'a, I, HN, HV>(&self, headers: I) -> &Selfwhere
I: IntoIterator<Item = (HN, HV)> + Send,
HN: Into<HeaderName<'a>>,
HV: Debug,
HeaderValues: PartialEq<HV>,
pub fn assert_headers<'a, I, HN, HV>(&self, headers: I) -> &Selfwhere
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.
Sourcepub fn assert_no_header(&self, name: &str) -> &Self
pub fn assert_no_header(&self, name: &str) -> &Self
Asserts that the response has no header named name.
Sourcepub fn assert_header_with<'a, F>(
&self,
name: impl Into<HeaderName<'a>>,
f: F,
) -> &Selfwhere
F: FnOnce(&HeaderValues),
pub fn assert_header_with<'a, F>(
&self,
name: impl Into<HeaderName<'a>>,
f: F,
) -> &Selfwhere
F: FnOnce(&HeaderValues),
Asserts that a header with the given name exists and runs the provided closure with its value.
Sourcepub fn assert_state_with<T, F>(&self, f: F) -> &Self
pub fn assert_state_with<T, F>(&self, f: F) -> &Self
Asserts that handler state of type T was set and runs the provided closure with it.
Sourcepub fn assert_body_with<F>(&self, f: F) -> &Self
pub fn assert_body_with<F>(&self, f: F) -> &Self
Runs the provided closure with the response body.
Sourcepub fn assert_json_body_with<T, F>(&self, f: F) -> &Self
Available on crate features sonic-rs or serde_json only.
pub fn assert_json_body_with<T, F>(&self, f: F) -> &Self
sonic-rs or serde_json only.Parses the response body as JSON and runs the provided closure with the parsed value.
Sourcepub fn assert_json_body<T>(&self, body: &T) -> &Self
Available on crate features sonic-rs or serde_json only.
pub fn assert_json_body<T>(&self, body: &T) -> &Self
sonic-rs or serde_json only.Parses the response body as JSON and runs the provided closure with the parsed value.
Sourcepub fn assert_trailer<'a, HV, HN>(&self, name: HN, expected: HV) -> &Self
pub fn assert_trailer<'a, HV, HN>(&self, name: HN, expected: HV) -> &Self
Asserts that the response has a trailer name with value value.
Sourcepub fn assert_trailers<'a, I, HN, HV>(&self, trailers: I) -> &Selfwhere
I: IntoIterator<Item = (HN, HV)> + Send,
HN: Into<HeaderName<'a>>,
HV: Debug,
HeaderValues: PartialEq<HV>,
pub fn assert_trailers<'a, I, HN, HV>(&self, trailers: I) -> &Selfwhere
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.
Sourcepub fn assert_no_trailer(&self, name: &str) -> &Self
pub fn assert_no_trailer(&self, name: &str) -> &Self
Asserts that the response has no trailer named name.
Sourcepub fn assert_trailer_with<'a, F>(
&self,
name: impl Into<HeaderName<'a>>,
f: F,
) -> &Selfwhere
F: FnOnce(&HeaderValues),
pub fn assert_trailer_with<'a, F>(
&self,
name: impl Into<HeaderName<'a>>,
f: F,
) -> &Selfwhere
F: FnOnce(&HeaderValues),
Asserts that a trailer with the given name exists and runs the provided closure with its value.