From 0fbe4c7d25fb4e9a979ef171482c7c5160d08cfd Mon Sep 17 00:00:00 2001 From: Justin Karneges Date: Wed, 20 Feb 2019 18:48:35 -0800 Subject: [PATCH] test streaming --- tests/test_http_stream.py | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/test_http_stream.py diff --git a/tests/test_http_stream.py b/tests/test_http_stream.py new file mode 100644 index 000000000..c492dc278 --- /dev/null +++ b/tests/test_http_stream.py @@ -0,0 +1,43 @@ +import asyncio + +import pytest + +from channels.generic.http import AsyncHttpConsumer +from channels.testing import HttpCommunicator + + +@pytest.mark.asyncio +async def test_async_http_consumer(): + """ + Tests that AsyncHttpConsumer is implemented correctly. + """ + + class TestConsumer(AsyncHttpConsumer): + async def handle(self, body): + self.is_streaming = True + await self.send_headers( + headers=[ + (b"Cache-Control", b"no-cache"), + (b"Content-Type", b"text/event-stream"), + (b"Transfer-Encoding", b"chunked"), + ] + ) + asyncio.get_event_loop().create_task(self.stream()) + + async def stream(self): + for n in range(0, 3): + if not self.is_streaming: + break + payload = "data: %d\n\n" % (n + 1) + await self.send_body(payload.encode("utf-8"), more_body=True) + await asyncio.sleep(0.2) + await self.send_body(b"") + + async def disconnect(self): + self.is_streaming = False + + # Open a connection + communicator = HttpCommunicator(TestConsumer, method="GET", path="/test/", body=b"") + response = await communicator.get_response() + assert response["body"] == b"data: 1\n\ndata: 2\n\ndata: 3\n\n" + assert response["status"] == 200