1
0

feat: HTTP 探针增强 — ignoreSSL、精确重定向控制、状态码范围匹配、编码自动检测

This commit is contained in:
2026-05-13 00:02:04 +08:00
parent 87d946a441
commit 2fd0f206be
16 changed files with 642 additions and 22 deletions

View File

@@ -95,7 +95,9 @@ describe("loadConfig", () => {
expect(t.http.url).toBe("http://example.com");
expect(t.http.method).toBe("GET");
expect(t.http.headers).toEqual({});
expect(t.http.ignoreSSL).toBe(false);
expect(t.http.maxBodyBytes).toBe(104857600);
expect(t.http.maxRedirects).toBe(0);
expect(t.intervalMs).toBe(30000);
expect(t.timeoutMs).toBe(10000);
}
@@ -157,8 +159,10 @@ targets:
interval: "1m"
http:
url: "http://example.com"
ignoreSSL: true
maxRedirects: 5
expect:
status: [200]
status: ["2xx", 301]
body:
- contains: "ok"
- name: "cmd-target"
@@ -184,7 +188,10 @@ targets:
expect(http.http.url).toBe("http://example.com");
expect(http.http.method).toBe("POST");
expect(http.http.headers).toEqual({ Authorization: "Bearer token" });
expect(http.http.ignoreSSL).toBe(true);
expect(http.http.maxBodyBytes).toBe(52428800);
expect(http.http.maxRedirects).toBe(5);
expect(http.expect?.status).toEqual(["2xx", 301]);
expect(http.intervalMs).toBe(60000);
expect(http.timeoutMs).toBe(5000);
}
@@ -277,6 +284,68 @@ targets:
await expect(loadConfig(configPath)).rejects.toThrow("缺少 http.url 字段");
});
test("HTTP target 缺少 http 分组抛出清晰错误", async () => {
const configPath = join(tempDir, "no-http-group.yaml");
await writeFile(
configPath,
`targets:
- name: "test"
type: http
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("缺少 http.url 字段");
});
test("HTTP target ignoreSSL 非布尔值抛出错误", async () => {
const configPath = join(tempDir, "bad-ignore-ssl.yaml");
await writeFile(
configPath,
`targets:
- name: "test"
type: http
http:
url: "http://example.com"
ignoreSSL: "true"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("ignoreSSL 必须为布尔值");
});
test("HTTP target maxRedirects 非负整数校验", async () => {
const configPath = join(tempDir, "bad-max-redirects.yaml");
await writeFile(
configPath,
`targets:
- name: "test"
type: http
http:
url: "http://example.com"
maxRedirects: 1.5
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("maxRedirects 必须为非负整数");
});
test("HTTP target status 模式非法抛出错误", async () => {
const configPath = join(tempDir, "bad-status-pattern.yaml");
await writeFile(
configPath,
`targets:
- name: "test"
type: http
http:
url: "http://example.com"
expect:
status: ["abc"]
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("status 模式");
});
test("command target 缺少 exec 抛出错误", async () => {
const configPath = join(tempDir, "no-exec.yaml");
await writeFile(

View File

@@ -190,7 +190,9 @@ describe("ProbeEngine", () => {
group: "default",
http: {
headers: {},
ignoreSSL: false,
maxBodyBytes: 1024 * 1024,
maxRedirects: 0,
method: "GET",
url: `http://localhost:${httpServer.port}/`,
},

View File

@@ -1,6 +1,6 @@
import { describe, expect, test } from "bun:test";
import { checkHttpExpect } from "../../../../../src/server/checker/runner/http/expect";
import { checkHttpExpect, checkStatus } from "../../../../../src/server/checker/runner/http/expect";
function obs(
overrides: { body?: null | string; durationMs?: number; headers?: Record<string, string>; statusCode?: number } = {},
@@ -145,3 +145,44 @@ describe("checkHttpExpect", () => {
expect(r.failure!.phase).toBe("body");
});
});
describe("checkStatus 范围匹配", () => {
test("2xx 范围匹配 200", () => {
expect(checkStatus(200, ["2xx"]).matched).toBe(true);
});
test("2xx 范围匹配 204", () => {
expect(checkStatus(204, ["2xx"]).matched).toBe(true);
});
test("2xx 范围不匹配 301", () => {
expect(checkStatus(301, ["2xx"]).matched).toBe(false);
});
test("5xx 范围匹配 503", () => {
expect(checkStatus(503, ["5xx"]).matched).toBe(true);
});
test("混合精确值与范围模式 — 精确命中", () => {
expect(checkStatus(301, ["2xx", 301]).matched).toBe(true);
});
test("混合精确值与范围模式 — 范围命中", () => {
expect(checkStatus(204, ["2xx", 301]).matched).toBe(true);
});
test("混合模式都不匹配", () => {
expect(checkStatus(404, ["2xx", 301]).matched).toBe(false);
});
test("纯精确值仍正常工作", () => {
expect(checkStatus(200, [200, 201]).matched).toBe(true);
expect(checkStatus(404, [200, 201]).matched).toBe(false);
});
test("范围匹配失败返回 phase=status 的 failure", () => {
const r = checkStatus(404, ["2xx"]);
expect(r.matched).toBe(false);
expect(r.failure!.phase).toBe("status");
});
});

View File

@@ -1,11 +1,58 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { CheckerContext } from "../../../../../src/server/checker/runner/types";
import type { CheckerContext, ResolveContext } from "../../../../../src/server/checker/runner/types";
import type { ResolvedHttpTarget } from "../../../../../src/server/checker/types";
import { HttpChecker } from "../../../../../src/server/checker/runner/http/runner";
const checker = new HttpChecker();
const SELF_SIGNED_CERT = `-----BEGIN CERTIFICATE-----
MIIDJTCCAg2gAwIBAgIUTwQU8FzvnvxNYR7mMO0DLcnq+wQwDQYJKoZIhvcNAQEL
BQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTI2MDUxMjE1NDAyOFoXDTM2MDUw
OTE1NDAyOFowFDESMBAGA1UEAwwJbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEF
AAOCAQ8AMIIBCgKCAQEArC0G46EXF8qPsCS2mtNwHzGFQvFQNcU8k7cZkCTwt4Cp
DlLOA2DbzR02LiVk/TA+d9qMUABAiXMndwebKv8EYxoKjwTY0jbVLKfEIIFxQS3F
uvKDgkYJz8P675p8fhR0Xa21+13b0/T8fperYC7fBZZsAqyo8+aF9QOUjy+kWRjr
lTTL1ez5L1nX0QCczTRaUDe51NTmcUYHJoiLqdKI2ZjXds7wnsaAfAgh7H9qr4wl
sUhCHV/Pg1LzBtfyLKZcImUJWWkj/KlgFgZ6aRyJHoGFmlZtXyaKhf3rEa+ZvKOy
MhcRmWC694PF+QjhrWS7oODLuY3XC5WKnL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oAV3KLaONWbK1rI9mHZf8KyWYiJ9cRXwTJ4rGYNMM/6QIUGQSx
agwJojCQqS4f6AfCNdUOzaRp
-----END PRIVATE KEY-----`;
describe("HttpChecker", () => {
let server: ReturnType<typeof Bun.serve>;
@@ -20,6 +67,10 @@ describe("HttpChecker", () => {
return new Response(JSON.stringify({ body: req.body ? "present" : "empty", method: req.method }), {
headers: { "content-type": "application/json" },
});
case "/gbk": {
const gbkBytes = new Uint8Array([0xc4, 0xe3, 0xba, 0xc3]);
return new Response(gbkBytes, { headers: { "content-type": "text/plain; charset=gbk" } });
}
case "/json":
return new Response(JSON.stringify({ status: "ok" }), {
headers: { "content-type": "application/json" },
@@ -32,6 +83,12 @@ describe("HttpChecker", () => {
return new Response("hello world", {
headers: { "content-type": "text/plain", "x-custom": "test-value" },
});
case "/redirect":
return new Response(null, { headers: { location: `${baseUrl}/ok` }, status: 301 });
case "/redirect-chain-1":
return new Response(null, { headers: { location: `${baseUrl}/redirect-chain-2` }, status: 302 });
case "/redirect-chain-2":
return new Response(null, { headers: { location: `${baseUrl}/ok` }, status: 302 });
default:
return new Response("ok");
}
@@ -49,7 +106,9 @@ describe("HttpChecker", () => {
body?: string;
expect?: Record<string, unknown>;
headers?: Record<string, string>;
ignoreSSL?: boolean;
maxBodyBytes?: number;
maxRedirects?: number;
method?: string;
timeoutMs?: number;
url?: string;
@@ -60,7 +119,9 @@ describe("HttpChecker", () => {
http: {
body: overrides.body,
headers: overrides.headers ?? {},
ignoreSSL: overrides.ignoreSSL ?? false,
maxBodyBytes: overrides.maxBodyBytes ?? 1024 * 1024,
maxRedirects: overrides.maxRedirects ?? 0,
method: overrides.method ?? "GET",
url: overrides.url ?? `${baseUrl}/ok`,
},
@@ -214,8 +275,192 @@ describe("HttpChecker", () => {
const target = makeTarget({});
const s = checker.serialize(target);
expect(s.target).toBe(target.http.url);
const config = JSON.parse(s.config) as { method: string; url: string };
const config = JSON.parse(s.config) as { ignoreSSL: boolean; maxRedirects: number; method: string; url: string };
expect(config.url).toBe(target.http.url);
expect(config.method).toBe("GET");
expect(config.ignoreSSL).toBe(false);
expect(config.maxRedirects).toBe(0);
});
test("maxRedirects=0 不跟随重定向", async () => {
const result = await checker.execute(makeTarget({ maxRedirects: 0, url: `${baseUrl}/redirect` }), makeCtx());
expect(result.matched).toBe(false);
expect(result.statusDetail).toBe("HTTP 301");
});
test("maxRedirects>0 跟随重定向", async () => {
const result = await checker.execute(makeTarget({ maxRedirects: 5, url: `${baseUrl}/redirect` }), makeCtx());
expect(result.matched).toBe(true);
expect(result.statusDetail).toBe("HTTP 200");
});
test("maxRedirects 精确限制跟随次数", async () => {
const result = await checker.execute(
makeTarget({ maxRedirects: 1, url: `${baseUrl}/redirect-chain-1` }),
makeCtx(),
);
expect(result.matched).toBe(false);
expect(result.statusDetail).toBe("HTTP 302");
});
test("maxRedirects 允许足够次数时到达最终目标", async () => {
const result = await checker.execute(
makeTarget({ maxRedirects: 2, url: `${baseUrl}/redirect-chain-1` }),
makeCtx(),
);
expect(result.matched).toBe(true);
expect(result.statusDetail).toBe("HTTP 200");
});
test("ignoreSSL 跳过自签名证书校验", async () => {
const httpsServer = Bun.serve({
fetch() {
return new Response("secure ok");
},
port: 0,
tls: { cert: SELF_SIGNED_CERT, key: SELF_SIGNED_KEY },
});
try {
const strictResult = await checker.execute(
makeTarget({ ignoreSSL: false, url: `https://localhost:${httpsServer.port}/` }),
makeCtx(),
);
expect(strictResult.matched).toBe(false);
expect(strictResult.statusDetail).toBeNull();
const ignoredResult = await checker.execute(
makeTarget({ ignoreSSL: true, url: `https://localhost:${httpsServer.port}/` }),
makeCtx(),
);
expect(ignoredResult.matched).toBe(true);
expect(ignoredResult.statusDetail).toBe("HTTP 200");
} finally {
void httpsServer.stop();
}
});
test("响应体编码自动检测 GBK", async () => {
const result = await checker.execute(
makeTarget({ expect: { body: [{ contains: "你好" }] }, url: `${baseUrl}/gbk` }),
makeCtx(),
);
expect(result.matched).toBe(true);
});
test("响应体编码回退 UTF-8", async () => {
const result = await checker.execute(
makeTarget({ expect: { body: [{ contains: "hello" }] }, url: `${baseUrl}/ok` }),
makeCtx(),
);
expect(result.matched).toBe(true);
});
});
describe("HttpChecker.resolve", () => {
function makeResolveContext(): ResolveContext {
return {
configDir: ".",
defaultIntervalMs: 30000,
defaults: {},
defaultTimeoutMs: 10000,
};
}
test("method 非法抛出错误", () => {
expect(() =>
checker.resolve(
{ http: { method: "INVALID", url: "https://example.com" }, name: "test", type: "http" },
makeResolveContext(),
),
).toThrow("不合法");
});
test("URL 不以 http(s):// 开头抛出错误", () => {
expect(() =>
checker.resolve({ http: { url: "ftp://example.com" }, name: "test", type: "http" }, makeResolveContext()),
).toThrow("格式不合法");
});
test("maxRedirects 为负数抛出错误", () => {
expect(() =>
checker.resolve(
{ http: { maxRedirects: -1, url: "https://example.com" }, name: "test", type: "http" },
makeResolveContext(),
),
).toThrow("非负整数");
});
test("maxRedirects 非整数抛出错误", () => {
const target = {
http: { maxRedirects: 1.5, url: "https://example.com" },
name: "test",
type: "http",
} as unknown as Parameters<HttpChecker["resolve"]>[0];
expect(() => checker.resolve(target, makeResolveContext())).toThrow("非负整数");
});
test("ignoreSSL 非布尔值抛出错误", () => {
const target = {
http: { ignoreSSL: "true", url: "https://example.com" },
name: "test",
type: "http",
} as unknown as Parameters<HttpChecker["resolve"]>[0];
expect(() => checker.resolve(target, makeResolveContext())).toThrow("ignoreSSL 必须为布尔值");
});
test("缺少 http 分组抛出清晰错误", () => {
const target = { name: "test", type: "http" } as unknown as Parameters<HttpChecker["resolve"]>[0];
expect(() => checker.resolve(target, makeResolveContext())).toThrow("缺少 http.url 字段");
});
test("expect.status 非法模式抛出错误", () => {
expect(() =>
checker.resolve(
{ expect: { status: ["abc"] }, http: { url: "https://example.com" }, name: "test", type: "http" },
makeResolveContext(),
),
).toThrow("不合法");
});
test("ignoreSSL 默认值为 false", () => {
const result = checker.resolve(
{ http: { url: "https://example.com" }, name: "test", type: "http" },
makeResolveContext(),
);
expect((result as ResolvedHttpTarget).http.ignoreSSL).toBe(false);
});
test("maxRedirects 默认值为 0", () => {
const result = checker.resolve(
{ http: { url: "https://example.com" }, name: "test", type: "http" },
makeResolveContext(),
);
expect((result as ResolvedHttpTarget).http.maxRedirects).toBe(0);
});
test("method 统一转大写", () => {
const result = checker.resolve(
{ http: { method: "get", url: "https://example.com" }, name: "test", type: "http" },
makeResolveContext(),
);
expect((result as ResolvedHttpTarget).http.method).toBe("GET");
});
test("合法 status 范围模式通过校验", () => {
const result = checker.resolve(
{ expect: { status: ["2xx", 301] }, http: { url: "https://example.com" }, name: "test", type: "http" },
makeResolveContext(),
);
expect((result as ResolvedHttpTarget).expect?.status).toEqual(["2xx", 301]);
});
test("显式 ignoreSSL 和 maxRedirects 正确解析", () => {
const result = checker.resolve(
{ http: { ignoreSSL: true, maxRedirects: 3, url: "https://example.com" }, name: "test", type: "http" },
makeResolveContext(),
);
expect((result as ResolvedHttpTarget).http.ignoreSSL).toBe(true);
expect((result as ResolvedHttpTarget).http.maxRedirects).toBe(3);
});
});

View File

@@ -27,7 +27,9 @@ const httpTarget: ResolvedTarget = {
group: "default",
http: {
headers: { Accept: "application/json" },
ignoreSSL: false,
maxBodyBytes: 104857600,
maxRedirects: 0,
method: "GET",
url: "https://example.com/health",
},
@@ -85,14 +87,18 @@ describe("ProbeStore", () => {
expect(t.target).toBe("https://example.com/health");
const config = JSON.parse(t.config) as {
headers: Record<string, string>;
ignoreSSL: boolean;
maxBodyBytes: number;
maxRedirects: number;
method: string;
url: string;
};
expect(config.url).toBe("https://example.com/health");
expect(config.method).toBe("GET");
expect(config.headers).toEqual({ Accept: "application/json" });
expect(config.ignoreSSL).toBe(false);
expect(config.maxBodyBytes).toBe(104857600);
expect(config.maxRedirects).toBe(0);
expect(t.interval_ms).toBe(30000);
expect(t.timeout_ms).toBe(10000);
expect(JSON.parse(t.expect!)).toEqual({ maxDurationMs: 3000, status: [200] });
@@ -283,7 +289,14 @@ describe("ProbeStore", () => {
const cascadeStore = new ProbeStore(join(tempDir, "cascade.db"));
const cascadeTarget: ResolvedTarget = {
group: "default",
http: { headers: {}, maxBodyBytes: 104857600, method: "GET", url: "http://cascade.test" },
http: {
headers: {},
ignoreSSL: false,
maxBodyBytes: 104857600,
maxRedirects: 0,
method: "GET",
url: "http://cascade.test",
},
intervalMs: 30000,
name: "cascade-test",
timeoutMs: 10000,
@@ -338,7 +351,14 @@ describe("ProbeStore", () => {
freshStore.syncTargets([
{
group: "default",
http: { headers: {}, maxBodyBytes: 104857600, method: "GET", url: "http://no.records" },
http: {
headers: {},
ignoreSSL: false,
maxBodyBytes: 104857600,
maxRedirects: 0,
method: "GET",
url: "http://no.records",
},
intervalMs: 30000,
name: "no-records",
timeoutMs: 10000,
@@ -377,7 +397,14 @@ describe("ProbeStore", () => {
freshStore.syncTargets([
{
group: "default",
http: { headers: {}, maxBodyBytes: 104857600, method: "GET", url: "http://no.stats" },
http: {
headers: {},
ignoreSSL: false,
maxBodyBytes: 104857600,
maxRedirects: 0,
method: "GET",
url: "http://no.stats",
},
intervalMs: 30000,
name: "no-stats",
timeoutMs: 10000,