From 2b084685819a61a5ffaec1a89ccfa37902e42e15 Mon Sep 17 00:00:00 2001 From: Nelu Platonov Date: Mon, 27 Nov 2023 22:13:28 +0100 Subject: [PATCH 1/2] feat(#946): Prepend 'http://' to URL if no protocol is specified --- .../bruno-electron/src/ipc/network/prepare-request.js | 9 ++++++++- .../bruno-electron/tests/network/prepare-request.spec.js | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 packages/bruno-electron/tests/network/prepare-request.spec.js diff --git a/packages/bruno-electron/src/ipc/network/prepare-request.js b/packages/bruno-electron/src/ipc/network/prepare-request.js index bd5f7a8d3..2d9d562e4 100644 --- a/packages/bruno-electron/src/ipc/network/prepare-request.js +++ b/packages/bruno-electron/src/ipc/network/prepare-request.js @@ -70,9 +70,16 @@ const setAuthHeaders = (axiosRequest, request, collectionRoot) => { return axiosRequest; }; +const PROTOCOLS = ['http://', 'https://', 'wss://']; + const prepareRequest = (request, collectionRoot) => { const headers = {}; let contentTypeDefined = false; + let url = request.url; + + if (PROTOCOLS.find((protocol) => url.startsWith(protocol)) === undefined) { + url = `http://${url}`; + } // collection headers each(get(collectionRoot, 'request.headers', []), (h) => { @@ -95,7 +102,7 @@ const prepareRequest = (request, collectionRoot) => { let axiosRequest = { method: request.method, - url: request.url, + url: url, headers: 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 new file mode 100644 index 000000000..41574fe96 --- /dev/null +++ b/packages/bruno-electron/tests/network/prepare-request.spec.js @@ -0,0 +1,8 @@ +const prepareRequest = require('../../src/ipc/network/prepare-request'); + +describe('prepare-request: prepareRequest', () => { + it("Should add 'http://' to the URL if no protocol is specified", () => { + const request = prepareRequest({ method: 'GET', url: 'test', body: {} }); + expect(request.url).toEqual('http://test'); + }); +}); From ce545724bdac6ec98f316b11015a5089012a61ea Mon Sep 17 00:00:00 2001 From: Nelu Platonov Date: Tue, 28 Nov 2023 13:39:55 +0100 Subject: [PATCH 2/2] 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 2d9d562e4..c5ec95640 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 41574fe96..5c97f23a5 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'); + }); });