Example #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"
Example #2
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",
    }