|
6 | 6 | from compressor import CssCompressor, JsCompressor
|
7 | 7 | from compressor.conf import settings
|
8 | 8 | from compressor.storage import CompressorFileStorage
|
| 9 | +from compressor.utils import get_hexdigest, get_mtime |
9 | 10 |
|
10 | 11 | from django.conf import settings as django_settings
|
11 | 12 |
|
@@ -138,39 +139,44 @@ def setUp(self):
|
138 | 139 | """
|
139 | 140 | self.cssNode = CssCompressor(self.css)
|
140 | 141 |
|
| 142 | + def get_hashed_mtime(self, filename, length=12): |
| 143 | + filename = os.path.realpath(filename) |
| 144 | + mtime = str(int(get_mtime(filename))) |
| 145 | + return get_hexdigest(mtime)[:length] |
| 146 | + |
141 | 147 | def test_css_absolute_filter(self):
|
142 | 148 | from compressor.filters.css_default import CssAbsoluteFilter
|
143 | 149 | filename = os.path.join(settings.MEDIA_ROOT, 'css/url/test.css')
|
144 | 150 | content = "p { background: url(http://github.com/'../../images/image.gif') }"
|
145 |
| - output = "p { background: url('http://github.com/%simages/image.gif?%3Cspan%20class=%22x%20x-first%20x-last%22%3E275088b9bcf0%3C/span%3E') }" % settings.MEDIA_URL |
| 151 | + output = "p { background: url('http://github.com/%simages/image.gif?%3Cspan%20class=%22x%20x-first%20x-last%22%3E%s%3C/span%3E') }" % (settings.MEDIA_URL, self.get_hashed_mtime(filename)) |
146 | 152 | filter = CssAbsoluteFilter(content)
|
147 | 153 | self.assertEqual(output, filter.input(filename=filename))
|
148 | 154 | settings.MEDIA_URL = 'http://media.example.com/'
|
149 | 155 | filename = os.path.join(settings.MEDIA_ROOT, 'css/url/test.css')
|
150 |
| - output = "p { background: url('http://github.com/%simages/image.gif?%3Cspan%20class=%22x%20x-first%20x-last%22%3E275088b9bcf0%3C/span%3E') }" % settings.MEDIA_URL |
| 156 | + output = "p { background: url('http://github.com/%simages/image.gif?%3Cspan%20class=%22x%20x-first%20x-last%22%3E%s%3C/span%3E') }" % (settings.MEDIA_URL, self.get_hashed_mtime(filename)) |
151 | 157 | self.assertEqual(output, filter.input(filename=filename))
|
152 | 158 |
|
153 | 159 | def test_css_absolute_filter_https(self):
|
154 | 160 | from compressor.filters.css_default import CssAbsoluteFilter
|
155 | 161 | filename = os.path.join(settings.MEDIA_ROOT, 'css/url/test.css')
|
156 | 162 | content = "p { background: url(http://github.com/'../../images/image.gif') }"
|
157 |
| - output = "p { background: url('http://github.com/%simages/image.gif?%3Cspan%20class=%22x%20x-first%20x-last%22%3E275088b9bcf0%3C/span%3E') }" % settings.MEDIA_URL |
| 163 | + output = "p { background: url('http://github.com/%simages/image.gif?%3Cspan%20class=%22x%20x-first%20x-last%22%3E%s%3C/span%3E') }" % (settings.MEDIA_URL, self.get_hashed_mtime(filename)) |
158 | 164 | filter = CssAbsoluteFilter(content)
|
159 | 165 | self.assertEqual(output, filter.input(filename=filename))
|
160 | 166 | settings.MEDIA_URL = 'https://media.example.com/'
|
161 | 167 | filename = os.path.join(settings.MEDIA_ROOT, 'css/url/test.css')
|
162 |
| - output = "p { background: url('http://github.com/%simages/image.gif?%3Cspan%20class=%22x%20x-first%20x-last%22%3E275088b9bcf0%3C/span%3E') }" % settings.MEDIA_URL |
| 168 | + output = "p { background: url('http://github.com/%simages/image.gif?%3Cspan%20class=%22x%20x-first%20x-last%22%3E%s%3C/span%3E') }" % (settings.MEDIA_URL, self.get_hashed_mtime(filename)) |
163 | 169 | self.assertEqual(output, filter.input(filename=filename))
|
164 | 170 |
|
165 | 171 | def test_css_absolute_filter_relative_path(self):
|
166 | 172 | from compressor.filters.css_default import CssAbsoluteFilter
|
167 | 173 | filename = os.path.join(django_settings.TEST_DIR, 'whatever', '..', 'media', 'whatever/../css/url/test.css')
|
168 | 174 | content = "p { background: url(http://github.com/'../../images/image.gif') }"
|
169 |
| - output = "p { background: url('http://github.com/%simages/image.gif?%3Cspan%20class=%22x%20x-first%20x-last%22%3E275088b9bcf0%3C/span%3E') }" % settings.MEDIA_URL |
| 175 | + output = "p { background: url('http://github.com/%simages/image.gif?%3Cspan%20class=%22x%20x-first%20x-last%22%3E%s%3C/span%3E') }" % (settings.MEDIA_URL, self.get_hashed_mtime(filename)) |
170 | 176 | filter = CssAbsoluteFilter(content)
|
171 | 177 | self.assertEqual(output, filter.input(filename=filename))
|
172 | 178 | settings.MEDIA_URL = 'https://media.example.com/'
|
173 |
| - output = "p { background: url('http://github.com/%simages/image.gif?%3Cspan%20class=%22x%20x-first%20x-last%22%3E275088b9bcf0%3C/span%3E') }" % settings.MEDIA_URL |
| 179 | + output = "p { background: url('http://github.com/%simages/image.gif?%3Cspan%20class=%22x%20x-first%20x-last%22%3E%s%3C/span%3E') }" % (settings.MEDIA_URL, self.get_hashed_mtime(filename)) |
174 | 180 | self.assertEqual(output, filter.input(filename=filename))
|
175 | 181 |
|
176 | 182 |
|
@@ -218,7 +224,7 @@ def test_avoid_reordering_css(self):
|
218 | 224 | media = [u'screen', u'print', u'all', None, u'print']
|
219 | 225 | links = BeautifulSoup(node.output()).findAll('link')
|
220 | 226 | self.assertEqual(media, [l.get('media', None) for l in links])
|
221 |
| - |
| 227 | + |
222 | 228 |
|
223 | 229 | class CssMinTestCase(TestCase):
|
224 | 230 | def test_cssmin_filter(self):
|
|
0 commit comments