From 71c1779c0740d2a9ac916ee33f261e7dea4b76fb Mon Sep 17 00:00:00 2001 From: Alessio Bogon <778703+youtux@users.noreply.github.com> Date: Sun, 9 Jun 2024 09:15:08 +0200 Subject: [PATCH 1/3] Add (failing) test for trailing slash when joining with "" --- tests/test_url.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_url.py b/tests/test_url.py index 725e9465b..c71f50c6a 100644 --- a/tests/test_url.py +++ b/tests/test_url.py @@ -806,6 +806,15 @@ def test_div_with_dots(): id="cleanup-query-and-fragment", ), pytest.param("", ("path/",), "http://example.com/path/", id="trailing-slash"), + pytest.param( + "", + ( + "path", + "", + ), + "http://example.com/path/", + id="trailing-slash-empty-string", + ), pytest.param( "", ("path/", "to/"), "http://example.com/path/to/", id="duplicate-slash" ), From 73e658aa4721300b049048dadc6cb32942ff5ecc Mon Sep 17 00:00:00 2001 From: Alessio Bogon <778703+youtux@users.noreply.github.com> Date: Sun, 9 Jun 2024 09:29:13 +0200 Subject: [PATCH 2/3] Add back trailing slash when joining with "" --- yarl/_url.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/yarl/_url.py b/yarl/_url.py index 9cca27ef8..f7ee3bf1a 100644 --- a/yarl/_url.py +++ b/yarl/_url.py @@ -718,9 +718,7 @@ def _make_child(self, segments, encoded=False): # 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 - if seg[0] == "/": + if seg and seg[0] == "/": raise ValueError( f"Appending path {seg!r} starting from slash is forbidden" ) From b553c6fbbc3933a13f891b19d10b17bad3e958b0 Mon Sep 17 00:00:00 2001 From: Alessio Bogon <778703+youtux@users.noreply.github.com> Date: Sun, 9 Jun 2024 09:31:38 +0200 Subject: [PATCH 3/3] Add another test for path joining to "" --- tests/test_url.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_url.py b/tests/test_url.py index c71f50c6a..d6c88d44b 100644 --- a/tests/test_url.py +++ b/tests/test_url.py @@ -799,6 +799,9 @@ def test_div_with_dots(): pytest.param( "/path/", ("to",), "http://example.com/path/to", id="path-with-slash" ), + pytest.param( + "/path", ("",), "http://example.com/path/", id="path-add-trailing-slash" + ), pytest.param( "/path?a=1#frag", ("to",),