|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { describe, test, expect, beforeAll, afterAll, beforeEach } from '@jest/globals'; |
| 7 | +import { getProxyAgent } from '../../../../src/packageManager/proxy'; |
| 8 | +import url from 'url'; |
| 9 | + |
| 10 | +describe(`${getProxyAgent.name}`, () => { |
| 11 | + let originalHttpProxy: string | undefined; |
| 12 | + let originalHttpsProxy: string | undefined; |
| 13 | + |
| 14 | + const requestUrl = url.parse('https://github.com'); |
| 15 | + |
| 16 | + beforeAll(() => { |
| 17 | + originalHttpProxy = process.env.HTTP_PROXY; |
| 18 | + originalHttpsProxy = process.env.HTTPS_PROXY; |
| 19 | + }); |
| 20 | + |
| 21 | + afterAll(() => { |
| 22 | + if (originalHttpProxy !== undefined) { |
| 23 | + process.env.HTTP_PROXY = originalHttpProxy; |
| 24 | + } else { |
| 25 | + delete process.env.HTTP_PROXY; |
| 26 | + } |
| 27 | + |
| 28 | + if (originalHttpsProxy !== undefined) { |
| 29 | + process.env.HTTPS_PROXY = originalHttpsProxy; |
| 30 | + } else { |
| 31 | + delete process.env.HTTPS_PROXY; |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + delete process.env.HTTP_PROXY; |
| 37 | + delete process.env.HTTPS_PROXY; |
| 38 | + }); |
| 39 | + |
| 40 | + test('Returns `undefined` for empty proxy url and `undefined` HTTP_PROXY env', async () => { |
| 41 | + const result = getProxyAgent(requestUrl, /* proxy */ '', /* strictSSL */ false); |
| 42 | + expect(result).toBe(undefined); |
| 43 | + }); |
| 44 | + |
| 45 | + test('Returns `undefined` for empty proxy url and empty HTTP_PROXY env', async () => { |
| 46 | + process.env.HTTP_PROXY = ''; |
| 47 | + process.env.HTTPS_PROXY = ''; |
| 48 | + |
| 49 | + const result = getProxyAgent(requestUrl, /* proxy */ '', /* strictSSL */ false); |
| 50 | + expect(result).toBe(undefined); |
| 51 | + }); |
| 52 | +}); |
0 commit comments