mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 08:42:43 +00:00
fix(discord): apply proxy to app-id and allowlist REST lookups
This commit is contained in:
63
src/discord/monitor/provider.rest-proxy.test.ts
Normal file
63
src/discord/monitor/provider.rest-proxy.test.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
const { undiciFetchMock, proxyAgentSpy } = vi.hoisted(() => ({
|
||||
undiciFetchMock: vi.fn(),
|
||||
proxyAgentSpy: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("undici", () => {
|
||||
class ProxyAgent {
|
||||
proxyUrl: string;
|
||||
constructor(proxyUrl: string) {
|
||||
if (proxyUrl === "bad-proxy") {
|
||||
throw new Error("bad proxy");
|
||||
}
|
||||
this.proxyUrl = proxyUrl;
|
||||
proxyAgentSpy(proxyUrl);
|
||||
}
|
||||
}
|
||||
return {
|
||||
ProxyAgent,
|
||||
fetch: undiciFetchMock,
|
||||
};
|
||||
});
|
||||
|
||||
describe("resolveDiscordRestFetch", () => {
|
||||
it("uses undici proxy fetch when a proxy URL is configured", async () => {
|
||||
const runtime = {
|
||||
log: vi.fn(),
|
||||
error: vi.fn(),
|
||||
} as const;
|
||||
undiciFetchMock.mockReset().mockResolvedValue(new Response("ok", { status: 200 }));
|
||||
proxyAgentSpy.mockReset();
|
||||
|
||||
const { __testing } = await import("./provider.js");
|
||||
const fetcher = __testing.resolveDiscordRestFetch("http://proxy.test:8080", runtime);
|
||||
|
||||
await fetcher("https://discord.com/api/v10/oauth2/applications/@me");
|
||||
|
||||
expect(proxyAgentSpy).toHaveBeenCalledWith("http://proxy.test:8080");
|
||||
expect(undiciFetchMock).toHaveBeenCalledWith(
|
||||
"https://discord.com/api/v10/oauth2/applications/@me",
|
||||
expect.objectContaining({
|
||||
dispatcher: expect.objectContaining({ proxyUrl: "http://proxy.test:8080" }),
|
||||
}),
|
||||
);
|
||||
expect(runtime.log).toHaveBeenCalledWith("discord: rest proxy enabled");
|
||||
expect(runtime.error).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("falls back to global fetch when proxy URL is invalid", async () => {
|
||||
const runtime = {
|
||||
log: vi.fn(),
|
||||
error: vi.fn(),
|
||||
} as const;
|
||||
const { __testing } = await import("./provider.js");
|
||||
|
||||
const fetcher = __testing.resolveDiscordRestFetch("bad-proxy", runtime);
|
||||
|
||||
expect(fetcher).toBe(fetch);
|
||||
expect(runtime.error).toHaveBeenCalled();
|
||||
expect(runtime.log).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user