diff --git a/CHANGES/1118.bugfix.rst b/CHANGES/1118.bugfix.rst new file mode 100644 index 00000000..adac595b --- /dev/null +++ b/CHANGES/1118.bugfix.rst @@ -0,0 +1,3 @@ +Fixed joining a path when the existing path was empty -- by :user:`bdraco`. + +A regression in :meth:`URL.join() ` was introduced in :issue:`1082`. diff --git a/tests/test_url.py b/tests/test_url.py index 4089b366..5f6e033c 100644 --- a/tests/test_url.py +++ b/tests/test_url.py @@ -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 == "" diff --git a/yarl/_url.py b/yarl/_url.py index 8289c2a2..94f31699 100644 --- a/yarl/_url.py +++ b/yarl/_url.py @@ -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: