From f39792319ad2eef57ec887f7fd6c4fc1b4aaa867 Mon Sep 17 00:00:00 2001 From: sipayrt Date: Fri, 6 Sep 2024 14:59:56 +0300 Subject: [PATCH] fix(gui): wrong base url for tests in gui mode --- lib/common-utils.ts | 24 ++++++++++--------- .../icons/view-in-browser/index.jsx | 4 ++-- .../new-ui/components/MetaInfo/index.tsx | 4 ++-- test/unit/lib/common-utils.ts | 4 ++-- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/lib/common-utils.ts b/lib/common-utils.ts index 671c2b751..8d4218b2d 100644 --- a/lib/common-utils.ts +++ b/lib/common-utils.ts @@ -71,22 +71,24 @@ export const determineFinalStatus = (statuses: TestStatus[]): TestStatus | null }; export const getUrlWithBase = (url: string | undefined, base: string | undefined): string => { + if (isEmpty(base)) { + return url ?? ''; + } + try { const userUrl = new URL(url ?? '', base); - // Manually overriding properties, because if url is absolute, the above won't work - if (!isEmpty(base)) { - const baseUrl = new URL(base as string); + // Manually overriding properties, because if userUrl is absolute, the above won't work + const baseUrl = new URL(base as string); - userUrl.host = baseUrl.host; - userUrl.protocol = baseUrl.protocol; - userUrl.port = baseUrl.port; - userUrl.username = baseUrl.username; - userUrl.password = baseUrl.password; + userUrl.host = baseUrl.host; + userUrl.protocol = baseUrl.protocol; + userUrl.port = baseUrl.port; + userUrl.username = baseUrl.username; + userUrl.password = baseUrl.password; - for (const [key, value] of baseUrl.searchParams) { - userUrl.searchParams.append(key, value); - } + for (const [key, value] of baseUrl.searchParams) { + userUrl.searchParams.append(key, value); } return userUrl.href; diff --git a/lib/static/components/icons/view-in-browser/index.jsx b/lib/static/components/icons/view-in-browser/index.jsx index 9f66bfd83..fca070abf 100644 --- a/lib/static/components/icons/view-in-browser/index.jsx +++ b/lib/static/components/icons/view-in-browser/index.jsx @@ -6,7 +6,7 @@ import classNames from 'classnames'; import {Eye, EyeSlash} from '@gravity-ui/icons'; import * as actions from '../../../modules/actions'; -import {getRelativeUrl, getUrlWithBase} from '../../../../common-utils'; +import {getUrlWithBase} from '../../../../common-utils'; import './index.styl'; @@ -41,7 +41,7 @@ class ViewInBrowser extends Component { return ( { if (item.label === 'url' || metaInfoBaseUrls[item.label] === 'auto') { - const url = getUrlWithBase(getRelativeUrl(item.content), baseHost); + const url = getUrlWithBase(item.content, baseHost); return { label: item.label, content: getRelativeUrl(item.content), @@ -105,7 +105,7 @@ function MetaInfoInternal(props: MetaInfoInternalProps): ReactNode { metaInfoItemsWithResolvedUrls.push({ label: 'url', content: getRelativeUrl(result.suiteUrl), - url: getUrlWithBase(getRelativeUrl(result.suiteUrl), baseHost) + url: getUrlWithBase(result.suiteUrl, baseHost) }); } diff --git a/test/unit/lib/common-utils.ts b/test/unit/lib/common-utils.ts index 205a33e5d..7ffc19b03 100644 --- a/test/unit/lib/common-utils.ts +++ b/test/unit/lib/common-utils.ts @@ -38,9 +38,9 @@ describe('common-utils', () => { }); it('should work without baseUrl', () => { - const result = getUrlWithBase('some-url', ''); + const result = getUrlWithBase('http://some-url.com/path', ''); - assert.equal(result, 'some-url'); + assert.equal(result, 'http://some-url.com/path'); }); });