| 1 |
import sys |
| 2 |
from typing import NoReturn |
| 3 |
|
| 4 |
import pytest |
| 5 |
from quart import ResponseReturnValue |
| 6 |
from quart.testing import WebsocketResponseError |
| 7 |
|
| 8 |
from quart_trio import QuartTrio |
| 9 |
|
| 10 |
if sys.version_info < (3, 11): |
| 11 |
from exceptiongroup import BaseExceptionGroup |
| 12 |
|
| 13 |
|
| 14 |
@pytest.fixture(name="error_app", scope="function") |
| 15 |
def _error_app() -> QuartTrio: |
| 16 |
app = QuartTrio(__name__) |
| 17 |
|
| 18 |
@app.route("/") |
| 19 |
async def index() -> NoReturn: |
| 20 |
raise BaseExceptionGroup( |
| 21 |
"msg1", [ValueError(), BaseExceptionGroup("msg2", [TypeError(), ValueError()])] |
| 22 |
) |
| 23 |
|
| 24 |
@app.websocket("/ws/") |
| 25 |
async def ws() -> NoReturn: |
| 26 |
raise BaseExceptionGroup( |
| 27 |
"msg3", [ValueError(), BaseExceptionGroup("msg4", [TypeError(), ValueError()])] |
| 28 |
) |
| 29 |
|
| 30 |
return app |
| 31 |
|
| 32 |
|
| 33 |
@pytest.mark.trio |
| 34 |
async def test_exception_group_handling(error_app: QuartTrio) -> None: |
| 35 |
@error_app.errorhandler(TypeError) |
| 36 |
async def handler(_: Exception) -> ResponseReturnValue: |
| 37 |
return "", 201 |
| 38 |
|
| 39 |
test_client = error_app.test_client() |
| 40 |
response = await test_client.get("/") |
| 41 |
assert response.status_code == 201 |
| 42 |
|
| 43 |
|
| 44 |
@pytest.mark.trio |
| 45 |
async def test_websocket_exception_group_handling(error_app: QuartTrio) -> None: |
| 46 |
@error_app.errorhandler(TypeError) |
| 47 |
async def handler(_: Exception) -> ResponseReturnValue: |
| 48 |
return "", 201 |
| 49 |
|
| 50 |
test_client = error_app.test_client() |
| 51 |
try: |
| 52 |
async with test_client.websocket("/ws/") as test_websocket: |
| 53 |
await test_websocket.receive() |
| 54 |
except BaseExceptionGroup as error: |
| 55 |
for exception in error.exceptions: |
| 56 |
if isinstance(exception, WebsocketResponseError): |
| 57 |
assert exception.response.status_code == 201 |
| 58 |
|
| 59 |
|
| 60 |
@pytest.mark.trio |
| 61 |
async def test_exception_group_unhandled(error_app: QuartTrio) -> None: |
| 62 |
test_client = error_app.test_client() |
| 63 |
response = await test_client.get("/") |
| 64 |
assert response.status_code == 500 |
| 65 |
|
| 66 |
|
| 67 |
@pytest.mark.trio |
| 68 |
async def test_websocket_exception_group_unhandled(error_app: QuartTrio) -> None: |
| 69 |
test_client = error_app.test_client() |
| 70 |
try: |
| 71 |
async with test_client.websocket("/ws/") as test_websocket: |
| 72 |
await test_websocket.receive() |
| 73 |
except BaseExceptionGroup as error: |
| 74 |
for exception in error.exceptions: |
| 75 |
if isinstance(exception, WebsocketResponseError): |
| 76 |
assert exception.response.status_code == 500 |
| 77 |
|
| 78 |
|
| 79 |
@pytest.mark.trio |
| 80 |
async def test_test_app() -> None: |
| 81 |
startup = False |
| 82 |
shutdown = False |
| 83 |
|
| 84 |
app = QuartTrio(__name__) |
| 85 |
|
| 86 |
@app.before_serving |
| 87 |
async def before() -> None: |
| 88 |
nonlocal startup |
| 89 |
startup = True |
| 90 |
|
| 91 |
@app.after_serving |
| 92 |
async def after() -> None: |
| 93 |
nonlocal shutdown |
| 94 |
shutdown = True |
| 95 |
|
| 96 |
@app.route("/") |
| 97 |
async def index() -> str: |
| 98 |
return "" |
| 99 |
|
| 100 |
async with app.test_app() as test_app: |
| 101 |
assert startup |
| 102 |
assert app.nursery is not None |
| 103 |
test_client = test_app.test_client() |
| 104 |
await test_client.get("/") |
| 105 |
assert not shutdown |
| 106 |
assert shutdown |