71 lines
1.8 KiB
Rust
71 lines
1.8 KiB
Rust
use {
|
|
crate::{serde::response::BaseResponse, serde::traits::DeserializeInto},
|
|
async_trait::async_trait,
|
|
axum::{
|
|
body::to_bytes,
|
|
response::{IntoResponse, Response},
|
|
Json,
|
|
},
|
|
serde::{
|
|
de::{DeserializeOwned, Error},
|
|
Serialize,
|
|
},
|
|
};
|
|
|
|
impl<T: Serialize> IntoResponse for BaseResponse<T> {
|
|
fn into_response(self) -> Response {
|
|
Json(self).into_response()
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl DeserializeInto for Response {
|
|
async fn deserialize_into<T: DeserializeOwned>(self) -> Result<T, serde_json::Error> {
|
|
let body = to_bytes(self.into_body(), usize::MAX).await.map_err(|e| {
|
|
serde_json::Error::custom(format!("Failed to read response body: {}", e))
|
|
})?;
|
|
serde_json::from_slice(&body)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use axum::http::header::CONTENT_TYPE;
|
|
use axum::http::{HeaderValue, StatusCode};
|
|
use axum::response::IntoResponse;
|
|
use mime::APPLICATION_JSON;
|
|
use serde::Serialize;
|
|
|
|
use crate::serde::response::BaseResponse;
|
|
|
|
#[derive(Serialize)]
|
|
struct Response {
|
|
message: String,
|
|
}
|
|
|
|
#[test]
|
|
fn test_into_response() {
|
|
let response = BaseResponse::new(
|
|
"",
|
|
Response {
|
|
message: "Hi".to_string(),
|
|
},
|
|
);
|
|
let json_response = response.into_response();
|
|
assert_eq!(json_response.status(), StatusCode::OK);
|
|
assert_eq!(
|
|
json_response.headers().get(CONTENT_TYPE),
|
|
Some(&HeaderValue::from_static(APPLICATION_JSON.as_ref()))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_into_response_with_primitive() {
|
|
let response = BaseResponse::new("", 42);
|
|
assert_eq!(
|
|
response.into_response().status(),
|
|
StatusCode::INTERNAL_SERVER_ERROR
|
|
);
|
|
}
|
|
}
|