Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep a trailing slash in URL.joinpath #866

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"),
gmacon marked this conversation as resolved.
Show resolved Hide resolved
],
)
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: 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