refactor(core): 抽象语义化 HTTP 客户端与统一防爬指纹管理
This commit is contained in:
1 parent
1322e2eab9
commit
aa69b50e6e
11 files changed
+834
-401
No files matched your search
@@ -0,0 +1,116 @@
|
||||
import { describe, expect, it } from "bun:test";
|
||||
import {
|
||||
getBrowserFingerprintHeaders,
|
||||
DEFAULT_USER_AGENT,
|
||||
DEFAULT_REFERER,
|
||||
DEFAULT_ORIGIN,
|
||||
} from "./fingerprint.js";
|
||||
import {
|
||||
getSessionHeaders,
|
||||
createSessionClient,
|
||||
createEkbClient,
|
||||
} from "./session-client.js";
|
||||
import {
|
||||
createOpenApiClient,
|
||||
getOpenApiHeaders,
|
||||
} from "./openapi-client.js";
|
||||
import { resolveCorpCluster } from "./unity-client.js";
|
||||
import { getScriptEngineClient, getPiperClient } from "./script-engine-client.js";
|
||||
import type { CorpConfig } from "../config/types.js";
|
||||
|
||||
function mockCorp(overrides: Partial<CorpConfig> = {}): CorpConfig {
|
||||
return {
|
||||
id: "mock-corp",
|
||||
name: "测试企业",
|
||||
source: "link",
|
||||
credentials: {
|
||||
accessToken: "mock-access-token",
|
||||
corpId: "mock-corp-id",
|
||||
staffId: "mock-staff-id",
|
||||
},
|
||||
baseUrl: "https://app.ekuaibao.com",
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("core/http/fingerprint", () => {
|
||||
it("generates default modern browser fingerprint headers", () => {
|
||||
const headers = getBrowserFingerprintHeaders();
|
||||
expect(headers["User-Agent"]).toBe(DEFAULT_USER_AGENT);
|
||||
expect(headers["Referer"]).toBe(DEFAULT_REFERER);
|
||||
expect(headers["Origin"]).toBe(DEFAULT_ORIGIN);
|
||||
expect(headers["sec-ch-ua-platform"]).toBe('"macOS"');
|
||||
expect(headers["sec-ch-ua"]).toContain("Chrome");
|
||||
expect(headers["Cookie"]).toBeUndefined();
|
||||
});
|
||||
|
||||
it("supports custom cookie and referer", () => {
|
||||
const headers = getBrowserFingerprintHeaders({
|
||||
cookie: "SESSION=abc123xyz; cloud_waf=token456",
|
||||
referer: "https://custom.ekuaibao.com/flow",
|
||||
});
|
||||
expect(headers["Cookie"]).toBe("SESSION=abc123xyz; cloud_waf=token456");
|
||||
expect(headers["Referer"]).toBe("https://custom.ekuaibao.com/flow");
|
||||
});
|
||||
});
|
||||
|
||||
describe("core/http/session-client", () => {
|
||||
it("merges closed session headers with browser fingerprint", () => {
|
||||
const corp = mockCorp();
|
||||
const headers = getSessionHeaders(corp);
|
||||
|
||||
// 核心三 Header 校验
|
||||
expect(headers.accesstoken).toBe("mock-access-token");
|
||||
expect(headers.corpid).toBe("mock-corp-id");
|
||||
expect(headers.staffid).toBe("mock-staff-id");
|
||||
|
||||
// 统一防爬指纹校验
|
||||
expect(headers["User-Agent"]).toBe(DEFAULT_USER_AGENT);
|
||||
expect(headers["Origin"]).toBe(DEFAULT_ORIGIN);
|
||||
});
|
||||
|
||||
it("createSessionClient and createEkbClient alias produce valid clients", async () => {
|
||||
const corp = mockCorp();
|
||||
const client1 = await createSessionClient(corp);
|
||||
const client2 = await createEkbClient(corp);
|
||||
|
||||
expect(client1).toBeDefined();
|
||||
expect(client2).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("core/http/openapi-client", () => {
|
||||
it("builds correct OpenAPI request headers", () => {
|
||||
const headers = getOpenApiHeaders("test-app-token-123");
|
||||
expect(headers["app-token"]).toBe("test-app-token-123");
|
||||
expect(headers["Authorization"]).toBe("Bearer test-app-token-123");
|
||||
expect(headers["User-Agent"]).toBe(DEFAULT_USER_AGENT);
|
||||
});
|
||||
|
||||
it("creates OpenAPI client with static appToken", async () => {
|
||||
const client = await createOpenApiClient({
|
||||
appToken: "static-token",
|
||||
baseUrl: "https://app.ekuaibao.com",
|
||||
});
|
||||
expect(client).toBeDefined();
|
||||
});
|
||||
|
||||
it("fails to create OpenAPI client when neither auth nor appToken is provided", async () => {
|
||||
await expect(createOpenApiClient({})).rejects.toThrow(
|
||||
"创建 OpenAPI 客户端失败: 必须提供 auth 凭据或 appToken"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("core/http/unity-client & script-engine-client", () => {
|
||||
it("resolves corp cluster from cluster property or baseUrl", () => {
|
||||
expect(resolveCorpCluster({ cluster: "shanghai" })).toBe("shanghai");
|
||||
expect(resolveCorpCluster({ baseUrl: "https://wx2.ekuaibao.com" })).toBe("wx2");
|
||||
expect(resolveCorpCluster({ baseUrl: "https://app.ekuaibao.com" })).toBe("app");
|
||||
expect(resolveCorpCluster({})).toBe("app");
|
||||
});
|
||||
|
||||
it("getPiperClient alias points to getScriptEngineClient", () => {
|
||||
expect(getPiperClient).toBe(getScriptEngineClient);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user