def test_close_payload_with_invalid_utf8_sequence(payload, expected_error): # in py2, the codec in exception is named utf8 rather than utf-8, despite # calling .decode('utf-8'). Yay! if six.PY2: expected_error = expected_error.replace("('utf-8", "('utf8") with pytest.raises(CriticalProtocolError) as e: Close.from_payload(payload) assert str(e.value) == expected_error
def test_calling_close_yields_close_event(websocket_with_fake_session): ws = websocket_with_fake_session ws.close() close_message = Close(1000, b'bye') close_events = list(ws._on_close(close_message)) assert len(close_events) == 1 assert isinstance(close_events[0], Closed) assert ws.is_closed is True assert ws.is_closing is False
def test_calling_on_close_when_websocket_is_closed_results_in_noop( websocket_with_fake_session): ws = websocket_with_fake_session ws.close() assert ws.is_closing is True assert ws.is_closed is False close_message = Close(1000, b'bye') list(ws._on_close(close_message)) assert ws.is_closed is True assert ws.is_closing is False assert len(list(ws._on_close(close_message))) == 0
def test_close_with_reserved_code(websocket): reserved_message = Close(code=1005, reason='reserved-close-code') with pytest.raises(ProtocolError): next(websocket._on_close(reserved_message))