pub struct Response {
pub headers: HeaderMap,
pub status: StatusCode,
pub text: String,
pub url: Url,
}Expand description
HTTP response wrapper with enhanced testing capabilities.
This struct wraps HTTP response data and provides convenient methods for accessing response information in tests. All data is captured for logging and debugging purposes.
§Examples
ⓘ
use tanu::{check_eq, http::Client};
#[tanu::test]
async fn test_response() -> eyre::Result<()> {
let client = Client::new();
let response = client.get("https://api.example.com").send().await?;
// Check status
check_eq!(200, response.status().as_u16());
// Access headers
let content_type = response.headers().get("content-type");
// Parse JSON
let data: serde_json::Value = response.json().await?;
Ok(())
}Fields§
§headers: HeaderMap§status: StatusCode§text: String§url: UrlImplementations§
Source§impl Response
impl Response
Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
Sourcepub async fn json<T: DeserializeOwned>(self) -> Result<T, Error>
pub async fn json<T: DeserializeOwned>(self) -> Result<T, Error>
Consumes the response and deserializes the JSON body into the given type.
§Examples
ⓘ
// Parse as serde_json::Value
let data: serde_json::Value = response.json().await?;
check_eq!("John", data["name"]);
// Parse into custom struct
#[derive(serde::Deserialize)]
struct User { name: String, id: u64 }
let user: User = response.json().await?;
check_eq!("John", user.name);Trait Implementations§
Auto Trait Implementations§
impl Freeze for Response
impl RefUnwindSafe for Response
impl Send for Response
impl Sync for Response
impl Unpin for Response
impl UnwindSafe for Response
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more