1
0

feat: 增强 expect 规则系统,支持多种 body 校验方法和操作符

- 新增 body 分组校验:contains、regex、json(JSONPath)、css(CSS选择器)、xpath
- 新增操作符系统:equals、contains、match、empty、exists、gte、lte、gt、lt
- 新增 headers 响应头校验
- 引入 cheerio、xpath、@xmldom/xmldom 依赖
- BREAKING: expect.bodyContains 迁移至 expect.body.contains
This commit is contained in:
2026-05-10 00:10:42 +08:00
parent 57d3a5cfb4
commit 599d973cbd
22 changed files with 923 additions and 80 deletions

View File

@@ -73,6 +73,27 @@ targets:
expect:
status: [200]
maxLatencyMs: 5000
- name: "JSON API 监控"
url: "https://httpbin.org/json"
expect:
status: [200]
headers:
Content-Type: application/json
body:
contains: "slideshow"
json:
$.slideshow.title: "Sample Slide Show"
- name: "HTML 页面监控"
url: "https://httpbin.org/html"
expect:
status: [200]
body:
css:
"h1": "Herman Melville - Moby-Dick"
xpath:
"/html/body/h1/text()": "Herman Melville - Moby-Dick"
```
### 配置说明
@@ -93,20 +114,27 @@ targets:
- `interval``timeout`: 覆盖全局默认值
- `expect`: 期望校验
- `status`: 可接受的状态码列表
- `bodyContains`: 响应体包含的文本
- `headers`: 响应头校验(键值对,全部匹配)
- `maxLatencyMs`: 最大延迟阈值(毫秒)
- `body`: 响应体校验(可组合使用)
- `contains`: 响应体包含的文本
- `regex`: 响应体匹配的正则表达式
- `json`: JSONPath 提取值比较(路径 → 期望值)
- `css`: CSS 选择器提取 HTML 元素比较(选择器 → 期望值,可选 `attr` 提取属性)
- `xpath`: XPath 提取 XML/HTML 节点比较(路径 → 期望值)
- body 比较支持操作符:`equals`(默认)、`contains``match`(正则)、`empty``exists``gte``lte``gt``lt`
时长格式支持:`30s``5m``500ms`
## API 端点
| 端点 | 说明 |
|------|------|
| `GET /health` | 健康检查 |
| `GET /api/summary` | 总览统计total/up/down/avgLatencyMs/lastCheckTime |
| `GET /api/targets` | 目标列表及最新状态和统计摘要 |
| `GET /api/targets/:id/history?limit=20` | 指定目标的最近 N 条拨测记录 |
| `GET /api/targets/:id/trend?hours=24` | 指定目标的按小时聚合趋势 |
| 端点 | 说明 |
| --------------------------------------- | ---------------------------------------------------- |
| `GET /health` | 健康检查 |
| `GET /api/summary` | 总览统计total/up/down/avgLatencyMs/lastCheckTime |
| `GET /api/targets` | 目标列表及最新状态和统计摘要 |
| `GET /api/targets/:id/history?limit=20` | 指定目标的最近 N 条拨测记录 |
| `GET /api/targets/:id/trend?hours=24` | 指定目标的按小时聚合趋势 |
## 代码质量