diff --git a/httpx/_urlparse.py b/httpx/_urlparse.py index bf190fd560..938b7dd2a9 100644 --- a/httpx/_urlparse.py +++ b/httpx/_urlparse.py @@ -472,6 +472,9 @@ def normalize_path(path: str) -> str: output.pop() else: output.append(component) + # RFC 3986 preserves a directory-ending slash for terminal dot segments. + if path.endswith(("/.", "/..")): + output.append("") return "/".join(output) diff --git a/tests/models/test_url.py b/tests/models/test_url.py index 03072e8f5c..a26122f161 100644 --- a/tests/models/test_url.py +++ b/tests/models/test_url.py @@ -861,3 +861,24 @@ def test_ipv6_url_copy_with_host(url_str, new_host): assert url.host == "::ffff:192.168.0.1" assert url.netloc == b"[::ffff:192.168.0.1]:1234" assert str(url) == "http://[::ffff:192.168.0.1]:1234" + + +@pytest.mark.parametrize( + ("path", "expected"), + [ + ("/x/.", "/x/"), + ("/x/y/..", "/x/"), + ("/x/y/.", "/x/y/"), + ("/.", "/"), + ("/..", "/"), + ("/x/y/../..", "/"), + ("/x/y/../", "/x/"), + ("/x/.file", "/x/.file"), + ], +) +def test_terminal_dot_segments_preserve_directory_slash(path, expected): + url = httpx.URL("https://example.com" + path) + assert url.path == expected + assert url.raw_path == expected.encode() + if path.endswith(("/.", "/..")): + assert str(url.join("child")) == "https://example.com" + expected + "child"