summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexandre Spaeth <Alexerson@users.noreply.github.com>2023-02-17 10:35:19 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-02-17 11:48:53 +0100
commit1ecbc046243c174b3b8c247091d44de2bea692b7 (patch)
tree13fde249170d49ae664f3039a8fa717fd23ad467
parente1c74bf45803cbde28149fbc80d91dc3b244e7fc (diff)
[4.2.x] Refs #34342 -- Added tests for handling sync streaming responses by test client.
Co-authored-by: Carlton Gibson <carlton.gibson@noumenal.es> Backport of bfb8fda3e69cc6f5c6695ba70117faff51cc25a9 from main
-rw-r--r--tests/admin_views/test_actions.py2
-rw-r--r--tests/handlers/tests.py19
-rw-r--r--tests/view_tests/tests/test_static.py5
3 files changed, 22 insertions, 4 deletions
diff --git a/tests/admin_views/test_actions.py b/tests/admin_views/test_actions.py
index 0c209019f3..35e4583d95 100644
--- a/tests/admin_views/test_actions.py
+++ b/tests/admin_views/test_actions.py
@@ -253,7 +253,7 @@ class AdminActionsTest(TestCase):
response = self.client.post(
reverse("admin:admin_views_externalsubscriber_changelist"), action_data
)
- content = b"".join(response.streaming_content)
+ content = b"".join(list(response))
self.assertEqual(content, b"This is the content of the file")
self.assertEqual(response.status_code, 200)
diff --git a/tests/handlers/tests.py b/tests/handlers/tests.py
index 96826d3056..0df481c2fc 100644
--- a/tests/handlers/tests.py
+++ b/tests/handlers/tests.py
@@ -171,7 +171,7 @@ class SignalsTests(SimpleTestCase):
def test_request_signals_streaming_response(self):
response = self.client.get("/streaming/")
self.assertEqual(self.signals, ["started"])
- self.assertEqual(b"".join(response.streaming_content), b"streaming content")
+ self.assertEqual(b"".join(list(response)), b"streaming content")
self.assertEqual(self.signals, ["started", "finished"])
@@ -248,6 +248,11 @@ class HandlerRequestTests(SimpleTestCase):
):
self.client.get(url)
+ def test_streaming(self):
+ response = self.client.get("/streaming/")
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(b"".join(list(response)), b"streaming content")
+
class ScriptNameTests(SimpleTestCase):
def test_get_script_name(self):
@@ -312,3 +317,15 @@ class AsyncHandlerRequestTests(SimpleTestCase):
)
with self.assertRaisesMessage(ValueError, msg):
await self.async_client.get("/unawaited/")
+
+ async def test_sync_streaming(self):
+ response = await self.async_client.get("/streaming/")
+ self.assertEqual(response.status_code, 200)
+ msg = (
+ "StreamingHttpResponse must consume synchronous iterators in order to "
+ "serve them asynchronously. Use an asynchronous iterator instead."
+ )
+ with self.assertWarnsMessage(Warning, msg):
+ self.assertEqual(
+ b"".join([chunk async for chunk in response]), b"streaming content"
+ )
diff --git a/tests/view_tests/tests/test_static.py b/tests/view_tests/tests/test_static.py
index 309b81f8fa..e25e50f640 100644
--- a/tests/view_tests/tests/test_static.py
+++ b/tests/view_tests/tests/test_static.py
@@ -40,9 +40,10 @@ class StaticTests(SimpleTestCase):
def test_chunked(self):
"The static view should stream files in chunks to avoid large memory usage"
response = self.client.get("/%s/%s" % (self.prefix, "long-line.txt"))
- first_chunk = next(response.streaming_content)
+ response_iterator = iter(response)
+ first_chunk = next(response_iterator)
self.assertEqual(len(first_chunk), FileResponse.block_size)
- second_chunk = next(response.streaming_content)
+ second_chunk = next(response_iterator)
response.close()
# strip() to prevent OS line endings from causing differences
self.assertEqual(len(second_chunk.strip()), 1449)