diff options
author | Jordan Cook <jordan.cook@pioneer.com> | 2022-01-14 15:54:55 -0600 |
---|---|---|
committer | Jordan Cook <jordan.cook@pioneer.com> | 2022-01-14 15:59:17 -0600 |
commit | 030e788121a2679ec4f39cf3b01d1d670a31f19a (patch) | |
tree | c82cd5dfcbbb0c2021ee9bfedfb4be78147834db | |
parent | 0185d02225a1b76627c65b51764266b7ab95e001 (diff) | |
download | requests-cache-030e788121a2679ec4f39cf3b01d1d670a31f19a.tar.gz |
Add support for key-only request parameters
-rw-r--r-- | requests_cache/cache_keys.py | 9 | ||||
-rw-r--r-- | tests/unit/test_cache_keys.py | 6 |
2 files changed, 13 insertions, 2 deletions
diff --git a/requests_cache/cache_keys.py b/requests_cache/cache_keys.py index 80cb6c5..2258d49 100644 --- a/requests_cache/cache_keys.py +++ b/requests_cache/cache_keys.py @@ -171,10 +171,15 @@ def normalize_json_body( return original_body -# TODO: More thorough tests def normalize_params(value: Union[str, bytes], ignored_parameters: ParamList) -> str: """Normalize and filter urlencoded params from either a URL or request body with form data""" - params = dict(parse_qsl(decode(value))) + query_str = decode(value) + params = dict(parse_qsl(query_str)) + + # parse_qsl doesn't handle key-only params, so add those here + key_only_params = [k for k in query_str.split('&') if k and '=' not in k] + params.update({k: '' for k in key_only_params}) + params = filter_sort_dict(params, ignored_parameters) return urlencode(params) diff --git a/tests/unit/test_cache_keys.py b/tests/unit/test_cache_keys.py index f2d8c26..b02ffa9 100644 --- a/tests/unit/test_cache_keys.py +++ b/tests/unit/test_cache_keys.py @@ -34,6 +34,12 @@ def test_create_key__normalize_url_params(url, params): assert create_key(request) == CACHE_KEY +def test_create_key__normalize_key_only_params(): + request_1 = Request(method='GET', url='https://img.site.com/base/img.jpg?param_1') + request_2 = Request(method='GET', url='https://img.site.com/base/img.jpg?param_2') + assert create_key(request_1) != create_key(request_2) + + def test_normalize_request__json_body(): request = Request( method='GET', |