pub struct TestResponse { /* private fields */ }Expand description
Captured in-memory HTTP response.
TestResponse owns the status, headers, and collected body returned by
crate::TestRequest::send. Assertion helpers panic with ordinary test
assertion failures, while try_json, text, and header_str expose
fallible parsing.
use http::StatusCode;
use serde_json::json;
let response = app.get("/health").send().await;
response.assert_status(StatusCode::OK);
assert_eq!(response.header_str("content-type")?.unwrap(), "application/json");
response.assert_json(json!({ "ok": true }));Implementations§
Source§impl TestResponse
impl TestResponse
Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
Returns the response status code.
Sourcepub fn header(&self, name: impl AsRef<str>) -> Option<&HeaderValue>
pub fn header(&self, name: impl AsRef<str>) -> Option<&HeaderValue>
Returns a response header by name.
Sourcepub fn header_str(
&self,
name: impl AsRef<str>,
) -> Result<Option<&str>, ToStrError>
pub fn header_str( &self, name: impl AsRef<str>, ) -> Result<Option<&str>, ToStrError>
Returns a response header as a UTF-8 string when present.
Sourcepub fn assert_status(&self, expected: StatusCode)
pub fn assert_status(&self, expected: StatusCode)
Asserts the response status code.
Sourcepub fn assert_header(&self, name: impl AsRef<str>, expected: &str)
pub fn assert_header(&self, name: impl AsRef<str>, expected: &str)
Asserts a response header as a UTF-8 string.
Sourcepub fn json<T>(&self) -> Twhere
T: DeserializeOwned,
pub fn json<T>(&self) -> Twhere
T: DeserializeOwned,
Decodes the response body as JSON.
Panics when the body is not valid JSON for T. Use Self::try_json
when asserting malformed responses.
Sourcepub fn try_json<T>(&self) -> Result<T>where
T: DeserializeOwned,
pub fn try_json<T>(&self) -> Result<T>where
T: DeserializeOwned,
Tries to decode the response body as JSON.
Sourcepub fn assert_text(self, expected: &str)
pub fn assert_text(self, expected: &str)
Asserts the response body as UTF-8 text.
This consumes the response so tests cannot accidentally assert against a body after moving it into another helper.
Sourcepub fn assert_json(self, expected: Value)
pub fn assert_json(self, expected: Value)
Asserts the response body as JSON.
This consumes the response and compares against a serde_json::Value.