Skip to content

Commit

Permalink
make use of URL().origin for retrieving protocol, domain and port
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinogiardino committed Aug 20, 2024
1 parent 013c397 commit 9158c91
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
38 changes: 33 additions & 5 deletions core-web/libs/utils/src/lib/dot-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe('Dot Utils', () => {
const url = 'https://developer.mozilla.org/en-US/observatory/analyze?host={domainName}';
const params: DotPageToolUrlParams = {
currentUrl: '',
requestHostName: 'https://my-site.com:80',
requestHostName: 'http://my-site.com:80',
siteId: '50a79decd9e21702cb2f52fc4935a52b',
languageId: 1
};
Expand Down Expand Up @@ -297,7 +297,7 @@ describe('Dot Utils', () => {
expect(getRunnableLink(url, params)).toEqual('https://example.com/');
});

it('should remove port in requestHostName and respect https protocol', () => {
it('should and respect https protocol and port in requestHostName', () => {
const url = 'https://example.com/{requestHostName}{currentUrl}{urlSearchParams}';
const params: DotPageToolUrlParams = {
currentUrl: '/current-page',
Expand All @@ -307,11 +307,11 @@ describe('Dot Utils', () => {
};

expect(getRunnableLink(url, params)).toEqual(
'https://example.com/https://my-site.com/current-page?host_id=123&language_id=456'
'https://example.com/https://my-site.com:4200/current-page?host_id=123&language_id=456'
);
});

it('should remove port in requestHostName and respect http protocol', () => {
it('should and respect http protocol and port in requestHostName', () => {
const url = 'https://example.com/{requestHostName}{currentUrl}{urlSearchParams}';
const params: DotPageToolUrlParams = {
currentUrl: '/current-page',
Expand All @@ -321,7 +321,7 @@ describe('Dot Utils', () => {
};

expect(getRunnableLink(url, params)).toEqual(
'https://example.com/http://my-site.com/current-page?host_id=123&language_id=456'
'https://example.com/http://my-site.com:4200/current-page?host_id=123&language_id=456'
);
});

Expand All @@ -338,5 +338,33 @@ describe('Dot Utils', () => {
'https://example.com/my-site.com/current-page?host_id=123&language_id=456'
);
});

it('should remove port in requestHostName given https protocol and 443 port ', () => {
const url = 'https://example.com/{requestHostName}{currentUrl}{urlSearchParams}';
const params: DotPageToolUrlParams = {
currentUrl: '/current-page',
requestHostName: 'https://my-site.com:443',
siteId: '123',
languageId: 456
};

expect(getRunnableLink(url, params)).toEqual(
'https://example.com/https://my-site.com/current-page?host_id=123&language_id=456'
);
});

it('should remove port in requestHostName given http protocol and 80 port ', () => {
const url = 'https://example.com/{requestHostName}{currentUrl}{urlSearchParams}';
const params: DotPageToolUrlParams = {
currentUrl: '/current-page',
requestHostName: 'http://my-site.com:80',
siteId: '123',
languageId: 456
};

expect(getRunnableLink(url, params)).toEqual(
'https://example.com/http://my-site.com/current-page?host_id=123&language_id=456'
);
});
});
});
5 changes: 1 addition & 4 deletions core-web/libs/utils/src/lib/dot-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ export function getRunnableLink(url: string, currentPageUrlParams: DotPageToolUr
// Replace placeholders in the base URL with actual values and append query parameters if they exist
const requestHostUrl = requestHostName ? new URL(requestHostName) : null;
const finalUrl = url
.replace(
/{requestHostName}/g,
requestHostUrl ? `${requestHostUrl.protocol}//${requestHostUrl.hostname}` : ''
)
.replace(/{requestHostName}/g, requestHostUrl ? requestHostUrl.origin : '')
.replace(/{domainName}/g, requestHostUrl ? requestHostUrl.hostname : '')
.replace(/{currentUrl}/g, currentUrl ?? '')
.replace(/{urlSearchParams}/g, pageParams.toString() ? `?${pageParams.toString()}` : '');
Expand Down

0 comments on commit 9158c91

Please sign in to comment.