Skip to content

Commit dfd38b4

Browse files
committed
Don't use urljoin for prepending a forward slash to paths.
Fixes #273. Previously, `Path` used `urljoin` to prepend a forward slash to the path. But `urljoin` is broken for no good reason: >>> from urllib.parse import urljoin >>> urljoin("/", "oh//no") '/oh/no' Fix this by just adding a "/" if it's not there, which is what this was trying to do in the first place.
1 parent 0dbd8c8 commit dfd38b4

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

respx/patterns.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
Union,
2525
)
2626
from unittest.mock import ANY
27-
from urllib.parse import urljoin
2827

2928
import httpx
3029

@@ -429,7 +428,11 @@ def clean(
429428
else "".join(f"%{byte:02x}" for byte in char.encode("utf-8")).upper()
430429
for char in value
431430
)
432-
path = urljoin("/", path) # Ensure leading slash
431+
# Ensure a leading slash. Note we don't use urljoin because its
432+
# behaviour with multiple slashes in the path is incorrect - see
433+
# https://github.com/lundberg/respx/issues/273
434+
if not path.startswith("/"):
435+
path = f"/{path}"
433436
value = httpx.URL(path).path
434437
elif self.lookup is Lookup.REGEX and isinstance(value, str):
435438
value = re.compile(value)

tests/test_patterns.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ def test_path_pattern():
224224
path.base = Path("/foo/")
225225
assert path.strip_base("/foo/bar/") == "/bar/"
226226

227+
# Regression test for https://github.com/lundberg/respx/issues/273
228+
assert Path("foo//bar").clean("foo//bar") == "/foo//bar"
229+
227230

228231
@pytest.mark.parametrize(
229232
("lookup", "params", "url", "expected"),

0 commit comments

Comments
 (0)