Skip to content
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
90 changes: 87 additions & 3 deletions src/core/utils/matching/matchRequestUrl.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/**
* @vitest-environment jsdom
*/
// @vitest-environment jsdom
import { coercePath, matchRequestUrl } from './matchRequestUrl'

describe('matchRequestUrl', () => {
Expand Down Expand Up @@ -87,6 +85,77 @@ describe('matchRequestUrl', () => {
})
})

test('returns true when matching URLs with wildcard ports', () => {
expect
.soft(
matchRequestUrl(new URL('http://localhost:3000'), 'http://localhost:*'),
)
.toEqual({
matches: true,
params: {
'0': '3000/',
},
})

expect
.soft(
matchRequestUrl(
new URL('http://localhost:3000'),
'http://localhost:*/',
),
)
.toEqual({
matches: true,
params: {
'0': '3000',
},
})
})

test('returns true when matching URLs with wildcard ports and pathnames', () => {
expect(
matchRequestUrl(
new URL('http://localhost:3000/resource'),
'http://localhost:*/resource',
),
).toEqual({
matches: true,
params: {
'0': '3000',
},
})
})

test('matches wildcard ports with other wildcard parameters', () => {
expect(
matchRequestUrl(
new URL('http://subdomain.localhost:3000/user/settings'),
'http://*.localhost:*/user/*',
),
).toEqual({
matches: true,
params: {
'0': 'subdomain',
'1': '3000',
'2': 'settings',
},
})
})

test('matches wildcard ports that also match a part of the pathname', () => {
expect(
matchRequestUrl(
new URL('http://localhost:3000/user/settings'),
'http://localhost:*/settings',
),
).toEqual({
matches: true,
params: {
'0': '3000/user',
},
})
})

test('returns true for matching WebSocket URL', () => {
expect(
matchRequestUrl(new URL('ws://test.mswjs.io'), 'ws://test.mswjs.io'),
Expand Down Expand Up @@ -130,6 +199,17 @@ describe('matchRequestUrl', () => {
},
})
})

test('returns true for matching WebSocket URLs with wildcard ports', () => {
expect(
matchRequestUrl(new URL('ws://localhost:3000'), 'ws://localhost:*'),
).toEqual({
matches: true,
params: {
'0': '3000/',
},
})
})
})

describe('coercePath', () => {
Expand All @@ -156,6 +236,10 @@ describe('coercePath', () => {
expect(coercePath('https://example.com:8080/:5678')).toEqual(
'https\\://example.com\\:8080/:5678',
)
expect(coercePath('http://localhost:*')).toEqual(
'http\\://localhost\\:(.*)',
)
expect(coercePath('ws://localhost:*')).toEqual('ws\\://localhost\\:(.*)')
})

test('replaces wildcard with an unnnamed capturing group', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/core/utils/matching/matchRequestUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export function coercePath(path: string): string {
)
/**
* Escape the port so that "path-to-regexp" can match
* absolute URLs including port numbers.
* absolute URLs with numeric or wildcard ports.
*/
.replace(/([^/])(:)(?=\d+)/, '$1\\$2')
.replace(/([^/])(:)(?=(?:\d+|\(\.\*\))(?=\/|$))/, '$1\\$2')
/**
* Escape the protocol so that "path-to-regexp" could match
* absolute URL.
Expand Down
Loading