Code

Ticket #16035: patch_for_16035.diff

File patch_for_16035.diff, 2.8 KB (added by ext, 2 years ago)

Uses new way to override settings

Line 
1diff --git a/django/middleware/gzip.py b/django/middleware/gzip.py
2index 39ec624..cc26712 100644
3--- a/django/middleware/gzip.py
4+++ b/django/middleware/gzip.py
5@@ -37,6 +37,9 @@ class GZipMiddleware(object):
6         if len(compressed_content) >= len(response.content):
7             return response
8 
9+        if response.has_header('ETag'): 
10+            response['ETag'] = re.sub('"$', ';gzip"', response['ETag'])
11+
12         response.content = compressed_content
13         response['Content-Encoding'] = 'gzip'
14         response['Content-Length'] = str(len(response.content))
15diff --git a/tests/regressiontests/middleware/tests.py b/tests/regressiontests/middleware/tests.py
16index 50cb81d..f54fbb8 100644
17--- a/tests/regressiontests/middleware/tests.py
18+++ b/tests/regressiontests/middleware/tests.py
19@@ -14,7 +14,7 @@ from django.middleware.common import CommonMiddleware
20 from django.middleware.http import ConditionalGetMiddleware
21 from django.middleware.gzip import GZipMiddleware
22 from django.test import TestCase
23-
24+from django.test.utils import override_settings
25 
26 class CommonMiddlewareTest(TestCase):
27     def setUp(self):
28@@ -582,3 +582,48 @@ class GZipMiddlewareTest(TestCase):
29         r = GZipMiddleware().process_response(self.req, self.resp)
30         self.assertEqual(r.content, self.uncompressible_string)
31         self.assertEqual(r.get('Content-Encoding'), None)
32+
33+
34+@override_settings(USE_ETAGS=True)
35+class ETagGZipMiddlewareTest(TestCase):
36+    """
37+    Tests if the ETag middleware behaves correctly with GZip middleware.
38+    """
39+    compressible_string = 'a' * 500
40+
41+    def _get_request(self):
42+        req = HttpRequest()
43+        req.META = {
44+            'SERVER_NAME': 'testserver',
45+            'SERVER_PORT': 80,
46+        }
47+        req.path = req.path_info = "/"
48+        req.META['HTTP_ACCEPT_ENCODING'] = 'gzip, deflate'
49+        req.META['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1'
50+        return req
51+
52+    def _get_response(self):
53+        resp = HttpResponse()
54+        resp.status_code = 200
55+        resp.content = self.compressible_string
56+        resp['Content-Type'] = 'text/html; charset=UTF-8'
57+        return resp
58+
59+    def test_compress_response(self):
60+        """
61+        Tests that ETag is changed after gzip compression is performed.
62+        """
63+        req = self._get_request()
64+        resp = self._get_response()
65+        r = GZipMiddleware().process_response(req,
66+                CommonMiddleware().process_response(req, resp))
67+        gzip_etag = r.get('ETag')
68+
69+        req = self._get_request()
70+        req.META['HTTP_ACCEPT_ENCODING'] = ''
71+        resp = self._get_response()
72+        r = GZipMiddleware().process_response(req,
73+                CommonMiddleware().process_response(req, resp))
74+        nogzip_etag = r.get('ETag')
75+
76+        self.assertNotEqual(gzip_etag, nogzip_etag)