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