Ticket #16035: gzip.patch.tests.diff
File gzip.patch.tests.diff, 3.5 KB (added by , 13 years ago) |
---|
-
django/middleware/gzip.py
32 32 if not re_accepts_gzip.search(ae): 33 33 return response 34 34 35 if response.has_header('ETag'): 36 response['ETag'] = re.sub('"$', ';gzip"', response['ETag']) 37 35 38 response.content = compress_string(response.content) 36 39 response['Content-Encoding'] = 'gzip' 37 40 response['Content-Length'] = str(len(response.content)) -
tests/regressiontests/middleware/tests.py
7 7 from django.http import HttpRequest 8 8 from django.middleware.common import CommonMiddleware 9 9 from django.middleware.http import ConditionalGetMiddleware 10 from django.middleware.gzip import GZipMiddleware 10 11 from django.test import TestCase 11 12 12 13 … … 371 372 self.resp['Last-Modified'] = 'Sat, 12 Feb 2011 17:41:44 GMT' 372 373 self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp) 373 374 self.assertEqual(self.resp.status_code, 200) 375 376 class 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
1 from django.conf.urls.defaults import patterns 2 from django.http import HttpResponse 3 4 urlpatterns = 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 )