Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #1191 from ambv/content_is_bytes

Fixed #20472: response.content should be bytes on both Python 2 and 3
  • Loading branch information...
commit 18856f866cc0e1cc703ae3f4c84b3c25a847b370 2 parents e24d486 + 0594fed
Aymeric Augustin authored
4  django/http/utils.py
@@ -31,13 +31,13 @@ def conditional_content_removal(request, response):
31 31
         if response.streaming:
32 32
             response.streaming_content = []
33 33
         else:
34  
-            response.content = ''
  34
+            response.content = b''
35 35
         response['Content-Length'] = '0'
36 36
     if request.method == 'HEAD':
37 37
         if response.streaming:
38 38
             response.streaming_content = []
39 39
         else:
40  
-            response.content = ''
  40
+            response.content = b''
41 41
     return response
42 42
 
43 43
 
24  tests/http_utils/tests.py
... ...
@@ -1,10 +1,21 @@
1 1
 from __future__ import unicode_literals
2 2
 
  3
+import io
  4
+import gzip
  5
+
3 6
 from django.http import HttpRequest, HttpResponse, StreamingHttpResponse
4 7
 from django.http.utils import conditional_content_removal
5 8
 from django.test import TestCase
6 9
 
7 10
 
  11
+# based on Python 3.3's gzip.compress
  12
+def gzip_compress(data):
  13
+    buf = io.BytesIO()
  14
+    with gzip.GzipFile(fileobj=buf, mode='wb', compresslevel=0) as f:
  15
+        f.write(data)
  16
+    return buf.getvalue()
  17
+
  18
+
8 19
 class HttpUtilTests(TestCase):
9 20
 
10 21
     def test_conditional_content_removal(self):
@@ -33,6 +44,19 @@ def test_conditional_content_removal(self):
33 44
             conditional_content_removal(req, res)
34 45
             self.assertEqual(b''.join(res), b'')
35 46
 
  47
+        # Issue #20472  
  48
+        abc = gzip_compress(b'abc')
  49
+        res = HttpResponse(abc, status=304)
  50
+        res['Content-Encoding'] = 'gzip'
  51
+        conditional_content_removal(req, res)
  52
+        self.assertEqual(res.content, b'')
  53
+
  54
+        res = StreamingHttpResponse([abc], status=304)
  55
+        res['Content-Encoding'] = 'gzip'
  56
+        conditional_content_removal(req, res)
  57
+        self.assertEqual(b''.join(res), b'')
  58
+
  59
+
36 60
         # Strip content for HEAD requests.
37 61
         req.method = 'HEAD'
38 62
 

0 notes on commit 18856f8

Please sign in to comment.
Something went wrong with that request. Please try again.