Debian Codegraph @

tests/test_webrtc_client.py

raw symbols SCIP
1 from __future__ import annotations
2
3 import base64
4 from collections.abc import Awaitable, Callable
5 from typing import Any, cast
6
7 import aiohttp
8 import pytest
9 from aiohttp import ClientSession, web
10 from aiohttp.test_utils import TestClient, TestServer
11
12 from rtsp_to_webrtc.client import Client
13 from rtsp_to_webrtc.exceptions import ResponseError
14
15 SERVER_URL = "https://example.com"
16 RTSP_URL = "rtsps://example"
17 OFFER_SDP = "v=0\r\no=carol 28908764872 28908764872 IN IP4 100.3.6.6\r\n..."
18 ANSWER_SDP = "v=0\r\no=bob 2890844730 2890844730 IN IP4 h.example.com\r\n..."
19 ANSWER_PAYLOAD = base64.b64encode(ANSWER_SDP.encode("utf-8")).decode("utf-8")
20
21
22 @pytest.fixture(autouse=True)
23 def setup_handler(
24 app: web.Application,
25 request_handler: Callable[[aiohttp.web.Request], Awaitable[aiohttp.web.Response]],
26 ) -> None:
27 app.router.add_get("/static", request_handler)
28 app.router.add_post("/stream", request_handler)
29
30
31 @pytest.fixture
32 def cli(
33 loop: Any,
34 app: web.Application,
35 aiohttp_client: Callable[[web.Application], Awaitable[TestClient]],
36 ) -> TestClient:
37 """Creates a fake aiohttp client."""
38 client = loop.run_until_complete(aiohttp_client(app))
39 return cast(TestClient, client)
40
41
42 async def test_offer(cli: TestClient) -> None:
43 """Test successful response from RTSPtoWebRTC server."""
44 assert isinstance(cli.server, TestServer)
45 cli.server.app["response"].append(
46 aiohttp.web.json_response({"sdp64": ANSWER_PAYLOAD})
47 )
48
49 client = Client(cast(ClientSession, cli))
50 answer_sdp = await client.offer(OFFER_SDP, RTSP_URL)
51 assert answer_sdp == ANSWER_SDP
52
53
54 async def test_response_missing_answer(cli: TestClient) -> None:
55 """Test invalid response from RTSPtoWebRTC server."""
56 assert isinstance(cli.server, TestServer)
57 cli.server.app["response"].append(aiohttp.web.json_response({}))
58
59 client = Client(cast(ClientSession, cli))
60 with pytest.raises(ResponseError, match=r".*missing SDP Answer.*"):
61 await client.offer(OFFER_SDP, RTSP_URL)
62
63
64 async def test_server_failure(cli: TestClient) -> None:
65 """Test a failure talking to RTSPtoWebRTC server."""
66 assert isinstance(cli.server, TestServer)
67 cli.server.app["response"].append(aiohttp.web.Response(status=502))
68
69 client = Client(cast(ClientSession, cli))
70 with pytest.raises(ResponseError, match=r"server failure.*"):
71 await client.offer(OFFER_SDP, RTSP_URL)
72
73
74 async def test_server_failure_with_error(cli: TestClient) -> None:
75 """Test invalid response from RTSPtoWebRTC server."""
76 assert isinstance(cli.server, TestServer)
77 cli.server.app["response"].append(
78 aiohttp.web.json_response({"error": "a message"}, status=502)
79 )
80
81 client = Client(cast(ClientSession, cli))
82 with pytest.raises(ResponseError, match=r"server failure:.*a message.*"):
83 await client.offer(OFFER_SDP, RTSP_URL)
84
85
86 async def test_heartbeat(cli: TestClient) -> None:
87 """Test successful response from RTSPtoWebRTC server."""
88 assert isinstance(cli.server, TestServer)
89 cli.server.app["response"].extend(
90 [
91 aiohttp.web.Response(status=200),
92 aiohttp.web.Response(status=502),
93 aiohttp.web.Response(status=404),
94 aiohttp.web.Response(status=200),
95 ]
96 )
97
98 client = Client(cast(ClientSession, cli))
99
100 await client.heartbeat()
101
102 with pytest.raises(ResponseError):
103 await client.heartbeat()
104
105 with pytest.raises(ResponseError):
106 await client.heartbeat()
107
108 await client.heartbeat()
109
110
111 async def test_offer_stream_id(cli: TestClient) -> None:
112 """Test offer with explicit stream id API."""
113 assert isinstance(cli.server, TestServer)
114 cli.server.app["response"].append(
115 aiohttp.web.json_response({"sdp64": ANSWER_PAYLOAD})
116 )
117
118 client = Client(cast(ClientSession, cli))
119 answer_sdp = await client.offer_stream_id("stream_id", OFFER_SDP, RTSP_URL)
120 assert answer_sdp == ANSWER_SDP
121 requests = cli.server.app["request"]
122 assert requests == [
123 "/stream",
124 ]
125 assert cli.server.app["request-post"] == [
126 {
127 "url": "rtsps://example",
128 "sdp64": "dj0wDQpvPWNhcm9sIDI4OTA4NzY0ODcyIDI4OTA4NzY0ODcyIElOIElQNCAxMDAuMy42LjYNCi4uLg==",
129 }
130 ]
131
132
133 async def test_offer_with_channel_data(cli: TestClient) -> None:
134 """Test that the channel data is passed, though likely ignored by server."""
135 assert isinstance(cli.server, TestServer)
136 cli.server.app["response"].append(
137 aiohttp.web.json_response({"sdp64": ANSWER_PAYLOAD})
138 )
139
140 client = Client(cast(ClientSession, cli))
141 answer_sdp = await client.offer_stream_id(
142 "stream_id", OFFER_SDP, RTSP_URL, channel_data={"insecure_skip_verify": True}
143 )
144 assert answer_sdp == ANSWER_SDP
145 requests = cli.server.app["request"]
146 assert requests == [
147 "/stream",
148 ]
149 assert cli.server.app["request-post"] == [
150 {
151 "url": "rtsps://example",
152 "sdp64": "dj0wDQpvPWNhcm9sIDI4OTA4NzY0ODcyIDI4OTA4NzY0ODcyIElOIElQNCAxMDAuMy42LjYNCi4uLg==",
153 "insecure_skip_verify": "True",
154 }
155 ]