feat: HTTP 探针增强 — ignoreSSL、精确重定向控制、状态码范围匹配、编码自动检测
This commit is contained in:
@@ -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+QjhrWS7oODLuY3XC5WKnLKxlBgfAwIDAQABo28wbTAdBgNVHQ4E
|
||||
FgQUrHJEbBSDHOx/HAQ5nQp35v0Ljw8wHwYDVR0jBBgwFoAUrHJEbBSDHOx/HAQ5
|
||||
nQp35v0Ljw8wDwYDVR0TAQH/BAUwAwEB/zAaBgNVHREEEzARgglsb2NhbGhvc3SH
|
||||
BH8AAAEwDQYJKoZIhvcNAQELBQADggEBAEJ0s/FJ6KZalSM0ntHxlMOB9taUa60I
|
||||
A6zqrEMauU8BqZO3QLmX6a821geZntQtz77kGtW6rQWxELBNjN3rXTbUKfKXN/Au
|
||||
ZLftNJLsQOjKF+1uFOF49D4/5Le9PGvwl79Qua/l6JO5HRJL9Dh545/zEr9W5Erb
|
||||
l4JoKKfyCEYjrPg5tl7d2PrHUmzk+sGlxEqNeKIl272+3UMVCbkVHI/v6rtb4F7p
|
||||
u77O0UYLNIRFZQOVqvE7A7rfYy93J8EEQcADKH/Nhx8clFxC5X187EakcVAfkeKX
|
||||
SL7R1kmUiLPiHbVCqGyS2m3RH2XDM3MbA9WCCczbwXn5Lwp5HEz0wb0=
|
||||
-----END CERTIFICATE-----`;
|
||||
const SELF_SIGNED_KEY = `-----BEGIN PRIVATE KEY-----
|
||||
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCsLQbjoRcXyo+w
|
||||
JLaa03AfMYVC8VA1xTyTtxmQJPC3gKkOUs4DYNvNHTYuJWT9MD532oxQAECJcyd3
|
||||
B5sq/wRjGgqPBNjSNtUsp8QggXFBLcW68oOCRgnPw/rvmnx+FHRdrbX7XdvT9Px+
|
||||
l6tgLt8FlmwCrKjz5oX1A5SPL6RZGOuVNMvV7PkvWdfRAJzNNFpQN7nU1OZxRgcm
|
||||
iIup0ojZmNd2zvCexoB8CCHsf2qvjCWxSEIdX8+DUvMG1/IsplwiZQlZaSP8qWAW
|
||||
BnppHIkegYWaVm1fJoqF/esRr5m8o7IyFxGZYLr3g8X5COGtZLug4Mu5jdcLlYqc
|
||||
srGUGB8DAgMBAAECggEAO91wDsedIu2QZlttjonD62SphCwpinio5md8oOznMbav
|
||||
kUZjUTNlWX01sHfaFFqo7b10mgBscB4086MWZa3D1b1hPHcf+H+OQXeXrwGy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Ya7Cl6+TopU+xu4537FCzHUJ3EPg3KsYCeCM0LEtR4
|
||||
GQjRzFM3qqWabzoAV3KLaONWbK1rI9mHZf8KyWYiJ9cRXwTJ4rGYNMM/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);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user