def test_that_on_ping_responds_with_pong(session, mocker):
    # we don't actually care that much for the whole stack underneath,
    # we only want to check whether a certain method was called..
    send_pong = mocker.patch('lomond.websocket.WebSocket.send_pong')

    session._send_pong(events.Ping(b'\x00'))

    assert send_pong.called_with(b'\x00')
Beispiel #2
0
def test_echo_poll():
    """Test against public echo server."""
    # TODO: host our own echo server
    ws = lomond.WebSocket('wss://echo.websocket.org')
    _events = []
    polls = 0
    for event in ws.connect(poll=1.0, ping_rate=1.0, auto_pong=True):
        _events.append(event)
        if event.name == 'poll':
            polls += 1
            if polls == 1:
                ws.session._on_event(events.Ping(b'foo'))
            elif polls == 2:
                # Covers some lesser used code paths
                ws.state.closed = True
                ws.session._sock.close()
def test_send_pong(session):
    session.websocket = FakeWebSocket()
    session._send_pong(events.Ping(b'foo'))
    (events.Text('A'), "Text('A')"),
    (events.Connecting('http://example.com'),
     "Connecting(url='http://example.com')"),
    (events.ConnectFail('404'), "ConnectFail('404')"),
    (events.Rejected('401', 'Insufficient permissions'),
     "Rejected('401', 'Insufficient permissions')"),
    (events.Ready('200', 'HTTP',
                  []), "Ready('200', protocol='HTTP', extensions=[])"),
    (events.Disconnected(), "Disconnected('closed', graceful=False)"),
    (events.Disconnected('error'), "Disconnected('error', graceful=False)"),
    (events.Disconnected('bye',
                         graceful=True), "Disconnected('bye', graceful=True)"),
    (events.Closed(1, 'closed'), "Closed(1, 'closed')"),
    (events.Closing(1, 'closed'), "Closing(1, 'closed')"),
    (events.UnknownMessage('?.!'), "UnknownMessage()"),
    (events.Ping('o |'), "Ping('o |')"),
    (events.Pong('  | o'), "Pong('  | o')"),
    (events.BackOff(0.1), "BackOff(delay=0.1)")
]

# we are splitting these two test cases into separate branches, because the
# underlying code which implements __repr__ calls __repr__ of the passed
# object. As we all know, Python2 treats b'' as a simple string, however
# Python3 understands it as a different type (bytes) and represents it with a
# leading b''. This could be done in a portable way using six in one line, but
# the code would be somewhat misleading
if six.PY2:
    test_cases.extend([
        (events.Binary(b'\xef' * 25),
         "Binary('%s' + 1 bytes)" % ('\\xef' * 24)),
        (events.Binary(b'\x01'), "Binary('\\x01')"),
        "Rejected(response='401', reason='Insufficient permissions')"
    ),
    (
        events.Ready('200', 'HTTP', []),
        "Ready(response='200', protocol='HTTP', extensions=[])"
    ),
    (events.Disconnected(), "Disconnected(reason='closed', graceful=False)"),
    (events.Disconnected(reason='error'), "Disconnected(reason='error', graceful=False)"),
    (
        events.Disconnected('bye', graceful=True),
        "Disconnected(reason='bye', graceful=True)"
    ),
    (events.Closed(1, 'closed'), "Closed(code=1, reason='closed')"),
    (events.Closing(1, 'closed'), "Closing(code=1, reason='closed')"),
    (events.UnknownMessage('?.!'), "UnknownMessage()"),
    (events.Ping('o |'), "Ping(data='o |')"),
    (events.Pong('  | o'), "Pong(data='  | o')"),
    (events.BackOff(0.1), "BackOff(delay=0.1)"),
    (events.ProtocolError('error', critical=False), "ProtocolError(error='error', critical=False)")
]

# we are splitting these two test cases into separate branches, because the
# underlying code which implements __repr__ calls __repr__ of the passed
# object. As we all know, Python2 treats b'' as a simple string, however
# Python3 understands it as a different type (bytes) and represents it with a
# leading b''. This could be done in a portable way using six in one line, but
# the code would be somewhat misleading
if six.PY2:
    test_cases.extend([
        (
            events.Binary(b'\xef' * 25),