Files
imphnen-backend-service/tests/src/common/response_helpers.rs
T
MythEclipse 9cecc8fa05 Refactor tests to improve response parsing and validation
- Introduced a new common module with response helper functions to streamline response parsing across tests.
- Updated role controller tests to assert success messages in responses for create, update, and delete operations.
- Enhanced role service tests to validate response structures and ensure proper error handling.
- Modified team controller and service tests to check for success messages and validate response data formats.
- Improved user controller and service tests to include success message assertions and error handling for invalid inputs.
- Ensured all tests utilize the new response parsing functions for consistency and maintainability.
2025-10-05 16:00:46 +07:00

28 lines
1.1 KiB
Rust

use axum::body::Body;
use axum::http::Response;
use axum::body::Bytes;
use serde::de::DeserializeOwned;
/// Parse a response body into a serde_json::Value
pub async fn parse_response_value(resp: Response<Body>, limit: usize) -> serde_json::Value {
let bytes: Bytes = axum::body::to_bytes(resp.into_body(), limit).await.unwrap();
serde_json::from_slice(&bytes).unwrap()
}
/// Parse a response body into a typed DTO
pub async fn parse_response<T: DeserializeOwned>(resp: Response<Body>, limit: usize) -> T {
let bytes: Bytes = axum::body::to_bytes(resp.into_body(), limit).await.unwrap();
serde_json::from_slice(&bytes).unwrap()
}
/// Convenience: parse and return the inner "data" field if the response uses the standard wrapper
pub async fn parse_response_data<T: DeserializeOwned>(resp: Response<Body>, limit: usize) -> T {
let v = parse_response_value(resp, limit).await;
// If response uses { "data": ... } wrapper, extract it; otherwise try to parse the whole body as T
if let Some(inner) = v.get("data") {
serde_json::from_value(inner.clone()).unwrap()
} else {
serde_json::from_value(v).unwrap()
}
}