feat(#946): Use Regex to check if URL has protocol

This commit is contained in:
Nelu Platonov 2023-11-28 13:39:55 +01:00
parent 2b08468581
commit ce545724bd
No known key found for this signature in database
GPG Key ID: DC1D6718097E5B33
2 changed files with 9 additions and 4 deletions

View File

@ -70,14 +70,14 @@ const setAuthHeaders = (axiosRequest, request, collectionRoot) => {
return axiosRequest; return axiosRequest;
}; };
const PROTOCOLS = ['http://', 'https://', 'wss://']; const protocolRegex = /([a-zA-Z]{2,20}:\/\/)(.*)/;
const prepareRequest = (request, collectionRoot) => { const prepareRequest = (request, collectionRoot) => {
const headers = {}; const headers = {};
let contentTypeDefined = false; let contentTypeDefined = false;
let url = request.url; let url = request.url;
if (PROTOCOLS.find((protocol) => url.startsWith(protocol)) === undefined) { if (!protocolRegex.test(url)) {
url = `http://${url}`; url = `http://${url}`;
} }
@ -102,8 +102,8 @@ const prepareRequest = (request, collectionRoot) => {
let axiosRequest = { let axiosRequest = {
method: request.method, method: request.method,
url: url, url,
headers: headers, headers,
responseType: 'arraybuffer' responseType: 'arraybuffer'
}; };

View File

@ -5,4 +5,9 @@ describe('prepare-request: prepareRequest', () => {
const request = prepareRequest({ method: 'GET', url: 'test', body: {} }); const request = prepareRequest({ method: 'GET', url: 'test', body: {} });
expect(request.url).toEqual('http://test'); 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');
});
}); });