Skip to content

Commit

Permalink
Fixing issue with normalize and empty query items
Browse files Browse the repository at this point in the history
  • Loading branch information
Yomguithereal committed Jul 18, 2023
1 parent af19923 commit 36430a5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions test/canonicalize_url_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
("http://example.com?test", "http://example.com/?test"),
("http://example.com#test", "http://example.com/#test"),
("http://example.com?test#test", "http://example.com/?test#test"),
('http://lemonde.fr/?test&test=&test=value', 'http://lemonde.fr/?test&test=&test=value')
]

TESTS_ADVANCED = [
Expand Down
3 changes: 2 additions & 1 deletion test/normalize_url_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
),
("http://lemonde.fr?%3d=value", "lemonde.fr?%3D=value"),
("http://lemonde.fr/default.asp", "lemonde.fr"),
('http://lemonde.fr/?test&test=&test=value', 'lemonde.fr?test&test=&test=value')
]


Expand Down Expand Up @@ -232,7 +233,7 @@
),
(
"https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&cad=rja&uact=8&ved=2ahUKEwjp8Lih_LnmAhWQlxQKHVTmCJYQFjADegQIARAB&url=http%3A%2F%2Fwww.mon-ip.com%2F&usg=AOvVaw0sfeZJyVtUS2smoyMlJmes",
"google.com/url?cad=rja&cd=4&esrc=s&q&rct=j&sa=t&source=web&uact=8&url=http://www.mon-ip.com/&usg=AOvVaw0sfeZJyVtUS2smoyMlJmes&ved=2ahUKEwjp8Lih_LnmAhWQlxQKHVTmCJYQFjADegQIARAB",
"google.com/url?cad=rja&cd=4&esrc=s&q=&rct=j&sa=t&source=web&uact=8&url=http://www.mon-ip.com/&usg=AOvVaw0sfeZJyVtUS2smoyMlJmes&ved=2ahUKEwjp8Lih_LnmAhWQlxQKHVTmCJYQFjADegQIARAB",
{"infer_redirection": False},
),
(
Expand Down
13 changes: 6 additions & 7 deletions ural/normalize_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,8 @@
]


def coerce_empty_query_item(item):
if item[1].strip() == "":
return item[0], None

return item
def qsl_sort_key(item):
return item[0], item[1] or '', 0 if item[1] is None else 1


def should_strip_query_item(
Expand Down Expand Up @@ -321,8 +318,10 @@ def normalize_url(
None,
)

# TODO: what to do of empty query items vs. no valued
# TODO: should be dedupe query items?
qsl = [
coerce_empty_query_item(item)
item
for item in safe_qsl_iter(query)
if not should_strip_query_item(
item,
Expand All @@ -333,7 +332,7 @@ def normalize_url(
]

if sort_query:
qsl = sorted(qsl)
qsl = sorted(qsl, key=qsl_sort_key)

# Dropping fragment if it's not routing
if fragment and strip_fragment:
Expand Down

0 comments on commit 36430a5

Please sign in to comment.