diff --git a/lib/helper/Playwright.js b/lib/helper/Playwright.js index f7a912895..522c46d71 100644 --- a/lib/helper/Playwright.js +++ b/lib/helper/Playwright.js @@ -27,6 +27,7 @@ import { normalizeSpacesInString, normalizePath, resolveUrl, + appendBaseUrl, relativeDir, getMimeType, base64EncodeFile, @@ -1365,7 +1366,7 @@ class Playwright extends Helper { } if (!/^\w+\:(\/\/|.+)/.test(url)) { - url = this.options.url + (!this.options.url.endsWith('/') && url.startsWith('/') ? url : `/${url}`) + url = appendBaseUrl(this.options.url, url) this.debug(`Changed URL to base url + relative path: ${url}`) } @@ -3124,7 +3125,7 @@ class Playwright extends Helper { if (url.startsWith('/')) { // local url - url = this.options.url + url + url = appendBaseUrl(this.options.url, url) this.debugSection('URL', url) } diff --git a/lib/helper/Puppeteer.js b/lib/helper/Puppeteer.js index 0b8bf2a5c..a679477a1 100644 --- a/lib/helper/Puppeteer.js +++ b/lib/helper/Puppeteer.js @@ -30,6 +30,7 @@ import { normalizeSpacesInString, normalizePath, resolveUrl, + appendBaseUrl, getMimeType, base64EncodeFile, } from '../utils.js' @@ -751,7 +752,7 @@ class Puppeteer extends Helper { */ async amOnPage(url) { if (!/^\w+\:\/\//.test(url)) { - url = this.options.url + url + url = appendBaseUrl(this.options.url, url) } if (this.options.basicAuth && this.isAuthenticated !== true) { diff --git a/lib/helper/WebDriver.js b/lib/helper/WebDriver.js index 847af2eee..2a8890697 100644 --- a/lib/helper/WebDriver.js +++ b/lib/helper/WebDriver.js @@ -27,6 +27,7 @@ import { modifierKeys, normalizePath, resolveUrl, + appendBaseUrl, getMimeType, base64EncodeFile, } from '../utils.js' @@ -1063,7 +1064,7 @@ class WebDriver extends Helper { let split_url if (this.options.basicAuth) { if (url.startsWith('/')) { - url = this.options.url + url + url = appendBaseUrl(this.options.url, url) } split_url = url.split('//') url = `${split_url[0]}//${this.options.basicAuth.username}:${this.options.basicAuth.password}@${split_url[1]}` diff --git a/lib/utils.js b/lib/utils.js index c3ef1b1eb..0b7a30109 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -372,11 +372,7 @@ function shouldAppendBaseUrl(url) { } function trimUrl(url) { - const firstChar = url.substr(1) - if (firstChar === '/') { - url = url.slice(1) - } - return url + return url.replace(/^\/+/, '') } function joinUrl(baseUrl, url) { @@ -390,11 +386,7 @@ export const appendBaseUrl = function (baseUrl = '', oneOrMoreUrls) { if (!(typeof oneOrMoreUrls === 'string' || Array.isArray(oneOrMoreUrls))) { throw new Error(`Expected type of Urls is 'string' or 'array', Found '${typeof oneOrMoreUrls}'.`) } - // Remove '/' if it's at the end of baseUrl - const lastChar = baseUrl.substr(-1) - if (lastChar === '/') { - baseUrl = baseUrl.slice(0, -1) - } + baseUrl = baseUrl.replace(/\/+$/, '') if (!Array.isArray(oneOrMoreUrls)) { return joinUrl(baseUrl, oneOrMoreUrls) diff --git a/test/unit/utils_test.js b/test/unit/utils_test.js index 0e034e427..ad950a2f3 100644 --- a/test/unit/utils_test.js +++ b/test/unit/utils_test.js @@ -47,6 +47,25 @@ describe('utils', () => { }) }) + describe('#appendBaseUrl', () => { + it('should join base url and path with a single slash', () => { + expect(utils.appendBaseUrl('http://localhost', '/login')).equal('http://localhost/login') + expect(utils.appendBaseUrl('http://localhost', 'login')).equal('http://localhost/login') + expect(utils.appendBaseUrl('http://localhost/', '/login')).equal('http://localhost/login') + expect(utils.appendBaseUrl('http://localhost/', 'login')).equal('http://localhost/login') + }) + + it('should keep the path of the base url', () => { + expect(utils.appendBaseUrl('http://localhost/app/', '/')).equal('http://localhost/app/') + expect(utils.appendBaseUrl('http://localhost/app/', '/login')).equal('http://localhost/app/login') + expect(utils.appendBaseUrl('http://localhost/app', '?q=1')).equal('http://localhost/app/?q=1') + }) + + it('should not touch absolute urls', () => { + expect(utils.appendBaseUrl('http://localhost', 'https://example.com/a')).equal('https://example.com/a') + }) + }) + describe('#beautify', () => { it('should beautify JS code', () => { expect(utils.beautify('module.exports = function(a, b) { a++; b = a; if (a == b) { return 2 }};')).eql(`module.exports = function(a, b) {