Index: django/middleware/gzip.py
===================================================================
--- django/middleware/gzip.py	(revision 17342)
+++ django/middleware/gzip.py	(working copy)
@@ -32,7 +32,12 @@
         if not re_accepts_gzip.search(ae):
             return response
 
-        response.content = compress_string(response.content)
+        compressed_content = compress_string(response.content)
+        # If compression didn't reduce the size of the content, don't return the compressed content.
+        if len(compressed_content) >= len(response.content):
+            return response
+
+        response.content = compressed_content
         response['Content-Encoding'] = 'gzip'
         response['Content-Length'] = str(len(response.content))
         return response
Index: tests/regressiontests/middleware/tests.py
===================================================================
--- tests/regressiontests/middleware/tests.py	(revision 17342)
+++ tests/regressiontests/middleware/tests.py	(working copy)
@@ -1,14 +1,17 @@
 # -*- coding: utf-8 -*-
 
 import re
+from random import randint
 
 from django.conf import settings
 from django.core import mail
+from django.utils.text import compress_string
 from django.http import HttpRequest
 from django.http import HttpResponse
 from django.middleware.clickjacking import XFrameOptionsMiddleware
 from django.middleware.common import CommonMiddleware
 from django.middleware.http import ConditionalGetMiddleware
+from django.middleware.gzip import GZipMiddleware
 from django.test import TestCase
 
 
@@ -495,3 +498,77 @@
         r = OtherXFrameOptionsMiddleware().process_response(HttpRequest(),
                                                        HttpResponse())
         self.assertEqual(r['X-Frame-Options'], 'DENY')
+
+
+class GZipMiddlewareTest(TestCase):
+    """
+    Tests the GZip middleware.
+    """
+    short_string = "This string is too short to be worth compressing."
+    compressible_string = 'a'*1000
+    uncompressible_string = []
+    for i in xrange(1000):
+        uncompressible_string.append(chr(randint(0,255)))
+    uncompressible_string = "".join(uncompressible_string)
+
+    def setUp(self):
+        self.req = HttpRequest()
+        self.req.META = {
+            'SERVER_NAME': 'testserver',
+            'SERVER_PORT': 80,
+        }
+        self.req.path = self.req.path_info = "/"
+        self.req.META['HTTP_ACCEPT_ENCODING'] = 'gzip, deflate'
+        self.req.META['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1'
+        self.resp = HttpResponse()
+        self.resp.status_code = 200
+        self.resp.content = self.compressible_string
+        self.resp['Content-Type'] = 'text/html; charset=UTF-8'
+
+    # tests if compression is performed on responses with compressible content,
+    # and no other prohibitting factors.
+    def test_compress_response(self):
+        r = GZipMiddleware().process_response(self.req, self.resp)
+        self.assertEqual(compress_string(self.compressible_string), r.content)
+        self.assertEqual(r.get('Content-Encoding', ''), 'gzip')
+        self.assertEqual(r.get('Content-Length', ''), str(len(r.content)))
+        self.assertNotEqual(r.content, self.compressible_string)
+
+    # tests if compression is performed on responses with a status other than 200.
+    def test_no_compress_non_200_response(self):
+        self.resp.status_code = 404
+        r = GZipMiddleware().process_response(self.req, self.resp)
+        self.assertNotEqual(compress_string(self.compressible_string), r.content)
+        self.assertNotEqual(r.get('Content-Encoding', ''), 'gzip')
+        self.assertEqual(r.content, self.compressible_string)
+
+    # tests if compression is performed on responses with short content
+    def test_no_compress_short_response(self):
+        self.resp.content = self.short_string
+        r = GZipMiddleware().process_response(self.req, self.resp)
+        self.assertNotEqual(compress_string(self.short_string), r.content)
+        self.assertNotEqual(r.get('Content-Encoding', ''), 'gzip')
+        self.assertEqual(r.content, self.short_string)
+
+    # tests if compression is performed on responses that are already compressed
+    def test_no_compress_compressed_response(self):
+        first_content = GZipMiddleware().process_response(self.req, self.resp).content
+        r = GZipMiddleware().process_response(self.req, self.resp)
+        self.assertEqual(r.content, first_content)
+
+    # tests if compression is performed on JavaScript requests from Internet Explorer
+    def test_no_compress_ie_js_requests(self):
+        self.req.META['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)'
+        self.resp['Content-Type'] = 'application/javascript; charset=UTF-8'
+        r = GZipMiddleware().process_response(self.req, self.resp)
+        self.assertNotEqual(compress_string(self.compressible_string), r.content)
+        self.assertNotEqual(r.get('Content-Encoding', ''), 'gzip')
+        self.assertEqual(r.content, self.compressible_string)
+
+    # tests if compression is performed on responses with uncompressible content
+    def test_no_compress_uncompressible_response(self):
+        self.resp.content = self.uncompressible_string
+        r = GZipMiddleware().process_response(self.req, self.resp)
+        self.assertNotEqual(compress_string(self.compressible_string), r.content)
+        self.assertNotEqual(r.get('Content-Encoding', ''), 'gzip')
+        self.assertEqual(r.content, self.uncompressible_string)
