feat(mod): add response
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use axum::{Router, body::Body, routing::get};
|
||||
use common_telemetry::{
|
||||
error::AppError,
|
||||
response::AppResponse,
|
||||
telemetry::{self, TelemetryConfig},
|
||||
};
|
||||
use http::{Request, StatusCode};
|
||||
@@ -28,6 +29,19 @@ async fn handler_success() -> Result<String, AppError> {
|
||||
Ok("Success Data".to_string())
|
||||
}
|
||||
|
||||
async fn handler_success_wrapped() -> Result<AppResponse<String>, AppError> {
|
||||
Ok(AppResponse::ok("Success Data".to_string()))
|
||||
}
|
||||
|
||||
async fn handler_bad_request() -> Result<String, AppError> {
|
||||
Err(AppError::BadRequest("bad params".into()))
|
||||
}
|
||||
|
||||
#[cfg(feature = "with-validator")]
|
||||
async fn handler_validation_error() -> Result<String, AppError> {
|
||||
Err(AppError::ValidationError("field required".into()))
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_full_flow_error_and_logging() {
|
||||
println!(">>> [Step 0] Test Initializing...");
|
||||
@@ -55,7 +69,12 @@ async fn test_full_flow_error_and_logging() {
|
||||
let app = Router::new()
|
||||
.route("/access-expired", get(handler_access_expired))
|
||||
.route("/refresh-expired", get(handler_refresh_expired))
|
||||
.route("/success", get(handler_success));
|
||||
.route("/success", get(handler_success))
|
||||
.route("/success-wrapped", get(handler_success_wrapped))
|
||||
.route("/bad-request", get(handler_bad_request));
|
||||
|
||||
#[cfg(feature = "with-validator")]
|
||||
let app = app.route("/validation-error", get(handler_validation_error));
|
||||
|
||||
println!(">>> [Step 1] Running Case A: Access Token Expired...");
|
||||
// --- 测试用例 A: Access Token 过期 ---
|
||||
@@ -120,6 +139,71 @@ async fn test_full_flow_error_and_logging() {
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// --- 测试用例 D: 统一成功响应格式 ---
|
||||
println!(">>> [Step 4] Running Case D: Success Response Wrapper...");
|
||||
let response = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri("/success-wrapped")
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let body_bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
|
||||
.await
|
||||
.unwrap();
|
||||
let body: Value = serde_json::from_slice(&body_bytes).unwrap();
|
||||
assert_eq!(body["code"], 0);
|
||||
assert_eq!(body["message"], "Success");
|
||||
assert_eq!(body["data"], "Success Data");
|
||||
|
||||
// --- 测试用例 E: 错误响应 details 字段 ---
|
||||
println!(">>> [Step 5] Running Case E: Error Response Details...");
|
||||
let response = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri("/bad-request")
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
||||
let body_bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
|
||||
.await
|
||||
.unwrap();
|
||||
let body: Value = serde_json::from_slice(&body_bytes).unwrap();
|
||||
assert_eq!(body["code"], 30000);
|
||||
assert_eq!(body["details"], "bad params");
|
||||
|
||||
#[cfg(feature = "with-validator")]
|
||||
{
|
||||
let response = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri("/validation-error")
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
||||
let body_bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
|
||||
.await
|
||||
.unwrap();
|
||||
let body: Value = serde_json::from_slice(&body_bytes).unwrap();
|
||||
assert_eq!(body["code"], 30001);
|
||||
assert_eq!(body["details"], "field required");
|
||||
}
|
||||
|
||||
// 稍微等待异步日志写入磁盘
|
||||
println!(" Waiting for logs to flush...");
|
||||
tokio::time::sleep(Duration::from_millis(200)).await;
|
||||
|
||||
Reference in New Issue
Block a user