예제 #1
0
def test_get_ws_connection_upgrade_error(client):
    """Test error opening a websocket connection with an engineIO session."""
    mock_ws = AsyncMock()
    receive_queue = asyncio.Queue(loop=client.loop)
    mock_ws.receive_str = receive_queue.get

    @asyncio.coroutine
    def mock_send_str(data):
        if data == "2probe":
            yield from receive_queue.put("3probe")
            return

        if data == "5":
            yield from receive_queue.put('44"socketIO Mock Error"')

    mock_ws.send_str = mock_send_str
    client._client_session.ws_connect.return_value = mock_ws
    session_data = {
        "sid": "mock_session_id",
        "pingTimeout": 12.345,
        "pingInterval": 23.456,
    }
    with pytest.raises(exceptions.SocketIOError) as exc:
        client.loop.run_until_complete(client._get_ws_connection(session_data))

    assert str(exc.value) == "socketIO Mock Error"
예제 #2
0
def test_ws_connect(client):
    """Test websocket connect and ping loop."""
    mock_ws = AsyncMock()
    send_queue = queue.Queue()
    receive_queue = asyncio.Queue(loop=client.loop)
    mock_ws.receive = receive_queue.get

    @asyncio.coroutine
    def mock_send_str(data):
        send_queue.put(data)

    mock_ws.send_str = mock_send_str

    session_data = {
        "sid": "mock_session_id",
        "pingTimeout": 12.345,
        "pingInterval": 23.456,
    }
    client._get_engineio_session = AsyncMock()
    client._get_engineio_session.return_value = session_data
    client._get_ws_connection = AsyncMock()
    client._get_ws_connection.return_value = mock_ws

    ws_loop = client.loop.run_until_complete(client.ws_connect())
    assert not ws_loop.done()

    packet = send_queue.get(False)
    assert send_queue.empty()
    assert packet == "2"

    msg = MagicMock()
    msg.type = aiohttp.WSMsgType.CLOSED
    client.loop.run_until_complete(receive_queue.put(msg))
    assert ws_loop.done()

    packet = send_queue.get(False)
    assert packet == "41"
    packet = send_queue.get(False)
    assert packet == "1"
    assert send_queue.empty()
    assert mock_ws.close.called
    assert len(mock_ws.close.mock_calls) == 1
예제 #3
0
def test_ws_ping(client):
    """Test websocket ping."""
    mock_ws = AsyncMock()
    send_queue = queue.Queue()
    receive_queue = asyncio.Queue(loop=client.loop)
    mock_ws.receive = receive_queue.get

    @asyncio.coroutine
    def mock_send_str(data):
        send_queue.put(data)

    mock_ws.send_str = mock_send_str

    old_handle = MagicMock()
    client.ws_close = AsyncMock()
    client.loop.call_later = MagicMock()
    client._ws_connection = mock_ws
    client._ws_session_data = {
        "sid": "mock_session_id",
        "pingTimeout": 12.345,
        "pingInterval": 23.456,
        "pingTimeoutHandle": old_handle,
    }
    client.loop.run_until_complete(client._ping())

    packet = send_queue.get(False)
    assert send_queue.empty()
    assert packet == "2"

    assert old_handle.cancel.called
    assert len(old_handle.cancel.mock_calls) == 1

    assert client.loop.call_later.called
    assert len(client.loop.call_later.mock_calls) == 1
    assert client.loop.call_later.mock_calls[0][1][0] == 12.345
    timeout = client.loop.call_later.mock_calls[0][1][1]
    assert not client.ws_close.called
    future = timeout()
    client.loop.run_until_complete(future)
    assert client.ws_close.called
    assert len(client.ws_close.mock_calls) == 1
예제 #4
0
def test_get_ws_connection(client):
    """Test opening a websocket connection with an engineIO session."""
    mock_ws = AsyncMock()
    receive_queue = asyncio.Queue(loop=client.loop)
    mock_ws.receive_str = receive_queue.get

    @asyncio.coroutine
    def mock_send_str(data):
        if data == "2probe":
            yield from receive_queue.put("3probe")
            return

        if data == "5":
            yield from receive_queue.put("40")

    mock_ws.send_str = mock_send_str
    client._client_session.ws_connect.return_value = mock_ws
    session_data = {
        "sid": "mock_session_id",
        "pingTimeout": 12.345,
        "pingInterval": 23.456,
    }
    client.loop.run_until_complete(client._get_ws_connection(session_data))
    assert client._client_session.ws_connect.called
    assert len(client._client_session.ws_connect.mock_calls) == 1
    assert client._client_session.ws_connect.mock_calls[0][1][0][:38] == \
        "wss://stream.automatic.com/socket.io/?"
    query = \
        client._client_session.ws_connect.mock_calls[0][1][0][38:].split('&')
    params = {}
    for item in query:
        k, v = item.split('=')
        params[k] = v
    assert params == {
        "EIO": "3",
        "token": "mock_id:mock_secret",
        "transport": "websocket",
        "sid": "mock_session_id",
    }