|
4 | 4 | import os
|
5 | 5 | import string
|
6 | 6 |
|
7 |
| -from random import randint, sample |
| 7 | +from random import randint, sample, shuffle |
8 | 8 |
|
9 | 9 | import pytest
|
10 | 10 | import requests
|
11 | 11 | from cachecontrol import CacheControl
|
12 | 12 | from cachecontrol.caches import FileCache
|
| 13 | +from cachecontrol.caches.file_cache import url_to_file_path |
13 | 14 | from lockfile import LockFile
|
14 | 15 | from lockfile.mkdirlockfile import MkdirLockFile
|
15 | 16 |
|
@@ -110,3 +111,59 @@ def test_lock_class(self, tmpdir):
|
110 | 111 | lock_class = object()
|
111 | 112 | cache = FileCache(str(tmpdir), lock_class=lock_class)
|
112 | 113 | 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