File: test_aiohttp.py

package info (click to toggle)
async-upnp-client 0.46.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,080 kB
  • sloc: python: 11,872; xml: 2,826; sh: 32; makefile: 6
file content (69 lines) | stat: -rw-r--r-- 2,501 bytes parent folder | download | duplicates (2)
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
"""Unit tests for aiohttp."""

# pylint: disable=protected-access

from unittest.mock import MagicMock, patch

import pytest

from async_upnp_client.aiohttp import (
    AiohttpNotifyServer,
    AiohttpRequester,
    _fixed_host_header,
)
from async_upnp_client.const import HttpRequest
from async_upnp_client.exceptions import UpnpCommunicationError

from .conftest import RESPONSE_MAP, UpnpTestRequester


def test_fixed_host_header() -> None:
    """Test _fixed_host_header."""
    # pylint: disable=C1803
    assert _fixed_host_header("http://192.168.1.1:8000/desc") == {}
    assert _fixed_host_header("http://router.local:8000/desc") == {}
    assert _fixed_host_header("http://[fe80::1%10]:8000/desc") == {
        "Host": "[fe80::1]:8000"
    }

    assert _fixed_host_header("http://192.168.1.1/desc") == {}
    assert _fixed_host_header("http://router.local/desc") == {}
    assert _fixed_host_header("http://[fe80::1%10]/desc") == {"Host": "[fe80::1]"}

    assert _fixed_host_header("https://192.168.1.1/desc") == {}
    assert _fixed_host_header("https://router.local/desc") == {}
    assert _fixed_host_header("https://[fe80::1%10]/desc") == {"Host": "[fe80::1]"}

    assert _fixed_host_header("http://192.168.1.1:8000/root%desc") == {}
    assert _fixed_host_header("http://router.local:8000/root%desc") == {}
    assert _fixed_host_header("http://[fe80::1]:8000/root%desc") == {}


@pytest.mark.asyncio
async def test_server_init() -> None:
    """Test initialization of an AiohttpNotifyServer."""
    requester = UpnpTestRequester(RESPONSE_MAP)
    server = AiohttpNotifyServer(requester, ("192.168.1.2", 8090))
    assert server._loop is not None
    assert server.listen_host == "192.168.1.2"
    assert server.listen_port == 8090
    assert server.callback_url == "http://192.168.1.2:8090/notify"
    assert server.event_handler is not None

    server = AiohttpNotifyServer(
        requester, ("192.168.1.2", 8090), "http://1.2.3.4:8091/"
    )
    assert server.callback_url == "http://1.2.3.4:8091/"


@pytest.mark.asyncio
@patch(
    "async_upnp_client.aiohttp.aiohttp.ClientSession.request",
    side_effect=UnicodeDecodeError("", b"", 0, 1, ""),
)
async def test_client_decode_error(_mock_request: MagicMock) -> None:
    """Test handling unicode decode error."""
    requester = AiohttpRequester()
    request = HttpRequest("GET", "http://192.168.1.1/desc.xml", {}, None)
    with pytest.raises(UpnpCommunicationError):
        await requester.async_http_request(request)