-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathtest_app.py
436 lines (354 loc) · 12.2 KB
/
test_app.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
from __future__ import annotations
import asyncio
from collections.abc import AsyncGenerator
from typing import NoReturn
from unittest.mock import AsyncMock
import pytest
from hypercorn.typing import HTTPScope
from hypercorn.typing import WebsocketScope
from werkzeug.datastructures import Headers
from werkzeug.exceptions import InternalServerError
from werkzeug.wrappers import Response as WerkzeugResponse
from quart.app import Quart
from quart.globals import session
from quart.globals import websocket
from quart.sessions import SecureCookieSession
from quart.sessions import SessionInterface
from quart.testing import no_op_push
from quart.testing import WebsocketResponseError
from quart.typing import ResponseReturnValue
from quart.typing import ResponseTypes
from quart.wrappers import Request
from quart.wrappers import Response
TEST_RESPONSE = Response("")
class SimpleError(Exception):
pass
def test_endpoint_overwrite() -> None:
app = Quart(__name__)
def route() -> str:
return ""
def route2() -> str:
return ""
async def route3() -> str:
return ""
app.add_url_rule("/a", "index", route, methods=["GET"])
app.add_url_rule(
"/a/a", "index", route, methods=["GET"]
) # Should not assert, as same view func
with pytest.raises(AssertionError):
app.add_url_rule("/a/b", "index", route2, methods=["GET"])
app.add_url_rule("/b", "async", route3, methods=["GET"])
app.add_url_rule(
"/b/a", "async", route3, methods=["GET"]
) # Should not assert, as same view func
with pytest.raises(AssertionError):
app.add_url_rule("/b/b", "async", route2, methods=["GET"])
@pytest.mark.parametrize(
"methods, required_methods, automatic_options",
[
({}, {}, False),
({}, {}, True),
({"GET", "PUT"}, {}, False),
({"GET", "PUT"}, {}, True),
({}, {"GET", "PUT"}, False),
({}, {"GET", "PUT"}, True),
],
)
def test_add_url_rule_methods(
methods: set[str], required_methods: set[str], automatic_options: bool
) -> None:
app = Quart(__name__)
def route() -> str:
return ""
route.methods = methods # type: ignore
route.required_methods = required_methods # type: ignore
non_func_methods = {"PATCH"} if not methods else None
app.add_url_rule(
"/",
"end",
route,
methods=non_func_methods,
provide_automatic_options=automatic_options,
)
result = {"PATCH"} if not methods else set()
result.update(methods)
result.update(required_methods)
if "GET" in result:
result.add("HEAD")
assert app.url_map._rules_by_endpoint["end"][0].methods == result
@pytest.mark.parametrize(
"methods, arg_automatic, func_automatic, expected_methods, expected_automatic",
[
({"GET"}, True, None, {"HEAD", "GET"}, True),
({"GET"}, None, None, {"HEAD", "GET", "OPTIONS"}, True),
({"GET"}, None, True, {"HEAD", "GET"}, True),
({"GET", "OPTIONS"}, None, None, {"HEAD", "GET", "OPTIONS"}, False),
({"GET"}, False, True, {"HEAD", "GET"}, False),
({"GET"}, None, False, {"HEAD", "GET"}, False),
],
)
def test_add_url_rule_automatic_options(
methods: set[str],
arg_automatic: bool | None,
func_automatic: bool | None,
expected_methods: set[str],
expected_automatic: bool,
) -> None:
app = Quart(__name__)
def route() -> str:
return ""
route.provide_automatic_options = func_automatic # type: ignore
app.add_url_rule(
"/", "end", route, methods=methods, provide_automatic_options=arg_automatic
)
assert app.url_map._rules_by_endpoint["end"][0].methods == expected_methods
assert (
app.url_map._rules_by_endpoint["end"][0].provide_automatic_options # type: ignore
== expected_automatic
)
async def test_host_matching() -> None:
app = Quart(__name__, static_host="quart.com", host_matching=True)
@app.route("/", host="quart.com")
async def route() -> str:
return ""
test_client = app.test_client()
response = await test_client.get("/", headers={"host": "quart.com"})
assert response.status_code == 200
response = await test_client.get("/", headers={"host": "localhost"})
assert response.status_code == 404
async def test_subdomain() -> None:
app = Quart(__name__, subdomain_matching=True)
app.config["SERVER_NAME"] = "quart.com"
@app.route("/", subdomain="<subdomain>")
async def route(subdomain: str) -> str:
return subdomain
test_client = app.test_client()
response = await test_client.get("/", headers={"host": "sub.quart.com"})
assert (await response.get_data(as_text=True)) == "sub"
@pytest.mark.parametrize(
"result, expected, raises",
[
(None, None, True),
((None, 201), None, True),
(TEST_RESPONSE, TEST_RESPONSE, False),
(
("hello", {"X-Header": "bob"}),
Response("hello", headers={"X-Header": "bob"}),
False,
),
(("hello", 201), Response("hello", 201), False),
(
("hello", 201, {"X-Header": "bob"}),
Response("hello", 201, headers={"X-Header": "bob"}),
False,
),
(
(WerkzeugResponse("hello"), 201, {"X-Header": "bob"}),
WerkzeugResponse("hello", 201, {"X-Header": "bob"}),
False,
),
(InternalServerError(), InternalServerError().get_response(), False),
((val for val in "abcd"), Response(val for val in "abcd"), False),
(int, None, True),
],
)
async def test_make_response(
result: ResponseReturnValue, expected: Response | WerkzeugResponse, raises: bool
) -> None:
app = Quart(__name__)
app.config["RESPONSE_TIMEOUT"] = None
try:
response = await app.make_response(result)
except TypeError:
if not raises:
raise
else:
assert set(response.headers.keys()) == set(expected.headers.keys())
assert response.status_code == expected.status_code
if isinstance(response, Response):
assert (await response.get_data()) == (await expected.get_data()) # type: ignore
elif isinstance(response, WerkzeugResponse):
assert response.get_data() == expected.get_data()
@pytest.fixture(name="basic_app")
def _basic_app() -> Quart:
app = Quart(__name__)
@app.route("/")
def route() -> str:
return ""
@app.route("/exception/")
def exception() -> str:
raise Exception()
return app
async def test_app_route_exception(basic_app: Quart) -> None:
test_client = basic_app.test_client()
response = await test_client.get("/exception/")
assert response.status_code == 500
async def test_app_before_request_exception(basic_app: Quart) -> None:
@basic_app.before_request
def before() -> None:
raise Exception()
test_client = basic_app.test_client()
response = await test_client.get("/")
assert response.status_code == 500
async def test_app_after_request_exception(basic_app: Quart) -> None:
@basic_app.after_request
def after(_: ResponseTypes) -> None:
raise Exception()
test_client = basic_app.test_client()
response = await test_client.get("/")
assert response.status_code == 500
async def test_app_after_request_handler_exception(basic_app: Quart) -> None:
@basic_app.after_request
def after(_: ResponseTypes) -> None:
raise Exception()
test_client = basic_app.test_client()
response = await test_client.get("/exception/")
assert response.status_code == 500
async def test_app_handle_request_asyncio_cancelled_error(
http_scope: HTTPScope,
) -> None:
app = Quart(__name__)
@app.route("/")
async def index() -> NoReturn:
raise asyncio.CancelledError()
request = app.request_class(
"GET",
"http",
"/",
b"",
Headers([("host", "quart.com")]),
"",
"1.1",
http_scope,
send_push_promise=no_op_push,
)
with pytest.raises(asyncio.CancelledError):
await app.handle_request(request)
async def test_app_handle_websocket_asyncio_cancelled_error(
websocket_scope: WebsocketScope,
) -> None:
app = Quart(__name__)
@app.websocket("/")
async def index() -> NoReturn:
raise asyncio.CancelledError()
websocket = app.websocket_class(
"/",
b"",
"wss",
Headers([("host", "quart.com")]),
"",
"1.1",
None,
None,
None,
None,
None,
websocket_scope,
)
with pytest.raises(asyncio.CancelledError):
await app.handle_websocket(websocket)
@pytest.fixture(name="session_app", scope="function")
def _session_app() -> Quart:
app = Quart(__name__)
app.session_interface = AsyncMock(spec=SessionInterface)
app.session_interface.open_session.return_value = SecureCookieSession()
app.session_interface.is_null_session.return_value = False
@app.route("/")
async def route() -> str:
session["a"] = "b"
return ""
@app.websocket("/ws/")
async def ws() -> None:
session["a"] = "b"
await websocket.accept()
await websocket.send("")
@app.websocket("/ws_return/")
async def ws_return() -> str:
session["a"] = "b"
return ""
return app
async def test_app_session(session_app: Quart) -> None:
test_client = session_app.test_client()
await test_client.get("/")
session_app.session_interface.open_session.assert_called() # type: ignore
session_app.session_interface.save_session.assert_called() # type: ignore
async def test_app_session_websocket(session_app: Quart) -> None:
test_client = session_app.test_client()
async with test_client.websocket("/ws/") as test_websocket:
await test_websocket.receive()
session_app.session_interface.open_session.assert_called() # type: ignore
session_app.session_interface.save_session.assert_called() # type: ignore
async def test_app_session_websocket_return(session_app: Quart) -> None:
test_client = session_app.test_client()
async with test_client.websocket("/ws_return/") as test_websocket:
with pytest.raises(WebsocketResponseError):
await test_websocket.receive()
session_app.session_interface.open_session.assert_called() # type: ignore
session_app.session_interface.save_session.assert_called() # type: ignore
@pytest.mark.parametrize(
"debug, testing, raises",
[
(False, False, False),
(True, False, True),
(False, True, True),
(True, True, True),
],
)
async def test_propagation(
debug: bool, testing: bool, raises: bool, http_scope: HTTPScope
) -> None:
app = Quart(__name__)
@app.route("/")
async def exception() -> ResponseReturnValue:
raise SimpleError()
app.debug = debug
app.testing = testing
test_client = app.test_client()
if raises:
with pytest.raises(SimpleError):
await app.handle_request(
Request(
"GET",
"http",
"/",
b"",
Headers(),
"",
"1.1",
http_scope,
send_push_promise=no_op_push,
)
)
else:
response = await test_client.get("/")
assert response.status_code == 500
async def test_test_app() -> None:
startup = False
shutdown = False
serving = []
app = Quart(__name__)
@app.before_serving
async def before() -> None:
nonlocal startup
startup = True
@app.after_serving
async def after() -> None:
nonlocal shutdown
shutdown = True
@app.while_serving
async def lifespan() -> AsyncGenerator[None, None]:
nonlocal serving
serving.append(1)
yield
serving.append(2)
@app.route("/")
async def index() -> str:
return ""
async with app.test_app() as test_app:
assert startup
test_client = test_app.test_client()
await test_client.get("/")
assert not shutdown
assert serving == [1]
assert shutdown
assert serving == [1, 2]