feat: 增加request对body字段的支持

- 实现body字段对json语法,yaml语法的支持的支持
- 新增测试用例login_out.yaml
This commit is contained in:
2025-12-28 21:54:34 +08:00
parent 1775d3659d
commit 5f3269bad5
7 changed files with 193 additions and 79 deletions

View File

@@ -23,19 +23,37 @@ pub async fn mock_handler(
Query(params): Query<HashMap<String, String>>,
req: Request<Body>,
) -> impl IntoResponse {
let path = req.uri().path();
// let path = req.uri().path();
// 1. 【关键】将需要的数据克隆出来,断开与 req 的借用关系
let path = req.uri().path().to_string();
let method_str = method.as_str().to_string();
// 1. 将 Axum HeaderMap 转换为简单的 HashMap 供 Router 使用
// 2. 现在可以安全地消耗 req 了,因为上面没有指向 req 内部的引用了
let body_bytes = match axum::body::to_bytes(req.into_body(), 10 * 1024 * 1024).await {
Ok(bytes) => bytes,
Err(_) => {
return Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from("Read body error"))
.unwrap();
}
};
let incoming_json: Option<serde_json::Value> = serde_json::from_slice(&body_bytes).ok();
let mut req_headers = HashMap::new();
for (name, value) in headers.iter() {
if let Ok(v) = value.to_str() {
req_headers.insert(name.as_str().to_string(), v.to_string());
}
}
// 2. 执行匹配逻辑
if let Some(rule) = state.router.match_rule(method.as_str(), path, &params, &req_headers) {
if let Some(rule) =
state
.router
.match_rule(&method_str, &path, &params, &req_headers, &incoming_json)
{
// 3. 处理模拟延迟
if let Some(ref settings) = rule.settings {
if let Some(delay) = settings.delay_ms {
@@ -49,7 +67,6 @@ pub async fn mock_handler(
// 注入 YAML 定义的 Header
if let Some(ref h) = rule.response.headers {
for (k, v) in h {
// println!("{}:{}",k.clone(), v.clone());
response_builder = response_builder.header(k, v);
@@ -65,23 +82,26 @@ pub async fn mock_handler(
let body = Body::from_stream(stream);
response_builder.body(body).unwrap()
}
Err(_) => {
Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::from(format!("Mock Error: File not found at {}", file_path)))
.unwrap()
}
Err(_) => Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::from(format!(
"Mock Error: File not found at {}",
file_path
)))
.unwrap(),
}
} else {
// B. 内联模式:直接返回字符串内容
response_builder.body(Body::from(rule.response.body.clone())).unwrap()
response_builder
.body(Body::from(rule.response.body.clone()))
.unwrap()
}
} else {
println!("请求头{:?}",req_headers.clone());
println!("请求头{:?}", req_headers.clone());
// 匹配失败返回 404
Response::builder()
.status(StatusCode::NOT_FOUND)
.body(Body::from("No mock rule matched this request"))
.unwrap()
}
}
}