def test_finish_request_pops_socket():
    """Test that ``finish_request`` always clears the connection after a request."""
    amock = Mock()
    client_addr = ("192.168.1.1", 55555)
    server_addr = ("", 51826)

    # Positive case: The request is handled
    server = hap_server.HAPServer(
        server_addr, amock, handler_type=lambda *args: MagicMock()
    )

    server.connections[client_addr] = amock
    server.finish_request(amock, client_addr)

    assert len(server.connections) == 0

    # Negative case: The request fails with a timeout
    def raises(*args):
        raise timeout()

    server = hap_server.HAPServer(server_addr, amock, handler_type=raises)
    server.connections[client_addr] = amock
    server.finish_request(amock, client_addr)

    assert len(server.connections) == 0

    # Negative case: The request raises some other exception
    server = hap_server.HAPServer(server_addr, amock, handler_type=lambda *args: 1 / 0)
    server.connections[client_addr] = amock

    with pytest.raises(Exception):
        server.finish_request(amock, client_addr)

    assert len(server.connections) == 0
Example #2
0
async def test_push_event(driver):
    """Test we can create and send an event."""
    addr_info = ("1.2.3.4", 1234)
    server = hap_server.HAPServer(addr_info, driver)
    server.loop = asyncio.get_event_loop()
    hap_events = []

    def _save_event(hap_event):
        hap_events.append(hap_event)

    hap_server_protocol = HAPServerProtocol(server.loop, server.connections,
                                            server.accessory_handler)
    hap_server_protocol.write = _save_event

    assert server.push_event({"aid": 1}, addr_info) is False
    await asyncio.sleep(0)
    server.connections[addr_info] = hap_server_protocol

    assert server.push_event({"aid": 1}, addr_info) is True
    assert server.push_event({"aid": 2}, addr_info) is True
    assert server.push_event({"aid": 3}, addr_info) is True

    await asyncio.sleep(0)
    assert hap_events == [
        b"EVENT/1.0 200 OK\r\nContent-Type: application/hap+json\r\nContent-Length: 51\r\n\r\n"
        b'{"characteristics":[{"aid":1},{"aid":2},{"aid":3}]}'
    ]
Example #3
0
async def test_idle_connection_cleanup():
    """Test we cleanup idle connections."""
    loop = asyncio.get_event_loop()
    addr_info = ("0.0.0.0", None)
    client_1_addr_info = ("1.2.3.4", 44433)

    with patch.object(
            hap_server, "IDLE_CONNECTION_CHECK_INTERVAL_SECONDS",
            0), patch("pyhap.accessory_driver.AsyncZeroconf"), patch(
                "pyhap.accessory_driver.AccessoryDriver.persist"), patch(
                    "pyhap.accessory_driver.AccessoryDriver.load"):
        driver = AccessoryDriver(loop=loop)
        server = hap_server.HAPServer(addr_info, driver)
        await server.async_start(loop)
        check_idle = MagicMock()
        server.connections[client_1_addr_info] = MagicMock(
            check_idle=check_idle)
        for _ in range(3):
            await asyncio.sleep(0)
        assert check_idle.called
        check_idle.reset_mock()
        for _ in range(3):
            await asyncio.sleep(0)
        assert check_idle.called
    server.async_stop()
Example #4
0
async def test_we_can_start_stop(driver):
    """Test we can start and stop."""
    loop = asyncio.get_event_loop()
    addr_info = ("0.0.0.0", None)
    client_1_addr_info = ("1.2.3.4", 44433)
    client_2_addr_info = ("4.5.6.7", 33444)

    server = hap_server.HAPServer(addr_info, driver)
    await server.async_start(loop)
    server.connections[client_1_addr_info] = MagicMock()
    server.connections[client_2_addr_info] = None
    server.async_stop()
Example #5
0
def test_push_event(driver):
    """Test we can create and send an event."""
    addr_info = ("1.2.3.4", 1234)
    server = hap_server.HAPServer(addr_info, driver)
    hap_events = []

    def _save_event(hap_event):
        hap_events.append(hap_event)

    hap_server_protocol = MagicMock()
    hap_server_protocol.write = _save_event

    assert server.push_event(b"data", addr_info) is False
    server.connections[addr_info] = hap_server_protocol

    assert server.push_event(b"data", addr_info) is True
    assert hap_events == [
        b"EVENT/1.0 200 OK\r\nContent-Type: application/hap+json\r\nContent-Length: 4\r\n\r\ndata"
    ]
Example #6
0
async def test_we_can_connect():
    """Test we can start, connect, and stop."""
    loop = asyncio.get_event_loop()
    with patch("pyhap.accessory_driver.Zeroconf"), patch(
        "pyhap.accessory_driver.AccessoryDriver.persist"
    ):
        driver = AccessoryDriver(loop=loop)

    driver.add_accessory(Accessory(driver, "TestAcc"))

    addr_info = ("0.0.0.0", None)
    server = hap_server.HAPServer(addr_info, driver)
    await server.async_start(loop)
    sock = server.server.sockets[0]
    assert server.connections == {}
    _, port = sock.getsockname()
    _, writer = await asyncio.open_connection("127.0.0.1", port)
    assert server.connections != {}
    server.async_stop()
    writer.close()