Skip to content

Commit

Permalink
Keep a trailing slash in URL.joinpath (#866)
Browse files Browse the repository at this point in the history
Issue: #862
  • Loading branch information
gmacon authored Jun 7, 2023
1 parent c3111ef commit 1f2cd22
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
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 @@ -810,6 +810,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 @@ -824,6 +839,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: 2 additions & 1 deletion yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,8 @@ 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"""
parsed = []
# keep the trailing slash if the last segment ends with /
parsed = [""] if segments and segments[-1][-1:] == "/" else []
for seg in reversed(segments):
if not seg:
continue
Expand Down

0 comments on commit 1f2cd22

Please sign in to comment.