Skip to content

Commit

Permalink
[4.2.x] Refs #34342 -- Added tests for handling sync streaming respon…
Browse files Browse the repository at this point in the history
…ses by test client.

Co-authored-by: Carlton Gibson <carlton.gibson@noumenal.es>

Backport of bfb8fda from main
  • Loading branch information
Alexerson authored and felixxm committed Feb 17, 2023
1 parent e1c74bf commit 1ecbc04
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion tests/admin_views/test_actions.py
Expand Up @@ -253,7 +253,7 @@ def test_custom_function_action_streaming_response(self):
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)

Expand Down
19 changes: 18 additions & 1 deletion tests/handlers/tests.py
Expand Up @@ -171,7 +171,7 @@ def test_request_signals(self):
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"])


Expand Down Expand Up @@ -248,6 +248,11 @@ def test_no_response(self):
):
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):
Expand Down Expand Up @@ -312,3 +317,15 @@ async def test_unawaited_response(self):
)
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"
)
5 changes: 3 additions & 2 deletions tests/view_tests/tests/test_static.py
Expand Up @@ -40,9 +40,10 @@ def test_serve(self):
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)
Expand Down

0 comments on commit 1ecbc04

Please sign in to comment.