1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #33735 -- Added async support to StreamingHttpResponse.

Thanks to Florian Vazelle for initial exploratory work, and to Nick
Pope and Mariusz Felisiak for review.
This commit is contained in:
Carlton Gibson
2022-12-13 16:15:25 +01:00
parent ae0899be0d
commit 0bd2c0c901
10 changed files with 264 additions and 39 deletions

View File

@@ -720,6 +720,42 @@ class StreamingHttpResponseTests(SimpleTestCase):
'<StreamingHttpResponse status_code=200, "text/html; charset=utf-8">',
)
async def test_async_streaming_response(self):
async def async_iter():
yield b"hello"
yield b"world"
r = StreamingHttpResponse(async_iter())
chunks = []
async for chunk in r:
chunks.append(chunk)
self.assertEqual(chunks, [b"hello", b"world"])
def test_async_streaming_response_warning(self):
async def async_iter():
yield b"hello"
yield b"world"
r = StreamingHttpResponse(async_iter())
msg = (
"StreamingHttpResponse must consume asynchronous iterators in order to "
"serve them synchronously. Use a synchronous iterator instead."
)
with self.assertWarnsMessage(Warning, msg):
self.assertEqual(list(r), [b"hello", b"world"])
async def test_sync_streaming_response_warning(self):
r = StreamingHttpResponse(iter(["hello", "world"]))
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"hello", await r.__aiter__().__anext__())
class FileCloseTests(SimpleTestCase):
def setUp(self):