Skip to content

Commit f90bf49

Browse files
trknhrkettanaito
andauthored
fix: support wildcard ports in url matching (#2677)
Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
1 parent 002f3e7 commit f90bf49

File tree

2 files changed

+89
-5
lines changed

2 files changed

+89
-5
lines changed

src/core/utils/matching/matchRequestUrl.test.ts

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/**
2-
* @vitest-environment jsdom
3-
*/
1+
// @vitest-environment jsdom
42
import { coercePath, matchRequestUrl } from './matchRequestUrl'
53

64
describe('matchRequestUrl', () => {
@@ -87,6 +85,77 @@ describe('matchRequestUrl', () => {
8785
})
8886
})
8987

88+
test('returns true when matching URLs with wildcard ports', () => {
89+
expect
90+
.soft(
91+
matchRequestUrl(new URL('http://localhost:3000'), 'http://localhost:*'),
92+
)
93+
.toEqual({
94+
matches: true,
95+
params: {
96+
'0': '3000/',
97+
},
98+
})
99+
100+
expect
101+
.soft(
102+
matchRequestUrl(
103+
new URL('http://localhost:3000'),
104+
'http://localhost:*/',
105+
),
106+
)
107+
.toEqual({
108+
matches: true,
109+
params: {
110+
'0': '3000',
111+
},
112+
})
113+
})
114+
115+
test('returns true when matching URLs with wildcard ports and pathnames', () => {
116+
expect(
117+
matchRequestUrl(
118+
new URL('http://localhost:3000/resource'),
119+
'http://localhost:*/resource',
120+
),
121+
).toEqual({
122+
matches: true,
123+
params: {
124+
'0': '3000',
125+
},
126+
})
127+
})
128+
129+
test('matches wildcard ports with other wildcard parameters', () => {
130+
expect(
131+
matchRequestUrl(
132+
new URL('http://subdomain.localhost:3000/user/settings'),
133+
'http://*.localhost:*/user/*',
134+
),
135+
).toEqual({
136+
matches: true,
137+
params: {
138+
'0': 'subdomain',
139+
'1': '3000',
140+
'2': 'settings',
141+
},
142+
})
143+
})
144+
145+
test('matches wildcard ports that also match a part of the pathname', () => {
146+
expect(
147+
matchRequestUrl(
148+
new URL('http://localhost:3000/user/settings'),
149+
'http://localhost:*/settings',
150+
),
151+
).toEqual({
152+
matches: true,
153+
params: {
154+
'0': '3000/user',
155+
},
156+
})
157+
})
158+
90159
test('returns true for matching WebSocket URL', () => {
91160
expect(
92161
matchRequestUrl(new URL('ws://test.mswjs.io'), 'ws://test.mswjs.io'),
@@ -130,6 +199,17 @@ describe('matchRequestUrl', () => {
130199
},
131200
})
132201
})
202+
203+
test('returns true for matching WebSocket URLs with wildcard ports', () => {
204+
expect(
205+
matchRequestUrl(new URL('ws://localhost:3000'), 'ws://localhost:*'),
206+
).toEqual({
207+
matches: true,
208+
params: {
209+
'0': '3000/',
210+
},
211+
})
212+
})
133213
})
134214

135215
describe('coercePath', () => {
@@ -156,6 +236,10 @@ describe('coercePath', () => {
156236
expect(coercePath('https://example.com:8080/:5678')).toEqual(
157237
'https\\://example.com\\:8080/:5678',
158238
)
239+
expect(coercePath('http://localhost:*')).toEqual(
240+
'http\\://localhost\\:(.*)',
241+
)
242+
expect(coercePath('ws://localhost:*')).toEqual('ws\\://localhost\\:(.*)')
159243
})
160244

161245
test('replaces wildcard with an unnnamed capturing group', () => {

src/core/utils/matching/matchRequestUrl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ export function coercePath(path: string): string {
4040
)
4141
/**
4242
* Escape the port so that "path-to-regexp" can match
43-
* absolute URLs including port numbers.
43+
* absolute URLs with numeric or wildcard ports.
4444
*/
45-
.replace(/([^/])(:)(?=\d+)/, '$1\\$2')
45+
.replace(/([^/])(:)(?=(?:\d+|\(\.\*\))(?=\/|$))/, '$1\\$2')
4646
/**
4747
* Escape the protocol so that "path-to-regexp" could match
4848
* absolute URL.

0 commit comments

Comments
 (0)