diff --git a/tests/test_url.py b/tests/test_url.py index 725e9465b..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",), @@ -806,6 +809,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" ), 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" )