Fixed #20472: response.content should be bytes on both Python 2 and 3
This commit is contained in:
parent
4ba1c2e785
commit
0594fed9ff
|
@ -31,13 +31,13 @@ def conditional_content_removal(request, response):
|
||||||
if response.streaming:
|
if response.streaming:
|
||||||
response.streaming_content = []
|
response.streaming_content = []
|
||||||
else:
|
else:
|
||||||
response.content = ''
|
response.content = b''
|
||||||
response['Content-Length'] = '0'
|
response['Content-Length'] = '0'
|
||||||
if request.method == 'HEAD':
|
if request.method == 'HEAD':
|
||||||
if response.streaming:
|
if response.streaming:
|
||||||
response.streaming_content = []
|
response.streaming_content = []
|
||||||
else:
|
else:
|
||||||
response.content = ''
|
response.content = b''
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,21 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import io
|
||||||
|
import gzip
|
||||||
|
|
||||||
from django.http import HttpRequest, HttpResponse, StreamingHttpResponse
|
from django.http import HttpRequest, HttpResponse, StreamingHttpResponse
|
||||||
from django.http.utils import conditional_content_removal
|
from django.http.utils import conditional_content_removal
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
|
|
||||||
|
# based on Python 3.3's gzip.compress
|
||||||
|
def gzip_compress(data):
|
||||||
|
buf = io.BytesIO()
|
||||||
|
with gzip.GzipFile(fileobj=buf, mode='wb', compresslevel=0) as f:
|
||||||
|
f.write(data)
|
||||||
|
return buf.getvalue()
|
||||||
|
|
||||||
|
|
||||||
class HttpUtilTests(TestCase):
|
class HttpUtilTests(TestCase):
|
||||||
|
|
||||||
def test_conditional_content_removal(self):
|
def test_conditional_content_removal(self):
|
||||||
|
@ -33,6 +44,19 @@ class HttpUtilTests(TestCase):
|
||||||
conditional_content_removal(req, res)
|
conditional_content_removal(req, res)
|
||||||
self.assertEqual(b''.join(res), b'')
|
self.assertEqual(b''.join(res), b'')
|
||||||
|
|
||||||
|
# Issue #20472
|
||||||
|
abc = gzip_compress(b'abc')
|
||||||
|
res = HttpResponse(abc, status=304)
|
||||||
|
res['Content-Encoding'] = 'gzip'
|
||||||
|
conditional_content_removal(req, res)
|
||||||
|
self.assertEqual(res.content, b'')
|
||||||
|
|
||||||
|
res = StreamingHttpResponse([abc], status=304)
|
||||||
|
res['Content-Encoding'] = 'gzip'
|
||||||
|
conditional_content_removal(req, res)
|
||||||
|
self.assertEqual(b''.join(res), b'')
|
||||||
|
|
||||||
|
|
||||||
# Strip content for HEAD requests.
|
# Strip content for HEAD requests.
|
||||||
req.method = 'HEAD'
|
req.method = 'HEAD'
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue