


























@@ -25,13 +25,32 @@ import {
2525registerManagedProxyGatewayLoopbackNoProxy,
2626startProxy,
2727stopProxy,
28+type ProxyHandle,
2829} from "./proxy-lifecycle.js";
29303031const mockForceResetGlobalDispatcher = vi.mocked(forceResetGlobalDispatcher);
3132const mockBootstrapGlobalAgent = vi.mocked(bootstrapGlobalAgent);
3233const mockLogInfo = vi.mocked(logInfo);
3334const mockLogWarn = vi.mocked(logWarn);
343536+function expectProxyHandle(handle: Awaited<ReturnType<typeof startProxy>>): ProxyHandle {
37+expect(handle).toEqual(expect.objectContaining({ proxyUrl: expect.any(String) }));
38+if (handle === null) {
39+throw new Error("Expected managed proxy handle");
40+}
41+return handle;
42+}
43+44+function expectNoProxyUnregister(
45+unregister: ReturnType<typeof registerManagedProxyGatewayLoopbackNoProxy>,
46+): () => void {
47+expect(unregister).toBeTypeOf("function");
48+if (typeof unregister !== "function") {
49+throw new Error("Expected Gateway NO_PROXY unregister callback");
50+}
51+return unregister;
52+}
53+3554describe("startProxy", () => {
3655const savedEnv: Record<string, string | undefined> = {};
3756const envKeysToClean = [
@@ -135,9 +154,14 @@ describe("startProxy", () => {
135154proxyUrl: "http://127.0.0.1:3128",
136155});
137156138-expect(getActiveManagedProxyUrl()?.href).toBe("http://127.0.0.1:3128/");
157+const activeProxyUrl = getActiveManagedProxyUrl();
158+expect(activeProxyUrl).toEqual(expect.any(URL));
159+if (activeProxyUrl === undefined) {
160+throw new Error("Expected active managed proxy URL");
161+}
162+expect(activeProxyUrl.href).toBe("http://127.0.0.1:3128/");
139163140-await stopProxy(handle);
164+await stopProxy(expectProxyHandle(handle));
141165142166expect(getActiveManagedProxyUrl()).toBeUndefined();
143167});
@@ -147,7 +171,7 @@ describe("startProxy", () => {
147171148172const handle = await startProxy({ enabled: true });
149173150-expect(handle?.proxyUrl).toBe("http://127.0.0.1:3128");
174+expect(expectProxyHandle(handle).proxyUrl).toBe("http://127.0.0.1:3128");
151175expect(process.env["HTTP_PROXY"]).toBe("http://127.0.0.1:3128");
152176});
153177@@ -159,7 +183,7 @@ describe("startProxy", () => {
159183proxyUrl: "http://127.0.0.1:3129",
160184});
161185162-expect(handle?.proxyUrl).toBe("http://127.0.0.1:3129");
186+expect(expectProxyHandle(handle).proxyUrl).toBe("http://127.0.0.1:3129");
163187expect(process.env["HTTP_PROXY"]).toBe("http://127.0.0.1:3129");
164188});
165189@@ -178,7 +202,7 @@ describe("startProxy", () => {
178202proxyUrl: "http://127.0.0.1:3128",
179203});
180204181-expect(handle).not.toBeNull();
205+expectProxyHandle(handle);
182206expect(process.env["http_proxy"]).toBe("http://127.0.0.1:3128");
183207expect(process.env["https_proxy"]).toBe("http://127.0.0.1:3128");
184208expect(process.env["HTTP_PROXY"]).toBe("http://127.0.0.1:3128");
@@ -261,12 +285,12 @@ describe("startProxy", () => {
261285proxyUrl: "http://127.0.0.1:3128",
262286});
263287264-expect(handle).not.toBeNull();
288+const proxyHandle = expectProxyHandle(handle);
265289expect(process.env["HTTP_PROXY"]).toBe("http://127.0.0.1:3128");
266290expect(process.env["NO_PROXY"]).toBe("");
267291mockForceResetGlobalDispatcher.mockClear();
268292269-await stopProxy(handle);
293+await stopProxy(proxyHandle);
270294271295expect(process.env["HTTP_PROXY"]).toBe("http://previous.example.com:8080");
272296expect(process.env["NO_PROXY"]).toBe("corp.example.com");
@@ -448,12 +472,12 @@ describe("startProxy", () => {
448472});
449473const agent = (global as Record<string, unknown>)["GLOBAL_AGENT"] as Record<string, unknown>;
450474451-const unregister = registerManagedProxyGatewayLoopbackNoProxy("ws://127.0.0.1:18789");
452-453-expect(unregister).toBeTypeOf("function");
475+const unregister = expectNoProxyUnregister(
476+ registerManagedProxyGatewayLoopbackNoProxy("ws://127.0.0.1:18789"),
477+);
454478expect(agent["NO_PROXY"]).toBe("127.0.0.1:18789");
455479456-unregister?.();
480+unregister();
457481expect(agent["NO_PROXY"]).toBeNull();
458482await stopProxy(handle);
459483});
@@ -465,15 +489,17 @@ describe("startProxy", () => {
465489});
466490const agent = (global as Record<string, unknown>)["GLOBAL_AGENT"] as Record<string, unknown>;
467491468-const unregisterIpv6 = registerManagedProxyGatewayLoopbackNoProxy("ws://[::1]:18789");
469-expect(unregisterIpv6).toBeTypeOf("function");
492+const unregisterIpv6 = expectNoProxyUnregister(
493+registerManagedProxyGatewayLoopbackNoProxy("ws://[::1]:18789"),
494+);
470495expect(agent["NO_PROXY"]).toBe("[::1]:18789");
471-unregisterIpv6?.();
496+unregisterIpv6();
472497473-const unregisterLocalhost = registerManagedProxyGatewayLoopbackNoProxy("ws://localhost.:18789");
474-expect(unregisterLocalhost).toBeTypeOf("function");
498+const unregisterLocalhost = expectNoProxyUnregister(
499+registerManagedProxyGatewayLoopbackNoProxy("ws://localhost.:18789"),
500+);
475501expect(agent["NO_PROXY"]).toBe("localhost.:18789");
476-unregisterLocalhost?.();
502+unregisterLocalhost();
477503478504await stopProxy(handle);
479505});
@@ -489,12 +515,12 @@ describe("startProxy", () => {
489515});
490516const agent = (global as Record<string, unknown>)["GLOBAL_AGENT"] as Record<string, unknown>;
491517492-const unregister = registerManagedProxyGatewayLoopbackNoProxy("ws://127.0.0.1:3000");
493-494-expect(unregister).toBeTypeOf("function");
518+const unregister = expectNoProxyUnregister(
519+ registerManagedProxyGatewayLoopbackNoProxy("ws://127.0.0.1:3000"),
520+);
495521expect(agent["NO_PROXY"]).toBe("127.0.0.1:3000");
496522497-unregister?.();
523+unregister();
498524await stopProxy(handle);
499525});
500526@@ -539,12 +565,12 @@ describe("startProxy", () => {
539565const agent = (global as Record<string, unknown>)["GLOBAL_AGENT"] as Record<string, unknown>;
540566agent["NO_PROXY"] = "corp.example.com";
541567542-const unregister = registerManagedProxyGatewayLoopbackNoProxy("ws://127.0.0.1:18789");
543-544-expect(unregister).toBeTypeOf("function");
568+const unregister = expectNoProxyUnregister(
569+ registerManagedProxyGatewayLoopbackNoProxy("ws://127.0.0.1:18789"),
570+);
545571expect(agent["NO_PROXY"]).toBe("corp.example.com,127.0.0.1:18789");
546572547-unregister?.();
573+unregister();
548574expect(agent["NO_PROXY"]).toBe("corp.example.com");
549575await stopProxy(handle);
550576});
@@ -556,8 +582,7 @@ describe("startProxy", () => {
556582proxyUrl: "http://127.0.0.1:3128",
557583});
558584559-expect(handle).not.toBeNull();
560-handle?.kill("SIGTERM");
585+expectProxyHandle(handle).kill("SIGTERM");
561586562587expect(process.env["HTTP_PROXY"]).toBeUndefined();
563588expect(process.env["NO_PROXY"]).toBe("corp.example.com");
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。