定义 Checker 接口(resolve/execute/serialize)和 CheckerRegistry 注册中心,消除 engine/config-loader/store 中硬编码类型分支。 按 checker 类型分子包(runner/http/、runner/command/),提取 共享 expect 到 runner/shared/。超时控制通过引擎注入 AbortSignal。 CheckFailure.phase 从联合类型改为 string。配置校验下沉到各 Checker.resolve() 内部。 新增 checker-runner-abstraction spec,更新 DEVELOPMENT.md。
143 lines
5.6 KiB
TypeScript
143 lines
5.6 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { checkHttpExpect } from "../../../../../src/server/checker/runner/http/expect";
|
|
|
|
function obs(overrides: { statusCode?: number; headers?: Record<string, string>; body?: string | null; durationMs?: number } = {}) {
|
|
return {
|
|
statusCode: overrides.statusCode ?? 200,
|
|
headers: overrides.headers ?? {},
|
|
body: overrides.body ?? "",
|
|
durationMs: overrides.durationMs ?? 100,
|
|
};
|
|
}
|
|
|
|
describe("checkHttpExpect", () => {
|
|
test("无 expect 配置时默认检查 status [200] 匹配成功", () => {
|
|
const r = checkHttpExpect(obs().statusCode, obs().headers, obs().body as string, obs().durationMs);
|
|
expect(r.matched).toBe(true);
|
|
expect(r.failure).toBeNull();
|
|
});
|
|
|
|
test("无 expect 配置时 status 非 200 匹配失败", () => {
|
|
const r = checkHttpExpect(500, {}, "", 100);
|
|
expect(r.matched).toBe(false);
|
|
expect(r.failure).not.toBeNull();
|
|
expect(r.failure!.phase).toBe("status");
|
|
expect(r.failure!.kind).toBe("mismatch");
|
|
});
|
|
|
|
test("status 匹配指定状态码", () => {
|
|
const cfg = { status: [200, 301] };
|
|
expect(checkHttpExpect(200, {}, "", 100, cfg).matched).toBe(true);
|
|
expect(checkHttpExpect(301, {}, "", 100, cfg).matched).toBe(true);
|
|
expect(checkHttpExpect(404, {}, "", 100, cfg).matched).toBe(false);
|
|
});
|
|
|
|
test("status 不匹配返回 phase=status 的失败", () => {
|
|
const r = checkHttpExpect(503, {}, "", 100, { status: [200] });
|
|
expect(r.matched).toBe(false);
|
|
expect(r.failure!.phase).toBe("status");
|
|
expect(r.failure!.expected).toEqual([200]);
|
|
expect(r.failure!.actual).toBe(503);
|
|
});
|
|
|
|
test("duration 在限制内匹配成功", () => {
|
|
const r = checkHttpExpect(200, {}, "", 50, { maxDurationMs: 100 });
|
|
expect(r.matched).toBe(true);
|
|
});
|
|
|
|
test("duration 超过限制匹配失败", () => {
|
|
const r = checkHttpExpect(200, {}, "", 200, { maxDurationMs: 100 });
|
|
expect(r.matched).toBe(false);
|
|
expect(r.failure!.phase).toBe("duration");
|
|
});
|
|
|
|
test("duration 恰好等于限制匹配成功", () => {
|
|
const r = checkHttpExpect(200, {}, "", 100, { maxDurationMs: 100 });
|
|
expect(r.matched).toBe(true);
|
|
});
|
|
|
|
test("headers 字符串格式检查(等于)", () => {
|
|
const h = { "content-type": "application/json", "x-api": "v1" };
|
|
expect(checkHttpExpect(200, h, "", 100, { headers: { "content-type": "application/json" } }).matched).toBe(true);
|
|
expect(checkHttpExpect(200, h, "", 100, { headers: { "content-type": "text/html" } }).matched).toBe(false);
|
|
});
|
|
|
|
test("headers 操作符格式检查", () => {
|
|
const h = { "content-type": "application/json" };
|
|
expect(checkHttpExpect(200, h, "", 100, { headers: { "content-type": { contains: "json" } } }).matched).toBe(true);
|
|
expect(checkHttpExpect(200, h, "", 100, { headers: { "content-type": { match: "^application/" } } }).matched).toBe(true);
|
|
expect(checkHttpExpect(200, h, "", 100, { headers: { "content-type": { contains: "xml" } } }).matched).toBe(false);
|
|
});
|
|
|
|
test("headers 大小写不敏感匹配", () => {
|
|
const h = { "content-type": "application/json" };
|
|
expect(checkHttpExpect(200, h, "", 100, { headers: { "Content-Type": "application/json" } }).matched).toBe(true);
|
|
});
|
|
|
|
test("headers 不存在时返回失败", () => {
|
|
const r = checkHttpExpect(200, {}, "", 100, { headers: { "x-missing": "value" } });
|
|
expect(r.matched).toBe(false);
|
|
expect(r.failure!.phase).toBe("headers");
|
|
});
|
|
|
|
test("body 规则数组按顺序检查", () => {
|
|
const body = JSON.stringify({ status: "ok", count: 5 });
|
|
const r = checkHttpExpect(200, {}, body, 100, {
|
|
body: [{ contains: "ok" }, { json: { path: "$.count", gte: 1 } }],
|
|
});
|
|
expect(r.matched).toBe(true);
|
|
});
|
|
|
|
test("body 第一条规则失败立即返回", () => {
|
|
const r = checkHttpExpect(200, {}, "hello world", 100, {
|
|
body: [{ contains: "missing" }, { contains: "hello" }],
|
|
});
|
|
expect(r.matched).toBe(false);
|
|
expect(r.failure!.path).toBe("body[0]");
|
|
});
|
|
|
|
test("body 为 null 但有 body 规则时报错", () => {
|
|
const r = checkHttpExpect(200, {}, null, 100, { body: [{ contains: "test" }] });
|
|
expect(r.matched).toBe(false);
|
|
expect(r.failure!.kind).toBe("error");
|
|
});
|
|
|
|
test("完整流水线 status->duration->headers->body 全部通过", () => {
|
|
const r = checkHttpExpect(200, { "content-type": "application/json" }, JSON.stringify({ status: "healthy" }), 50, {
|
|
status: [200],
|
|
maxDurationMs: 100,
|
|
headers: { "content-type": { contains: "json" } },
|
|
body: [{ json: { path: "$.status", equals: "healthy" } }],
|
|
});
|
|
expect(r.matched).toBe(true);
|
|
expect(r.failure).toBeNull();
|
|
});
|
|
|
|
test("完整流水线 status 通过但 duration 失败", () => {
|
|
const r = checkHttpExpect(200, {}, "", 500, { status: [200], maxDurationMs: 100 });
|
|
expect(r.matched).toBe(false);
|
|
expect(r.failure!.phase).toBe("duration");
|
|
});
|
|
|
|
test("完整流水线 status 和 duration 通过但 headers 失败", () => {
|
|
const r = checkHttpExpect(200, { "x-api": "v1" }, "", 50, {
|
|
status: [200],
|
|
maxDurationMs: 100,
|
|
headers: { "x-api": "v2" },
|
|
});
|
|
expect(r.matched).toBe(false);
|
|
expect(r.failure!.phase).toBe("headers");
|
|
});
|
|
|
|
test("完整流水线 status/duration/headers 通过但 body 失败", () => {
|
|
const r = checkHttpExpect(200, { "content-type": "text/plain" }, "error occurred", 50, {
|
|
status: [200],
|
|
maxDurationMs: 100,
|
|
headers: { "content-type": "text/plain" },
|
|
body: [{ contains: "success" }],
|
|
});
|
|
expect(r.matched).toBe(false);
|
|
expect(r.failure!.phase).toBe("body");
|
|
});
|
|
});
|