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
impl TestResponse
Sourcepub fn request_id(&self) -> u64
pub fn request_id(&self) -> u64
Returns the request ID for tracing.
Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
Returns the HTTP status code.
Sourcepub fn status_code(&self) -> u16
pub fn status_code(&self) -> u16
Returns the status code as a u16.
Sourcepub fn is_success(&self) -> bool
pub fn is_success(&self) -> bool
Checks if the status is successful (2xx).
Sourcepub fn is_redirect(&self) -> bool
pub fn is_redirect(&self) -> bool
Checks if the status is a redirect (3xx).
Sourcepub fn is_client_error(&self) -> bool
pub fn is_client_error(&self) -> bool
Checks if the status is a client error (4xx).
Sourcepub fn is_server_error(&self) -> bool
pub fn is_server_error(&self) -> bool
Checks if the status is a server error (5xx).
Sourcepub fn header(&self, name: &str) -> Option<&[u8]>
pub fn header(&self, name: &str) -> Option<&[u8]>
Returns a header value by name (case-insensitive).
Sourcepub fn header_str(&self, name: &str) -> Option<&str>
pub fn header_str(&self, name: &str) -> Option<&str>
Returns a header value as a string (case-insensitive).
Sourcepub fn content_type(&self) -> Option<&str>
pub fn content_type(&self) -> Option<&str>
Returns the Content-Type header value.
Sourcepub fn json<T: DeserializeOwned>(&self) -> Result<T, Error>
pub fn json<T: DeserializeOwned>(&self) -> Result<T, Error>
Sourcepub fn content_length(&self) -> usize
pub fn content_length(&self) -> usize
Returns the body length.
Sourcepub fn into_inner(self) -> Response
pub fn into_inner(self) -> Response
Returns the underlying response.
Sourcepub fn assert_status(&self, expected: StatusCode) -> &Self
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.
Sourcepub fn assert_status_code(&self, expected: u16) -> &Self
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.
Sourcepub fn assert_success(&self) -> &Self
pub fn assert_success(&self) -> &Self
Asserts that the response is successful (2xx).
§Panics
Panics if the status is not in the 2xx range.
Sourcepub fn assert_header(&self, name: &str, expected: &str) -> &Self
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.
Sourcepub fn assert_text(&self, expected: &str) -> &Self
pub fn assert_text(&self, expected: &str) -> &Self
Sourcepub fn assert_text_contains(&self, expected: &str) -> &Self
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.
Sourcepub fn assert_json<T>(&self, expected: &T) -> &Self
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.
Sourcepub fn assert_json_contains(&self, expected: &Value) -> &Self
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"}));Sourcepub fn assert_header_exists(&self, name: &str) -> &Self
pub fn assert_header_exists(&self, name: &str) -> &Self
Sourcepub fn assert_header_missing(&self, name: &str) -> &Self
pub fn assert_header_missing(&self, name: &str) -> &Self
Sourcepub fn assert_content_type_contains(&self, expected: &str) -> &Self
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.