Files
Netcatty/electron/bridges/httpNetworkProxyBridge.test.cjs
T
bincxzandCursor c02848bb08 Add app-level HTTP(S) proxy for cloud sync and AI.
Lets Windows users (and others) route Google Drive / OneDrive / GitHub sync and AI provider traffic through system or custom proxies without touching SSH ProxyJump settings.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-09 14:12:08 +08:00

135 lines
3.8 KiB
JavaScript

const test = require("node:test");
const assert = require("node:assert/strict");
const {
buildElectronProxyConfigFromPayload,
applyNodeProxyEnv,
normalizeProxySettingsPayload,
} = require("./httpNetworkProxyBridge.cjs");
test("normalizeProxySettingsPayload defaults to system", () => {
assert.deepEqual(normalizeProxySettingsPayload(undefined), {
mode: "system",
url: "",
bypass: "<local>",
});
});
test("buildElectronProxyConfigFromPayload maps custom mode", () => {
assert.deepEqual(
buildElectronProxyConfigFromPayload({
mode: "custom",
url: "http://127.0.0.1:7890",
bypass: "localhost",
}),
{
mode: "fixed_servers",
proxyRules: "http://127.0.0.1:7890",
proxyBypassRules: "localhost",
},
);
});
test("applyNodeProxyEnv sets and clears process.env for custom/direct", () => {
const { resetProxyEnvOwnershipForTests } = require("./httpNetworkProxyBridge.cjs");
resetProxyEnvOwnershipForTests();
const env = {
HTTP_PROXY: "stale",
HTTPS_PROXY: "stale",
NO_PROXY: "stale",
http_proxy: "stale",
https_proxy: "stale",
no_proxy: "stale",
};
applyNodeProxyEnv(
{ mode: "custom", url: "http://proxy:1", bypass: "a,b" },
env,
);
assert.equal(env.HTTP_PROXY, "http://proxy:1");
assert.equal(env.HTTPS_PROXY, "http://proxy:1");
assert.equal(env.NO_PROXY, "a,b");
assert.equal(env.http_proxy, "http://proxy:1");
applyNodeProxyEnv({ mode: "direct", url: "", bypass: "<local>" }, env);
assert.equal(env.HTTP_PROXY, undefined);
assert.equal(env.HTTPS_PROXY, undefined);
assert.equal(env.NO_PROXY, undefined);
assert.equal(env.http_proxy, undefined);
});
test("applyNodeProxyEnv restores prior env when returning to system after custom", () => {
const { resetProxyEnvOwnershipForTests } = require("./httpNetworkProxyBridge.cjs");
resetProxyEnvOwnershipForTests();
const env = {
HTTP_PROXY: "os-proxy",
HTTPS_PROXY: "os-proxy",
NO_PROXY: "localhost",
};
applyNodeProxyEnv(
{ mode: "custom", url: "http://user:pass@proxy:1", bypass: "a" },
env,
);
// Credentials must not be written into process.env.
assert.equal(env.HTTP_PROXY, "http://proxy:1");
assert.equal(env.HTTPS_PROXY, "http://proxy:1");
applyNodeProxyEnv({ mode: "system", url: "", bypass: "<local>" }, env);
assert.equal(env.HTTP_PROXY, "os-proxy");
assert.equal(env.HTTPS_PROXY, "os-proxy");
assert.equal(env.NO_PROXY, "localhost");
});
test("empty custom mode applies as system for Electron/env", () => {
assert.deepEqual(
buildElectronProxyConfigFromPayload({ mode: "custom", url: "", bypass: "<local>" }),
{ mode: "system" },
);
});
test("applyHttpNetworkProxy serializes so older setProxy cannot overwrite newer", async () => {
const {
applyHttpNetworkProxy,
getCurrentProxySettings,
resetProxyEnvOwnershipForTests,
} = require("./httpNetworkProxyBridge.cjs");
resetProxyEnvOwnershipForTests();
const applied = [];
let releaseFirst;
const firstGate = new Promise((resolve) => {
releaseFirst = resolve;
});
const session = {
async setProxy(config) {
applied.push(config);
if (applied.length === 1) {
await firstGate;
}
},
};
const first = applyHttpNetworkProxy(
{ mode: "custom", url: "http://first:1", bypass: "<local>" },
{ session, env: {} },
);
// Let first reach its gate inside setProxy.
await new Promise((r) => setImmediate(r));
const second = applyHttpNetworkProxy(
{ mode: "direct", url: "", bypass: "<local>" },
{ session, env: {} },
);
releaseFirst();
const [firstResult, secondResult] = await Promise.all([first, second]);
assert.equal(firstResult.superseded, true);
assert.equal(secondResult.success, true);
assert.equal(getCurrentProxySettings().mode, "direct");
assert.deepEqual(applied.at(-1), { mode: "direct" });
});