| 1 |
import gzip |
| 2 |
|
| 3 |
from django.http import HttpRequest, HttpResponse, StreamingHttpResponse |
| 4 |
from django.test import SimpleTestCase |
| 5 |
from django.test.client import conditional_content_removal |
| 6 |
|
| 7 |
|
| 8 |
class ConditionalContentTests(SimpleTestCase): |
| 9 |
def test_conditional_content_removal(self): |
| 10 |
""" |
| 11 |
Content is removed from regular and streaming responses with a |
| 12 |
status_code of 100-199, 204, 304, or a method of "HEAD". |
| 13 |
""" |
| 14 |
req = HttpRequest() |
| 15 |
|
| 16 |
# Do nothing for 200 responses. |
| 17 |
res = HttpResponse("abc") |
| 18 |
conditional_content_removal(req, res) |
| 19 |
self.assertEqual(res.content, b"abc") |
| 20 |
|
| 21 |
res = StreamingHttpResponse(["abc"]) |
| 22 |
conditional_content_removal(req, res) |
| 23 |
self.assertEqual(b"".join(res), b"abc") |
| 24 |
|
| 25 |
# Strip content for some status codes. |
| 26 |
for status_code in (100, 150, 199, 204, 304): |
| 27 |
res = HttpResponse("abc", status=status_code) |
| 28 |
conditional_content_removal(req, res) |
| 29 |
self.assertEqual(res.content, b"") |
| 30 |
|
| 31 |
res = StreamingHttpResponse(["abc"], status=status_code) |
| 32 |
conditional_content_removal(req, res) |
| 33 |
self.assertEqual(b"".join(res), b"") |
| 34 |
|
| 35 |
# Issue #20472 |
| 36 |
abc = gzip.compress(b"abc") |
| 37 |
res = HttpResponse(abc, status=304) |
| 38 |
res["Content-Encoding"] = "gzip" |
| 39 |
conditional_content_removal(req, res) |
| 40 |
self.assertEqual(res.content, b"") |
| 41 |
|
| 42 |
res = StreamingHttpResponse([abc], status=304) |
| 43 |
res["Content-Encoding"] = "gzip" |
| 44 |
conditional_content_removal(req, res) |
| 45 |
self.assertEqual(b"".join(res), b"") |
| 46 |
|
| 47 |
# Strip content for HEAD requests. |
| 48 |
req.method = "HEAD" |
| 49 |
|
| 50 |
res = HttpResponse("abc") |
| 51 |
conditional_content_removal(req, res) |
| 52 |
self.assertEqual(res.content, b"") |
| 53 |
|
| 54 |
res = StreamingHttpResponse(["abc"]) |
| 55 |
conditional_content_removal(req, res) |
| 56 |
self.assertEqual(b"".join(res), b"") |