-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtest_routes.py
304 lines (214 loc) · 9 KB
/
test_routes.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import pytest
from lilya import status
from esmerald.exceptions import ImproperlyConfigured
from esmerald.routing.apis.views import APIView
from esmerald.routing.gateways import Gateway, WebhookGateway, WebSocketGateway
from esmerald.routing.handlers import delete, get, post, put, route, websocket
from esmerald.routing.webhooks import whget
from esmerald.testclient import create_client
from esmerald.websockets import WebSocket
@whget("new-event")
async def new_event() -> bool:
return True
@get(status_code=status.HTTP_202_ACCEPTED)
def route_one() -> dict:
return {"test": 1}
@get(status_code=status.HTTP_206_PARTIAL_CONTENT)
def route_two() -> dict:
return {"test": 2}
@get(status_code=status.HTTP_200_OK)
def route_three() -> dict:
return {"test": 3}
@route(status_code=status.HTTP_202_ACCEPTED, methods=["GET", "POST", "PUT"])
def routes_multiple() -> dict:
return {"test": 1}
@websocket(path="/")
async def simple_websocket_handler(socket: WebSocket) -> None:
await socket.accept()
data = await socket.receive_json()
assert data
await socket.send_json({"data": "esmerald"})
await socket.close()
@websocket(path="/websocket")
async def simple_websocket_handler_two(socket: WebSocket) -> None:
await socket.accept()
data = await socket.receive_json()
assert data
await socket.send_json({"data": "esmerald"})
await socket.close()
def test_add_route_from_router(test_client_factory) -> None:
"""
Adds a route to the router.
"""
with create_client(routes=[Gateway(handler=route_one)]) as client:
response = client.get("/")
assert response.json() == {"test": 1}
assert response.status_code == status.HTTP_202_ACCEPTED
client.app.router.add_route("/second", handler=route_two)
response = client.get("/second")
assert response.json() == {"test": 2}
assert response.status_code == status.HTTP_206_PARTIAL_CONTENT
def test_add_route_from_router_direct(test_client_factory) -> None:
"""
Adds a route to the router.
"""
with create_client(routes=[route_one]) as client:
response = client.get("/")
assert response.json() == {"test": 1}
assert response.status_code == status.HTTP_202_ACCEPTED
client.app.router.add_route("/second", handler=route_two)
response = client.get("/second")
assert response.json() == {"test": 2}
assert response.status_code == status.HTTP_206_PARTIAL_CONTENT
def test_add_route_from_router_multiple_methods(test_client_factory) -> None:
"""
Adds a route to the router using @route
"""
with create_client(routes=[Gateway(handler=route_one)]) as client:
response = client.get("/")
assert response.json() == {"test": 1}
assert response.status_code == status.HTTP_202_ACCEPTED
client.app.router.add_route("/multiple", handler=routes_multiple)
response = client.get("/multiple")
assert response.json() == {"test": 1}
assert response.status_code == status.HTTP_202_ACCEPTED
response = client.post("/multiple")
assert response.json() == {"test": 1}
assert response.status_code == status.HTTP_202_ACCEPTED
response = client.put("/multiple")
assert response.json() == {"test": 1}
assert response.status_code == status.HTTP_202_ACCEPTED
def test_add_route_from_application(test_client_factory) -> None:
"""
Adds a route to the application router.
"""
with create_client(routes=[Gateway(handler=route_one)]) as client:
response = client.get("/")
assert response.json() == {"test": 1}
assert response.status_code == status.HTTP_202_ACCEPTED
client.app.add_route("/second", handler=route_two)
response = client.get("/second")
assert response.json() == {"test": 2}
assert response.status_code == status.HTTP_206_PARTIAL_CONTENT
client.app.add_route("/third", handler=route_three)
response = client.get("/third")
assert response.json() == {"test": 3}
assert response.status_code == status.HTTP_200_OK
def test_add_route_multiple_from_application(test_client_factory) -> None:
"""
Adds a route to the application router using @route
"""
with create_client(routes=[Gateway(handler=route_one)]) as client:
response = client.get("/")
assert response.json() == {"test": 1}
assert response.status_code == status.HTTP_202_ACCEPTED
client.app.router.add_route("/multiple", handler=routes_multiple)
response = client.get("/multiple")
assert response.json() == {"test": 1}
assert response.status_code == status.HTTP_202_ACCEPTED
response = client.post("/multiple")
assert response.json() == {"test": 1}
assert response.status_code == status.HTTP_202_ACCEPTED
response = client.put("/multiple")
assert response.json() == {"test": 1}
assert response.status_code == status.HTTP_202_ACCEPTED
@pytest.mark.parametrize("arg", [new_event, WebhookGateway(handler=new_event)])
def test_raise_exception_on_create_webhook(test_client_factory, arg) -> None:
"""
Raises improperly configured.
"""
with pytest.raises(ImproperlyConfigured):
with create_client(routes=[arg]):
pass
@pytest.mark.parametrize("arg", [new_event, WebhookGateway(handler=new_event)])
def test_raise_exception_on_add_webhook(test_client_factory, arg) -> None:
"""
Raises improperly configured.
"""
with create_client(routes=[]) as client:
with pytest.raises(ImproperlyConfigured):
client.app.add_route("/fooobar", handler=arg)
def test_raise_exception_on_add_route(test_client_factory) -> None:
"""
Raises improperly configured.
"""
with pytest.raises(ImproperlyConfigured):
with create_client(routes=[Gateway(handler=route_one)]) as client:
handler = Gateway(handler=route_one)
client.app.add_route("/one", handler=handler)
@pytest.mark.parametrize(
"arg",
[
simple_websocket_handler,
WebSocketGateway(handler=simple_websocket_handler),
WebSocketGateway(path="/", handler=simple_websocket_handler),
],
)
def test_websocket_handler_gateway_from_router(test_client_factory, arg) -> None:
client = create_client(routes=[arg])
with client.websocket_connect("/") as websocket_client:
websocket_client.send_json({"data": "esmerald"})
data = websocket_client.receive_json()
assert data
client.app.router.add_websocket_route("/ws", handler=simple_websocket_handler_two)
with client.websocket_connect("/ws/websocket") as websocket_client:
websocket_client.send_json({"data": "esmerald"})
data = websocket_client.receive_json()
assert data
@pytest.mark.parametrize(
"arg",
[
simple_websocket_handler,
WebSocketGateway(handler=simple_websocket_handler),
WebSocketGateway(path="/", handler=simple_websocket_handler),
],
)
def test_websocket_handler_gateway_from_application(test_client_factory, arg) -> None:
client = create_client(routes=[arg])
with client.websocket_connect("/") as websocket_client:
websocket_client.send_json({"data": "esmerald"})
data = websocket_client.receive_json()
assert data
client.app.add_websocket_route("/ws", handler=simple_websocket_handler_two)
with client.websocket_connect("/ws/websocket") as websocket_client:
websocket_client.send_json({"data": "esmerald"})
data = websocket_client.receive_json()
assert data
def test_raise_exception_on_add_websocket_route(test_client_factory) -> None:
"""
Raises improperly configured for websockets.
"""
with pytest.raises(ImproperlyConfigured):
with create_client(routes=[]) as client:
handler = WebSocketGateway(handler=simple_websocket_handler)
client.app.add_websocket_route("/ws", handler=handler)
@pytest.mark.parametrize(
"method, fn_path, status_code, response_text",
[
(get, "/get", 200, "ok"),
(post, "/post", 200, "ok"),
(put, "/put", 200, "ok"),
(delete, "/delete", 200, "ok"),
(post, "/another-post", 201, "created!"),
(put, "/another-post", 201, "updated!"),
(delete, "/another-post", 201, "updated!"),
(get, "/another-post", 201, "updated!"),
],
)
def test_add_apiview_multiple_from_application(
method, fn_path, status_code, response_text, test_client_factory
) -> None:
"""
Adds a route to the application router using @route
"""
class View(APIView):
path = "/"
@method(fn_path, status_code=status_code)
async def test(self) -> str:
return response_text
with create_client(routes=[]) as client:
gateway = Gateway(handler=View)
client.app.add_apiview(value=gateway)
response = getattr(client, method.__name__)(fn_path)
assert response.json() == response_text
assert response.status_code == status_code