Commits

Anonymous committed 879f53f

Fixed #16035 -- Appended the Etag response header if the GZipMiddleware is in use to follow RFC2616 better. Thanks, ext and dracos2.

Comments (0)

Files changed (2)

django/middleware/gzip.py

         if len(compressed_content) >= len(response.content):
             return response
 
+        if response.has_header('ETag'):
+            response['ETag'] = re.sub('"$', ';gzip"', response['ETag'])
+
         response.content = compressed_content
         response['Content-Encoding'] = 'gzip'
         response['Content-Length'] = str(len(response.content))

tests/regressiontests/middleware/tests.py

 from django.middleware.common import CommonMiddleware
 from django.middleware.http import ConditionalGetMiddleware
 from django.middleware.gzip import GZipMiddleware
-from django.test import TestCase
-
+from django.test import TestCase, RequestFactory
+from django.test.utils import override_settings
 
 class CommonMiddlewareTest(TestCase):
     def setUp(self):
         r = GZipMiddleware().process_response(self.req, self.resp)
         self.assertEqual(r.content, self.uncompressible_string)
         self.assertEqual(r.get('Content-Encoding'), None)
+
+
+@override_settings(USE_ETAGS=True)
+class ETagGZipMiddlewareTest(TestCase):
+    """
+    Tests if the ETag middleware behaves correctly with GZip middleware.
+    """
+    compressible_string = 'a' * 500
+
+    def setUp(self):
+        self.rf = RequestFactory()
+
+    def test_compress_response(self):
+        """
+        Tests that ETag is changed after gzip compression is performed.
+        """
+        request = self.rf.get('/', HTTP_ACCEPT_ENCODING='gzip, deflate')
+        response = GZipMiddleware().process_response(request,
+            CommonMiddleware().process_response(request,
+                HttpResponse(self.compressible_string)))
+        gzip_etag = response.get('ETag')
+
+        request = self.rf.get('/', HTTP_ACCEPT_ENCODING='')
+        response = GZipMiddleware().process_response(request,
+            CommonMiddleware().process_response(request,
+                HttpResponse(self.compressible_string)))
+        nogzip_etag = response.get('ETag')
+
+        self.assertNotEqual(gzip_etag, nogzip_etag)