Skip to content

Commit 4dbe19b

Browse files
committed
fix: preserve optional path parameters before a dot-separated parameter
1 parent f348c25 commit 4dbe19b

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎src/core/utils/matching/match-request-url.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,18 @@ test('returns true when matching optional path parameters in the middle of the p
122122
params: {},
123123
})
124124
})
125+
126+
test('returns true when matching optional path parameters separated by a dot', () => {
127+
expect(
128+
matchRequestUrl(
129+
new URL('https://test.mswjs.io/files/report.pdf'),
130+
'https://test.mswjs.io/files/:name?.:ext?',
131+
),
132+
).toEqual({
133+
matches: true,
134+
params: {
135+
name: 'report',
136+
ext: 'pdf',
137+
},
138+
})
139+
})

‎src/utils/get-clean-url-string.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ test('removes query parameters and hashes after optional path parameters', () =>
4646
expect(getCleanUrlString('/user/:id?tab=1')).toEqual('/user/:id')
4747
})
4848

49+
test('preserves optional path parameters separated by a dot', () => {
50+
expect(getCleanUrlString('/files/:name?.:ext?')).toEqual(
51+
'/files/:name?.:ext?',
52+
)
53+
expect(getCleanUrlString('/files/:name?.:ext?#hash')).toEqual(
54+
'/files/:name?.:ext?',
55+
)
56+
expect(getCleanUrlString('/files/:name?.:ext?tab=1')).toEqual(
57+
'/files/:name?.:ext',
58+
)
59+
})
60+
61+
test('removes a query string that starts with a dot after a path parameter', () => {
62+
expect(getCleanUrlString('/user/:id?.json')).toEqual('/user/:id')
63+
})
64+
4965
test('preserves pipe characters in the path', () => {
5066
expect(getCleanUrlString('/user/a|b/settings')).toEqual('/user/a|b/settings')
5167
expect(getCleanUrlString('/user/a|b?id=123')).toEqual('/user/a|b')

‎src/utils/get-clean-url-string.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
* Matches either an optional path parameter (e.g. ":id?"), captured
33
* so it can be preserved, or the search and fragment of the URL.
44
* A question mark is an optional parameter modifier only when it
5-
* follows a parameter name and ends the path segment.
5+
* follows a parameter name and either ends the path segment or is
6+
* followed by a literal dot and another parameter (e.g. ":name?.:ext?").
7+
* A dot followed by anything else (e.g. ":id?.json") is ambiguous
8+
* with a query string and is treated as such.
69
*/
7-
const REDUNDANT_CHARACTERS_EXP = /(:[a-zA-Z_]\w*\?(?=[/#]|$))|[?#].*$/g
10+
const REDUNDANT_CHARACTERS_EXP =
11+
/(:[a-zA-Z_]\w*\?(?=[/#]|\.:[a-zA-Z_]|$))|[?#].*$/g
812

913
/**
1014
* Removes search parameters and the fragment from a given URL string.

0 commit comments

Comments
 (0)