Skip to content

Commit 43bd91e

Browse files
committed
Some tests. Probably worth pruning.
1 parent 2c6f027 commit 43bd91e

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

tests/test_adapter.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,10 @@ def test_close(self):
5454

5555
sess.close()
5656
assert cache.close.called
57+
58+
def test_cache_control_sets_sort_query():
59+
for bool_ in (True, False):
60+
sess = CacheControl(Session(), mock.Mock(spec=DictCache),
61+
sort_query=bool_)
62+
assert sess.adapters['http://'].sort_query == bool_
63+
assert sess.adapters['http://'].controller.sort_query == bool_

tests/test_cache_control.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import pytest
55
from mock import ANY, Mock
66
import time
7+
from random import shuffle
8+
import string
79

810
from cachecontrol import CacheController
911
from cachecontrol.cache import DictCache
@@ -225,3 +227,30 @@ def test_cached_request_with_bad_max_age_headers_not_returned(self):
225227
self.c.cache = DictCache({self.url: resp})
226228

227229
assert not self.req({})
230+
231+
def test_cache_url_sorting():
232+
letter_n_numbers = list(enumerate(string.ascii_lowercase[3:], start=4))
233+
suff = '&' + '&'.join('%s=%s' % (k, v) for v, k in letter_n_numbers)
234+
235+
def get_param(url):
236+
"""Mock losing order when processing params"""
237+
shuffle(letter_n_numbers)
238+
params = {k: v for v, k in letter_n_numbers}
239+
url = url.replace(suff, '')
240+
query = '&' + '&'.join('%s=%s' % item for item in params.items())
241+
return url + query
242+
243+
no_query = 'http://example.com'
244+
unsorted_query = 'http://example.com?b=2&c=3&a=1' + suff
245+
sorted_query = 'http://example.com?a=1&b=2&c=3' + suff
246+
247+
cache_url = CacheController.cache_url
248+
assert cache_url(no_query, sort_query=True) == cache_url(no_query)
249+
assert cache_url(unsorted_query) != cache_url(sorted_query)
250+
assert cache_url(unsorted_query, True) == cache_url(sorted_query)
251+
randomized = get_param(unsorted_query)
252+
assert randomized != unsorted_query
253+
assert cache_url(randomized) != cache_url(sorted_query)
254+
assert cache_url(randomized, True) == cache_url(sorted_query)
255+
randomized_again = get_param(unsorted_query)
256+
assert cache_url(randomized, True) == cache_url(randomized_again, True)

tests/test_storage_filecache.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
import os
55
import string
66

7-
from random import randint, sample
7+
from random import randint, sample, shuffle
88

99
import pytest
1010
import requests
1111
from cachecontrol import CacheControl
1212
from cachecontrol.caches import FileCache
13+
from cachecontrol.caches.file_cache import url_to_file_path
1314
from lockfile import LockFile
1415
from lockfile.mkdirlockfile import MkdirLockFile
1516

@@ -110,3 +111,59 @@ def test_lock_class(self, tmpdir):
110111
lock_class = object()
111112
cache = FileCache(str(tmpdir), lock_class=lock_class)
112113
assert cache.lock_class is lock_class
114+
115+
def test_url_to_file_path(self, tmpdir):
116+
cache = FileCache(str(tmpdir))
117+
# We'll add a long sorted suffix so that unsorted queries have a low
118+
# collision probability (about 3.8e-23 for each sorted/unsorted
119+
# comparison).
120+
letter_n_numbers = list(enumerate(string.ascii_lowercase[3:], start=4))
121+
suff = '&' + '&'.join('%s=%s' % (k, v) for v, k in letter_n_numbers)
122+
123+
def get_param(url):
124+
"""Mock losing order when processing params"""
125+
shuffle(letter_n_numbers)
126+
params = {k: v for v, k in letter_n_numbers}
127+
url = url.replace(suff, '')
128+
query = '&' + '&'.join('%s=%s' % item for item in params.items())
129+
return url + query
130+
131+
urls = {
132+
'no_query': 'http://example.com',
133+
'unsorted_query': 'http://example.com?b=2&c=3&a=1' + suff,
134+
'sorted_query': 'http://example.com?a=1&b=2&c=3' + suff,
135+
'unsorted_empty_value': 'http://example.com?b=&c=&a=1' + suff,
136+
'sorted_empty_value': 'http://example.com?a=1&b=&c=3' + suff,
137+
'unsorted_repeated_key': 'http://example.com?b=2&c=3&b=0'
138+
'&c=5&a=1' + suff,
139+
'sorted_repeated_key': 'http://example.com?a=1&b=0&b=2&c=3&'
140+
'c=5' + suff}
141+
142+
unoquery = url_to_file_path(urls['no_query'], cache, sort_query=False)
143+
snoquery = url_to_file_path(urls['no_query'], cache, sort_query=True)
144+
assert unoquery == snoquery
145+
urls.pop('no_query')
146+
147+
sortedpaths = {urlname: url_to_file_path(urlvalue, cache, True) for
148+
urlname, urlvalue in urls.items()}
149+
150+
for key, value in urls.items():
151+
if key.startswith('sorted'):
152+
assert url_to_file_path(value, cache, True) == sortedpaths[key]
153+
154+
unsortedpaths = {urlname: url_to_file_path(urlvalue, cache, False) for
155+
urlname, urlvalue in urls.items()}
156+
157+
for key, url in urls.items():
158+
if key.startswith('unsorted'):
159+
assert sortedpaths[key] != unsortedpaths[key]
160+
else:
161+
assert sortedpaths[key] == unsortedpaths[key]
162+
163+
# Randomize and sort params
164+
sort_param = url_to_file_path(get_param(url), cache, True)
165+
assert sort_param == sortedpaths[key]
166+
167+
# Randomize but don't sort params
168+
unsort_param = url_to_file_path(get_param(url), cache, False)
169+
assert unsort_param != unsortedpaths[key]

0 commit comments

Comments
 (0)