summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBaptiste Mispelon <baptiste.mispelon@torchbox.com>2026-01-12 14:51:32 +0100
committernessita <124304+nessita@users.noreply.github.com>2026-03-16 17:44:40 -0300
commit0ed8d4e7d1029e1283b04f70d51d2af96ad85d6a (patch)
treeed05472acc2f4cf13e4539d15f851ce139ff3e01
parent4b2edb3418851c09dd839f1a2cba74051fe9d420 (diff)
Fixed #36859 -- Made assertContains and assertNotContains idempotent for streaming responses.
-rw-r--r--django/test/testcases.py2
-rw-r--r--docs/releases/6.1.txt6
-rw-r--r--tests/test_utils/tests.py13
3 files changed, 19 insertions, 2 deletions
diff --git a/django/test/testcases.py b/django/test/testcases.py
index 5f83612fe5..de2a3eb61d 100644
--- a/django/test/testcases.py
+++ b/django/test/testcases.py
@@ -575,6 +575,8 @@ class SimpleTestCase(unittest.TestCase):
if response.streaming:
content = b"".join(response.streaming_content)
+ # Reset the content so it can be checked again (idempotency).
+ response.streaming_content = [content]
else:
content = response.content
response_content = content
diff --git a/docs/releases/6.1.txt b/docs/releases/6.1.txt
index 6b18331a1b..1bd4f091aa 100644
--- a/docs/releases/6.1.txt
+++ b/docs/releases/6.1.txt
@@ -355,7 +355,11 @@ Templates
Tests
~~~~~
-* ...
+* :meth:`~django.test.SimpleTestCase.assertContains` and
+ :meth:`~django.test.SimpleTestCase.assertNotContains` can now be called
+ multiple times on the same :class:`~django.http.StreamingHttpResponse`.
+ Previously, they would consume the streaming response's content, causing
+ subsequent calls to fail.
URLs
~~~~
diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py
index ef1d02cf41..b9c8e2ac58 100644
--- a/tests/test_utils/tests.py
+++ b/tests/test_utils/tests.py
@@ -29,7 +29,7 @@ from django.forms import (
ValidationError,
formset_factory,
)
-from django.http import HttpResponse
+from django.http import HttpResponse, StreamingHttpResponse
from django.template import Context, Template
from django.template.loader import render_to_string
from django.test import (
@@ -1047,6 +1047,17 @@ class HTMLEqualTests(SimpleTestCase):
html=True,
)
+ def test_streaming_response_idempotent(self):
+ response = StreamingHttpResponse(["hello world"])
+ self.assertContains(response, "hello")
+ self.assertContains(response, "world")
+
+ def test_streaming_response_not_contains_idempotent(self):
+ response = StreamingHttpResponse(["hello world"])
+ self.assertNotContains(response, "bye")
+ self.assertNotContains(response, "bye")
+ self.assertContains(response, "world")
+
class InHTMLTests(SimpleTestCase):
def test_needle_msg(self):