node-http-proxy.test.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { afterEach, describe, expect, it } from "vitest";
  2. import { resolveHttpProxyUrlForTarget, UNSUPPORTED_PROXY_PROTOCOL_MESSAGE } from "../src/utils/node-http-proxy.ts";
  3. const PROXY_ENV_KEYS = [
  4. "HTTP_PROXY",
  5. "HTTPS_PROXY",
  6. "NO_PROXY",
  7. "ALL_PROXY",
  8. "http_proxy",
  9. "https_proxy",
  10. "no_proxy",
  11. "all_proxy",
  12. "npm_config_http_proxy",
  13. "npm_config_https_proxy",
  14. "npm_config_proxy",
  15. "npm_config_no_proxy",
  16. ] as const;
  17. const originalEnv = new Map<string, string | undefined>();
  18. for (const key of PROXY_ENV_KEYS) {
  19. originalEnv.set(key, process.env[key]);
  20. }
  21. function resetProxyEnv(): void {
  22. for (const key of PROXY_ENV_KEYS) {
  23. delete process.env[key];
  24. }
  25. }
  26. afterEach(() => {
  27. resetProxyEnv();
  28. for (const [key, value] of originalEnv) {
  29. if (value !== undefined) {
  30. process.env[key] = value;
  31. }
  32. }
  33. });
  34. describe("node HTTP proxy resolution", () => {
  35. it("respects NO_PROXY exclusions", () => {
  36. resetProxyEnv();
  37. process.env.HTTPS_PROXY = "http://proxy.example:8080";
  38. process.env.NO_PROXY = "bedrock-runtime.us-east-1.amazonaws.com";
  39. expect(resolveHttpProxyUrlForTarget("https://bedrock-runtime.us-east-1.amazonaws.com")).toBeUndefined();
  40. });
  41. it("resolves HTTP and HTTPS proxy URLs", () => {
  42. resetProxyEnv();
  43. process.env.HTTPS_PROXY = "http://proxy.example:8080";
  44. expect(resolveHttpProxyUrlForTarget("https://bedrock-runtime.us-east-1.amazonaws.com")?.toString()).toBe(
  45. "http://proxy.example:8080/",
  46. );
  47. });
  48. it("rejects SOCKS and PAC proxy URLs explicitly", () => {
  49. resetProxyEnv();
  50. process.env.HTTPS_PROXY = "socks5://proxy.example:1080";
  51. expect(() => resolveHttpProxyUrlForTarget("https://bedrock-runtime.us-east-1.amazonaws.com")).toThrow(
  52. UNSUPPORTED_PROXY_PROTOCOL_MESSAGE,
  53. );
  54. });
  55. });