2013-05-21 06:41:23 +08:00
|
|
|
import gzip
|
|
|
|
|
2015-03-14 06:40:14 +08:00
|
|
|
from django.http import HttpRequest, HttpResponse, StreamingHttpResponse
|
2015-04-18 05:38:20 +08:00
|
|
|
from django.test import SimpleTestCase
|
2016-04-25 19:56:07 +08:00
|
|
|
from django.test.client import conditional_content_removal
|
2012-10-22 04:40:31 +08:00
|
|
|
|
|
|
|
|
2016-04-25 19:56:07 +08:00
|
|
|
class ConditionalContentTests(SimpleTestCase):
|
2012-10-22 04:40:31 +08:00
|
|
|
|
|
|
|
def test_conditional_content_removal(self):
|
|
|
|
"""
|
2016-04-25 19:56:07 +08:00
|
|
|
Content is removed from regular and streaming responses with a
|
|
|
|
status_code of 100-199, 204, 304, or a method of "HEAD".
|
2012-10-22 04:40:31 +08:00
|
|
|
"""
|
|
|
|
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-11 04:42:30 +08:00
|
|
|
# Issue #20472
|
2017-01-26 02:59:25 +08:00
|
|
|
abc = gzip.compress(b'abc')
|
2013-05-21 06:41:23 +08:00
|
|
|
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-22 04:40:31 +08: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'')
|