forked from django/channels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_generic_http.py
172 lines (134 loc) · 5.5 KB
/
test_generic_http.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import asyncio
import json
import time
from unittest.mock import patch
import pytest
from channels.generic.http import AsyncHttpConsumer
from channels.testing import HttpCommunicator
@pytest.mark.django_db(transaction=True)
@pytest.mark.asyncio
async def test_async_http_consumer():
"""
Tests that AsyncHttpConsumer is implemented correctly.
"""
class TestConsumer(AsyncHttpConsumer):
async def handle(self, body):
data = json.loads(body.decode("utf-8"))
await self.send_response(
200,
json.dumps({"value": data["value"]}).encode("utf-8"),
headers={b"Content-Type": b"application/json"},
)
app = TestConsumer()
# Open a connection
communicator = HttpCommunicator(
app,
method="POST",
path="/test/",
body=json.dumps({"value": 42, "anything": False}).encode("utf-8"),
)
response = await communicator.get_response()
assert response["body"] == b'{"value": 42}'
assert response["status"] == 200
assert response["headers"] == [(b"Content-Type", b"application/json")]
@pytest.mark.django_db(transaction=True)
@pytest.mark.asyncio
async def test_error():
class TestConsumer(AsyncHttpConsumer):
async def handle(self, body):
raise AssertionError("Error correctly raised")
communicator = HttpCommunicator(TestConsumer(), "GET", "/")
with pytest.raises(AssertionError) as excinfo:
await communicator.get_response(timeout=0.05)
assert str(excinfo.value) == "Error correctly raised"
@pytest.mark.django_db(transaction=True)
@pytest.mark.asyncio
async def test_per_scope_consumers():
"""
Tests that a distinct consumer is used per scope.
"""
class TestConsumer(AsyncHttpConsumer):
def __init__(self):
super().__init__()
self.time = time.time()
async def handle(self, body):
body = f"{self.__class__.__name__} {id(self)} {self.time}"
await self.send_response(
200,
body.encode("utf-8"),
headers={b"Content-Type": b"text/plain"},
)
app = TestConsumer.as_asgi()
communicator = HttpCommunicator(app, method="GET", path="/test/")
response = await communicator.get_response()
assert response["status"] == 200
communicator = HttpCommunicator(app, method="GET", path="/test2/")
second_response = await communicator.get_response()
assert second_response["status"] == 200
assert response["body"] != second_response["body"]
@pytest.mark.django_db(transaction=True)
@pytest.mark.asyncio
async def test_async_http_consumer_future():
"""
Regression test for channels accepting only coroutines.
"""
class TestConsumer(AsyncHttpConsumer):
async def handle(self, body):
await self.send_response(
200,
b"42",
headers={b"Content-Type": b"text/plain"},
)
app = TestConsumer()
async def coroutine_app(scope, receive, send):
async def receive_coroutine():
return await asyncio.ensure_future(receive())
async def send_coroutine(*args, **kwargs):
return await asyncio.ensure_future(send(*args, **kwargs))
await app(scope, receive_coroutine, send_coroutine)
communicator = HttpCommunicator(coroutine_app, method="GET", path="/")
response = await communicator.get_response()
assert response["body"] == b"42"
assert response["status"] == 200
assert response["headers"] == [(b"Content-Type", b"text/plain")]
async def awaitable_callable_app(scope, receive, send):
def receive_awaitable_callable():
return asyncio.ensure_future(receive())
def send_awaitable_callable(*args, **kwargs):
return asyncio.ensure_future(send(*args, **kwargs))
await app(scope, receive_awaitable_callable, send_awaitable_callable)
communicator = HttpCommunicator(awaitable_callable_app, method="GET", path="/")
response = await communicator.get_response()
assert response["body"] == b"42"
assert response["status"] == 200
assert response["headers"] == [(b"Content-Type", b"text/plain")]
@pytest.mark.django_db(transaction=True)
@pytest.mark.asyncio
async def test_error_logging():
"""Regression test for error logging."""
class TestConsumer(AsyncHttpConsumer):
async def handle(self, body):
raise AssertionError("Error correctly raised")
communicator = HttpCommunicator(TestConsumer(), "GET", "/")
with patch("channels.generic.http.logger.error") as mock_logger_error:
try:
await communicator.get_response(timeout=0.05)
except AssertionError:
pass
args, _ = mock_logger_error.call_args
assert "Error in handle()" in args[0]
assert "AssertionError: Error correctly raised" in args[0]
@pytest.mark.django_db(transaction=True)
@pytest.mark.asyncio
async def test_error_handling_and_send_response():
"""Regression test to check error handling."""
class TestConsumer(AsyncHttpConsumer):
async def handle(self, body):
raise AssertionError("Error correctly raised")
communicator = HttpCommunicator(TestConsumer(), "GET", "/")
with patch.object(AsyncHttpConsumer, "send_response") as mock_send_response:
try:
await communicator.get_response(timeout=0.05)
except AssertionError:
pass
mock_send_response.assert_called_once_with(500, b"Internal Server Error")