Ticket #16035: gzip.patch.tests.diff

File gzip.patch.tests.diff, 3.5 KB (added by Matthew Somerville, 13 years ago)

Patch from right directory, and some tests

  • django/middleware/gzip.py

     
    3232        if not re_accepts_gzip.search(ae):
    3333            return response
    3434
     35        if response.has_header('ETag'):
     36            response['ETag'] = re.sub('"$', ';gzip"', response['ETag'])
     37
    3538        response.content = compress_string(response.content)
    3639        response['Content-Encoding'] = 'gzip'
    3740        response['Content-Length'] = str(len(response.content))
  • tests/regressiontests/middleware/tests.py

     
    77from django.http import HttpRequest
    88from django.middleware.common import CommonMiddleware
    99from django.middleware.http import ConditionalGetMiddleware
     10from django.middleware.gzip import GZipMiddleware
    1011from django.test import TestCase
    1112
    1213
     
    371372        self.resp['Last-Modified'] = 'Sat, 12 Feb 2011 17:41:44 GMT'
    372373        self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
    373374        self.assertEqual(self.resp.status_code, 200)
     375
     376class GZipMiddlewareTest(TestCase):
     377    urls = 'regressiontests.middleware.gzip_urls'
     378
     379    def setUp(self):
     380        self.use_etags = settings.USE_ETAGS
     381        settings.USE_ETAGS = True
     382
     383    def tearDown(self):
     384        settings.USE_ETAGS = self.use_etags
     385
     386    def _get_request(self, path):
     387        request = HttpRequest()
     388        request.META = {
     389            'SERVER_NAME': 'testserver',
     390            'SERVER_PORT': 80,
     391        }
     392        request.path = request.path_info = path
     393        return request
     394
     395    def test_short_response_not_gzipped(self):
     396        request = self._get_request('/short')
     397        request.META['HTTP_ACCEPT_ENCODING'] = 'gzip'
     398        response = self.client.get(request.path)
     399        response = GZipMiddleware().process_response(request, response)
     400        self.assertEqual(response.has_header('Content-Encoding'), False)
     401
     402    def test_gzip_etag_differs_from_non_gzipped(self):
     403        request = self._get_request('/etag')
     404        request.META['HTTP_ACCEPT_ENCODING'] = ''
     405        response = self.client.get(request.path)
     406        response = GZipMiddleware().process_response(request, response)
     407        no_gzip_etag = response['ETag']
     408
     409        request.META['HTTP_ACCEPT_ENCODING'] = 'gzip'
     410        response = self.client.get(request.path)
     411        response = GZipMiddleware().process_response(request, response)
     412        self.assertEqual(response['Content-Encoding'], 'gzip')
     413        gzip_etag = response['ETag']
     414
     415        self.assertNotEqual(no_gzip_etag, gzip_etag)
  • tests/regressiontests/middleware/gzip_urls.py

     
     1from django.conf.urls.defaults import patterns
     2from django.http import HttpResponse
     3
     4urlpatterns = patterns('',
     5    (r'^short$', lambda request: HttpResponse('This is a short response to a request.')),
     6    (r'^etag$', lambda request: HttpResponse('This is the response to a request which is going to be fetched both gzipped and not. This is so we can test whether the ETag has changed for the gzipped content, and this is being quite wordy to get over the 200 character start point for being gzipped.')),
     7)
Back to Top