Skip to content

Commit 0fbe4c7

Browse files
committed
test streaming
1 parent a660d81 commit 0fbe4c7

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

tests/test_http_stream.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import asyncio
2+
3+
import pytest
4+
5+
from channels.generic.http import AsyncHttpConsumer
6+
from channels.testing import HttpCommunicator
7+
8+
9+
@pytest.mark.asyncio
10+
async def test_async_http_consumer():
11+
"""
12+
Tests that AsyncHttpConsumer is implemented correctly.
13+
"""
14+
15+
class TestConsumer(AsyncHttpConsumer):
16+
async def handle(self, body):
17+
self.is_streaming = True
18+
await self.send_headers(
19+
headers=[
20+
(b"Cache-Control", b"no-cache"),
21+
(b"Content-Type", b"text/event-stream"),
22+
(b"Transfer-Encoding", b"chunked"),
23+
]
24+
)
25+
asyncio.get_event_loop().create_task(self.stream())
26+
27+
async def stream(self):
28+
for n in range(0, 3):
29+
if not self.is_streaming:
30+
break
31+
payload = "data: %d\n\n" % (n + 1)
32+
await self.send_body(payload.encode("utf-8"), more_body=True)
33+
await asyncio.sleep(0.2)
34+
await self.send_body(b"")
35+
36+
async def disconnect(self):
37+
self.is_streaming = False
38+
39+
# Open a connection
40+
communicator = HttpCommunicator(TestConsumer, method="GET", path="/test/", body=b"")
41+
response = await communicator.get_response()
42+
assert response["body"] == b"data: 1\n\ndata: 2\n\ndata: 3\n\n"
43+
assert response["status"] == 200

0 commit comments

Comments
 (0)