Skip to content

Commit

Permalink
Fix truediv joining against absolute URL with no path (#857)
Browse files Browse the repository at this point in the history
When joining path elements against an absolute URL, ensure that
the resulting path starts with a slash.
  • Loading branch information
mjpieters authored Apr 25, 2023
1 parent ace3552 commit 475ee4a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES/854.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix regression with truediv and absolute URLs with empty paths causing the raw path to lack the leading ``/``.
20 changes: 12 additions & 8 deletions tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,23 +690,27 @@ def test_clear_query_on_getting_parent_toplevel():


def test_div_root():
url = URL("http://example.com")
assert str(url / "path" / "to") == "http://example.com/path/to"
url = URL("http://example.com") / "path" / "to"
assert str(url) == "http://example.com/path/to"
assert url.raw_path == "/path/to"


def test_div_root_with_slash():
url = URL("http://example.com/")
assert str(url / "path" / "to") == "http://example.com/path/to"
url = URL("http://example.com/") / "path" / "to"
assert str(url) == "http://example.com/path/to"
assert url.raw_path == "/path/to"


def test_div():
url = URL("http://example.com/path")
assert str(url / "to") == "http://example.com/path/to"
url = URL("http://example.com/path") / "to"
assert str(url) == "http://example.com/path/to"
assert url.raw_path == "/path/to"


def test_div_with_slash():
url = URL("http://example.com/path/")
assert str(url / "to") == "http://example.com/path/to"
url = URL("http://example.com/path/") / "to"
assert str(url) == "http://example.com/path/to"
assert url.raw_path == "/path/to"


def test_div_path_starting_from_slash_is_forbidden():
Expand Down
4 changes: 4 additions & 0 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,10 @@ def _make_child(self, segments, encoded=False):
parsed = [*old_path.rstrip("/").split("/"), *parsed]
if self.is_absolute():
parsed = _normalize_path_segments(parsed)
if parsed and parsed[0] != "":
# inject a leading slash when adding a path to an absolute URL
# where there was none before
parsed = ["", *parsed]
new_path = "/".join(parsed)
return URL(
self._val._replace(path=new_path, query="", fragment=""), encoded=True
Expand Down

0 comments on commit 475ee4a

Please sign in to comment.