mirror of
https://github.com/modelstudioai/cli.git
synced 2026-09-14 19:49:23 +08:00
8a0de83c24
Node's built-in fetch (undici) ignores proxy environment variables, so bl always connected directly and failed with ECONNRESET behind a VPN or corporate proxy. Install an EnvHttpProxyAgent as the global dispatcher at startup, but only when a proxy variable is actually set — behavior is unchanged otherwise. Lowercase variables take precedence over uppercase (curl convention) and NO_PROXY is honored. Values are trimmed and passed explicitly to work around undici reading env vars with ??, where an empty lowercase variable (https_proxy="") masks a configured uppercase one. Invalid proxy URLs fail with a clear usage error instead of a stack trace, and the ECONNRESET hint now suggests exporting HTTPS_PROXY. Tests are fully offline and need no credentials: unit tests cover env parsing, and the e2e test runs a minimal probe (setupProxyFromEnv + a bare fetch) against a .invalid host through a local CONNECT proxy to verify traffic routes through the proxy, NO_PROXY is honored, no dispatcher is installed when unset, and invalid values error clearly.
43 lines
1.5 KiB
TypeScript
43 lines
1.5 KiB
TypeScript
import { expect, test } from "vite-plus/test";
|
|
import { readProxyEnv } from "../src/proxy.ts";
|
|
|
|
test("readProxyEnv: 未设置任何代理变量时全部为 undefined", () => {
|
|
expect(readProxyEnv({})).toEqual({
|
|
httpProxy: undefined,
|
|
httpsProxy: undefined,
|
|
noProxy: undefined,
|
|
});
|
|
});
|
|
|
|
test("readProxyEnv: 空白值视为未设置", () => {
|
|
expect(readProxyEnv({ HTTPS_PROXY: "", HTTP_PROXY: " ", NO_PROXY: "" })).toEqual({
|
|
httpProxy: undefined,
|
|
httpsProxy: undefined,
|
|
noProxy: undefined,
|
|
});
|
|
});
|
|
|
|
test("readProxyEnv: 大小写变量均可识别,小写优先", () => {
|
|
expect(readProxyEnv({ HTTPS_PROXY: "http://upper:1" }).httpsProxy).toBe("http://upper:1");
|
|
expect(readProxyEnv({ https_proxy: "http://lower:1" }).httpsProxy).toBe("http://lower:1");
|
|
expect(
|
|
readProxyEnv({ https_proxy: "http://lower:1", HTTPS_PROXY: "http://upper:1" }).httpsProxy,
|
|
).toBe("http://lower:1");
|
|
});
|
|
|
|
test("readProxyEnv: 空字符串小写变量不屏蔽已设置的大写变量", () => {
|
|
expect(readProxyEnv({ https_proxy: "", HTTPS_PROXY: "http://upper:1" }).httpsProxy).toBe(
|
|
"http://upper:1",
|
|
);
|
|
expect(readProxyEnv({ http_proxy: "", HTTP_PROXY: "http://upper:2" }).httpProxy).toBe(
|
|
"http://upper:2",
|
|
);
|
|
});
|
|
|
|
test("readProxyEnv: NO_PROXY 独立读取", () => {
|
|
const r = readProxyEnv({ NO_PROXY: "*.aliyuncs.com" });
|
|
expect(r.noProxy).toBe("*.aliyuncs.com");
|
|
expect(r.httpProxy).toBeUndefined();
|
|
expect(r.httpsProxy).toBeUndefined();
|
|
});
|