- 将mock配置从YAML格式迁移到JSON格式 - 修复JSON字符串格式body匹配失败问题 - 添加MCP功能模块 - 更新mock-spec.md规范文档 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
87 lines
2.4 KiB
Rust
87 lines
2.4 KiB
Rust
use std::fs::{self, File};
|
||
use std::io::Write;
|
||
use tempfile::tempdir;
|
||
use mock_server::models::MockRule;
|
||
use mock_server::loader::MockLoader;
|
||
|
||
#[test]
|
||
fn test_config_deserialization() {
|
||
// 测试 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]
|
||
fn test_recursive_loading_logic() {
|
||
// 创建临时 Mock 目录结构
|
||
let root_dir = tempdir().expect("创建临时目录失败");
|
||
let root_path = root_dir.path();
|
||
|
||
// 构造物理层级:mocks/v1/user/
|
||
let user_dir = root_path.join("v1/user");
|
||
fs::create_dir_all(&user_dir).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"
|
||
}}
|
||
}}"#
|
||
)
|
||
.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"
|
||
}}
|
||
}}"#
|
||
)
|
||
.unwrap();
|
||
|
||
// 执行加载
|
||
let index = MockLoader::load_all_from_dir(root_path);
|
||
|
||
// 断言结果:
|
||
// 1. 检查 Key 是否根据路径首段正确提取
|
||
assert!(index.contains_key("api"), "索引应包含 'api' 键");
|
||
assert!(index.contains_key("health"), "索引应包含 'health' 键");
|
||
|
||
// 2. 检查规则总数
|
||
let total_rules: usize = index.values().map(|v| v.len()).sum();
|
||
assert_eq!(total_rules, 2, "总规则数应为 2");
|
||
|
||
// 3. 检查深层文件是否被正确读取
|
||
let api_rules = &index["api"];
|
||
assert_eq!(api_rules[0].name, "user_profile");
|
||
}
|