diff options
author | Carlton Gibson <carlton.gibson@noumenal.es> | 2022-12-13 16:15:25 +0100 |
---|---|---|
committer | Carlton Gibson <carlton.gibson@noumenal.es> | 2022-12-22 10:41:12 +0100 |
commit | 0bd2c0c9015b53c41394a1c0989afbfd94dc2830 (patch) | |
tree | 6b24758335cf10eeedfdf7dec50cda3500796305 /tests/httpwrappers | |
parent | ae0899be0d787fbfc5f5ab2b18c5a8219d822d2b (diff) | |
download | django-0bd2c0c9015b53c41394a1c0989afbfd94dc2830.tar.gz |
Fixed #33735 -- Added async support to StreamingHttpResponse.
Thanks to Florian Vazelle for initial exploratory work, and to Nick
Pope and Mariusz Felisiak for review.
Diffstat (limited to 'tests/httpwrappers')
-rw-r--r-- | tests/httpwrappers/tests.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/tests/httpwrappers/tests.py b/tests/httpwrappers/tests.py index e1920e2eda..fa2c8fd5d2 100644 --- a/tests/httpwrappers/tests.py +++ b/tests/httpwrappers/tests.py @@ -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): |