定义 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。
181 lines
6.8 KiB
TypeScript
181 lines
6.8 KiB
TypeScript
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
||
import { HttpChecker } from "../../../../../src/server/checker/runner/http/runner";
|
||
import type { CheckerContext } from "../../../../../src/server/checker/runner/types";
|
||
import type { ResolvedHttpTarget } from "../../../../../src/server/checker/types";
|
||
|
||
const checker = new HttpChecker();
|
||
|
||
describe("HttpChecker", () => {
|
||
let server: ReturnType<typeof Bun.serve>;
|
||
let baseUrl: string;
|
||
|
||
beforeAll(() => {
|
||
server = Bun.serve({
|
||
port: 0,
|
||
fetch(req) {
|
||
const url = new URL(req.url);
|
||
switch (url.pathname) {
|
||
case "/ok":
|
||
return new Response("hello world", {
|
||
headers: { "content-type": "text/plain", "x-custom": "test-value" },
|
||
});
|
||
case "/json":
|
||
return new Response(JSON.stringify({ status: "ok" }), {
|
||
headers: { "content-type": "application/json" },
|
||
});
|
||
case "/echo":
|
||
return new Response(JSON.stringify({ method: req.method, body: req.body ? "present" : "empty" }), {
|
||
headers: { "content-type": "application/json" },
|
||
});
|
||
case "/large":
|
||
return new Response("x".repeat(2000));
|
||
case "/notfound":
|
||
return new Response("not found", { status: 404 });
|
||
default:
|
||
return new Response("ok");
|
||
}
|
||
},
|
||
});
|
||
baseUrl = `http://localhost:${server.port}`;
|
||
});
|
||
|
||
afterAll(() => {
|
||
server.stop();
|
||
});
|
||
|
||
function makeTarget(overrides: {
|
||
url?: string;
|
||
method?: string;
|
||
body?: string;
|
||
headers?: Record<string, string>;
|
||
expect?: Record<string, unknown>;
|
||
maxBodyBytes?: number;
|
||
timeoutMs?: number;
|
||
}): ResolvedHttpTarget {
|
||
return {
|
||
type: "http",
|
||
name: "test-http",
|
||
group: "default",
|
||
http: {
|
||
url: overrides.url ?? `${baseUrl}/ok`,
|
||
method: overrides.method ?? "GET",
|
||
headers: overrides.headers ?? {},
|
||
body: overrides.body,
|
||
maxBodyBytes: overrides.maxBodyBytes ?? 1024 * 1024,
|
||
},
|
||
intervalMs: 60000,
|
||
timeoutMs: overrides.timeoutMs ?? 5000,
|
||
expect: overrides.expect as ResolvedHttpTarget["expect"],
|
||
};
|
||
}
|
||
|
||
function makeCtx(timeoutMs = 5000): CheckerContext {
|
||
const controller = new AbortController();
|
||
setTimeout(() => controller.abort(), timeoutMs);
|
||
return { signal: controller.signal };
|
||
}
|
||
|
||
test("成功请求 200", async () => {
|
||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok` }), makeCtx());
|
||
expect(result.matched).toBe(true);
|
||
expect(result.statusDetail).toBe("HTTP 200");
|
||
expect(result.durationMs).not.toBeNull();
|
||
expect(result.failure).toBeNull();
|
||
});
|
||
|
||
test("404 不匹配默认 status [200]", async () => {
|
||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/notfound` }), makeCtx());
|
||
expect(result.matched).toBe(false);
|
||
expect(result.statusDetail).toBe("HTTP 404");
|
||
expect(result.failure!.phase).toBe("status");
|
||
});
|
||
|
||
test("404 匹配自定义 status [404]", async () => {
|
||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/notfound`, expect: { status: [404] } }), makeCtx());
|
||
expect(result.matched).toBe(true);
|
||
});
|
||
|
||
test("headers 检查通过", async () => {
|
||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok`, expect: { headers: { "x-custom": "test-value" } } }), makeCtx());
|
||
expect(result.matched).toBe(true);
|
||
});
|
||
|
||
test("headers 检查失败", async () => {
|
||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok`, expect: { headers: { "x-custom": "wrong-value" } } }), makeCtx());
|
||
expect(result.matched).toBe(false);
|
||
expect(result.failure!.phase).toBe("headers");
|
||
});
|
||
|
||
test("body contains 检查", async () => {
|
||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok`, expect: { body: [{ contains: "hello" }] } }), makeCtx());
|
||
expect(result.matched).toBe(true);
|
||
});
|
||
|
||
test("body contains 失败", async () => {
|
||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok`, expect: { body: [{ contains: "nonexistent" }] } }), makeCtx());
|
||
expect(result.matched).toBe(false);
|
||
expect(result.failure!.phase).toBe("body");
|
||
});
|
||
|
||
test("body json 检查", async () => {
|
||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/json`, expect: { body: [{ json: { path: "$.status", equals: "ok" } }] } }), makeCtx());
|
||
expect(result.matched).toBe(true);
|
||
});
|
||
|
||
test("响应体超过 maxBodyBytes", async () => {
|
||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/large`, maxBodyBytes: 100, expect: { body: [{ contains: "x" }] } }), makeCtx());
|
||
expect(result.matched).toBe(false);
|
||
expect(result.failure!.phase).toBe("body");
|
||
expect(result.failure!.message).toContain("超过限制");
|
||
});
|
||
|
||
test("请求超时", async () => {
|
||
const timeoutServer = Bun.serve({
|
||
port: 0,
|
||
async fetch() {
|
||
await new Promise((resolve) => setTimeout(resolve, 10000));
|
||
return new Response("late");
|
||
},
|
||
});
|
||
|
||
try {
|
||
const result = await checker.execute(makeTarget({ url: `http://localhost:${timeoutServer.port}/`, timeoutMs: 100 }), makeCtx(100));
|
||
expect(result.matched).toBe(false);
|
||
expect(result.failure!.message).toContain("超时");
|
||
} finally {
|
||
timeoutServer.stop();
|
||
}
|
||
});
|
||
|
||
test("快速失败:status 失败时不读取 body", async () => {
|
||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/notfound`, expect: { status: [200], body: [{ contains: "something" }] } }), makeCtx());
|
||
expect(result.matched).toBe(false);
|
||
expect(result.failure!.phase).toBe("status");
|
||
});
|
||
|
||
test("status 通过但 body 失败", async () => {
|
||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok`, expect: { status: [200], body: [{ contains: "not-in-body" }] } }), makeCtx());
|
||
expect(result.matched).toBe(false);
|
||
expect(result.failure!.phase).toBe("body");
|
||
});
|
||
|
||
test("无 expect 时默认检查 status 200", async () => {
|
||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok`, expect: undefined }), makeCtx());
|
||
expect(result.matched).toBe(true);
|
||
});
|
||
|
||
test("POST 请求携带 body", async () => {
|
||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/echo`, method: "POST", body: "test-body", headers: { "content-type": "text/plain" }, expect: { status: [200], body: [{ json: { path: "$.body", equals: "present" } }] } }), makeCtx());
|
||
expect(result.matched).toBe(true);
|
||
});
|
||
|
||
test("serialize 返回 URL 和 config JSON", () => {
|
||
const target = makeTarget({});
|
||
const s = checker.serialize(target);
|
||
expect(s.target).toBe(target.http.url);
|
||
const config = JSON.parse(s.config);
|
||
expect(config.url).toBe(target.http.url);
|
||
expect(config.method).toBe("GET");
|
||
});
|
||
});
|