Ticket #17514: middleware_gzip_patch.diff

File middleware_gzip_patch.diff, 5.5 KB (added by cannona@…, 13 years ago)

Patch.

  • django/middleware/gzip.py

     
    3232        if not re_accepts_gzip.search(ae):
    3333            return response
    3434
    35         response.content = compress_string(response.content)
     35        compressed_content = compress_string(response.content)
     36        # If compression didn't reduce the size of the content, don't return the compressed content.
     37        if len(compressed_content) >= len(response.content):
     38            return response
     39
     40        response.content = compressed_content
    3641        response['Content-Encoding'] = 'gzip'
    3742        response['Content-Length'] = str(len(response.content))
    3843        return response
  • tests/regressiontests/middleware/tests.py

     
    11# -*- coding: utf-8 -*-
    22
    33import re
     4from random import randint
    45
    56from django.conf import settings
    67from django.core import mail
     8from django.utils.text import compress_string
    79from django.http import HttpRequest
    810from django.http import HttpResponse
    911from django.middleware.clickjacking import XFrameOptionsMiddleware
    1012from django.middleware.common import CommonMiddleware
    1113from django.middleware.http import ConditionalGetMiddleware
     14from django.middleware.gzip import GZipMiddleware
    1215from django.test import TestCase
    1316
    1417
     
    495498        r = OtherXFrameOptionsMiddleware().process_response(HttpRequest(),
    496499                                                       HttpResponse())
    497500        self.assertEqual(r['X-Frame-Options'], 'DENY')
     501
     502
     503class GZipMiddlewareTest(TestCase):
     504    """
     505    Tests the GZip middleware.
     506    """
     507    short_string = "This string is too short to be worth compressing."
     508    compressible_string = 'a'*1000
     509    uncompressible_string = []
     510    for i in xrange(1000):
     511        uncompressible_string.append(chr(randint(0,255)))
     512    uncompressible_string = "".join(uncompressible_string)
     513
     514    def setUp(self):
     515        self.req = HttpRequest()
     516        self.req.META = {
     517            'SERVER_NAME': 'testserver',
     518            'SERVER_PORT': 80,
     519        }
     520        self.req.path = self.req.path_info = "/"
     521        self.req.META['HTTP_ACCEPT_ENCODING'] = 'gzip, deflate'
     522        self.req.META['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1'
     523        self.resp = HttpResponse()
     524        self.resp.status_code = 200
     525        self.resp.content = self.compressible_string
     526        self.resp['Content-Type'] = 'text/html; charset=UTF-8'
     527
     528    # tests if compression is performed on responses with compressible content,
     529    # and no other prohibitting factors.
     530    def test_compress_response(self):
     531        r = GZipMiddleware().process_response(self.req, self.resp)
     532        self.assertEqual(compress_string(self.compressible_string), r.content)
     533        self.assertEqual(r.get('Content-Encoding', ''), 'gzip')
     534        self.assertEqual(r.get('Content-Length', ''), str(len(r.content)))
     535        self.assertNotEqual(r.content, self.compressible_string)
     536
     537    # tests if compression is performed on responses with a status other than 200.
     538    def test_no_compress_non_200_response(self):
     539        self.resp.status_code = 404
     540        r = GZipMiddleware().process_response(self.req, self.resp)
     541        self.assertNotEqual(compress_string(self.compressible_string), r.content)
     542        self.assertNotEqual(r.get('Content-Encoding', ''), 'gzip')
     543        self.assertEqual(r.content, self.compressible_string)
     544
     545    # tests if compression is performed on responses with short content
     546    def test_no_compress_short_response(self):
     547        self.resp.content = self.short_string
     548        r = GZipMiddleware().process_response(self.req, self.resp)
     549        self.assertNotEqual(compress_string(self.short_string), r.content)
     550        self.assertNotEqual(r.get('Content-Encoding', ''), 'gzip')
     551        self.assertEqual(r.content, self.short_string)
     552
     553    # tests if compression is performed on responses that are already compressed
     554    def test_no_compress_compressed_response(self):
     555        first_content = GZipMiddleware().process_response(self.req, self.resp).content
     556        r = GZipMiddleware().process_response(self.req, self.resp)
     557        self.assertEqual(r.content, first_content)
     558
     559    # tests if compression is performed on JavaScript requests from Internet Explorer
     560    def test_no_compress_ie_js_requests(self):
     561        self.req.META['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)'
     562        self.resp['Content-Type'] = 'application/javascript; charset=UTF-8'
     563        r = GZipMiddleware().process_response(self.req, self.resp)
     564        self.assertNotEqual(compress_string(self.compressible_string), r.content)
     565        self.assertNotEqual(r.get('Content-Encoding', ''), 'gzip')
     566        self.assertEqual(r.content, self.compressible_string)
     567
     568    # tests if compression is performed on responses with uncompressible content
     569    def test_no_compress_uncompressible_response(self):
     570        self.resp.content = self.uncompressible_string
     571        r = GZipMiddleware().process_response(self.req, self.resp)
     572        self.assertNotEqual(compress_string(self.compressible_string), r.content)
     573        self.assertNotEqual(r.get('Content-Encoding', ''), 'gzip')
     574        self.assertEqual(r.content, self.uncompressible_string)
Back to Top