File: conftest.py

package info (click to toggle)
rtsp-to-webrtc 0.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 240 kB
  • sloc: python: 1,126; makefile: 7; sh: 5
file content (55 lines) | stat: -rw-r--r-- 1,358 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from __future__ import annotations

import logging
from collections.abc import Awaitable, Callable, Generator
from json import JSONDecodeError
from typing import Any, cast

import aiohttp
import pytest
from aiohttp import web

from rtsp_to_webrtc import diagnostics

_LOGGER = logging.getLogger(__name__)


@pytest.fixture
def loop(event_loop: Any) -> Any:
    return event_loop


async def handler(request: aiohttp.web.Request) -> aiohttp.web.Response:
    """Handles the request, inserting response prepared by tests."""
    if request.method == "POST":
        try:
            request.app["request-json"].append(await request.json())
        except JSONDecodeError:
            pass
        request.app["request-post"].append(await request.post())
    response = request.app["response"].pop(0)
    request.app["request"].append(request.url.path)
    return cast(aiohttp.web.Response, response)


@pytest.fixture
async def request_handler() -> Callable[
    [aiohttp.web.Request], Awaitable[aiohttp.web.Response]
]:
    return handler


@pytest.fixture
def app() -> web.Application:
    app = web.Application()
    app["response"] = []
    app["request"] = []
    app["request-json"] = []
    app["request-post"] = []
    return app


@pytest.fixture(autouse=True)
def reset_diagnostics() -> Generator[None, None, None]:
    yield
    diagnostics.reset()