def test_connect(self): s = server.Server() environ = {'REQUEST_METHOD': 'GET', 'QUERY_STRING': ''} start_response = mock.MagicMock() r = s.handle_request(environ, start_response) assert len(s.sockets) == 1 assert start_response.call_count == 1 assert start_response.call_args[0][0] == '200 OK' assert ( 'Content-Type', 'application/octet-stream', ) in start_response.call_args[0][1] assert len(r) == 1 packets = payload.Payload(encoded_payload=r[0]).packets assert len(packets) == 1 assert packets[0].packet_type == packet.OPEN assert 'upgrades' in packets[0].data assert packets[0].data['upgrades'] == ['websocket'] assert 'sid' in packets[0].data
def test_connect(self, import_module): a = self.get_async_mock() import_module.side_effect = [a] s = asyncio_server.AsyncServer() _run(s.handle_request('request')) assert len(s.sockets) == 1 assert a._async['make_response'].call_count == 1 assert a._async['make_response'].call_args[0][0] == '200 OK' assert ('Content-Type', 'application/octet-stream') in a._async[ 'make_response' ].call_args[0][1] packets = payload.Payload( encoded_payload=a._async['make_response'].call_args[0][2] ).packets assert len(packets) == 1 assert packets[0].packet_type == packet.OPEN assert 'upgrades' in packets[0].data assert packets[0].data['upgrades'] == ['websocket'] assert 'sid' in packets[0].data
def test_write_loop_polling_bad_status(self): c = client.Client() c.base_url = 'http://foo' c.state = 'connected' c.current_transport = 'polling' c.queue = mock.MagicMock() c.queue_empty = RuntimeError c.queue.get.side_effect = [ packet.Packet(packet.MESSAGE, {'foo': 'bar'}), RuntimeError, ] c._send_request = mock.MagicMock() c._send_request.return_value.status = 500 c._write_loop() self.assertEqual(c.queue.task_done.call_count, 1) p = payload.Payload( packets=[packet.Packet(packet.MESSAGE, {'foo': 'bar'})]) c._send_request.assert_called_once_with( 'POST', 'http://foo', body=p.encode(), headers={'Content-Type': 'application/octet-stream'}) self.assertEqual(c.state, 'disconnected')
def test_connect_async_request_response_handlers(self, import_module): a = self.get_async_mock() a._async['translate_request'] = AsyncMock( return_value=a._async['translate_request'].return_value) a._async['make_response'] = AsyncMock( return_value=a._async['make_response'].return_value) import_module.side_effect = [a] s = asyncio_server.AsyncServer() _run(s.handle_request('request')) self.assertEqual(len(s.sockets), 1) self.assertEqual(a._async['make_response'].mock.call_count, 1) self.assertEqual(a._async['make_response'].mock.call_args[0][0], '200 OK') self.assertIn(('Content-Type', 'application/octet-stream'), a._async['make_response'].mock.call_args[0][1]) packets = payload.Payload(encoded_payload=a._async['make_response']. mock.call_args[0][2]).packets self.assertEqual(len(packets), 1) self.assertEqual(packets[0].packet_type, packet.OPEN) self.assertIn('upgrades', packets[0].data) self.assertEqual(packets[0].data['upgrades'], ['websocket']) self.assertIn('sid', packets[0].data)
def test_write_loop_polling_one_packet(self): c = asyncio_client.AsyncClient() c.base_url = 'http://foo' c.state = 'connected' c.ping_interval = 1 c.ping_timeout = 2 c.current_transport = 'polling' c.queue = mock.MagicMock() c.queue.Empty = RuntimeError c.queue.get = AsyncMock(side_effect=[ packet.Packet(packet.MESSAGE, {'foo': 'bar'}), RuntimeError ]) c.queue.get_nowait = mock.MagicMock(side_effect=RuntimeError) c._send_request = AsyncMock() c._send_request.mock.return_value.status = 200 _run(c._write_loop()) self.assertEqual(c.queue.task_done.call_count, 1) p = payload.Payload( packets=[packet.Packet(packet.MESSAGE, {'foo': 'bar'})]) c._send_request.mock.assert_called_once_with( 'POST', 'http://foo', body=p.encode(), headers={'Content-Type': 'application/octet-stream'}, timeout=5)
def test_polling_connection_with_more_packets(self, _send_request): _send_request.return_value.status_code = 200 _send_request.return_value.content = payload.Payload(packets=[ packet.Packet(packet.OPEN, { 'sid': '123', 'upgrades': [], 'pingInterval': 1000, 'pingTimeout': 2000 }), packet.Packet(packet.NOOP) ]).encode() c = client.Client() c._ping_loop = mock.MagicMock() c._read_loop_polling = mock.MagicMock() c._read_loop_websocket = mock.MagicMock() c._write_loop = mock.MagicMock() c._receive_packet = mock.MagicMock() on_connect = mock.MagicMock() c.on('connect', on_connect) c.connect('http://foo') time.sleep(0.1) self.assertEqual(c._receive_packet.call_count, 1) self.assertEqual( c._receive_packet.call_args_list[0][0][0].packet_type, packet.NOOP)
def test_read_loop_polling(self): c = client.Client() c.state = 'connected' c.base_url = 'http://foo' c.queue = mock.MagicMock() c._send_request = mock.MagicMock() c._send_request.side_effect = [ mock.MagicMock(status=200, data=payload.Payload(packets=[ packet.Packet(packet.PING), packet.Packet(packet.NOOP)]).encode()), None ] c.write_loop_task = mock.MagicMock() c._receive_packet = mock.MagicMock() c._read_loop_polling() self.assertEqual(c.state, 'disconnected') c.queue.put.assert_called_once_with(None) self.assertEqual(c._send_request.call_count, 2) self.assertEqual(c._receive_packet.call_count, 2) self.assertEqual(c._receive_packet.call_args_list[0][0][0].encode(), b'2') self.assertEqual(c._receive_packet.call_args_list[1][0][0].encode(), b'6')
def test_polling_connection_successful(self): c = asyncio_client.AsyncClient() c._send_request = AsyncMock() c._send_request.mock.return_value.status = 200 c._send_request.mock.return_value.read = AsyncMock( return_value=payload.Payload(packets=[ packet.Packet( packet.OPEN, { 'sid': '123', 'upgrades': [], 'pingInterval': 1000, 'pingTimeout': 2000 }) ]).encode()) c._ping_loop = AsyncMock() c._read_loop_polling = AsyncMock() c._read_loop_websocket = AsyncMock() c._write_loop = AsyncMock() on_connect = AsyncMock() c.on('connect', on_connect) _run(c.connect('http://foo')) time.sleep(0.1) c._ping_loop.mock.assert_called_once_with() c._read_loop_polling.mock.assert_called_once_with() c._read_loop_websocket.mock.assert_not_called() c._write_loop.mock.assert_called_once_with() on_connect.mock.assert_called_once_with() self.assertIn(c, client.connected_clients) self.assertEqual( c.base_url, 'http://foo/engine.io/?transport=polling&EIO=3&sid=123') self.assertEqual(c.sid, '123') self.assertEqual(c.ping_interval, 1) self.assertEqual(c.ping_timeout, 2) self.assertEqual(c.upgrades, []) self.assertEqual(c.transport(), 'polling')
def test_decode_payload_xhr_text(self): p = payload.Payload(encoded_payload=b'4:4abc') self.assertEqual(p.encode(), b'\x00\x04\xff4abc')
def test_encode_payload_xhr2(self): pkt = packet.Packet(packet.MESSAGE, data=six.text_type('abc')) p = payload.Payload([pkt]) self.assertEqual(p.packets, [pkt]) self.assertEqual(p.encode(), b'\x00\x04\xff4abc')
def test_encode_payload_xhr_text(self): pkt = packet.Packet(packet.MESSAGE, data=six.text_type('abc')) p = payload.Payload([pkt]) self.assertEqual(p.packets, [pkt]) self.assertEqual(p.encode(b64=True), b'4:4abc')
def test_encode_payload_binary(self): pkt = packet.Packet(packet.MESSAGE, data=b'\x00\x01\x02') p = payload.Payload([pkt]) assert p.packets == [pkt] assert p.encode() == 'bAAEC'
def test_decode_empty_payload(self): p = payload.Payload(encoded_payload=b'') self.assertEqual(p.encode(), b'')
def test_decode_multi_payload(self): p = payload.Payload(encoded_payload=b'4:4abc\x00\x04\xff4def') self.assertEqual(len(p.packets), 2) self.assertEqual(p.packets[0].data, 'abc') self.assertEqual(p.packets[1].data, 'def')
def test_decode_jsonp_payload(self): p = payload.Payload(encoded_payload=b'd=4:4abc') self.assertEqual(p.encode(), b'\x00\x04\xff4abc')
def test_encode_payload_text(self): pkt = packet.Packet(packet.MESSAGE, data='abc') p = payload.Payload([pkt]) assert p.packets == [pkt] assert p.encode() == '4abc'
def test_encode_empty_payload(self): p = payload.Payload() assert p.packets == [] assert p.encode() == ''
def test_decode_jsonp_payload(self): p = payload.Payload(encoded_payload='d=4abc') assert p.encode() == '4abc'
def test_decode_invalid_payload(self): with pytest.raises(ValueError): payload.Payload(encoded_payload='bad payload')
def test_encode_jsonp_payload(self): pkt = packet.Packet(packet.MESSAGE, data='abc') p = payload.Payload([pkt]) assert p.packets == [pkt] assert p.encode(jsonp_index=233) == '___eio[233]("4abc");'
def test_encode_payload_text_binary_multiple(self): pkt = packet.Packet(packet.MESSAGE, data='abc') pkt2 = packet.Packet(packet.MESSAGE, data=b'\x03\x04\x05\x06') p = payload.Payload([pkt, pkt2, pkt2, pkt]) assert p.packets == [pkt, pkt2, pkt2, pkt] assert p.encode() == '4abc\x1ebAwQFBg==\x1ebAwQFBg==\x1e4abc'
def test_encode_payload_binary_multiple(self): pkt = packet.Packet(packet.MESSAGE, data=b'\x00\x01\x02') pkt2 = packet.Packet(packet.MESSAGE, data=b'\x03\x04\x05\x06') p = payload.Payload([pkt, pkt2]) assert p.packets == [pkt, pkt2] assert p.encode() == 'bAAEC\x1ebAwQFBg=='
def test_encode_payload_xhr_binary(self): pkt = packet.Packet(packet.MESSAGE, data=b'\x00\x01\x02', binary=True) p = payload.Payload([pkt]) self.assertEqual(p.packets, [pkt]) self.assertEqual(p.encode(b64=True), b'6:b4AAEC')
def test_decode_empty_payload(self): p = payload.Payload(encoded_payload='') assert p.encode() == ''
def test_decode_payload_xhr_binary(self): p = payload.Payload(encoded_payload=b'6:b4AAEC') self.assertEqual(p.encode(), b'\x01\x04\xff\x04\x00\x01\x02')
def test_decode_single_encoded_utf8_payload(self): p = payload.Payload(encoded_payload=b'3:4\xc3\xa9') self.assertEqual(len(p.packets), 1) self.assertEqual(p.packets[0].data.encode('utf-8'), b'\xc3\xa9')
def test_encode_empty_payload(self): p = payload.Payload() self.assertEqual(p.packets, []) self.assertEqual(p.encode(), b'')
def test_decode_double_encoded_utf8_multi_payload(self): p = payload.Payload(encoded_payload=b'3:4\xc3\x83\xc2\xa94:4abc') self.assertEqual(len(p.packets), 2) self.assertEqual(p.packets[0].data.encode('utf-8'), b'\xc3\xa9') self.assertEqual(p.packets[1].data, 'abc')
def test_decode_multi_payload_with_too_many_packets(self): with pytest.raises(ValueError): payload.Payload(encoded_payload='4abc\x1e4def\x1e' * 9 + '6')
def test_encode_payload_text_multiple(self): pkt = packet.Packet(packet.MESSAGE, data='abc') pkt2 = packet.Packet(packet.MESSAGE, data='def') p = payload.Payload([pkt, pkt2]) assert p.packets == [pkt, pkt2] assert p.encode() == '4abc\x1e4def'