diff --git a/src/utils/matching/normalizePath.test.ts b/src/utils/matching/normalizePath.test.ts index 9cdd28597..f9e3a5c06 100644 --- a/src/utils/matching/normalizePath.test.ts +++ b/src/utils/matching/normalizePath.test.ts @@ -39,10 +39,12 @@ test('removes query parameters and hashes from a relative URL', () => { }) test('returns a path pattern string as-is', () => { - expect(normalizePath(':api/user')).toEqual(':api/user') + expect(normalizePath(':api/user')).toEqual('http://localhost/:api/user') expect(normalizePath('*/resource/*')).toEqual('*/resource/*') }) test('removeß query parameters and hashes from a path pattern string', () => { - expect(normalizePath(':api/user?query=123#some')).toEqual(':api/user') + expect(normalizePath(':api/user?query=123#some')).toEqual( + 'http://localhost/:api/user', + ) }) diff --git a/src/utils/url/getAbsoluteUrl.test.ts b/src/utils/url/getAbsoluteUrl.test.ts index 5ca89f46c..879fac706 100644 --- a/src/utils/url/getAbsoluteUrl.test.ts +++ b/src/utils/url/getAbsoluteUrl.test.ts @@ -18,3 +18,12 @@ test('returns a given absolute URL as-is', () => { 'https://api.mswjs.io/users', ) }) + +test('returns an absolute URL given a relative path without a leading slash', () => { + expect(getAbsoluteUrl('users')).toEqual('http://localhost/users') +}) + +test('returns a path with a pattern as-is', () => { + expect(getAbsoluteUrl(':api/user')).toEqual('http://localhost/:api/user') + expect(getAbsoluteUrl('*/resource/*')).toEqual('*/resource/*') +}) diff --git a/src/utils/url/getAbsoluteUrl.ts b/src/utils/url/getAbsoluteUrl.ts index e7e0c8d11..41dd23771 100644 --- a/src/utils/url/getAbsoluteUrl.ts +++ b/src/utils/url/getAbsoluteUrl.ts @@ -1,9 +1,16 @@ +import { isAbsoluteUrl } from './isAbsoluteUrl' + /** * Returns an absolute URL based on the given path. */ export function getAbsoluteUrl(path: string, baseUrl?: string): string { - // Ignore absolute URLs. - if (!path.startsWith('/')) { + // already absolute URL + if (isAbsoluteUrl(path)) { + return path + } + + // Ignore path with pattern start with * + if (path.startsWith('*')) { return path } diff --git a/src/utils/url/isAbsoluteUrl.test.ts b/src/utils/url/isAbsoluteUrl.test.ts new file mode 100644 index 000000000..96c4fbdbb --- /dev/null +++ b/src/utils/url/isAbsoluteUrl.test.ts @@ -0,0 +1,32 @@ +/** + * @jest-environment node + */ +import { isAbsoluteUrl } from './isAbsoluteUrl' + +test('returns true for the "http" scheme', () => { + expect(isAbsoluteUrl('http://www.domain.com')).toEqual(true) +}) + +test('returns true for the "https" scheme', () => { + expect(isAbsoluteUrl('https://www.domain.com')).toEqual(true) +}) + +test('returns true for the "ws" scheme', () => { + expect(isAbsoluteUrl('ws://www.domain.com')).toEqual(true) +}) + +test('returns true for the "ftp" scheme', () => { + expect(isAbsoluteUrl('ftp://www.domain.com')).toEqual(true) +}) + +test('returns true for the custom scheme', () => { + expect(isAbsoluteUrl('web+my://www.example.com')).toEqual(true) +}) + +test('returns false for the relative URL', () => { + expect(isAbsoluteUrl('/test')).toEqual(false) +}) + +test('returns false for the relative URL without a leading slash', () => { + expect(isAbsoluteUrl('test')).toEqual(false) +}) diff --git a/src/utils/url/isAbsoluteUrl.ts b/src/utils/url/isAbsoluteUrl.ts new file mode 100644 index 000000000..a91dbde43 --- /dev/null +++ b/src/utils/url/isAbsoluteUrl.ts @@ -0,0 +1,6 @@ +/** + * Determines if the given URL string is an absolute URL. + */ +export function isAbsoluteUrl(url: string): boolean { + return /^([a-z][a-z\d\+\-\.]*:)?\/\//i.test(url) +}