Ticket #16035: patch_for_16035.diff

File patch_for_16035.diff, 2.8 KB (added by Jakub Wiśniowski, 13 years ago)

Uses new way to override settings

  • django/middleware/gzip.py

    diff --git a/django/middleware/gzip.py b/django/middleware/gzip.py
    index 39ec624..cc26712 100644
    a b class GZipMiddleware(object):  
    3737        if len(compressed_content) >= len(response.content):
    3838            return response
    3939
     40        if response.has_header('ETag'): 
     41            response['ETag'] = re.sub('"$', ';gzip"', response['ETag'])
     42
    4043        response.content = compressed_content
    4144        response['Content-Encoding'] = 'gzip'
    4245        response['Content-Length'] = str(len(response.content))
  • tests/regressiontests/middleware/tests.py

    diff --git a/tests/regressiontests/middleware/tests.py b/tests/regressiontests/middleware/tests.py
    index 50cb81d..f54fbb8 100644
    a b from django.middleware.common import CommonMiddleware  
    1414from django.middleware.http import ConditionalGetMiddleware
    1515from django.middleware.gzip import GZipMiddleware
    1616from django.test import TestCase
    17 
     17from django.test.utils import override_settings
    1818
    1919class CommonMiddlewareTest(TestCase):
    2020    def setUp(self):
    class GZipMiddlewareTest(TestCase):  
    582582        r = GZipMiddleware().process_response(self.req, self.resp)
    583583        self.assertEqual(r.content, self.uncompressible_string)
    584584        self.assertEqual(r.get('Content-Encoding'), None)
     585
     586
     587@override_settings(USE_ETAGS=True)
     588class ETagGZipMiddlewareTest(TestCase):
     589    """
     590    Tests if the ETag middleware behaves correctly with GZip middleware.
     591    """
     592    compressible_string = 'a' * 500
     593
     594    def _get_request(self):
     595        req = HttpRequest()
     596        req.META = {
     597            'SERVER_NAME': 'testserver',
     598            'SERVER_PORT': 80,
     599        }
     600        req.path = req.path_info = "/"
     601        req.META['HTTP_ACCEPT_ENCODING'] = 'gzip, deflate'
     602        req.META['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1'
     603        return req
     604
     605    def _get_response(self):
     606        resp = HttpResponse()
     607        resp.status_code = 200
     608        resp.content = self.compressible_string
     609        resp['Content-Type'] = 'text/html; charset=UTF-8'
     610        return resp
     611
     612    def test_compress_response(self):
     613        """
     614        Tests that ETag is changed after gzip compression is performed.
     615        """
     616        req = self._get_request()
     617        resp = self._get_response()
     618        r = GZipMiddleware().process_response(req,
     619                CommonMiddleware().process_response(req, resp))
     620        gzip_etag = r.get('ETag')
     621
     622        req = self._get_request()
     623        req.META['HTTP_ACCEPT_ENCODING'] = ''
     624        resp = self._get_response()
     625        r = GZipMiddleware().process_response(req,
     626                CommonMiddleware().process_response(req, resp))
     627        nogzip_etag = r.get('ETag')
     628
     629        self.assertNotEqual(gzip_etag, nogzip_etag)
Back to Top