Skip to content

Commit

Permalink
fix(core): getBasePath() to always return path without trailing slash…
Browse files Browse the repository at this point in the history
… '/'
  • Loading branch information
tadayosi committed Oct 26, 2023
1 parent c76f448 commit 3d858a6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
12 changes: 12 additions & 0 deletions packages/hawtio/src/core/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,23 @@ describe('HawtioCore', () => {
expect(hawtio.getBasePath()).toEqual('/test')
})

test('base path with base href ending with slash in document head', () => {
document.head.innerHTML = `
<base href='/test/'/>
`
expect(hawtio.getBasePath()).toEqual('/test')
})

test('custom base path', () => {
hawtio.setBasePath('/custom')
expect(hawtio.getBasePath()).toEqual('/custom')
})

test('custom base path with trailing slash', () => {
hawtio.setBasePath('/custom/')
expect(hawtio.getBasePath()).toEqual('/custom')
})

test('bootstrap', async () => {
// response for fetching 'plugin'
fetchMock.mockResponse(async req => {
Expand Down
22 changes: 20 additions & 2 deletions packages/hawtio/src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,31 @@ class HawtioCore {
*/
private plugins: Plugins = {}

/**
* Sets the base path of the Hawtio console.
* If the given path includes trailing '/', it will be trimmed.
*/
setBasePath(path: string) {
this.basePath = path
if (path.length > 1 && path.endsWith('/')) {
// Remove trailing '/'
this.basePath = path.slice(0, -1)
} else {
this.basePath = path
}
}

/**
* Returns the base path of the Hawtio console without trailing '/'.
*/
getBasePath(): string | undefined {
if (!this.basePath) {
this.basePath = this.documentBase()
const basePath = this.documentBase()
if (basePath && basePath.length > 1 && basePath.endsWith('/')) {
// Remove trailing '/'
this.basePath = basePath.slice(0, -1)
} else {
this.basePath = basePath
}
}
return this.basePath
}
Expand Down
13 changes: 6 additions & 7 deletions packages/hawtio/src/plugins/shared/jolokia-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,12 @@ class JolokiaService implements IJolokiaService {
// If window was opened to connect to remote Jolokia endpoint
if (url.searchParams.has(PARAM_KEY_CONNECTION)) {
// ... and not showing the login modal
if (url.pathname !== '/connect/login') {
const basePath = hawtio.getBasePath()
const loginPath = `${basePath}/connect/login`
if (url.pathname !== loginPath) {
this.jolokia?.then(jolokia => jolokia.stop())
const redirectUrl = window.location.href
url.pathname = '/connect/login'
url.pathname = loginPath
url.searchParams.append('redirect', redirectUrl)
window.location.href = url.href
}
Expand Down Expand Up @@ -393,11 +395,8 @@ class JolokiaService implements IJolokiaService {
return `${origin}${jolokiaUrl}`
}

let basePath = hawtio.getBasePath() ?? ''
if (!basePath.endsWith('/')) {
basePath += '/'
}
return `${origin}${basePath + jolokiaUrl}`
const basePath = hawtio.getBasePath() ?? ''
return `${origin}${basePath}/${jolokiaUrl}`
}

async getListMethod(): Promise<JolokiaListMethod> {
Expand Down

0 comments on commit 3d858a6

Please sign in to comment.