summaryrefslogtreecommitdiff
path: root/tests/middleware
diff options
context:
space:
mode:
authorKevin Christopher Henry <k@severian.com>2016-10-14 07:41:42 -0400
committerTim Graham <timograham@gmail.com>2016-10-14 07:41:42 -0400
commit9108696a7553123f57c5d42f9c4a90cad44532f4 (patch)
tree0d0fb03a31a0067a386d98d0d60ddf7d275fa089 /tests/middleware
parent9eb49af821546af1cae8f3a91aefea4b99a6478f (diff)
downloaddjango-9108696a7553123f57c5d42f9c4a90cad44532f4.tar.gz
Refs #19705 -- Changed gzip modification times to 0.
This makes gzip output deterministic, which allows ConditionalGetMiddleware to reliably compare ETags on gzipped content (views using the gzip_page() decorator in particular).
Diffstat (limited to 'tests/middleware')
-rw-r--r--tests/middleware/tests.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/tests/middleware/tests.py b/tests/middleware/tests.py
index db8ed6ea90..12109b3137 100644
--- a/tests/middleware/tests.py
+++ b/tests/middleware/tests.py
@@ -770,6 +770,12 @@ class GZipMiddlewareTest(SimpleTestCase):
with gzip.GzipFile(mode='rb', fileobj=BytesIO(gzipped_string)) as f:
return f.read()
+ @staticmethod
+ def get_mtime(gzipped_string):
+ with gzip.GzipFile(mode='rb', fileobj=BytesIO(gzipped_string)) as f:
+ f.read() # must read the data before accessing the header
+ return f.mtime
+
def test_compress_response(self):
"""
Compression is performed on responses with compressible content.
@@ -850,6 +856,20 @@ class GZipMiddlewareTest(SimpleTestCase):
self.assertEqual(r.content, self.incompressible_string)
self.assertIsNone(r.get('Content-Encoding'))
+ def test_compress_deterministic(self):
+ """
+ Compression results are the same for the same content and don't
+ include a modification time (since that would make the results
+ of compression non-deterministic and prevent
+ ConditionalGetMiddleware from recognizing conditional matches
+ on gzipped content).
+ """
+ r1 = GZipMiddleware().process_response(self.req, self.resp)
+ r2 = GZipMiddleware().process_response(self.req, self.resp)
+ self.assertEqual(r1.content, r2.content)
+ self.assertEqual(self.get_mtime(r1.content), 0)
+ self.assertEqual(self.get_mtime(r2.content), 0)
+
@ignore_warnings(category=RemovedInDjango21Warning)
@override_settings(USE_ETAGS=True)