Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly handle baseUrl when generating test result #507

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: если тут использовать не _.isEmpty а base && base.length или base && !isEmpty(base), ниже не придется приводить 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
Loading