fix: reject conflicting managed proxy activation

This commit is contained in:
jesse-merhi
2026-05-04 02:30:39 +10:00
committed by clawsweeper
parent ce5d7fe8a6
commit bfca5feed7

View File

@@ -291,7 +291,7 @@ describe("startProxy", () => {
expect((global as Record<string, unknown>)["GLOBAL_AGENT"]).toBeUndefined();
});
it("keeps process-wide proxy hooks active until the last overlapping handle stops", async () => {
it("keeps process-wide proxy hooks active until the last same-URL overlapping handle stops", async () => {
const patchedHttpRequest = vi.fn() as unknown as typeof http.request;
const patchedHttpGet = vi.fn() as unknown as typeof http.get;
const patchedHttpsRequest = vi.fn() as unknown as typeof https.request;
@@ -313,18 +313,18 @@ describe("startProxy", () => {
});
const secondHandle = await startProxy({
enabled: true,
proxyUrl: "http://127.0.0.1:3129",
proxyUrl: "http://127.0.0.1:3128",
});
expect(http.request).toBe(patchedHttpRequest);
expect(https.request).toBe(patchedHttpsRequest);
expect(process.env["HTTP_PROXY"]).toBe("http://127.0.0.1:3129");
expect(process.env["HTTP_PROXY"]).toBe("http://127.0.0.1:3128");
await stopProxy(firstHandle);
expect(http.request).toBe(patchedHttpRequest);
expect(https.request).toBe(patchedHttpsRequest);
expect(process.env["HTTP_PROXY"]).toBe("http://127.0.0.1:3129");
expect(process.env["HTTP_PROXY"]).toBe("http://127.0.0.1:3128");
expect(process.env["OPENCLAW_PROXY_ACTIVE"]).toBe("1");
await stopProxy(secondHandle);
@@ -337,6 +337,25 @@ describe("startProxy", () => {
expect(process.env["OPENCLAW_PROXY_ACTIVE"]).toBeUndefined();
});
it("rejects overlapping handles with different managed proxy URLs", async () => {
const firstHandle = await startProxy({
enabled: true,
proxyUrl: "http://127.0.0.1:3128",
});
await expect(
startProxy({
enabled: true,
proxyUrl: "http://127.0.0.1:3129",
}),
).rejects.toThrow("cannot activate a different managed proxy");
expect(process.env["HTTP_PROXY"]).toBe("http://127.0.0.1:3128");
expect(process.env["OPENCLAW_PROXY_ACTIVE"]).toBe("1");
await stopProxy(firstHandle);
});
it("restores env and throws when undici activation fails", async () => {
mockForceResetGlobalDispatcher.mockImplementationOnce(() => {
throw new Error("dispatcher failed");