From 91d3933eb52b351f437151400a88ba7d57442a9b Mon Sep 17 00:00:00 2001 From: Ferenc Hammerl <31069338+fhammerl@users.noreply.github.com> Date: Thu, 22 Jun 2023 11:03:38 +0200 Subject: [PATCH] Prepend http:// to http(s)_proxy env if missing (#1439) * Prepend http:// to http(s)_proxy env if missing * Formatting * Fix linting --- packages/http-client/__tests__/proxy.test.ts | 6 ++++++ packages/http-client/src/proxy.ts | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/http-client/__tests__/proxy.test.ts b/packages/http-client/__tests__/proxy.test.ts index 97cf7853..1a0e28a7 100644 --- a/packages/http-client/__tests__/proxy.test.ts +++ b/packages/http-client/__tests__/proxy.test.ts @@ -91,6 +91,12 @@ describe('proxy', () => { expect(proxyUrl).toBeDefined() }) + it('getProxyUrl returns proxyUrl if http_proxy has no protocol', () => { + process.env['http_proxy'] = 'myproxysvr' + const proxyUrl = pm.getProxyUrl(new URL('http://github.com')) + expect(proxyUrl?.toString()).toBe('http://myproxysvr/') + }) + it('checkBypass returns true if host as no_proxy list', () => { process.env['no_proxy'] = 'myserver' const bypass = pm.checkBypass(new URL('https://myserver')) diff --git a/packages/http-client/src/proxy.ts b/packages/http-client/src/proxy.ts index 1a967e34..32afce6a 100644 --- a/packages/http-client/src/proxy.ts +++ b/packages/http-client/src/proxy.ts @@ -14,7 +14,12 @@ export function getProxyUrl(reqUrl: URL): URL | undefined { })() if (proxyVar) { - return new URL(proxyVar) + try { + return new URL(proxyVar) + } catch { + if (!proxyVar.startsWith('http://') && !proxyVar.startsWith('https://')) + return new URL(`http://${proxyVar}`) + } } else { return undefined }