Ticket #17514: middleware_gzip_patch_with_docs.diff

File middleware_gzip_patch_with_docs.diff, 7.4 KB (added by cannona, 3 years ago)

Same as the previous patch, except I updated the documentation as well. The old documentation did not accurately or comprehensively describe when GZip compression would be applied.

  • 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
  • docs/ref/middleware.txt

     
    8282Django's :doc:`automatic documentation system </ref/contrib/admin/admindocs>`.
    8383Depends on :class:`~django.contrib.auth.middleware.AuthenticationMiddleware`.
    8484
    85 GZIP middleware
     85GZip middleware
    8686---------------
    8787
    8888.. module:: django.middleware.gzip
    89    :synopsis: Middleware to serve gziped content for performance.
     89   :synopsis: Middleware to serve GZipped content for performance.
    9090
    9191.. class:: GZipMiddleware
    9292
     
    9494browsers).
    9595
    9696It is suggested to place this first in the middleware list, so that the
    97 compression of the response content is the last thing that happens. Will not
    98 compress content bodies less than 200 bytes long, when the response code is
    99 something other than 200, JavaScript files (for IE compatibility), or
    100 responses that have the ``Content-Encoding`` header already specified.
     97compression of the response content is the last thing that happens.
    10198
     99It will not compress content bodies less than 200 bytes long, when the response
     100code is something other than 200, when the ``Content-Encoding`` header is
     101already set, or when the browser does not send a ``Accept_Encoding`` header
     102containing ``gzip``. Content will also not be compressed when the browser is
     103Internet Explorer and the ``Content-Type`` header contains ``javascript`` or
     104starts with anything other than ``text/``.  This is done to overcome a bug
     105present in early versions of Internet Explorer which caused decompression not
     106to be performed on certain content types.
     107
    102108GZip compression can be applied to individual views using the
    103109:func:`~django.views.decorators.http.gzip_page()` decorator.
    104110
  • tests/regressiontests/middleware/tests.py

    Property changes on: docs\ref\middleware.txt
    ___________________________________________________________________
    Added: svn:eol-style
       + native
    
     
    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