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:
@@ -141,7 +141,9 @@ describe("API 路由", () => {
|
||||
|
||||
test("无效 limit 参数返回 400", async () => {
|
||||
const targets = store.getTargets();
|
||||
const response = await fetchHandler(new Request(`http://localhost/api/targets/${targets[0]!.id}/history?limit=abc`));
|
||||
const response = await fetchHandler(
|
||||
new Request(`http://localhost/api/targets/${targets[0]!.id}/history?limit=abc`),
|
||||
);
|
||||
const body = await response.json();
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
|
||||
230
tests/server/checker/body-expect.test.ts
Normal file
230
tests/server/checker/body-expect.test.ts
Normal file
@@ -0,0 +1,230 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { applyOperator, checkBodyExpect, evaluateJsonPath } from "../../../src/server/checker/body-expect";
|
||||
|
||||
describe("evaluateJsonPath", () => {
|
||||
const obj = {
|
||||
status: "ok",
|
||||
code: 0,
|
||||
active: true,
|
||||
error: null,
|
||||
data: {
|
||||
count: 42,
|
||||
items: [{ name: "a" }, { name: "b" }],
|
||||
nested: { deep: "value" },
|
||||
},
|
||||
emptyObj: {},
|
||||
emptyArr: [],
|
||||
};
|
||||
|
||||
test("简单字段访问", () => {
|
||||
expect(evaluateJsonPath(obj, "$.status")).toBe("ok");
|
||||
expect(evaluateJsonPath(obj, "$.code")).toBe(0);
|
||||
expect(evaluateJsonPath(obj, "$.active")).toBe(true);
|
||||
expect(evaluateJsonPath(obj, "$.error")).toBeNull();
|
||||
});
|
||||
|
||||
test("嵌套对象访问", () => {
|
||||
expect(evaluateJsonPath(obj, "$.data.count")).toBe(42);
|
||||
expect(evaluateJsonPath(obj, "$.data.nested.deep")).toBe("value");
|
||||
});
|
||||
|
||||
test("数组索引访问", () => {
|
||||
expect(evaluateJsonPath(obj, "$.data.items[0].name")).toBe("a");
|
||||
expect(evaluateJsonPath(obj, "$.data.items[1].name")).toBe("b");
|
||||
});
|
||||
|
||||
test("路径不存在返回 undefined", () => {
|
||||
expect(evaluateJsonPath(obj, "$.notExist")).toBeUndefined();
|
||||
expect(evaluateJsonPath(obj, "$.data.notExist")).toBeUndefined();
|
||||
expect(evaluateJsonPath(obj, "$.data.items[99]")).toBeUndefined();
|
||||
});
|
||||
|
||||
test("空对象和空数组", () => {
|
||||
expect(evaluateJsonPath(obj, "$.emptyObj")).toEqual({});
|
||||
expect(evaluateJsonPath(obj, "$.emptyArr")).toEqual([]);
|
||||
});
|
||||
|
||||
test("非 $ 开头路径返回 undefined", () => {
|
||||
expect(evaluateJsonPath(obj, "status")).toBeUndefined();
|
||||
expect(evaluateJsonPath(obj, ".status")).toBeUndefined();
|
||||
});
|
||||
|
||||
test("null 对象上访问", () => {
|
||||
expect(evaluateJsonPath(null, "$.any")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("applyOperator", () => {
|
||||
test("equals 操作符", () => {
|
||||
expect(applyOperator("ok", { equals: "ok" })).toBe(true);
|
||||
expect(applyOperator("ok", { equals: "error" })).toBe(false);
|
||||
expect(applyOperator(42, { equals: 42 })).toBe(true);
|
||||
expect(applyOperator(42, { equals: 41 })).toBe(false);
|
||||
expect(applyOperator(null, { equals: null })).toBe(true);
|
||||
expect(applyOperator(true, { equals: true })).toBe(true);
|
||||
});
|
||||
|
||||
test("contains 操作符", () => {
|
||||
expect(applyOperator("hello world", { contains: "hello" })).toBe(true);
|
||||
expect(applyOperator("hello world", { contains: "missing" })).toBe(false);
|
||||
expect(applyOperator(12345, { contains: "23" })).toBe(true);
|
||||
});
|
||||
|
||||
test("match 操作符", () => {
|
||||
expect(applyOperator("v2.1.0", { match: "\\d+\\.\\d+\\.\\d+" })).toBe(true);
|
||||
expect(applyOperator("v2.1", { match: "\\d+\\.\\d+\\.\\d+" })).toBe(false);
|
||||
expect(applyOperator("abc123", { match: "^\\w+\\d+$" })).toBe(true);
|
||||
});
|
||||
|
||||
test("empty 操作符", () => {
|
||||
expect(applyOperator("", { empty: true })).toBe(true);
|
||||
expect(applyOperator(null, { empty: true })).toBe(true);
|
||||
expect(applyOperator(undefined, { empty: true })).toBe(true);
|
||||
expect(applyOperator([], { empty: true })).toBe(true);
|
||||
expect(applyOperator({}, { empty: true })).toBe(true);
|
||||
expect(applyOperator("ok", { empty: true })).toBe(false);
|
||||
expect(applyOperator([1, 2], { empty: false })).toBe(true);
|
||||
expect(applyOperator([], { empty: false })).toBe(false);
|
||||
});
|
||||
|
||||
test("exists 操作符", () => {
|
||||
expect(applyOperator("ok", { exists: true })).toBe(true);
|
||||
expect(applyOperator(null, { exists: true })).toBe(true);
|
||||
expect(applyOperator(undefined, { exists: true })).toBe(false);
|
||||
expect(applyOperator(undefined, { exists: false })).toBe(true);
|
||||
expect(applyOperator("ok", { exists: false })).toBe(false);
|
||||
});
|
||||
|
||||
test("gte 操作符", () => {
|
||||
expect(applyOperator(10, { gte: 5 })).toBe(true);
|
||||
expect(applyOperator(5, { gte: 5 })).toBe(true);
|
||||
expect(applyOperator(3, { gte: 5 })).toBe(false);
|
||||
expect(applyOperator("10", { gte: 5 })).toBe(true);
|
||||
});
|
||||
|
||||
test("lte 操作符", () => {
|
||||
expect(applyOperator(3, { lte: 5 })).toBe(true);
|
||||
expect(applyOperator(5, { lte: 5 })).toBe(true);
|
||||
expect(applyOperator(10, { lte: 5 })).toBe(false);
|
||||
});
|
||||
|
||||
test("gt 操作符", () => {
|
||||
expect(applyOperator(10, { gt: 5 })).toBe(true);
|
||||
expect(applyOperator(5, { gt: 5 })).toBe(false);
|
||||
});
|
||||
|
||||
test("lt 操作符", () => {
|
||||
expect(applyOperator(3, { lt: 5 })).toBe(true);
|
||||
expect(applyOperator(5, { lt: 5 })).toBe(false);
|
||||
});
|
||||
|
||||
test("多操作符 AND 组合", () => {
|
||||
expect(applyOperator(7, { gte: 5, lte: 10 })).toBe(true);
|
||||
expect(applyOperator(3, { gte: 5, lte: 10 })).toBe(false);
|
||||
expect(applyOperator(15, { gte: 5, lte: 10 })).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("checkBodyExpect", () => {
|
||||
test("无 body config 返回 true", () => {
|
||||
expect(checkBodyExpect("anything", undefined)).toBe(true);
|
||||
});
|
||||
|
||||
test("contains 匹配", () => {
|
||||
expect(checkBodyExpect("hello world", { contains: "hello" })).toBe(true);
|
||||
expect(checkBodyExpect("hello world", { contains: "missing" })).toBe(false);
|
||||
});
|
||||
|
||||
test("regex 匹配", () => {
|
||||
expect(checkBodyExpect("status: ok", { regex: "ok" })).toBe(true);
|
||||
expect(checkBodyExpect("status: error", { regex: "ok" })).toBe(false);
|
||||
});
|
||||
|
||||
test("json 简单等值匹配", () => {
|
||||
const body = JSON.stringify({ status: "ok", code: 0 });
|
||||
expect(checkBodyExpect(body, { json: { "$.status": "ok" } })).toBe(true);
|
||||
expect(checkBodyExpect(body, { json: { "$.code": 0 } })).toBe(true);
|
||||
expect(checkBodyExpect(body, { json: { "$.status": "error" } })).toBe(false);
|
||||
});
|
||||
|
||||
test("json 操作符匹配", () => {
|
||||
const body = JSON.stringify({ count: 42, version: "v2.1.0", message: "success" });
|
||||
expect(checkBodyExpect(body, { json: { "$.count": { gte: 10 } } })).toBe(true);
|
||||
expect(checkBodyExpect(body, { json: { "$.version": { match: "\\d+\\.\\d+\\.\\d+" } } })).toBe(true);
|
||||
expect(checkBodyExpect(body, { json: { "$.message": { contains: "success" } } })).toBe(true);
|
||||
expect(checkBodyExpect(body, { json: { "$.count": { gte: 100 } } })).toBe(false);
|
||||
});
|
||||
|
||||
test("json 路径不存在", () => {
|
||||
const body = JSON.stringify({ status: "ok" });
|
||||
expect(checkBodyExpect(body, { json: { "$.notExist": "value" } })).toBe(false);
|
||||
});
|
||||
|
||||
test("json 解析失败", () => {
|
||||
expect(checkBodyExpect("not json", { json: { "$.status": "ok" } })).toBe(false);
|
||||
});
|
||||
|
||||
test("css textContent 匹配", () => {
|
||||
const html = "<div id='health'>OK</div><span class='ver'>1.0</span>";
|
||||
expect(checkBodyExpect(html, { css: { "div#health": "OK" } })).toBe(true);
|
||||
expect(checkBodyExpect(html, { css: { "span.ver": "1.0" } })).toBe(true);
|
||||
expect(checkBodyExpect(html, { css: { "div#health": "ERROR" } })).toBe(false);
|
||||
});
|
||||
|
||||
test("css 选择器无匹配元素", () => {
|
||||
const html = "<div>OK</div>";
|
||||
expect(checkBodyExpect(html, { css: { "span.missing": "OK" } })).toBe(false);
|
||||
});
|
||||
|
||||
test("css attr 提取", () => {
|
||||
const html = '<meta name="version" content="2.0.1"><link rel="icon" href="/favicon.ico">';
|
||||
expect(checkBodyExpect(html, { css: { 'meta[name="version"]': { attr: "content", equals: "2.0.1" } } })).toBe(true);
|
||||
expect(
|
||||
checkBodyExpect(html, { css: { 'meta[name="version"]': { attr: "content", match: "\\d+\\.\\d+\\.\\d+" } } }),
|
||||
).toBe(true);
|
||||
expect(checkBodyExpect(html, { css: { 'link[rel="icon"]': { attr: "href", contains: "favicon" } } })).toBe(true);
|
||||
});
|
||||
|
||||
test("css exists 检查", () => {
|
||||
const html = "<div id='test'>OK</div>";
|
||||
expect(checkBodyExpect(html, { css: { "div#test": { exists: true } } })).toBe(true);
|
||||
expect(checkBodyExpect(html, { css: { "span#missing": { exists: false } } })).toBe(true);
|
||||
expect(checkBodyExpect(html, { css: { "div#test": { exists: false } } })).toBe(false);
|
||||
});
|
||||
|
||||
test("xpath 节点文本匹配", () => {
|
||||
const xml = "<root><status>ok</status><code>200</code></root>";
|
||||
expect(checkBodyExpect(xml, { xpath: { "/root/status/text()": "ok" } })).toBe(true);
|
||||
expect(checkBodyExpect(xml, { xpath: { "/root/status/text()": "error" } })).toBe(false);
|
||||
});
|
||||
|
||||
test("xpath 无匹配节点", () => {
|
||||
const xml = "<root><status>ok</status></root>";
|
||||
expect(checkBodyExpect(xml, { xpath: { "/root/missing/text()": "ok" } })).toBe(false);
|
||||
});
|
||||
|
||||
test("xpath 包含匹配", () => {
|
||||
const html = "<html><body><div id='msg'>success</div></body></html>";
|
||||
expect(checkBodyExpect(html, { xpath: { "//div[@id='msg']/text()": "success" } })).toBe(true);
|
||||
});
|
||||
|
||||
test("多种 body 方法 AND 组合", () => {
|
||||
const body = JSON.stringify({ status: "healthy", count: 5 });
|
||||
expect(
|
||||
checkBodyExpect(body, {
|
||||
contains: "healthy",
|
||||
json: { "$.status": "healthy", "$.count": { gte: 1 } },
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
test("多种 body 方法部分失败", () => {
|
||||
const body = JSON.stringify({ status: "error" });
|
||||
expect(
|
||||
checkBodyExpect(body, {
|
||||
contains: "healthy",
|
||||
json: { "$.status": "error" },
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -215,7 +215,8 @@ targets:
|
||||
url: "http://example.com"
|
||||
expect:
|
||||
status: [200, 201]
|
||||
bodyContains: "ok"
|
||||
body:
|
||||
contains: "ok"
|
||||
maxLatencyMs: 3000
|
||||
`,
|
||||
);
|
||||
@@ -223,7 +224,7 @@ targets:
|
||||
const config = await loadConfig(configPath);
|
||||
expect(config.targets[0]!.expect).toEqual({
|
||||
status: [200, 201],
|
||||
bodyContains: "ok",
|
||||
body: { contains: "ok" },
|
||||
maxLatencyMs: 3000,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -56,7 +56,9 @@ describe("ProbeEngine", () => {
|
||||
test("单次拨测写入数据库", async () => {
|
||||
const engine = new ProbeEngine(store, [target]);
|
||||
// 手动调用 probeGroup 不启动 timer
|
||||
const probeGroup = (engine as unknown as { probeGroup: (t: ResolvedTarget[]) => Promise<void> }).probeGroup.bind(engine);
|
||||
const probeGroup = (engine as unknown as { probeGroup: (t: ResolvedTarget[]) => Promise<void> }).probeGroup.bind(
|
||||
engine,
|
||||
);
|
||||
await probeGroup([target]);
|
||||
|
||||
const dbTargets = store.getTargets();
|
||||
@@ -78,7 +80,9 @@ describe("ProbeEngine", () => {
|
||||
store.syncTargets([target, badTarget]);
|
||||
|
||||
const engine = new ProbeEngine(store, [target, badTarget]);
|
||||
const probeGroup = (engine as unknown as { probeGroup: (t: ResolvedTarget[]) => Promise<void> }).probeGroup.bind(engine);
|
||||
const probeGroup = (engine as unknown as { probeGroup: (t: ResolvedTarget[]) => Promise<void> }).probeGroup.bind(
|
||||
engine,
|
||||
);
|
||||
await probeGroup([target, badTarget]);
|
||||
|
||||
const dbTargets = store.getTargets();
|
||||
|
||||
@@ -1,33 +1,82 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { checkExpect } from "../../../src/server/checker/fetcher";
|
||||
|
||||
const emptyHeaders: Record<string, string> = {};
|
||||
|
||||
describe("checkExpect", () => {
|
||||
test("无 expect 配置时 matched 为 true", () => {
|
||||
expect(checkExpect(200, "ok", 100, undefined)).toBe(true);
|
||||
expect(checkExpect(200, "ok", 100, emptyHeaders, undefined)).toBe(true);
|
||||
});
|
||||
|
||||
test("status 匹配", () => {
|
||||
expect(checkExpect(200, "", 100, { status: [200, 201] })).toBe(true);
|
||||
expect(checkExpect(201, "", 100, { status: [200, 201] })).toBe(true);
|
||||
expect(checkExpect(404, "", 100, { status: [200, 201] })).toBe(false);
|
||||
expect(checkExpect(200, "", 100, emptyHeaders, { status: [200, 201] })).toBe(true);
|
||||
expect(checkExpect(201, "", 100, emptyHeaders, { status: [200, 201] })).toBe(true);
|
||||
expect(checkExpect(404, "", 100, emptyHeaders, { status: [200, 201] })).toBe(false);
|
||||
});
|
||||
|
||||
test("bodyContains 匹配", () => {
|
||||
expect(checkExpect(200, "hello world", 100, { bodyContains: "hello" })).toBe(true);
|
||||
expect(checkExpect(200, "hello world", 100, { bodyContains: "missing" })).toBe(false);
|
||||
test("headers 匹配", () => {
|
||||
const headers = { "content-type": "application/json", "x-custom": "test" };
|
||||
expect(checkExpect(200, "", 100, headers, { headers: { "Content-Type": "application/json" } })).toBe(true);
|
||||
expect(checkExpect(200, "", 100, headers, { headers: { "Content-Type": "text/html" } })).toBe(false);
|
||||
expect(checkExpect(200, "", 100, headers, { headers: { "X-Missing": "test" } })).toBe(false);
|
||||
});
|
||||
|
||||
test("body.contains 匹配", () => {
|
||||
expect(checkExpect(200, "hello world", 100, emptyHeaders, { body: { contains: "hello" } })).toBe(true);
|
||||
expect(checkExpect(200, "hello world", 100, emptyHeaders, { body: { contains: "missing" } })).toBe(false);
|
||||
});
|
||||
|
||||
test("body.regex 匹配", () => {
|
||||
expect(checkExpect(200, "status: ok", 100, emptyHeaders, { body: { regex: "status.*ok" } })).toBe(true);
|
||||
expect(checkExpect(200, "status: error", 100, emptyHeaders, { body: { regex: "status.*ok" } })).toBe(false);
|
||||
});
|
||||
|
||||
test("body.json 匹配", () => {
|
||||
expect(
|
||||
checkExpect(200, JSON.stringify({ status: "ok" }), 100, emptyHeaders, { body: { json: { "$.status": "ok" } } }),
|
||||
).toBe(true);
|
||||
expect(
|
||||
checkExpect(200, JSON.stringify({ status: "error" }), 100, emptyHeaders, {
|
||||
body: { json: { "$.status": "ok" } },
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
test("body.json 解析失败", () => {
|
||||
expect(checkExpect(200, "not json", 100, emptyHeaders, { body: { json: { "$.status": "ok" } } })).toBe(false);
|
||||
});
|
||||
|
||||
test("body 多种方法 AND 组合", () => {
|
||||
expect(
|
||||
checkExpect(200, "healthy", 100, emptyHeaders, {
|
||||
body: {
|
||||
contains: "healthy",
|
||||
regex: "healthy",
|
||||
},
|
||||
}),
|
||||
).toBe(true);
|
||||
|
||||
expect(
|
||||
checkExpect(200, "healthy", 100, emptyHeaders, {
|
||||
body: {
|
||||
contains: "healthy",
|
||||
regex: "unhealthy",
|
||||
},
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
test("maxLatencyMs 匹配", () => {
|
||||
expect(checkExpect(200, "", 100, { maxLatencyMs: 200 })).toBe(true);
|
||||
expect(checkExpect(200, "", 300, { maxLatencyMs: 200 })).toBe(false);
|
||||
expect(checkExpect(200, "", 200, { maxLatencyMs: 200 })).toBe(true);
|
||||
expect(checkExpect(200, "", 100, emptyHeaders, { maxLatencyMs: 200 })).toBe(true);
|
||||
expect(checkExpect(200, "", 300, emptyHeaders, { maxLatencyMs: 200 })).toBe(false);
|
||||
expect(checkExpect(200, "", 200, emptyHeaders, { maxLatencyMs: 200 })).toBe(true);
|
||||
});
|
||||
|
||||
test("多条 expect 全部通过", () => {
|
||||
expect(
|
||||
checkExpect(200, "healthy", 100, {
|
||||
checkExpect(200, "healthy", 100, emptyHeaders, {
|
||||
status: [200],
|
||||
bodyContains: "healthy",
|
||||
body: { contains: "healthy" },
|
||||
maxLatencyMs: 200,
|
||||
}),
|
||||
).toBe(true);
|
||||
@@ -35,9 +84,33 @@ describe("checkExpect", () => {
|
||||
|
||||
test("多条 expect 部分失败", () => {
|
||||
expect(
|
||||
checkExpect(200, "healthy", 500, {
|
||||
checkExpect(200, "healthy", 500, emptyHeaders, {
|
||||
status: [200],
|
||||
bodyContains: "healthy",
|
||||
body: { contains: "healthy" },
|
||||
maxLatencyMs: 200,
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
test("status + headers + body + maxLatencyMs 全组合", () => {
|
||||
const headers = { "content-type": "application/json" };
|
||||
expect(
|
||||
checkExpect(200, JSON.stringify({ status: "ok" }), 100, headers, {
|
||||
status: [200],
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: { contains: "ok", json: { "$.status": "ok" } },
|
||||
maxLatencyMs: 200,
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
test("全组合中 headers 失败", () => {
|
||||
const headers = { "content-type": "text/html" };
|
||||
expect(
|
||||
checkExpect(200, JSON.stringify({ status: "ok" }), 100, headers, {
|
||||
status: [200],
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: { contains: "ok", json: { "$.status": "ok" } },
|
||||
maxLatencyMs: 200,
|
||||
}),
|
||||
).toBe(false);
|
||||
|
||||
Reference in New Issue
Block a user