Skip to content

Commit 01dd1f4

Browse files
committed
Preserve empty relative paths during normalization
1 parent 978f2e6 commit 01dd1f4

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎src/hyperlink/_url.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,8 @@ def normalize(
14691469
* Uppercase remaining percent-encoded octets (`RFC 3986 2.1`_)
14701470
* Convert scheme and host casing to lowercase (`RFC 3986 3.2.2`_)
14711471
* Resolve any "." and ".." references in the path (`RFC 3986 6.2.2.3`_)
1472-
* Ensure an ending slash on URLs with an empty path (`RFC 3986 6.2.3`_)
1472+
* Ensure an ending slash on URLs with a host and an empty path
1473+
(`RFC 3986 6.2.3`_)
14731474
* Encode any stray percent signs (`%`) in percent-encoded
14741475
fields (path, query, fragment, userinfo) (`RFC 3986 2.4`_)
14751476
@@ -1515,7 +1516,7 @@ def _dec_unres(target):
15151516
kw["path"] = [
15161517
_dec_unres(p) for p in _resolve_dot_segments(self.path)
15171518
]
1518-
else:
1519+
elif self.host:
15191520
kw["path"] = (u"",)
15201521
if query:
15211522
kw["query"] = [

‎src/hyperlink/test/test_decoded_url.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@
1515

1616

1717
class TestURL(HyperlinkTestCase):
18+
def test_normalize_empty_relative_path(self):
19+
# type: () -> None
20+
base = DecodedURL.from_text("http://example.com/a/b?old=1")
21+
for text in ("", "?x=1", "#section", "?x=1#section"):
22+
reference = DecodedURL.from_text(text)
23+
normalized = reference.normalize()
24+
assert normalized.path == ()
25+
assert not normalized.rooted
26+
assert normalized.to_text() == text
27+
assert base.click(normalized.to_text()) == base.click(text)
28+
assert base.encoded_url.click(normalized.encoded_url) == (
29+
base.encoded_url.click(reference.encoded_url)
30+
)
31+
1832
def test_durl_basic(self):
1933
# type: () -> None
2034
bdurl = DecodedURL.from_text(BASIC_URL)

‎src/hyperlink/test/test_url.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,18 @@ def test_normalize(self):
14611461
== "ftp://%25:%25@/%25%25%25/%25a%25b?%25=%25%25#%25"
14621462
)
14631463

1464+
def test_normalize_empty_relative_path(self):
1465+
# type: () -> None
1466+
base = URL.from_text("http://example.com/a/b?old=1")
1467+
for text in ("", "?x=1", "#section", "?x=1#section"):
1468+
reference = URL.from_text(text)
1469+
normalized = reference.normalize()
1470+
assert normalized.path == ()
1471+
assert not normalized.rooted
1472+
assert normalized.to_text() == text
1473+
assert base.click(normalized) == base.click(reference)
1474+
assert normalized.normalize() == normalized
1475+
14641476
def test_str(self):
14651477
# type: () -> None
14661478

0 commit comments

Comments
 (0)