| 1 |
import threading |
| 2 |
from typing import Generator |
| 3 |
|
| 4 |
import pytest |
| 5 |
from quart import request, ResponseReturnValue |
| 6 |
|
| 7 |
from quart_trio import QuartTrio |
| 8 |
|
| 9 |
|
| 10 |
@pytest.fixture(name="app") |
| 11 |
def _app() -> QuartTrio: |
| 12 |
app = QuartTrio(__name__) |
| 13 |
|
| 14 |
@app.route("/", methods=["GET", "POST"]) |
| 15 |
def index() -> ResponseReturnValue: |
| 16 |
return request.method |
| 17 |
|
| 18 |
@app.route("/gen") |
| 19 |
def gen() -> ResponseReturnValue: |
| 20 |
def _gen() -> Generator[bytes, None, None]: |
| 21 |
yield b"%d" % threading.current_thread().ident |
| 22 |
for _ in range(2): |
| 23 |
yield b"b" |
| 24 |
|
| 25 |
return _gen(), 200 |
| 26 |
|
| 27 |
return app |
| 28 |
|
| 29 |
|
| 30 |
@pytest.mark.trio |
| 31 |
async def test_sync_request_context(app: QuartTrio) -> None: |
| 32 |
test_client = app.test_client() |
| 33 |
response = await test_client.get("/") |
| 34 |
assert b"GET" in (await response.get_data()) |
| 35 |
response = await test_client.post("/") |
| 36 |
assert b"POST" in (await response.get_data()) |
| 37 |
|
| 38 |
|
| 39 |
@pytest.mark.trio |
| 40 |
async def test_sync_generator(app: QuartTrio) -> None: |
| 41 |
test_client = app.test_client() |
| 42 |
response = await test_client.get("/gen") |
| 43 |
result = await response.get_data() |
| 44 |
assert result[-2:] == b"bb" |
| 45 |
assert int(result[:-2]) != threading.current_thread().ident |