1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
"""The cache_keys module is mostly covered indirectly via other tests.
This just contains tests for some extra edge cases not covered elsewhere.
"""
import pytest
from requests import PreparedRequest, Request
from requests_cache.cache_keys import create_key, normalize_request
CACHE_KEY = 'e8cb526891875e37'
@pytest.mark.parametrize(
'url, params',
[
('https://example.com?foo=bar¶m=1', None),
('https://example.com?foo=bar¶m=1', {}),
('https://example.com/?foo=bar¶m=1', {}),
('https://example.com?foo=bar¶m=1&', {}),
('https://example.com?param=1&foo=bar', {}),
('https://example.com?param=1', {'foo': 'bar'}),
('https://example.com?foo=bar', {'param': '1'}),
('https://example.com', {'foo': 'bar', 'param': '1'}),
('https://example.com', {'foo': 'bar', 'param': 1}),
('https://example.com?', {'foo': 'bar', 'param': '1'}),
],
)
def test_create_key__normalize_url_params(url, params):
"""All of the above variations should produce the same cache key"""
request = Request(
method='GET',
url=url,
params=params,
)
assert create_key(request) == CACHE_KEY
def test_normalize_request__json_body():
request = Request(
method='GET',
url='https://img.site.com/base/img.jpg',
data=b'{"param_1": "value_1", "param_2": "value_2"}',
headers={'Content-Type': 'application/json'},
)
assert (
normalize_request(request, ignored_parameters=['param_2']).body == b'{"param_1": "value_1"}'
)
def test_normalize_request__invalid_json_body():
request = Request(
method='GET',
url='https://img.site.com/base/img.jpg',
data=b'invalid JSON!',
headers={'Content-Type': 'application/json'},
)
assert normalize_request(request, ignored_parameters=['param_2']).body == b'invalid JSON!'
def test_normalize_request__binary_body():
request = Request(
method='GET',
url='https://img.site.com/base/img.jpg',
data=b'some bytes',
headers={'Content-Type': 'application/octet-stream'},
)
assert normalize_request(request, ignored_parameters=['param']).body == request.data
def test_remove_ignored_headers__empty():
request = PreparedRequest()
request.prepare(
method='GET',
url='https://img.site.com/base/img.jpg',
headers={'foo': 'bar'},
)
assert normalize_request(request, ignored_parameters=None).headers == request.headers
|