| 1 |
"""Profiles for upnp_client.""" |
| 2 |
|
| 3 |
import asyncio |
| 4 |
import os.path |
| 5 |
from collections import deque |
| 6 |
from copy import deepcopy |
| 7 |
from typing import Deque, Mapping, MutableMapping, cast |
| 8 |
|
| 9 |
from async_upnp_client.client import UpnpRequester |
| 10 |
from async_upnp_client.const import AddressTupleVXType, HttpRequest, HttpResponse |
| 11 |
from async_upnp_client.event_handler import UpnpEventHandler, UpnpNotifyServer |
| 12 |
|
| 13 |
|
| 14 |
def read_file(filename: str) -> str: |
| 15 |
"""Read file.""" |
| 16 |
path = os.path.join("tests", "fixtures", filename) |
| 17 |
with open(path, encoding="utf-8") as file: |
| 18 |
return file.read() |
| 19 |
|
| 20 |
|
| 21 |
class UpnpTestRequester(UpnpRequester): |
| 22 |
"""Test requester.""" |
| 23 |
|
| 24 |
# pylint: disable=too-few-public-methods |
| 25 |
|
| 26 |
def __init__( |
| 27 |
self, |
| 28 |
response_map: Mapping[tuple[str, str], HttpResponse], |
| 29 |
) -> None: |
| 30 |
"""Class initializer.""" |
| 31 |
self.response_map: MutableMapping[tuple[str, str], HttpResponse] = deepcopy(cast(MutableMapping, response_map)) |
| 32 |
self.exceptions: Deque[Exception | None] = deque() |
| 33 |
|
| 34 |
async def async_http_request( |
| 35 |
self, |
| 36 |
http_request: HttpRequest, |
| 37 |
) -> HttpResponse: |
| 38 |
"""Do a HTTP request.""" |
| 39 |
await asyncio.sleep(0.01) |
| 40 |
|
| 41 |
if self.exceptions: |
| 42 |
exception = self.exceptions.popleft() |
| 43 |
if exception is not None: |
| 44 |
raise exception |
| 45 |
|
| 46 |
key = (http_request.method, http_request.url) |
| 47 |
if key not in self.response_map: |
| 48 |
raise KeyError(f"Request not in response map: {key}") |
| 49 |
|
| 50 |
return self.response_map[key] |
| 51 |
|
| 52 |
|
| 53 |
RESPONSE_MAP: Mapping[tuple[str, str], HttpResponse] = { |
| 54 |
# DLNA/DMR |
| 55 |
("GET", "http://dlna_dmr:1234/device.xml"): HttpResponse( |
| 56 |
200, |
| 57 |
{}, |
| 58 |
read_file("dlna/dmr/device.xml"), |
| 59 |
), |
| 60 |
("GET", "http://dlna_dmr:1234/device_embedded.xml"): HttpResponse( |
| 61 |
200, |
| 62 |
{}, |
| 63 |
read_file("dlna/dmr/device_embedded.xml"), |
| 64 |
), |
| 65 |
("GET", "http://dlna_dmr:1234/device_incomplete.xml"): HttpResponse( |
| 66 |
200, |
| 67 |
{}, |
| 68 |
read_file("dlna/dmr/device_incomplete.xml"), |
| 69 |
), |
| 70 |
("GET", "http://dlna_dmr:1234/device_with_empty_descriptor.xml"): HttpResponse( |
| 71 |
200, |
| 72 |
{}, |
| 73 |
read_file("dlna/dmr/device_with_empty_descriptor.xml"), |
| 74 |
), |
| 75 |
("GET", "http://dlna_dmr:1234/RenderingControl_1.xml"): HttpResponse( |
| 76 |
200, |
| 77 |
{}, |
| 78 |
read_file("dlna/dmr/RenderingControl_1.xml"), |
| 79 |
), |
| 80 |
("GET", "http://dlna_dmr:1234/ConnectionManager_1.xml"): HttpResponse( |
| 81 |
200, |
| 82 |
{}, |
| 83 |
read_file("dlna/dmr/ConnectionManager_1.xml"), |
| 84 |
), |
| 85 |
("GET", "http://dlna_dmr:1234/AVTransport_1.xml"): HttpResponse( |
| 86 |
200, |
| 87 |
{}, |
| 88 |
read_file("dlna/dmr/AVTransport_1.xml"), |
| 89 |
), |
| 90 |
("GET", "http://dlna_dmr:1234/Empty_Descriptor.xml"): HttpResponse( |
| 91 |
200, |
| 92 |
{}, |
| 93 |
read_file("dlna/dmr/Empty_Descriptor.xml"), |
| 94 |
), |
| 95 |
("SUBSCRIBE", "http://dlna_dmr:1234/upnp/event/ConnectionManager1"): HttpResponse( |
| 96 |
200, |
| 97 |
{"sid": "uuid:dummy-cm1", "timeout": "Second-175"}, |
| 98 |
"", |
| 99 |
), |
| 100 |
("SUBSCRIBE", "http://dlna_dmr:1234/upnp/event/RenderingControl1"): HttpResponse( |
| 101 |
200, |
| 102 |
{"sid": "uuid:dummy", "timeout": "Second-300"}, |
| 103 |
"", |
| 104 |
), |
| 105 |
("SUBSCRIBE", "http://dlna_dmr:1234/upnp/event/AVTransport1"): HttpResponse( |
| 106 |
200, |
| 107 |
{"sid": "uuid:dummy-avt1", "timeout": "Second-150"}, |
| 108 |
"", |
| 109 |
), |
| 110 |
("SUBSCRIBE", "http://dlna_dmr:1234/upnp/event/QPlay"): HttpResponse( |
| 111 |
200, |
| 112 |
{"sid": "uuid:dummy-qp1", "timeout": "Second-150"}, |
| 113 |
"", |
| 114 |
), |
| 115 |
("UNSUBSCRIBE", "http://dlna_dmr:1234/upnp/event/ConnectionManager1"): HttpResponse( |
| 116 |
200, |
| 117 |
{"sid": "uuid:dummy-cm1"}, |
| 118 |
"", |
| 119 |
), |
| 120 |
("UNSUBSCRIBE", "http://dlna_dmr:1234/upnp/event/RenderingControl1"): HttpResponse( |
| 121 |
200, |
| 122 |
{"sid": "uuid:dummy"}, |
| 123 |
"", |
| 124 |
), |
| 125 |
("UNSUBSCRIBE", "http://dlna_dmr:1234/upnp/event/AVTransport1"): HttpResponse( |
| 126 |
200, |
| 127 |
{"sid": "uuid:dummy-avt1"}, |
| 128 |
"", |
| 129 |
), |
| 130 |
("UNSUBSCRIBE", "http://dlna_dmr:1234/upnp/event/QPlay"): HttpResponse( |
| 131 |
200, |
| 132 |
{"sid": "uuid:dummy-qp1"}, |
| 133 |
"", |
| 134 |
), |
| 135 |
# DLNA/DMS |
| 136 |
("GET", "http://dlna_dms:1234/device.xml"): HttpResponse( |
| 137 |
200, |
| 138 |
{}, |
| 139 |
read_file("dlna/dms/device.xml"), |
| 140 |
), |
| 141 |
("GET", "http://dlna_dms:1234/ConnectionManager_1.xml"): HttpResponse( |
| 142 |
200, |
| 143 |
{}, |
| 144 |
read_file("dlna/dms/ConnectionManager_1.xml"), |
| 145 |
), |
| 146 |
("GET", "http://dlna_dms:1234/ContentDirectory_1.xml"): HttpResponse( |
| 147 |
200, |
| 148 |
{}, |
| 149 |
read_file("dlna/dms/ContentDirectory_1.xml"), |
| 150 |
), |
| 151 |
("SUBSCRIBE", "http://dlna_dms:1234/upnp/event/ConnectionManager1"): HttpResponse( |
| 152 |
200, |
| 153 |
{"sid": "uuid:dummy-cm1", "timeout": "Second-150"}, |
| 154 |
"", |
| 155 |
), |
| 156 |
("SUBSCRIBE", "http://dlna_dms:1234/upnp/event/ContentDirectory1"): HttpResponse( |
| 157 |
200, |
| 158 |
{"sid": "uuid:dummy-cd1", "timeout": "Second-150"}, |
| 159 |
"", |
| 160 |
), |
| 161 |
("UNSUBSCRIBE", "http://dlna_dms:1234/upnp/event/ConnectionManager1"): HttpResponse( |
| 162 |
200, |
| 163 |
{"sid": "uuid:dummy-cm1"}, |
| 164 |
"", |
| 165 |
), |
| 166 |
("UNSUBSCRIBE", "http://dlna_dms:1234/upnp/event/ContentDirectory1"): HttpResponse( |
| 167 |
200, |
| 168 |
{"sid": "uuid:dummy-cd1"}, |
| 169 |
"", |
| 170 |
), |
| 171 |
# IGD |
| 172 |
("GET", "http://igd:1234/device.xml"): HttpResponse(200, {}, read_file("igd/device.xml")), |
| 173 |
("GET", "http://igd:1234/Layer3Forwarding.xml"): HttpResponse( |
| 174 |
200, |
| 175 |
{}, |
| 176 |
read_file("igd/Layer3Forwarding.xml"), |
| 177 |
), |
| 178 |
("GET", "http://igd:1234/WANCommonInterfaceConfig.xml"): HttpResponse( |
| 179 |
200, |
| 180 |
{}, |
| 181 |
read_file("igd/WANCommonInterfaceConfig.xml"), |
| 182 |
), |
| 183 |
("GET", "http://igd:1234/WANIPConnection.xml"): HttpResponse( |
| 184 |
200, |
| 185 |
{}, |
| 186 |
read_file("igd/WANIPConnection.xml"), |
| 187 |
), |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
class UpnpTestNotifyServer(UpnpNotifyServer): |
| 192 |
"""Test notify server.""" |
| 193 |
|
| 194 |
def __init__( |
| 195 |
self, |
| 196 |
requester: UpnpRequester, |
| 197 |
source: AddressTupleVXType, |
| 198 |
callback_url: str | None = None, |
| 199 |
) -> None: |
| 200 |
"""Initialize.""" |
| 201 |
self._requester = requester |
| 202 |
self._source = source |
| 203 |
self._callback_url = callback_url |
| 204 |
self.event_handler = UpnpEventHandler(self, requester) |
| 205 |
|
| 206 |
@property |
| 207 |
def callback_url(self) -> str: |
| 208 |
"""Return callback URL on which we are callable.""" |
| 209 |
return self._callback_url or f"http://{self._source[0]}:{self._source[1]}/notify" |
| 210 |
|
| 211 |
async def async_start_server(self) -> None: |
| 212 |
"""Start the server.""" |
| 213 |
|
| 214 |
async def async_stop_server(self) -> None: |
| 215 |
"""Stop the server.""" |
| 216 |
await self.event_handler.async_unsubscribe_all() |