2012-10-21 20:40:31 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-05-20 22:41:23 +00:00
|
|
|
import gzip
|
2015-01-28 12:35:27 +00:00
|
|
|
import io
|
2013-05-20 22:41:23 +00:00
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.http import (
|
|
|
|
HttpRequest, HttpResponse, HttpResponseRedirect, StreamingHttpResponse,
|
|
|
|
)
|
2014-08-15 16:40:46 +00:00
|
|
|
from django.http.utils import conditional_content_removal, fix_location_header
|
2012-10-21 20:40:31 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
|
2013-05-20 22:41:23 +00:00
|
|
|
# based on Python 3.3's gzip.compress
|
|
|
|
def gzip_compress(data):
|
|
|
|
buf = io.BytesIO()
|
2013-05-21 11:18:53 +00:00
|
|
|
f = gzip.GzipFile(fileobj=buf, mode='wb', compresslevel=0)
|
|
|
|
try:
|
2013-05-20 22:41:23 +00:00
|
|
|
f.write(data)
|
2013-05-21 11:18:53 +00:00
|
|
|
finally:
|
|
|
|
f.close()
|
2013-05-20 22:41:23 +00:00
|
|
|
return buf.getvalue()
|
|
|
|
|
|
|
|
|
2012-10-21 20:40:31 +00:00
|
|
|
class HttpUtilTests(TestCase):
|
|
|
|
|
|
|
|
def test_conditional_content_removal(self):
|
|
|
|
"""
|
|
|
|
Tests that content is removed from regular and streaming responses with
|
|
|
|
a status_code of 100-199, 204, 304 or a method of "HEAD".
|
|
|
|
"""
|
|
|
|
req = HttpRequest()
|
|
|
|
|
|
|
|
# Do nothing for 200 responses.
|
|
|
|
res = HttpResponse('abc')
|
|
|
|
conditional_content_removal(req, res)
|
|
|
|
self.assertEqual(res.content, b'abc')
|
|
|
|
|
|
|
|
res = StreamingHttpResponse(['abc'])
|
|
|
|
conditional_content_removal(req, res)
|
|
|
|
self.assertEqual(b''.join(res), b'abc')
|
|
|
|
|
|
|
|
# Strip content for some status codes.
|
|
|
|
for status_code in (100, 150, 199, 204, 304):
|
|
|
|
res = HttpResponse('abc', status=status_code)
|
|
|
|
conditional_content_removal(req, res)
|
|
|
|
self.assertEqual(res.content, b'')
|
|
|
|
|
|
|
|
res = StreamingHttpResponse(['abc'], status=status_code)
|
|
|
|
conditional_content_removal(req, res)
|
|
|
|
self.assertEqual(b''.join(res), b'')
|
|
|
|
|
2013-10-10 20:42:30 +00:00
|
|
|
# Issue #20472
|
2013-05-20 22:41:23 +00:00
|
|
|
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'')
|
|
|
|
|
2012-10-21 20:40:31 +00:00
|
|
|
# Strip content for HEAD requests.
|
|
|
|
req.method = 'HEAD'
|
|
|
|
|
|
|
|
res = HttpResponse('abc')
|
|
|
|
conditional_content_removal(req, res)
|
|
|
|
self.assertEqual(res.content, b'')
|
|
|
|
|
|
|
|
res = StreamingHttpResponse(['abc'])
|
|
|
|
conditional_content_removal(req, res)
|
|
|
|
self.assertEqual(b''.join(res), b'')
|
2014-08-15 16:40:46 +00:00
|
|
|
|
|
|
|
def test_fix_location_without_get_host(self):
|
|
|
|
"""
|
|
|
|
Tests that you can return an absolute redirect when the request
|
|
|
|
host is not in ALLOWED_HOSTS. Issue #20472
|
|
|
|
"""
|
|
|
|
request = HttpRequest()
|
2014-08-18 17:44:10 +00:00
|
|
|
|
2014-08-15 16:40:46 +00:00
|
|
|
def bomb():
|
|
|
|
self.assertTrue(False)
|
|
|
|
request.get_host = bomb
|
|
|
|
fix_location_header(request, HttpResponseRedirect('http://example.com'))
|