Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions django/test/testcases.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,8 @@ def _assert_contains(self, response, text, status_code, msg_prefix, html):

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
Expand Down
6 changes: 5 additions & 1 deletion docs/releases/6.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~
Expand Down
13 changes: 12 additions & 1 deletion tests/test_utils/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
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 (
Expand Down Expand Up @@ -1047,6 +1047,17 @@ def test_unicode_handling(self):
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):
Expand Down
Loading