Skip to content

Commit

Permalink
fix: correctly handle baseUrl when generating test result
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowusr committed Sep 18, 2023
1 parent 08a10fb commit 7de8230
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
19 changes: 16 additions & 3 deletions lib/common-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,24 @@ export const determineStatus = (statuses: TestStatus[]): TestStatus | null => {
return null;
};

export const getAbsoluteUrl = (url: string | undefined, baseUrl: string | undefined): string => {
export const getAbsoluteUrl = (url: string | undefined, base: string | undefined): string => {
try {
return new URL(url ?? '', isEmpty(baseUrl) ? undefined : baseUrl).href;
const userUrl = new URL(url ?? '', base);

if (!isEmpty(base)) {
const baseUrl = new URL(base as string);

if (baseUrl.host) {
userUrl.host = baseUrl.host;
}
if (baseUrl.protocol) {
userUrl.protocol = baseUrl.protocol;
}
}

return userUrl.href;
} catch {
return baseUrl || url || '';
return url || base || '';
}
};

Expand Down
28 changes: 27 additions & 1 deletion test/unit/lib/common-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
import {determineStatus, buildUrl, getError, hasDiff} from 'lib/common-utils';
import {determineStatus, buildUrl, getError, hasDiff, getAbsoluteUrl} from 'lib/common-utils';
import {RUNNING, QUEUED, ERROR, FAIL, UPDATED, SUCCESS, IDLE, SKIPPED} from 'lib/constants/test-statuses';
import {ErrorName} from 'lib/errors';

describe('common-utils', () => {
describe('getAbsoluteUrl', () => {
it('should change host of the url', () => {
const userUrl = 'https://example.com/test/123?a=1#hello';
const baseUrl = 'https://some.site.xyz';

const result = getAbsoluteUrl(userUrl, baseUrl);

assert.equal(result, 'https://some.site.xyz/test/123?a=1#hello');
});

it('should work with relative urls', () => {
const userUrl = '/test/123?a=1#hello';
const baseUrl = 'https://example.com';

const result = getAbsoluteUrl(userUrl, baseUrl);

assert.equal(result, 'https://example.com/test/123?a=1#hello');
});

it('should work without baseUrl', () => {
const result = getAbsoluteUrl('some-url', '');

assert.equal(result, 'some-url');
});
});

describe('getError', () => {
it('should return test error with "message", "stack" and "stateName"', () => {
const error = {
Expand Down

0 comments on commit 7de8230

Please sign in to comment.