From e33cc22497f705bc94e70c8289e0b5b2abac0744 Mon Sep 17 00:00:00 2001 From: SuZhou-Joe Date: Tue, 16 Apr 2024 11:06:25 +0800 Subject: [PATCH] [Workspace] Add base path when parsing url in http service (#6233) (#6451) * fix: add base path when parse url in http service Signed-off-by: SuZhou-Joe * feat: add CHANGELOG Signed-off-by: SuZhou-Joe * feat: optimize unit test cases for parse clientBasePath from url when basePath enabled Signed-off-by: SuZhou-Joe * feat: add empty line before getWorkspaceIdFromURL method Signed-off-by: SuZhou-Joe * feat: optimize comment Signed-off-by: SuZhou-Joe * feat: optimize code Signed-off-by: SuZhou-Joe --------- Signed-off-by: SuZhou-Joe Co-authored-by: Xinrui Bai-amazon <139305463+xinruiba@users.noreply.github.com> (cherry picked from commit 0dce00a806bf8815ca84802541010767b6b3a8bd) --- src/core/public/http/http_service.test.ts | 7 ++++--- src/core/public/http/http_service.ts | 2 +- src/core/utils/workspace.test.ts | 4 ++-- src/core/utils/workspace.ts | 2 +- src/plugins/workspace/public/plugin.ts | 8 ++++---- src/plugins/workspace/server/plugin.ts | 5 ++++- 6 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/core/public/http/http_service.test.ts b/src/core/public/http/http_service.test.ts index 5671064e4c5..73010e1c5a9 100644 --- a/src/core/public/http/http_service.test.ts +++ b/src/core/public/http/http_service.test.ts @@ -83,21 +83,22 @@ describe('#setup()', () => { expect(setupResult.basePath.get()).toEqual(''); }); - it('setup basePath with workspaceId provided in window.location.href', () => { + it('setup basePath with workspaceId provided in window.location.href and basePath present in injectedMetadata', () => { const windowSpy = jest.spyOn(window, 'window', 'get'); windowSpy.mockImplementation( () => ({ location: { - href: 'http://localhost/w/workspaceId/app', + href: 'http://localhost/base_path/w/workspaceId/app', }, } as any) ); const injectedMetadata = injectedMetadataServiceMock.createSetupContract(); + injectedMetadata.getBasePath.mockReturnValue('/base_path'); const fatalErrors = fatalErrorsServiceMock.createSetupContract(); const httpService = new HttpService(); const setupResult = httpService.setup({ fatalErrors, injectedMetadata }); - expect(setupResult.basePath.get()).toEqual('/w/workspaceId'); + expect(setupResult.basePath.get()).toEqual('/base_path/w/workspaceId'); windowSpy.mockRestore(); }); }); diff --git a/src/core/public/http/http_service.ts b/src/core/public/http/http_service.ts index 6832703c792..c1a538dfca1 100644 --- a/src/core/public/http/http_service.ts +++ b/src/core/public/http/http_service.ts @@ -53,7 +53,7 @@ export class HttpService implements CoreService { public setup({ injectedMetadata, fatalErrors }: HttpDeps): HttpSetup { const opensearchDashboardsVersion = injectedMetadata.getOpenSearchDashboardsVersion(); let clientBasePath = ''; - const workspaceId = getWorkspaceIdFromUrl(window.location.href); + const workspaceId = getWorkspaceIdFromUrl(window.location.href, injectedMetadata.getBasePath()); if (workspaceId) { clientBasePath = `${WORKSPACE_PATH_PREFIX}/${workspaceId}`; } diff --git a/src/core/utils/workspace.test.ts b/src/core/utils/workspace.test.ts index a852ddcc519..722bbca566e 100644 --- a/src/core/utils/workspace.test.ts +++ b/src/core/utils/workspace.test.ts @@ -8,11 +8,11 @@ import { httpServiceMock } from '../public/mocks'; describe('#getWorkspaceIdFromUrl', () => { it('return workspace when there is a match', () => { - expect(getWorkspaceIdFromUrl('http://localhost/w/foo')).toEqual('foo'); + expect(getWorkspaceIdFromUrl('http://localhost/w/foo', '')).toEqual('foo'); }); it('return empty when there is not a match', () => { - expect(getWorkspaceIdFromUrl('http://localhost/w2/foo')).toEqual(''); + expect(getWorkspaceIdFromUrl('http://localhost/w2/foo', '')).toEqual(''); }); it('return workspace when there is a match with basePath provided', () => { diff --git a/src/core/utils/workspace.ts b/src/core/utils/workspace.ts index c383967483a..f16b28423b6 100644 --- a/src/core/utils/workspace.ts +++ b/src/core/utils/workspace.ts @@ -6,7 +6,7 @@ import { WORKSPACE_PATH_PREFIX } from './constants'; import { IBasePath } from '../public'; -export const getWorkspaceIdFromUrl = (url: string, basePath?: string): string => { +export const getWorkspaceIdFromUrl = (url: string, basePath: string): string => { const regexp = new RegExp(`^${basePath || ''}\/w\/([^\/]*)`); const urlObject = new URL(url); const matchedResult = urlObject.pathname.match(regexp); diff --git a/src/plugins/workspace/public/plugin.ts b/src/plugins/workspace/public/plugin.ts index e3ecdc34bfb..c1ae0cfaaf1 100644 --- a/src/plugins/workspace/public/plugin.ts +++ b/src/plugins/workspace/public/plugin.ts @@ -30,9 +30,6 @@ export class WorkspacePlugin implements Plugin<{}, {}, {}> { }); } } - private getWorkspaceIdFromURL(basePath?: string): string | null { - return getWorkspaceIdFromUrl(window.location.href, basePath); - } public async setup(core: CoreSetup) { const workspaceClient = new WorkspaceClient(core.http, core.workspaces); await workspaceClient.init(); @@ -40,7 +37,10 @@ export class WorkspacePlugin implements Plugin<{}, {}, {}> { /** * Retrieve workspace id from url */ - const workspaceId = this.getWorkspaceIdFromURL(core.http.basePath.getBasePath()); + const workspaceId = getWorkspaceIdFromUrl( + window.location.href, + core.http.basePath.getBasePath() + ); if (workspaceId) { const result = await workspaceClient.enterWorkspace(workspaceId); diff --git a/src/plugins/workspace/server/plugin.ts b/src/plugins/workspace/server/plugin.ts index 6f126ebd503..56abf1afb91 100644 --- a/src/plugins/workspace/server/plugin.ts +++ b/src/plugins/workspace/server/plugin.ts @@ -27,7 +27,10 @@ export class WorkspacePlugin implements Plugin<{}, {}> { * Proxy all {basePath}/w/{workspaceId}{osdPath*} paths to {basePath}{osdPath*} */ setupDeps.http.registerOnPreRouting(async (request, response, toolkit) => { - const workspaceId = getWorkspaceIdFromUrl(request.url.toString()); + const workspaceId = getWorkspaceIdFromUrl( + request.url.toString(), + '' // No need to pass basePath here because the request.url will be rewrite by registerOnPreRouting method in `src/core/server/http/http_server.ts` + ); if (workspaceId) { const requestUrl = new URL(request.url.toString());