Code

Ticket #10762: 10762_with_tests.patch

File 10762_with_tests.patch, 2.3 KB (added by madjar, 2 years ago)

The previous patch, with added tests

  • django/middleware/gzip.py

    diff --git a/django/middleware/gzip.py b/django/middleware/gzip.py
    index 8e88a8c..6479c08 100644
    a b class GZipMiddleware(object): 
    1212    on the Accept-Encoding header. 
    1313    """ 
    1414    def process_response(self, request, response): 
    15         # It's not worth compressing non-OK or really short responses. 
    16         if response.status_code != 200 or len(response.content) < 200: 
     15        # It's not worth compressing really short responses. 
     16        if len(response.content) < 200: 
    1717            return response 
    1818 
    1919        patch_vary_headers(response, ('Accept-Encoding',)) 
  • tests/regressiontests/middleware/tests.py

    diff --git a/tests/regressiontests/middleware/tests.py b/tests/regressiontests/middleware/tests.py
    index 124eb19..639e1c7 100644
    a b from django.http import HttpResponse 
    99from django.middleware.clickjacking import XFrameOptionsMiddleware 
    1010from django.middleware.common import CommonMiddleware 
    1111from django.middleware.http import ConditionalGetMiddleware 
     12from django.middleware.gzip import GZipMiddleware 
    1213from django.test import TestCase 
    1314 
    1415 
    class XFrameOptionsMiddlewareTest(TestCase): 
    495496        r = OtherXFrameOptionsMiddleware().process_response(HttpRequest(), 
    496497                                                       HttpResponse()) 
    497498        self.assertEqual(r['X-Frame-Options'], 'DENY') 
     499 
     500class GZipMiddlewareTest(TestCase): 
     501    def _get_processed_response(self, content, status): 
     502        request = HttpRequest() 
     503        request.META = {'HTTP_ACCEPT_ENCODING': 'gzip'} 
     504        response = HttpResponse(content, status=status) 
     505        return GZipMiddleware().process_response(request, response) 
     506 
     507    def test_short_ok_should_not_be_gzipped(self): 
     508        r = self._get_processed_response('hello world', status=200) 
     509 
     510        self.assertNotIn('Content-Encoding', r) 
     511 
     512    def test_long_ok_should_be_gzipped(self): 
     513        r = self._get_processed_response('hello world'*20, status=200) 
     514 
     515        self.assertEqual(r['Content-Encoding'], 'gzip') 
     516 
     517    def test_long_created_should_be_gzipped(self): 
     518        r = self._get_processed_response('hello world'*20, status=201) 
     519 
     520        self.assertEqual(r['Content-Encoding'], 'gzip')