|
| 1 | +import * as vscode from "vscode" |
| 2 | + |
| 3 | +const mockHttpsProxyAgentConstructor = vi.fn() |
| 4 | + |
| 5 | +vi.mock("https-proxy-agent", () => ({ |
| 6 | + HttpsProxyAgent: mockHttpsProxyAgentConstructor, |
| 7 | +})) |
| 8 | + |
| 9 | +vi.mock("vscode", () => ({ |
| 10 | + workspace: { |
| 11 | + getConfiguration: vi.fn(), |
| 12 | + onDidChangeConfiguration: vi.fn(() => ({ dispose: vi.fn() })), |
| 13 | + }, |
| 14 | +})) |
| 15 | + |
| 16 | +function createMockContext(): vscode.ExtensionContext { |
| 17 | + return { |
| 18 | + extensionMode: 1, // Production |
| 19 | + subscriptions: [], |
| 20 | + extensionPath: "/test/path", |
| 21 | + globalState: { |
| 22 | + get: vi.fn(), |
| 23 | + update: vi.fn(), |
| 24 | + keys: vi.fn().mockReturnValue([]), |
| 25 | + setKeysForSync: vi.fn(), |
| 26 | + }, |
| 27 | + workspaceState: { |
| 28 | + get: vi.fn(), |
| 29 | + update: vi.fn(), |
| 30 | + keys: vi.fn().mockReturnValue([]), |
| 31 | + }, |
| 32 | + secrets: { |
| 33 | + get: vi.fn(), |
| 34 | + store: vi.fn(), |
| 35 | + delete: vi.fn(), |
| 36 | + onDidChange: vi.fn(), |
| 37 | + }, |
| 38 | + extensionUri: { fsPath: "/test/path" } as vscode.Uri, |
| 39 | + globalStorageUri: { fsPath: "/test/global" } as vscode.Uri, |
| 40 | + logUri: { fsPath: "/test/logs" } as vscode.Uri, |
| 41 | + storageUri: { fsPath: "/test/storage" } as vscode.Uri, |
| 42 | + storagePath: "/test/storage", |
| 43 | + globalStoragePath: "/test/global", |
| 44 | + logPath: "/test/logs", |
| 45 | + asAbsolutePath: vi.fn((p) => `/test/path/${p}`), |
| 46 | + environmentVariableCollection: {} as vscode.GlobalEnvironmentVariableCollection, |
| 47 | + extension: {} as vscode.Extension<unknown>, |
| 48 | + languageModelAccessInformation: {} as vscode.LanguageModelAccessInformation, |
| 49 | + } as unknown as vscode.ExtensionContext |
| 50 | +} |
| 51 | + |
| 52 | +describe("proxyFetch", () => { |
| 53 | + let mockHttpConfig: { get: ReturnType<typeof vi.fn> } |
| 54 | + let savedFetch: typeof globalThis.fetch |
| 55 | + |
| 56 | + beforeEach(() => { |
| 57 | + vi.clearAllMocks() |
| 58 | + vi.resetModules() |
| 59 | + |
| 60 | + savedFetch = globalThis.fetch |
| 61 | + |
| 62 | + mockHttpConfig = { |
| 63 | + get: vi.fn().mockReturnValue(undefined), |
| 64 | + } |
| 65 | + |
| 66 | + vi.mocked(vscode.workspace.getConfiguration).mockReturnValue( |
| 67 | + mockHttpConfig as unknown as vscode.WorkspaceConfiguration, |
| 68 | + ) |
| 69 | + |
| 70 | + // Clear proxy env vars |
| 71 | + delete process.env.HTTPS_PROXY |
| 72 | + delete process.env.https_proxy |
| 73 | + delete process.env.HTTP_PROXY |
| 74 | + delete process.env.http_proxy |
| 75 | + }) |
| 76 | + |
| 77 | + afterEach(() => { |
| 78 | + globalThis.fetch = savedFetch |
| 79 | + }) |
| 80 | + |
| 81 | + describe("resolveProxyUrl", () => { |
| 82 | + it("should return undefined when no proxy is configured", async () => { |
| 83 | + const { resolveProxyUrl } = await import("../proxyFetch") |
| 84 | + |
| 85 | + const result = resolveProxyUrl() |
| 86 | + |
| 87 | + expect(result).toBeUndefined() |
| 88 | + }) |
| 89 | + |
| 90 | + it("should return VSCode http.proxy setting when configured", async () => { |
| 91 | + mockHttpConfig.get.mockImplementation((key: string) => { |
| 92 | + if (key === "proxy") return "http://corporate-proxy:8080" |
| 93 | + return undefined |
| 94 | + }) |
| 95 | + |
| 96 | + const { resolveProxyUrl } = await import("../proxyFetch") |
| 97 | + |
| 98 | + const result = resolveProxyUrl() |
| 99 | + |
| 100 | + expect(result).toBe("http://corporate-proxy:8080") |
| 101 | + expect(vscode.workspace.getConfiguration).toHaveBeenCalledWith("http") |
| 102 | + }) |
| 103 | + |
| 104 | + it("should trim whitespace from VSCode proxy setting", async () => { |
| 105 | + mockHttpConfig.get.mockImplementation((key: string) => { |
| 106 | + if (key === "proxy") return " http://corporate-proxy:8080 " |
| 107 | + return undefined |
| 108 | + }) |
| 109 | + |
| 110 | + const { resolveProxyUrl } = await import("../proxyFetch") |
| 111 | + |
| 112 | + const result = resolveProxyUrl() |
| 113 | + |
| 114 | + expect(result).toBe("http://corporate-proxy:8080") |
| 115 | + }) |
| 116 | + |
| 117 | + it("should ignore empty VSCode proxy setting and fall back to env vars", async () => { |
| 118 | + mockHttpConfig.get.mockImplementation((key: string) => { |
| 119 | + if (key === "proxy") return " " |
| 120 | + return undefined |
| 121 | + }) |
| 122 | + process.env.HTTPS_PROXY = "http://env-proxy:3128" |
| 123 | + |
| 124 | + const { resolveProxyUrl } = await import("../proxyFetch") |
| 125 | + |
| 126 | + const result = resolveProxyUrl() |
| 127 | + |
| 128 | + expect(result).toBe("http://env-proxy:3128") |
| 129 | + }) |
| 130 | + |
| 131 | + it("should prefer HTTPS_PROXY over HTTP_PROXY", async () => { |
| 132 | + process.env.HTTPS_PROXY = "http://https-proxy:3128" |
| 133 | + process.env.HTTP_PROXY = "http://http-proxy:3128" |
| 134 | + |
| 135 | + const { resolveProxyUrl } = await import("../proxyFetch") |
| 136 | + |
| 137 | + const result = resolveProxyUrl() |
| 138 | + |
| 139 | + expect(result).toBe("http://https-proxy:3128") |
| 140 | + }) |
| 141 | + |
| 142 | + it("should fall back to lowercase env vars", async () => { |
| 143 | + process.env.https_proxy = "http://lowercase-proxy:3128" |
| 144 | + |
| 145 | + const { resolveProxyUrl } = await import("../proxyFetch") |
| 146 | + |
| 147 | + const result = resolveProxyUrl() |
| 148 | + |
| 149 | + expect(result).toBe("http://lowercase-proxy:3128") |
| 150 | + }) |
| 151 | + |
| 152 | + it("should prefer VSCode setting over env vars", async () => { |
| 153 | + mockHttpConfig.get.mockImplementation((key: string) => { |
| 154 | + if (key === "proxy") return "http://vscode-proxy:8080" |
| 155 | + return undefined |
| 156 | + }) |
| 157 | + process.env.HTTPS_PROXY = "http://env-proxy:3128" |
| 158 | + |
| 159 | + const { resolveProxyUrl } = await import("../proxyFetch") |
| 160 | + |
| 161 | + const result = resolveProxyUrl() |
| 162 | + |
| 163 | + expect(result).toBe("http://vscode-proxy:8080") |
| 164 | + }) |
| 165 | + }) |
| 166 | + |
| 167 | + describe("getProxyHttpAgent", () => { |
| 168 | + it("should return undefined when no proxy is configured", async () => { |
| 169 | + const { getProxyHttpAgent } = await import("../proxyFetch") |
| 170 | + |
| 171 | + const agent = getProxyHttpAgent() |
| 172 | + |
| 173 | + expect(agent).toBeUndefined() |
| 174 | + expect(mockHttpsProxyAgentConstructor).not.toHaveBeenCalled() |
| 175 | + }) |
| 176 | + |
| 177 | + it("should return an HttpsProxyAgent when proxy is configured", async () => { |
| 178 | + mockHttpConfig.get.mockImplementation((key: string) => { |
| 179 | + if (key === "proxy") return "http://corporate-proxy:8080" |
| 180 | + if (key === "proxyStrictSSL") return true |
| 181 | + return undefined |
| 182 | + }) |
| 183 | + |
| 184 | + const mockAgent = { mock: true } |
| 185 | + mockHttpsProxyAgentConstructor.mockReturnValue(mockAgent) |
| 186 | + |
| 187 | + const { getProxyHttpAgent } = await import("../proxyFetch") |
| 188 | + |
| 189 | + const agent = getProxyHttpAgent() |
| 190 | + |
| 191 | + expect(agent).toBe(mockAgent) |
| 192 | + expect(mockHttpsProxyAgentConstructor).toHaveBeenCalledWith("http://corporate-proxy:8080", { |
| 193 | + rejectUnauthorized: true, |
| 194 | + }) |
| 195 | + }) |
| 196 | + |
| 197 | + it("should disable TLS verification when proxyStrictSSL is false", async () => { |
| 198 | + mockHttpConfig.get.mockImplementation((key: string) => { |
| 199 | + if (key === "proxy") return "http://corporate-proxy:8080" |
| 200 | + if (key === "proxyStrictSSL") return false |
| 201 | + return undefined |
| 202 | + }) |
| 203 | + |
| 204 | + mockHttpsProxyAgentConstructor.mockReturnValue({ mock: true }) |
| 205 | + |
| 206 | + const { getProxyHttpAgent } = await import("../proxyFetch") |
| 207 | + |
| 208 | + getProxyHttpAgent() |
| 209 | + |
| 210 | + expect(mockHttpsProxyAgentConstructor).toHaveBeenCalledWith("http://corporate-proxy:8080", { |
| 211 | + rejectUnauthorized: false, |
| 212 | + }) |
| 213 | + }) |
| 214 | + |
| 215 | + it("should return undefined and log error when HttpsProxyAgent constructor throws", async () => { |
| 216 | + mockHttpConfig.get.mockImplementation((key: string) => { |
| 217 | + if (key === "proxy") return "http://bad-proxy:9999" |
| 218 | + if (key === "proxyStrictSSL") return true |
| 219 | + return undefined |
| 220 | + }) |
| 221 | + |
| 222 | + mockHttpsProxyAgentConstructor.mockImplementation(() => { |
| 223 | + throw new Error("Invalid proxy URL") |
| 224 | + }) |
| 225 | + |
| 226 | + const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) |
| 227 | + |
| 228 | + const { getProxyHttpAgent } = await import("../proxyFetch") |
| 229 | + |
| 230 | + const agent = getProxyHttpAgent() |
| 231 | + |
| 232 | + expect(agent).toBeUndefined() |
| 233 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 234 | + expect.stringContaining("[ProxyFetch] Failed to create HttpsProxyAgent"), |
| 235 | + ) |
| 236 | + |
| 237 | + consoleErrorSpy.mockRestore() |
| 238 | + }) |
| 239 | + |
| 240 | + it("should use env proxy when VSCode setting is not configured", async () => { |
| 241 | + process.env.HTTPS_PROXY = "http://env-proxy:3128" |
| 242 | + |
| 243 | + mockHttpsProxyAgentConstructor.mockReturnValue({ mock: true }) |
| 244 | + |
| 245 | + const { getProxyHttpAgent } = await import("../proxyFetch") |
| 246 | + |
| 247 | + const agent = getProxyHttpAgent() |
| 248 | + |
| 249 | + expect(agent).toBeDefined() |
| 250 | + expect(mockHttpsProxyAgentConstructor).toHaveBeenCalledWith("http://env-proxy:3128", { |
| 251 | + rejectUnauthorized: true, |
| 252 | + }) |
| 253 | + }) |
| 254 | + }) |
| 255 | +}) |
0 commit comments