Skip to content

Commit

Permalink
fix(getAbsoluteUrl): fix full url check (#982)
Browse files Browse the repository at this point in the history
* feat(getAbsoluteUrl): handle url schemes, relative urls without leading slashes

* feat(getAbsoluteUrl): convert path start with colon to absolute URL
  • Loading branch information
MarvelSQ committed Dec 13, 2021
1 parent d5b7312 commit d8bd81a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/utils/matching/normalizePath.test.ts
Expand Up @@ -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',
)
})
9 changes: 9 additions & 0 deletions src/utils/url/getAbsoluteUrl.test.ts
Expand Up @@ -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/*')
})
11 changes: 9 additions & 2 deletions 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
}

Expand Down
32 changes: 32 additions & 0 deletions 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)
})
6 changes: 6 additions & 0 deletions 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)
}

0 comments on commit d8bd81a

Please sign in to comment.