Skip to content

Commit

Permalink
Keep a trailing slash in URL.joinpath
Browse files Browse the repository at this point in the history
Issue: #862
  • Loading branch information
gmacon committed May 3, 2023
1 parent cfc634e commit bd789e0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/862.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Stopped dropping trailing slashes in ``URL.joinpath``.
18 changes: 18 additions & 0 deletions tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,21 @@ def test_div_with_dots():
"http://example.com/path/to",
id="cleanup-query-and-fragment",
),
pytest.param("", ("path/",), "http://example.com/path/", id="trailing-slash"),
pytest.param(
"", ("path/", "to/"), "http://example.com/path/to/", id="duplicate-slash"
),
pytest.param("", (), "http://example.com", id="empty-segments"),
pytest.param(
"/", ("path/",), "http://example.com/path/", id="base-slash-trailing-slash"
),
pytest.param(
"/",
("path/", "to/"),
"http://example.com/path/to/",
id="base-slash-duplicate-slash",
),
pytest.param("/", (), "http://example.com", id="base-slash-empty-segments"),
],
)
def test_joinpath(base, to_join, expected):
Expand All @@ -811,6 +826,9 @@ def test_joinpath(base, to_join, expected):
pytest.param(URL("a"), ("b",), ("a", "b"), id="relative-path"),
pytest.param(URL("a"), ("b", "", "c"), ("a", "b", "c"), id="empty-element"),
pytest.param(URL("/a"), ("b"), ("/", "a", "b"), id="absolute-path"),
pytest.param(URL(), ("a/",), ("a", ""), id="trailing-slash"),
pytest.param(URL(), ("a/", "b/"), ("a", "b", ""), id="duplicate-slash"),
pytest.param(URL(), (), ("",), id="empty-segments"),
],
)
def test_joinpath_relative(url, to_join, expected):
Expand Down
3 changes: 3 additions & 0 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,7 @@ def _validate_authority_uri_abs_path(host, path):

def _make_child(self, segments, encoded=False):
"""add segments to self._val.path, accounting for absolute vs relative paths"""
need_trailing_slash = segments and segments[-1].endswith("/")
parsed = []
for seg in reversed(segments):
if not seg:
Expand All @@ -729,6 +730,8 @@ def _make_child(self, segments, encoded=False):
elif seg != ".":
parsed.append(seg)
parsed.reverse()
if need_trailing_slash:
parsed.append("")
old_path = self._val.path
if old_path:
parsed = [*old_path.rstrip("/").split("/"), *parsed]
Expand Down

0 comments on commit bd789e0

Please sign in to comment.