| 1 |
from __future__ import annotations |
| 2 |
|
| •3 |
import asyncio |
| 4 |
import logging |
| 5 |
from collections.abc import Awaitable, Callable, Generator |
| 6 |
from json import JSONDecodeError |
| 7 |
from typing import Any, cast |
| 8 |
|
| 9 |
import aiohttp |
| 10 |
import pytest |
| •11 |
import pytest_asyncio |
| 12 |
from aiohttp import web |
| 13 |
|
| 14 |
from rtsp_to_webrtc import diagnostics |
| 15 |
|
| 16 |
_LOGGER = logging.getLogger(__name__) |
| 17 |
|
| 18 |
|
| •19 |
@pytest_asyncio.fixture |
| 20 |
async def loop() -> Any: |
| 21 |
return asyncio.get_running_loop() |
| 22 |
|
| 23 |
|
| 24 |
async def handler(request: aiohttp.web.Request) -> aiohttp.web.Response: |
| 25 |
"""Handles the request, inserting response prepared by tests.""" |
| 26 |
if request.method == "POST": |
| 27 |
try: |
| 28 |
request.app["request-json"].append(await request.json()) |
| 29 |
except JSONDecodeError: |
| 30 |
pass |
| 31 |
request.app["request-post"].append(await request.post()) |
| 32 |
response = request.app["response"].pop(0) |
| 33 |
request.app["request"].append(request.url.path) |
| 34 |
return cast(aiohttp.web.Response, response) |
| 35 |
|
| 36 |
|
| 37 |
@pytest.fixture |
| 38 |
async def request_handler() -> Callable[ |
| 39 |
[aiohttp.web.Request], Awaitable[aiohttp.web.Response] |
| 40 |
]: |
| 41 |
return handler |
| 42 |
|
| 43 |
|
| 44 |
@pytest.fixture |
| 45 |
def app() -> web.Application: |
| 46 |
app = web.Application() |
| 47 |
app["response"] = [] |
| 48 |
app["request"] = [] |
| 49 |
app["request-json"] = [] |
| 50 |
app["request-post"] = [] |
| 51 |
return app |
| 52 |
|
| 53 |
|
| 54 |
@pytest.fixture(autouse=True) |
| 55 |
def reset_diagnostics() -> Generator[None, None, None]: |
| 56 |
yield |
| 57 |
diagnostics.reset() |