feat: mock配置迁移至JSON格式并修复body匹配

- 将mock配置从YAML格式迁移到JSON格式
- 修复JSON字符串格式body匹配失败问题
- 添加MCP功能模块
- 更新mock-spec.md规范文档

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-29 09:43:11 +08:00
parent 061ceff4b8
commit d364307131
42 changed files with 1509 additions and 893 deletions

View File

@@ -1,35 +1,25 @@
use std::fs::{self, File};
use std::io::Write;
use tempfile::tempdir;
use mock_server::models::MockSource;
use mock_server::models::MockRule;
use mock_server::loader::MockLoader;
#[test]
fn test_config_deserialization() {
// 测试 1验证单接口 YAML 解析
let single_yaml = r#"
name: "auth_v1"
request:
method: "POST"
path: "/api/v1/login"
response:
status: 200
body: "inline_content"
"#;
let res: MockSource = serde_yaml::from_str(single_yaml).expect("应该成功解析单接口");
assert_eq!(res.flatten().len(), 1);
// 测试 2验证多接口 YAML 数组解析
let multi_yaml = r#"
- name: "api_1"
request: { method: "GET", path: "/1" }
response: { status: 200, body: "b1" }
- name: "api_2"
request: { method: "GET", path: "/2" }
response: { status: 200, body: "b2" }
"#;
let res_multi: MockSource = serde_yaml::from_str(multi_yaml).expect("应该成功解析接口数组");
assert_eq!(res_multi.flatten().len(), 2);
// 测试 1验证 JSON 解析
let single_json = r#"{
"name": "auth_v1",
"request": {
"method": "POST",
"path": "/api/v1/login"
},
"response": {
"status": 200,
"body": "inline_content"
}
}"#;
let res: MockRule = serde_json::from_str(single_json).expect("应该成功解析 JSON");
assert_eq!(res.name, "auth_v1");
}
#[test]
@@ -42,34 +32,39 @@ fn test_recursive_loading_logic() {
let user_dir = root_path.join("v1/user");
fs::create_dir_all(&user_dir).unwrap();
// 在深层目录创建单接口文件
let mut file1 = File::create(user_dir.join("get_profile.yaml")).unwrap();
// 在深层目录创建单接口 JSON 文件
let mut file1 = File::create(user_dir.join("get_profile.json")).unwrap();
writeln!(
file1,
r#"
name: "user_profile"
request:
method: "GET"
path: "/api/v1/user/profile"
response:
status: 200
body: "profile_data"
"#
r#"{{
"name": "user_profile",
"request": {{
"method": "GET",
"path": "/api/v1/user/profile"
}},
"response": {{
"status": 200,
"body": "profile_data"
}}
}}"#
)
.unwrap();
// 在根目录创建多接口文件
let mut file2 = File::create(root_path.join("system.yaml")).unwrap();
// 在根目录创建另一个 JSON 文件
let mut file2 = File::create(root_path.join("health.json")).unwrap();
writeln!(
file2,
r#"
- name: "sys_health"
request: {{ method: "GET", path: "/health" }}
response: {{ status: 200, body: "ok" }}
- name: "sys_version"
request: {{ method: "GET", path: "/version" }}
response: {{ status: 200, body: "1.0.0" }}
"#
r#"{{
"name": "sys_health",
"request": {{
"method": "GET",
"path": "/health"
}},
"response": {{
"status": 200,
"body": "ok"
}}
}}"#
)
.unwrap();
@@ -80,11 +75,10 @@ fn test_recursive_loading_logic() {
// 1. 检查 Key 是否根据路径首段正确提取
assert!(index.contains_key("api"), "索引应包含 'api' 键");
assert!(index.contains_key("health"), "索引应包含 'health' 键");
assert!(index.contains_key("version"), "索引应包含 'version' 键");
// 2. 检查规则总数
let total_rules: usize = index.values().map(|v| v.len()).sum();
assert_eq!(total_rules, 3, "总规则数应为 3");
assert_eq!(total_rules, 2, "总规则数应为 2");
// 3. 检查深层文件是否被正确读取
let api_rules = &index["api"];