Preserve empty query strings and fragments

This commit is contained in:
Ian Cordasco
2015-12-06 17:37:31 -06:00
parent d6147479a8
commit be7538a91f
5 changed files with 25 additions and 8 deletions

View File

@@ -43,10 +43,14 @@ def normalize_path(path):
def normalize_query(query):
if not query:
return query
return normalize_percent_characters(query)
def normalize_fragment(fragment):
if not fragment:
return fragment
return normalize_percent_characters(fragment)

View File

@@ -76,8 +76,8 @@ class ParseResult(namedtuple('ParseResult', PARSED_COMPONENTS),
host,
port or None,
path or None,
query or None,
fragment or None)
query,
fragment)
parse_result.encoding = encoding
parse_result.reference = uri_ref
return parse_result

View File

@@ -38,8 +38,8 @@ class URIReference(namedtuple('URIReference', URI_COMPONENTS)):
scheme or None,
authority or None,
path or None,
query or None,
fragment or None)
query,
fragment)
ref.encoding = encoding
return ref
@@ -264,8 +264,8 @@ class URIReference(namedtuple('URIReference', URI_COMPONENTS)):
normalize_authority(
(self.userinfo, self.host, self.port)),
normalize_path(self.path or ''),
normalize_query(self.query or ''),
normalize_fragment(self.fragment or ''))
normalize_query(self.query),
normalize_fragment(self.fragment))
def normalized_equality(self, other_ref):
"""Compare this URIReference to another URIReference.
@@ -358,9 +358,9 @@ class URIReference(namedtuple('URIReference', URI_COMPONENTS)):
result_list.extend(['//', self.authority])
if self.path:
result_list.append(self.path)
if self.query:
if self.query is not None:
result_list.extend(['?', self.query])
if self.fragment:
if self.fragment is not None:
result_list.extend(['#', self.fragment])
return ''.join(result_list)

View File

@@ -70,3 +70,9 @@ def test_authority_normalization():
uri = URIReference(
None, 'user%2aName@EXAMPLE.COM', None, None, None).normalize()
assert uri.authority == 'user%2AName@example.com'
def test_fragment_normalization():
uri = URIReference(
None, 'example.com', None, None, 'fiz%DF').normalize()
assert uri.fragment == 'fiz%DF'

View File

@@ -337,3 +337,10 @@ class TestURIReferencesResolve:
T = R.resolve_with(B)
assert T.path is None
assert T.query == 'query'
def test_empty_querystrings_persist():
url = 'https://httpbin.org/get?'
ref = URIReference.from_string(url)
assert ref.query == ''
assert ref.unsplit() == url