From ce545724bdac6ec98f316b11015a5089012a61ea Mon Sep 17 00:00:00 2001 From: Nelu Platonov Date: Tue, 28 Nov 2023 13:39:55 +0100 Subject: [PATCH] feat(#946): Use Regex to check if URL has protocol --- .../bruno-electron/src/ipc/network/prepare-request.js | 8 ++++---- .../bruno-electron/tests/network/prepare-request.spec.js | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/bruno-electron/src/ipc/network/prepare-request.js b/packages/bruno-electron/src/ipc/network/prepare-request.js index 2d9d562e..c5ec9564 100644 --- a/packages/bruno-electron/src/ipc/network/prepare-request.js +++ b/packages/bruno-electron/src/ipc/network/prepare-request.js @@ -70,14 +70,14 @@ const setAuthHeaders = (axiosRequest, request, collectionRoot) => { return axiosRequest; }; -const PROTOCOLS = ['http://', 'https://', 'wss://']; +const protocolRegex = /([a-zA-Z]{2,20}:\/\/)(.*)/; const prepareRequest = (request, collectionRoot) => { const headers = {}; let contentTypeDefined = false; let url = request.url; - if (PROTOCOLS.find((protocol) => url.startsWith(protocol)) === undefined) { + if (!protocolRegex.test(url)) { url = `http://${url}`; } @@ -102,8 +102,8 @@ const prepareRequest = (request, collectionRoot) => { let axiosRequest = { method: request.method, - url: url, - headers: headers, + url, + headers, responseType: 'arraybuffer' }; diff --git a/packages/bruno-electron/tests/network/prepare-request.spec.js b/packages/bruno-electron/tests/network/prepare-request.spec.js index 41574fe9..5c97f23a 100644 --- a/packages/bruno-electron/tests/network/prepare-request.spec.js +++ b/packages/bruno-electron/tests/network/prepare-request.spec.js @@ -5,4 +5,9 @@ describe('prepare-request: prepareRequest', () => { const request = prepareRequest({ method: 'GET', url: 'test', body: {} }); expect(request.url).toEqual('http://test'); }); + + it("Should NOT add 'http://' to the URL if a protocol is specified", () => { + const request = prepareRequest({ method: 'GET', url: 'ftp://test', body: {} }); + expect(request.url).toEqual('ftp://test'); + }); });