Skip to content

Commit

Permalink
Fix joining a path when the existing path is empty (#1118)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Sep 6, 2024
1 parent c3e4197 commit 0f01077
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES/1118.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed joining a path when the existing path was empty -- by :user:`bdraco`.

A regression in :meth:`URL.join() <yarl.URL.join>` was introduced in :issue:`1082`.
8 changes: 8 additions & 0 deletions tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1767,6 +1767,14 @@ def test_join_cpython_urljoin(base, url, expected):
assert joined == expected


def test_join_preserves_leading_slash():
"""Test that join preserves leading slash in path."""
base = URL.build(scheme="https", host="localhost", port=443)
new = base.join(URL("") / "_msearch")
assert str(new) == "https://localhost/_msearch"
assert new.path == "/_msearch"


def test_empty_authority():
assert URL("http:///").authority == ""

Expand Down
4 changes: 3 additions & 1 deletion yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1379,8 +1379,10 @@ def join(self, url: "URL") -> "URL":
if not other_val.path:
return URL(val._replace(**parts), encoded=True)

if other_val.path[0] == "/" or not val.path:
if other_val.path[0] == "/":
path = other_val.path
elif not val.path:
path = f"/{other_val.path}"
elif val.path[-1] == "/":
path = f"{val.path}{other_val.path}"
else:
Expand Down

0 comments on commit 0f01077

Please sign in to comment.