| 1 |
"""Unit tests for advertisement.""" |
| 2 |
|
| 3 |
from unittest.mock import AsyncMock |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from async_upnp_client.advertisement import SsdpAdvertisementListener |
| 8 |
from async_upnp_client.utils import CaseInsensitiveDict |
| 9 |
|
| 10 |
from .common import ( |
| 11 |
ADVERTISEMENT_HEADERS_DEFAULT, |
| 12 |
ADVERTISEMENT_REQUEST_LINE, |
| 13 |
SEARCH_HEADERS_DEFAULT, |
| 14 |
SEARCH_REQUEST_LINE, |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
@pytest.mark.asyncio |
| 19 |
async def test_receive_ssdp_alive() -> None: |
| 20 |
"""Test handling a ssdp:alive advertisement.""" |
| 21 |
# pylint: disable=protected-access |
| 22 |
async_on_alive = AsyncMock() |
| 23 |
async_on_byebye = AsyncMock() |
| 24 |
async_on_update = AsyncMock() |
| 25 |
listener = SsdpAdvertisementListener( |
| 26 |
async_on_alive=async_on_alive, |
| 27 |
async_on_byebye=async_on_byebye, |
| 28 |
async_on_update=async_on_update, |
| 29 |
) |
| 30 |
headers = CaseInsensitiveDict(ADVERTISEMENT_HEADERS_DEFAULT) |
| 31 |
headers["NTS"] = "ssdp:alive" |
| 32 |
listener._on_data(ADVERTISEMENT_REQUEST_LINE, headers) |
| 33 |
|
| 34 |
async_on_alive.assert_called_with(headers) |
| 35 |
async_on_byebye.assert_not_called() |
| 36 |
async_on_update.assert_not_called() |
| 37 |
|
| 38 |
|
| 39 |
@pytest.mark.asyncio |
| 40 |
async def test_receive_ssdp_byebye() -> None: |
| 41 |
"""Test handling a ssdp:alive advertisement.""" |
| 42 |
# pylint: disable=protected-access |
| 43 |
async_on_alive = AsyncMock() |
| 44 |
async_on_byebye = AsyncMock() |
| 45 |
async_on_update = AsyncMock() |
| 46 |
listener = SsdpAdvertisementListener( |
| 47 |
async_on_alive=async_on_alive, |
| 48 |
async_on_byebye=async_on_byebye, |
| 49 |
async_on_update=async_on_update, |
| 50 |
) |
| 51 |
headers = CaseInsensitiveDict(ADVERTISEMENT_HEADERS_DEFAULT) |
| 52 |
headers["NTS"] = "ssdp:byebye" |
| 53 |
listener._on_data(ADVERTISEMENT_REQUEST_LINE, headers) |
| 54 |
|
| 55 |
async_on_alive.assert_not_called() |
| 56 |
async_on_byebye.assert_called_with(headers) |
| 57 |
async_on_update.assert_not_called() |
| 58 |
|
| 59 |
|
| 60 |
@pytest.mark.asyncio |
| 61 |
async def test_receive_ssdp_update() -> None: |
| 62 |
"""Test handling a ssdp:alive advertisement.""" |
| 63 |
# pylint: disable=protected-access |
| 64 |
async_on_alive = AsyncMock() |
| 65 |
async_on_byebye = AsyncMock() |
| 66 |
async_on_update = AsyncMock() |
| 67 |
listener = SsdpAdvertisementListener( |
| 68 |
async_on_alive=async_on_alive, |
| 69 |
async_on_byebye=async_on_byebye, |
| 70 |
async_on_update=async_on_update, |
| 71 |
) |
| 72 |
headers = CaseInsensitiveDict(ADVERTISEMENT_HEADERS_DEFAULT) |
| 73 |
headers["NTS"] = "ssdp:update" |
| 74 |
listener._on_data(ADVERTISEMENT_REQUEST_LINE, headers) |
| 75 |
|
| 76 |
async_on_alive.assert_not_called() |
| 77 |
async_on_byebye.assert_not_called() |
| 78 |
async_on_update.assert_called_with(headers) |
| 79 |
|
| 80 |
|
| 81 |
@pytest.mark.asyncio |
| 82 |
async def test_receive_ssdp_search_response() -> None: |
| 83 |
"""Test handling a ssdp search response, which is ignored.""" |
| 84 |
# pylint: disable=protected-access |
| 85 |
async_on_alive = AsyncMock() |
| 86 |
async_on_byebye = AsyncMock() |
| 87 |
async_on_update = AsyncMock() |
| 88 |
listener = SsdpAdvertisementListener( |
| 89 |
async_on_alive=async_on_alive, |
| 90 |
async_on_byebye=async_on_byebye, |
| 91 |
async_on_update=async_on_update, |
| 92 |
) |
| 93 |
headers = CaseInsensitiveDict(SEARCH_HEADERS_DEFAULT) |
| 94 |
listener._on_data(SEARCH_REQUEST_LINE, headers) |
| 95 |
|
| 96 |
async_on_alive.assert_not_called() |
| 97 |
async_on_byebye.assert_not_called() |
| 98 |
async_on_update.assert_not_called() |