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