Browse Source

Retain blank query params in url transformations

merge-requests/55/head
Deimos 6 years ago
parent
commit
a13b5b0176
  1. 12
      tildes/tests/test_url_transform.py
  2. 6
      tildes/tildes/lib/url_transform.py

12
tildes/tests/test_url_transform.py

@ -4,6 +4,18 @@
from tildes.lib.url_transform import apply_url_transformations from tildes.lib.url_transform import apply_url_transformations
def test_blank_query_params_kept():
"""Ensure that query params with no value make it through the process.
Some sites treat the presence of blank params different from their absence, so
we don't want to remove them (which urllib's parse_qs and parse_qsl do by default).
"""
url = "http://example.com/path?one=1&two=2&blank=&three=3"
transformed_url = apply_url_transformations(url)
assert transformed_url == url
def test_remove_utm_query_params(): def test_remove_utm_query_params():
"""Ensure that utm query params are removed but others are left.""" """Ensure that utm query params are removed but others are left."""
url = "http://example.com/path?utm_source=tildes&utm_campaign=test&something=ok" url = "http://example.com/path?utm_source=tildes&utm_campaign=test&something=ok"

6
tildes/tildes/lib/url_transform.py

@ -118,7 +118,7 @@ class UtmQueryParamRemover(UrlTransformer):
@classmethod @classmethod
def apply_transformation(cls, parsed_url: ParseResult) -> ParseResult: def apply_transformation(cls, parsed_url: ParseResult) -> ParseResult:
"""Apply the actual transformation process to the url.""" """Apply the actual transformation process to the url."""
query_params = parse_qs(parsed_url.query)
query_params = parse_qs(parsed_url.query, keep_blank_values=True)
cleaned_params = { cleaned_params = {
param: value param: value
@ -154,7 +154,7 @@ class RedditTrackingRemover(UrlTransformer):
@classmethod @classmethod
def apply_transformation(cls, parsed_url: ParseResult) -> ParseResult: def apply_transformation(cls, parsed_url: ParseResult) -> ParseResult:
"""Apply the actual transformation process to the url.""" """Apply the actual transformation process to the url."""
query_params = parse_qs(parsed_url.query)
query_params = parse_qs(parsed_url.query, keep_blank_values=True)
query_params.pop("st", None) query_params.pop("st", None)
query_params.pop("sh", None) query_params.pop("sh", None)
@ -195,7 +195,7 @@ class YoutubeUnshortener(UrlTransformer):
video_id = parsed_url.path.strip("/") video_id = parsed_url.path.strip("/")
# use parse_qsl() and insert() here so the v= is always the first query param # use parse_qsl() and insert() here so the v= is always the first query param
query_params = parse_qsl(parsed_url.query)
query_params = parse_qsl(parsed_url.query, keep_blank_values=True)
query_params.insert(0, ("v", video_id)) query_params.insert(0, ("v", video_id))
return parsed_url._replace( return parsed_url._replace(

Loading…
Cancel
Save