Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/helper/Playwright.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
normalizeSpacesInString,
normalizePath,
resolveUrl,
appendBaseUrl,
relativeDir,
getMimeType,
base64EncodeFile,
Expand Down Expand Up @@ -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}`)
}

Expand Down Expand Up @@ -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)
}

Expand Down
3 changes: 2 additions & 1 deletion lib/helper/Puppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
normalizeSpacesInString,
normalizePath,
resolveUrl,
appendBaseUrl,
getMimeType,
base64EncodeFile,
} from '../utils.js'
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion lib/helper/WebDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
modifierKeys,
normalizePath,
resolveUrl,
appendBaseUrl,
getMimeType,
base64EncodeFile,
} from '../utils.js'
Expand Down Expand Up @@ -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]}`
Expand Down
12 changes: 2 additions & 10 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions test/unit/utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading