mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-26 17:32:16 +00:00
65 lines
2.0 KiB
TypeScript
65 lines
2.0 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => {
|
|
const undiciFetch = vi.fn();
|
|
const proxyAgentSpy = vi.fn();
|
|
const setGlobalDispatcher = vi.fn();
|
|
class ProxyAgent {
|
|
static lastCreated: ProxyAgent | undefined;
|
|
proxyUrl: string;
|
|
constructor(proxyUrl: string) {
|
|
this.proxyUrl = proxyUrl;
|
|
ProxyAgent.lastCreated = this;
|
|
proxyAgentSpy(proxyUrl);
|
|
}
|
|
}
|
|
|
|
return {
|
|
ProxyAgent,
|
|
undiciFetch,
|
|
proxyAgentSpy,
|
|
setGlobalDispatcher,
|
|
getLastAgent: () => ProxyAgent.lastCreated,
|
|
};
|
|
});
|
|
|
|
let getProxyUrlFromFetch: typeof import("./proxy.js").getProxyUrlFromFetch;
|
|
let makeProxyFetch: typeof import("./proxy.js").makeProxyFetch;
|
|
|
|
describe("makeProxyFetch", () => {
|
|
beforeEach(async () => {
|
|
vi.resetModules();
|
|
mocks.undiciFetch.mockReset();
|
|
mocks.proxyAgentSpy.mockClear();
|
|
mocks.setGlobalDispatcher.mockClear();
|
|
vi.doMock("undici", () => ({
|
|
ProxyAgent: mocks.ProxyAgent,
|
|
fetch: mocks.undiciFetch,
|
|
setGlobalDispatcher: mocks.setGlobalDispatcher,
|
|
}));
|
|
({ getProxyUrlFromFetch, makeProxyFetch } = await import("./proxy.js"));
|
|
});
|
|
|
|
it("uses undici fetch with ProxyAgent dispatcher", async () => {
|
|
const proxyUrl = "http://proxy.test:8080";
|
|
mocks.undiciFetch.mockResolvedValue({ ok: true });
|
|
|
|
const proxyFetch = makeProxyFetch(proxyUrl);
|
|
await proxyFetch("https://api.telegram.org/bot123/getMe");
|
|
|
|
expect(mocks.proxyAgentSpy).toHaveBeenCalledWith(proxyUrl);
|
|
expect(mocks.undiciFetch).toHaveBeenCalledWith(
|
|
"https://api.telegram.org/bot123/getMe",
|
|
expect.objectContaining({ dispatcher: mocks.getLastAgent() }),
|
|
);
|
|
expect(mocks.setGlobalDispatcher).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("attaches proxy metadata for resolver transport handling", () => {
|
|
const proxyUrl = "http://proxy.test:8080";
|
|
const proxyFetch = makeProxyFetch(proxyUrl);
|
|
|
|
expect(getProxyUrlFromFetch(proxyFetch)).toBe(proxyUrl);
|
|
});
|
|
});
|