use axum::http::StatusCode;
use bytes::Bytes;
#[derive(Debug)]
pub struct TestResponse {
status: StatusCode,
headers: http::HeaderMap,
body: Bytes,
}
impl TestResponse {
pub(crate) const fn new(status: StatusCode, headers: http::HeaderMap, body: Bytes) -> Self {
Self {
status,
headers,
body,
}
}
pub const fn status(&self) -> StatusCode {
self.status
}
pub const fn headers(&self) -> &http::HeaderMap {
&self.headers
}
pub const fn bytes(&self) -> &Bytes {
&self.body
}
pub fn text(&self) -> &str {
std::str::from_utf8(&self.body).expect("response body is not valid UTF-8")
}
pub fn json<T: serde::de::DeserializeOwned>(&self) -> T {
serde_json::from_slice(&self.body).expect("response body is not valid JSON")
}
pub fn json_value(&self) -> serde_json::Value {
self.json()
}
pub fn assert_status(&self, expected: StatusCode) {
assert_eq!(
self.status,
expected,
"Expected status {expected}, got {}. Body: {}",
self.status,
String::from_utf8_lossy(&self.body)
);
}
pub fn assert_json_error(&self, expected_status: StatusCode, expected_code: &str) {
self.assert_status(expected_status);
let json = self.json_value();
assert_eq!(
json["error"].as_str(),
Some(expected_code),
"Expected error code '{expected_code}', got {:?}. Full body: {json}",
json["error"]
);
assert!(
json["correlationId"].is_string(),
"Missing correlationId in error response. Full body: {json}"
);
}
}