Files
mock-server/tests/integration_test.rs
CNWei d364307131 feat: mock配置迁移至JSON格式并修复body匹配
- 将mock配置从YAML格式迁移到JSON格式
- 修复JSON字符串格式body匹配失败问题
- 添加MCP功能模块
- 更新mock-spec.md规范文档

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-29 09:43:11 +08:00

87 lines
2.4 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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");
}