better compliance with empty-path behavior recommended in RFC 3986 6.2.3 (and Twisted test suite)

This commit is contained in:
Mahmoud Hashemi 2017-05-06 17:20:00 -07:00
parent ba0d9bbb80
commit d02db54803
3 changed files with 9 additions and 4 deletions

View File

@ -1,5 +1,7 @@
# hyperlink TODO
* RFC 3986 6.2.3 (always slash on empty path and non-empty host, not
just when query string is present)
* Polish logo
* Optimize percent decoding
* Get coverage up

View File

@ -514,7 +514,10 @@ class URL(object):
if port is None:
port = SCHEME_PORT_MAP.get(scheme)
if host and query and not path:
path = () # (u'',)
# per RFC 3986 6.2.3, "a URI that uses the generic syntax
# for authority with an empty path should be normalized to
# a path of '/'."
path = (u'',)
# Now that we're done detecting whether they were passed, we can set
# them to their defaults:
@ -1049,7 +1052,7 @@ class URL(object):
"""
scheme = self.scheme
authority = self.authority(with_password)
path = u'/'.join(([u''] if (self.rooted and self.path) else [])
path = u'/'.join(([u''] if self.rooted else [])
+ [_encode_path_part(segment, maximal=False)
for segment in self.path])
query_string = u'&'.join(

View File

@ -97,7 +97,7 @@ ROUNDTRIP_TESTS = (
'http://googlewebsite.com/e-shops.aspx',
'http://example.com:8080/search?q=123&business=Nothing%20Special',
'http://hatnote.com:9000?arg=1&arg=2&arg=3',
'http://hatnote.com:9000/?arg=1&arg=2&arg=3',
'https://xn--bcher-kva.ch',
'http://xn--ggbla1c4e.xn--ngbc5azd/',
'http://tools.ietf.org/html/rfc3986#section-3.4',
@ -431,7 +431,7 @@ class TestURL(TestCase):
URL.from_text("http://www.foo.com/a/nice/path/")
.add(u"foo", u"bar").to_text())
self.assertEqual(
"http://www.foo.com?foo=bar",
"http://www.foo.com/?foo=bar",
URL(host=u"www.foo.com").add(u"foo", u"bar")
.to_text())
urlpath = URL.from_text(BASIC_URL)