UNPKG

aws-sdk-v3-proxy

Version:

A wrapper for adding proxy support to AWS SDK v3 clients

180 lines (179 loc) 7.92 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const client_s3_1 = require("@aws-sdk/client-s3"); const hpagent = __importStar(require("hpagent")); const node_http_handler_1 = require("@smithy/node-http-handler"); const src_1 = require("../src"); jest.mock('@smithy/node-http-handler'); jest.mock('hpagent'); const MockNodeHttpHandler = jest.mocked(node_http_handler_1.NodeHttpHandler); describe('add-proxy', () => { describe('addProxyToClient', () => { let client; let logSpy = jest.spyOn(global.console, 'log'); let proxyAgentSpy = jest.spyOn(hpagent, 'HttpsProxyAgent'); const PREV_ENV = process.env; beforeEach(() => { jest.resetAllMocks(); process.env = Object.assign({}, PREV_ENV); client = new client_s3_1.S3Client({ region: 'us-east-1', }); logSpy = jest.spyOn(global.console, 'log'); proxyAgentSpy = jest.spyOn(hpagent, 'HttpsProxyAgent'); }); afterEach(() => { process.env = PREV_ENV; }); it('should return an instance of the client with the NodeHttpHandler attached', () => { process.env.HTTPS_PROXY = 'https://localhost'; const s3 = (0, src_1.addProxyToClient)(client); expect(s3.config.requestHandler).toBeInstanceOf(node_http_handler_1.NodeHttpHandler); }); it('should throw when no proxy is found on env', () => { const error = new Error('Unable to add proxy to AWS SDK client. No proxy found in process.env'); const fn = () => (0, src_1.addProxyToClient)(client); expect(fn).toThrowError(error); }); it('should not throw when no proxy is found on env and throwOnNoProxy is false', () => { const opts = { throwOnNoProxy: false, }; const fn = () => (0, src_1.addProxyToClient)(client, opts); expect(fn).not.toThrow(); }); it('should attach an httpAgent AND httpsAgent when both proxies are set', () => { process.env.HTTP_PROXY = 'http://localhost'; process.env.HTTPS_PROXY = 'https://localhost'; (0, src_1.addProxyToClient)(client); expect(proxyAgentSpy).toHaveBeenNthCalledWith(1, { proxy: 'http://localhost', }); expect(proxyAgentSpy).toHaveBeenNthCalledWith(2, { proxy: 'https://localhost', }); }); it('should attach an httpAgent as the requestHandler when an http proxy is set', () => { process.env.HTTP_PROXY = 'http://localhost'; (0, src_1.addProxyToClient)(client); expect(proxyAgentSpy).toHaveBeenCalledTimes(1); expect(proxyAgentSpy).toHaveBeenCalledWith({ proxy: 'http://localhost', }); }); it('should attach an httpsAgent as the requestHandler when an https proxy is set', () => { process.env.HTTPS_PROXY = 'https://localhost'; (0, src_1.addProxyToClient)(client); expect(proxyAgentSpy).toHaveBeenCalledTimes(1); expect(proxyAgentSpy).toHaveBeenCalledWith({ proxy: 'https://localhost', }); }); it('should use https proxy when httpsOnly is true and both proxies are set on env', () => { const opts = { httpsOnly: true, }; process.env.HTTPS_PROXY = 'https://localhost'; process.env.HTTP_PROXY = 'http://localhost'; (0, src_1.addProxyToClient)(client, opts); expect(proxyAgentSpy).toHaveBeenCalledTimes(2); expect(proxyAgentSpy).toHaveBeenCalledWith({ proxy: 'https://localhost', }); }); it('should use https proxy when httpsOnly is true and only https proxy is set on env', () => { const opts = { httpsOnly: true, }; process.env.https_proxy = 'https://localhost'; (0, src_1.addProxyToClient)(client, opts); expect(proxyAgentSpy).toHaveBeenCalledTimes(1); expect(proxyAgentSpy).toHaveBeenCalledWith({ proxy: 'https://localhost', }); }); it('should pass additional agent options to the proxy agent', () => { const agentOptions = { keepAlive: true, keepAliveMsecs: 1000, maxSockets: 256, maxFreeSockets: 256, proxy: 'https://localhost:8080', proxyRequestOptions: { ca: ['sample-cert'], }, }; process.env.http_proxy = 'http://localhost'; (0, src_1.addProxyToClient)(client, { agentOptions }); expect(proxyAgentSpy).toHaveBeenCalledWith(agentOptions); }); it('should print to the console when debug is set to true', () => { const opts = { debug: true, }; process.env.HTTP_PROXY = 'http://localhost'; (0, src_1.addProxyToClient)(client, opts); expect(logSpy).toHaveBeenCalledTimes(1); }); it('should not print to the console when debug is set to false', () => { const opts = { debug: false, }; process.env.HTTP_PROXY = 'http://localhost'; (0, src_1.addProxyToClient)(client, opts); expect(logSpy).not.toHaveBeenCalled(); }); it('should add connectionTimeout when provided', () => { const opts = { connectionTimeout: 100, }; process.env.HTTP_PROXY = 'http://localhost'; (0, src_1.addProxyToClient)(client, opts); expect(MockNodeHttpHandler).toHaveBeenCalledWith(expect.objectContaining(opts)); }); it('should add socketTimeout when provided', () => { const opts = { socketTimeout: 10, }; process.env.HTTP_PROXY = 'http://localhost'; (0, src_1.addProxyToClient)(client, opts); expect(MockNodeHttpHandler).toHaveBeenCalledWith(expect.objectContaining(opts)); }); it('should attach an httpAgent AND httpsAgent when both proxies are set by options', () => { const opts = { httpProxy: 'http://localhost', httpsProxy: 'https://localhost', }; (0, src_1.addProxyToClient)(client, opts); expect(proxyAgentSpy).toHaveBeenNthCalledWith(1, { proxy: 'http://localhost', }); expect(proxyAgentSpy).toHaveBeenNthCalledWith(2, { proxy: 'https://localhost', }); }); }); });