| 1 |
import asyncio |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from ninja import NinjaAPI |
| 6 |
from ninja.security import APIKeyQuery |
| 7 |
from ninja.testing import TestAsyncClient |
| 8 |
|
| 9 |
|
| 10 |
@pytest.mark.asyncio |
| 11 |
async def test_asyncio_operations(): |
| 12 |
api = NinjaAPI() |
| 13 |
|
| 14 |
class KeyQuery(APIKeyQuery): |
| 15 |
def authenticate(self, request, key): |
| 16 |
if key == "secret": |
| 17 |
return key |
| 18 |
|
| 19 |
@api.get("/async", auth=KeyQuery()) |
| 20 |
async def async_view(request, payload: int): |
| 21 |
await asyncio.sleep(0) |
| 22 |
return {"async": True} |
| 23 |
|
| 24 |
@api.post("/async") |
| 25 |
def sync_post_to_async_view(request): |
| 26 |
return {"sync": True} |
| 27 |
|
| 28 |
client = TestAsyncClient(api) |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
res = await client.get("/async?payload=1") |
| 34 |
assert res.status_code == 401 |
| 35 |
|
| 36 |
|
| 37 |
res = await client.get("/async?payload=1&key=secret") |
| 38 |
assert res.json() == {"async": True} |
| 39 |
|
| 40 |
|
| 41 |
res = await client.get("/async?payload=str&key=secret") |
| 42 |
assert res.status_code == 422 |
| 43 |
|
| 44 |
|
| 45 |
res = await client.post("/async") |
| 46 |
assert res.json() == {"sync": True} |
| 47 |
|
| 48 |
|
| 49 |
res = await client.put("/async") |
| 50 |
assert res.status_code == 405 |