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,26 +1,26 @@
---
paths:
- "mocks/**/*.{yml,yaml}"
- "mocks/**/*.json"
---
## YAML 配置规范
## JSON 配置规范
AI 在生成 Mock 规则时必须遵循以下格式。
### 字段说明
| 字段 | 类型 | 必填 | 说明 |
|------|------|:----:|------|
| id | string | 是 | 规则唯一标识 |
| request.method | string | 是 | HTTP 方法 (GET/POST/PUT/DELETE...) |
| request.path | string | 是 | 请求路径,精确匹配 |
| request.query_params | object | 否 | 查询参数匹配 |
| request.headers | object | 否 | 请求头匹配(大小写不敏感) |
| request.body | any | 否 | 请求体匹配,支持 JSON 对象或字符串 |
| response.status | number | 是 | HTTP 状态码 |
| response.headers | object | 否 | 响应头 |
| response.body | string | 是 | 响应体,支持 file:// 前缀 |
| settings.delay_ms | number | 否 | 延迟响应(毫秒) |
| 字段 | 类型 | 必填 | 说明 |
|----------------------|--------|:--:|----------------------------------|
| name | string | 是 | 规则唯一标识 |
| request.method | string | 是 | HTTP 方法 (GET/POST/PUT/DELETE...) |
| request.path | string | 是 | 请求路径,精确匹配 |
| request.query_params | object | 否 | 查询参数匹配 |
| request.headers | object | 否 | 请求头匹配(大小写不敏感) |
| request.body | any | 否 | 请求体匹配,支持 JSON 对象或字符串 |
| response.status | number | 是 | HTTP 状态码 |
| response.headers | object | 否 | 响应头 |
| response.body | string | 是 | 响应体,支持 file:// 前缀 |
| settings.delay_ms | number | 否 | 延迟响应(毫秒) |
### 匹配规则
@@ -30,58 +30,91 @@ AI 在生成 Mock 规则时必须遵循以下格式。
### 配置示例
**单接口模式**
**基础示例**
```yaml
id: "login"
request:
method: "POST"
path: "/api/login"
body: { "username": "test" }
response:
status: 200
body: '{"token": "xxx"}'
settings:
delay_ms: 100
```
**多接口模式(数组):**
```yaml
- id: "get-users"
request: { method: "GET", path: "/api/users" }
response: { status: 200, body: '{"users": []}' }
- id: "create-user"
request: { method: "POST", path: "/api/users" }
response: { status: 201, body: '{"id": 1}' }
```json
{
"name": "login",
"request": {
"method": "POST",
"path": "/v1/auth/login",
"body": {
"username": "user001",
"password": "password123"
}
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "{\"token\": \"xxx\"}"
},
"settings": {
"delay_ms": 100
}
}
```
**带查询参数和请求头:**
```yaml
id: "search-users"
request:
method: "GET"
path: "/api/users"
query_params: { "role": "admin" }
headers: { "Authorization": "Bearer token" }
response:
status: 200
headers: { "X-Total-Count": "100" }
body: '{"users": []}'
```json
{
"name": "search_users",
"request": {
"method": "GET",
"path": "/v1/users",
"query_params": {
"role": "admin"
},
"headers": {
"Authorization": "Bearer token"
}
},
"response": {
"status": 200,
"headers": {
"X-Total-Count": "100"
},
"body": "{\"users\": []}"
}
}
```
**文件响应:**
```yaml
id: "download-pdf"
request:
method: "GET"
path: "/api/download"
response:
status: 200
body: "file://./data/large-file.pdf"
```json
{
"name": "download_pdf",
"request": {
"method": "GET",
"path": "/v1/download"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/pdf"
},
"body": "file://./storage/data/report.pdf"
}
}
```
**字符串格式 Body兼容格式**
```json
{
"name": "string_body_example",
"request": {
"method": "POST",
"path": "/v1/api/test",
"body": "{\"username\":\"user001\",\"password\":\"password123\"}"
},
"response": {
"status": 200,
"body": "{\"success\": true}"
}
}
```
> `file://` 支持两种路径: