Use built-in URL data type instead of custom parse

The getHomepageUrl function searches for /# when trying to find hash
anchors in the URL. This misses URLs that don't end with a trailing slash
(i.e http://localhost:3000/auth_callback#state=231231231)

To fix this, this commit switches to using the built-in URL data type
which handles this case and all the other cases.

Change-Id: I15827f4cb4c28f3163a4b4e84b872331b972b156
Signed-off-by: Flavio Percoco <flavio@pacerevenue.com>
Co-Authored-By: Radosław Piliszek <radoslaw.piliszek@gmail.com>
This commit is contained in:
Flavio Percoco 2023-06-07 11:53:59 +01:00 committed by Radosław Piliszek
parent 6e935f3ad7
commit e7c79dad8e
2 changed files with 70 additions and 43 deletions

View File

@ -20,12 +20,12 @@ export function setAuthToken(token) {
authToken = token
}
function getHomepageUrl(url) {
function getHomepageUrl() {
//
// Discover serving location from href.
//
// This is only needed for sub-directory serving.
// Serving the application from '/' may simply default to '/'
// This is only needed for sub-directory serving. Serving the application
// from 'scheme://domain/' may simply default to 'scheme://domain/'
//
// Note that this is not enough for sub-directory serving,
// The static files location also needs to be adapted with the 'homepage'
@ -33,49 +33,15 @@ function getHomepageUrl(url) {
//
// This homepage url is used for the Router and Link resolution logic
//
let baseUrl
if (url) {
baseUrl = url
let url = new URL(window.location.href)
if ('PUBLIC_URL' in process.env) {
url.pathname = process.env.PUBLIC_URL
} else {
baseUrl = window.location.href
}
// Get dirname of the current url
baseUrl = baseUrl.replace(/\\/g, '/').replace(/\/[^/]*$/, '/')
// Remove any query strings
if (baseUrl.includes('?')) {
baseUrl = baseUrl.slice(0, baseUrl.lastIndexOf('?'))
}
// Remove any hash anchor
if (baseUrl.includes('/#')) {
baseUrl = baseUrl.slice(0, baseUrl.lastIndexOf('/#') + 1)
url.pathname = ''
}
// Remove known sub-path
const subDir = [
'/autohold/',
'/build/',
'/buildset/',
'/job/',
'/project/',
'/stream/',
'/status/',
]
subDir.forEach(path => {
if (baseUrl.includes(path)) {
baseUrl = baseUrl.slice(0, baseUrl.lastIndexOf(path) + 1)
}
})
// Remove tenant scope
if (baseUrl.includes('/t/')) {
baseUrl = baseUrl.slice(0, baseUrl.lastIndexOf('/t/') + 1)
}
if (!baseUrl.endsWith('/')) {
baseUrl = baseUrl + '/'
}
// console.log('Homepage url is ', baseUrl)
return baseUrl
return url.origin + url.pathname
}
function getZuulUrl() {

61
web/src/api.test.js Normal file
View File

@ -0,0 +1,61 @@
import { getHomepageUrl } from './api'
it('should should return the homepage url', () => {
const homepage = 'https://my-zuul.com/'
Object.defineProperty(window, 'location', {
value: new URL(homepage)
} )
// Test some of the known, possible, URLs to verify
// that the origin is returned.
const urls = [
// auth_callback test as some providers build
// different callback urls
'https://my-zuul.com/auth_callback',
'https://my-zuul.com/auth_callback#state=12345',
// Regular browser navigation urls
'https://my-zuul.com/status',
'https://my-zuul.com/t/zuul-tenant/status',
'https://my-zuul.com/t/zuul-tenant/jobs',
// API urls
'https://my-zuul.com/api/tenant/zuul-tenant/status',
'https://my-zuul.com/api/tenant/zuul-tenant/authorization',
]
for (let url of urls) {
window.location.href = url
expect(getHomepageUrl()).toEqual(homepage)
}
})
it('should return the subdir homepage url', () => {
const homepage = 'https://example.com/zuul/'
Object.defineProperty(window, 'location', {
value: new URL(homepage)
} )
Object.defineProperty(process.env, 'PUBLIC_URL', {
value: '/zuul/'
} )
// Test some of the known, possible, URLs to verify
// that the origin is returned.
const urls = [
// auth_callback test as some providers build
// different callback urls
'https://example.com/zuul/auth_callback',
'https://example.com/zuul/auth_callback#state=12345',
// Regular browser navigation urls
'https://example.com/zuul/status',
'https://example.com/zuul/t/zuul-tenant/status',
'https://example.com/zuul/t/zuul-tenant/jobs',
]
for (let url of urls) {
window.location.href = url
expect(getHomepageUrl()).toEqual(homepage)
}
})