Skip to content

Commit b5bd577

Browse files
committed
Re-quote when cleaning Path pattern
1 parent 33438de commit b5bd577

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

respx/patterns.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,16 @@ def clean(
413413
self, value: Union[str, RegexPattern[str]]
414414
) -> Union[str, RegexPattern[str]]:
415415
if self.lookup in (Lookup.EQUAL, Lookup.STARTS_WITH) and isinstance(value, str):
416-
path = urljoin("/", value) # Ensure leading slash
416+
# Percent encode path, i.e. revert parsed path by httpx.URL.
417+
# Borrowed from HTTPX's "private" quote and percent_encode utilities.
418+
path = "".join(
419+
char
420+
if char
421+
in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~/"
422+
else "".join(f"%{byte:02x}" for byte in char.encode("utf-8")).upper()
423+
for char in value
424+
)
425+
path = urljoin("/", path) # Ensure leading slash
417426
value = httpx.URL(path).path
418427
elif self.lookup is Lookup.REGEX and isinstance(value, str):
419428
value = re.compile(value)

tests/test_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ async def test_http_methods(client):
8282
("https://foo.bar/baz/", re.compile(r"^https://foo.bar/\w+/$")),
8383
("https://foo.bar/baz/", (b"https", b"foo.bar", None, b"/baz/")),
8484
("https://foo.bar:443/baz/", (b"https", b"foo.bar", 443, b"/baz/")),
85+
("https://foo.bar/%08", "https://foo.bar/%08"),
8586
],
8687
)
8788
async def test_url_match(client, url, pattern):

0 commit comments

Comments
 (0)