- 重命名 ContentRules→ContentExpectations, KeyValueExpect→KeyedExpectations - 新增 Raw/Resolved 双层模型:resolve 阶段物化为执行计划,store 持久化 Raw 快照 - HTTP body 按需读取:status/headers 失败或无 body expectation 时不读取 body - 新增 displayValueExpectation() 解包 failure.expected 用户可读展示 - 修复 checkEarlyTimeout 独立 lte/lt 检查,修复 KeyedExpectations JSON Schema - 新增 expect/value.ts(resolve/check/display)、keyed.ts、content.ts、headers.ts、status.ts - 删除旧 normalize.ts/matcher.ts/validate-matcher.ts/key-value.ts - 更新 DEVELOPMENT.md:expect 五层管线表、displayValueExpectation、1.7↔1.10 交叉引用 - 同步 13 个 main specs,归档 refactor-expect-type-model 变更(62/62 tasks)
117 lines
4.0 KiB
TypeScript
117 lines
4.0 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
|
|
import type { RawKeyedExpectations } from "../../../../../src/server/checker/expect/types";
|
|
|
|
import { checkHeaderExpectations } from "../../../../../src/server/checker/expect/headers";
|
|
import { resolveKeyedExpectations } from "../../../../../src/server/checker/expect/keyed";
|
|
import { checkStatusCode } from "../../../../../src/server/checker/expect/status";
|
|
|
|
function checkHeaders(headers: Record<string, unknown>, raw?: RawKeyedExpectations) {
|
|
return checkHeaderExpectations(headers, resolveKeyedExpectations(raw));
|
|
}
|
|
|
|
describe("checkHeaders", () => {
|
|
test("未配置 headers expect 时匹配成功", () => {
|
|
const result = checkHeaders({});
|
|
expect(result.matched).toBe(true);
|
|
expect(result.failure).toBeNull();
|
|
});
|
|
|
|
test("字符串格式按等值匹配", () => {
|
|
const headers = { "content-type": "application/json", "x-api": "v1" };
|
|
|
|
expect(checkHeaders(headers, { "content-type": "application/json" }).matched).toBe(true);
|
|
expect(checkHeaders(headers, { "content-type": "text/html" }).matched).toBe(false);
|
|
});
|
|
|
|
test("header 名称按小写响应头匹配", () => {
|
|
const headers = { "content-type": "application/json" };
|
|
|
|
expect(checkHeaders(headers, { "Content-Type": "application/json" }).matched).toBe(true);
|
|
});
|
|
|
|
test("操作符格式匹配", () => {
|
|
const headers = { "content-type": "application/json" };
|
|
|
|
expect(checkHeaders(headers, { "content-type": { contains: "json" } }).matched).toBe(true);
|
|
expect(checkHeaders(headers, { "content-type": { regex: "^application/" } }).matched).toBe(true);
|
|
expect(checkHeaders(headers, { "content-type": { contains: "xml" } }).matched).toBe(false);
|
|
});
|
|
|
|
test("缺失 header 默认返回失败", () => {
|
|
const result = checkHeaders({}, { "x-missing": "value" });
|
|
|
|
expect(result.matched).toBe(false);
|
|
expect(result.failure!.phase).toBe("headers");
|
|
expect(result.failure!.kind).toBe("mismatch");
|
|
});
|
|
|
|
test("缺失 header 且 exists=false 时匹配成功", () => {
|
|
const result = checkHeaders({}, { "x-missing": { exists: false } });
|
|
|
|
expect(result.matched).toBe(true);
|
|
expect(result.failure).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("checkStatusCode 范围匹配", () => {
|
|
test("无 expect 配置时默认 status [200] 可由调用方使用 checkStatusCode 表达", () => {
|
|
const result = checkStatusCode(200, [200]);
|
|
expect(result.matched).toBe(true);
|
|
expect(result.failure).toBeNull();
|
|
});
|
|
|
|
test("status 不匹配返回 phase=status 的失败", () => {
|
|
const result = checkStatusCode(503, [200]);
|
|
expect(result.matched).toBe(false);
|
|
expect(result.failure!.phase).toBe("status");
|
|
expect(result.failure!.expected).toEqual([200]);
|
|
expect(result.failure!.actual).toBe(503);
|
|
});
|
|
|
|
test("2xx 范围匹配 200", () => {
|
|
expect(checkStatusCode(200, ["2xx"]).matched).toBe(true);
|
|
});
|
|
|
|
test("2xx 范围匹配 204", () => {
|
|
expect(checkStatusCode(204, ["2xx"]).matched).toBe(true);
|
|
});
|
|
|
|
test("2xx 范围不匹配 301", () => {
|
|
expect(checkStatusCode(301, ["2xx"]).matched).toBe(false);
|
|
});
|
|
|
|
test("5xx 范围匹配 503", () => {
|
|
expect(checkStatusCode(503, ["5xx"]).matched).toBe(true);
|
|
});
|
|
|
|
test("混合精确值与范围模式命中精确值", () => {
|
|
expect(checkStatusCode(301, ["2xx", 301]).matched).toBe(true);
|
|
});
|
|
|
|
test("混合精确值与范围模式命中范围", () => {
|
|
expect(checkStatusCode(204, ["2xx", 301]).matched).toBe(true);
|
|
});
|
|
|
|
test("混合模式都不匹配", () => {
|
|
expect(checkStatusCode(404, ["2xx", 301]).matched).toBe(false);
|
|
});
|
|
|
|
test("纯精确值仍正常工作", () => {
|
|
expect(checkStatusCode(200, [200, 201]).matched).toBe(true);
|
|
expect(checkStatusCode(404, [200, 201]).matched).toBe(false);
|
|
});
|
|
|
|
test("1xx 范围匹配 101", () => {
|
|
expect(checkStatusCode(101, ["1xx"]).matched).toBe(true);
|
|
});
|
|
|
|
test("3xx 范围匹配 301", () => {
|
|
expect(checkStatusCode(301, ["3xx"]).matched).toBe(true);
|
|
});
|
|
|
|
test("4xx 范围匹配 404", () => {
|
|
expect(checkStatusCode(404, ["4xx"]).matched).toBe(true);
|
|
});
|
|
});
|