| 1 |
"""Unit tests for aiohttp.""" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from unittest.mock import MagicMock, patch |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from async_upnp_client.aiohttp import ( |
| 10 |
AiohttpNotifyServer, |
| 11 |
AiohttpRequester, |
| 12 |
_fixed_host_header, |
| 13 |
) |
| 14 |
from async_upnp_client.const import HttpRequest |
| 15 |
from async_upnp_client.exceptions import UpnpCommunicationError |
| 16 |
|
| 17 |
from .conftest import RESPONSE_MAP, UpnpTestRequester |
| 18 |
|
| 19 |
|
| 20 |
def test_fixed_host_header() -> None: |
| 21 |
"""Test _fixed_host_header.""" |
| 22 |
|
| 23 |
assert _fixed_host_header("http://192.168.1.1:8000/desc") == {} |
| 24 |
assert _fixed_host_header("http://router.local:8000/desc") == {} |
| 25 |
assert _fixed_host_header("http://[fe80::1%10]:8000/desc") == {"Host": "[fe80::1]:8000"} |
| 26 |
|
| 27 |
assert _fixed_host_header("http://192.168.1.1/desc") == {} |
| 28 |
assert _fixed_host_header("http://router.local/desc") == {} |
| 29 |
assert _fixed_host_header("http://[fe80::1%10]/desc") == {"Host": "[fe80::1]"} |
| 30 |
|
| 31 |
assert _fixed_host_header("https://192.168.1.1/desc") == {} |
| 32 |
assert _fixed_host_header("https://router.local/desc") == {} |
| 33 |
assert _fixed_host_header("https://[fe80::1%10]/desc") == {"Host": "[fe80::1]"} |
| 34 |
|
| 35 |
assert _fixed_host_header("http://192.168.1.1:8000/root%desc") == {} |
| 36 |
assert _fixed_host_header("http://router.local:8000/root%desc") == {} |
| 37 |
assert _fixed_host_header("http://[fe80::1]:8000/root%desc") == {} |
| 38 |
|
| 39 |
|
| 40 |
@pytest.mark.asyncio |
| 41 |
async def test_server_init() -> None: |
| 42 |
"""Test initialization of an AiohttpNotifyServer.""" |
| 43 |
requester = UpnpTestRequester(RESPONSE_MAP) |
| 44 |
server = AiohttpNotifyServer(requester, ("192.168.1.2", 8090)) |
| 45 |
assert server._loop is not None |
| 46 |
assert server.listen_host == "192.168.1.2" |
| 47 |
assert server.listen_port == 8090 |
| 48 |
assert server.callback_url == "http://192.168.1.2:8090/notify" |
| 49 |
assert server.event_handler is not None |
| 50 |
|
| 51 |
server = AiohttpNotifyServer(requester, ("192.168.1.2", 8090), "http://1.2.3.4:8091/") |
| 52 |
assert server.callback_url == "http://1.2.3.4:8091/" |
| 53 |
|
| 54 |
|
| 55 |
@pytest.mark.asyncio |
| 56 |
@patch( |
| 57 |
"async_upnp_client.aiohttp.aiohttp.ClientSession.request", |
| 58 |
side_effect=UnicodeDecodeError("", b"", 0, 1, ""), |
| 59 |
) |
| 60 |
async def test_client_decode_error(_mock_request: MagicMock) -> None: |
| 61 |
"""Test handling unicode decode error.""" |
| 62 |
requester = AiohttpRequester() |
| 63 |
request = HttpRequest("GET", "http://192.168.1.1/desc.xml", {}, None) |
| 64 |
with pytest.raises(UpnpCommunicationError): |
| 65 |
await requester.async_http_request(request) |