Skip to main content

TestResponse

Struct TestResponse 

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

Response from a test request with assertion helpers.

TestResponse wraps a Response and provides convenient methods for accessing response data and making assertions in tests.

§Example

let response = client.get("/api/user").send();

assert_eq!(response.status(), StatusCode::OK);
assert!(response.header("content-type").contains("application/json"));

let user: User = response.json().unwrap();
assert_eq!(user.name, "Alice");

Implementations§

Source§

impl TestResponse

Source

pub fn request_id(&self) -> u64

Returns the request ID for tracing.

Source

pub fn status(&self) -> StatusCode

Returns the HTTP status code.

Source

pub fn status_code(&self) -> u16

Returns the status code as a u16.

Source

pub fn is_success(&self) -> bool

Checks if the status is successful (2xx).

Source

pub fn is_redirect(&self) -> bool

Checks if the status is a redirect (3xx).

Source

pub fn is_client_error(&self) -> bool

Checks if the status is a client error (4xx).

Source

pub fn is_server_error(&self) -> bool

Checks if the status is a server error (5xx).

Source

pub fn headers(&self) -> &[(String, Vec<u8>)]

Returns all headers.

Source

pub fn header(&self, name: &str) -> Option<&[u8]>

Returns a header value by name (case-insensitive).

Source

pub fn header_str(&self, name: &str) -> Option<&str>

Returns a header value as a string (case-insensitive).

Source

pub fn content_type(&self) -> Option<&str>

Returns the Content-Type header value.

Source

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

Returns the body as raw bytes.

Source

pub fn text(&self) -> &str

Returns the body as a UTF-8 string.

§Panics

Panics if the body is not valid UTF-8.

Source

pub fn text_opt(&self) -> Option<&str>

Tries to return the body as a UTF-8 string.

Source

pub fn json<T: DeserializeOwned>(&self) -> Result<T, Error>

Parses the body as JSON.

§Errors

Returns an error if the body cannot be parsed as the target type.

§Example
#[derive(Deserialize)]
struct User { name: String }

let user: User = response.json().unwrap();
Source

pub fn content_length(&self) -> usize

Returns the body length.

Source

pub fn into_inner(self) -> Response

Returns the underlying response.

Source

pub fn assert_status(&self, expected: StatusCode) -> &Self

Asserts that the status code equals the expected value.

§Panics

Panics with a descriptive message if the assertion fails.

Source

pub fn assert_status_code(&self, expected: u16) -> &Self

Asserts that the status code equals the expected u16 value.

§Panics

Panics with a descriptive message if the assertion fails.

Source

pub fn assert_success(&self) -> &Self

Asserts that the response is successful (2xx).

§Panics

Panics if the status is not in the 2xx range.

Source

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

Asserts that a header exists with the given value.

§Panics

Panics if the header doesn’t exist or doesn’t match.

Source

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

Asserts that the body equals the expected string.

§Panics

Panics if the body doesn’t match.

Source

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

Asserts that the body contains the expected substring.

§Panics

Panics if the body doesn’t contain the substring.

Source

pub fn assert_json<T>(&self, expected: &T) -> &Self

Asserts that the JSON body equals the expected value.

§Panics

Panics if parsing fails or the value doesn’t match.

Source

pub fn assert_json_contains(&self, expected: &Value) -> &Self

Asserts that the JSON body contains all fields from the expected value.

This performs partial matching: the actual response may contain additional fields not present in expected, but all fields in expected must be present in the actual response with matching values.

§Panics

Panics if parsing fails or partial matching fails.

§Example
// Response body: {"id": 1, "name": "Alice", "email": "alice@example.com"}
// This passes because all expected fields match:
response.assert_json_contains(&json!({"name": "Alice"}));
Source

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

Asserts that a header exists (regardless of value).

§Panics

Panics if the header doesn’t exist.

Source

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

Asserts that a header does not exist.

§Panics

Panics if the header exists.

Source

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

Asserts that the Content-Type header contains the expected value.

This is a convenience method that checks if the Content-Type header contains the given string (useful for checking media types ignoring charset).

§Panics

Panics if the Content-Type header doesn’t exist or doesn’t contain the expected value.

Trait Implementations§

Source§

impl Debug for TestResponse

Source§

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

Formats the value using the given formatter. 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> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ResponseProduces<T> for T