def test_decode_text_event_packet(self): pkt = packet.Packet(encoded_packet='2["foo"]') assert pkt.packet_type == packet.EVENT assert pkt.data == ['foo'] assert pkt.encode() == '2["foo"]'
def test_data_is_binary_list(self): pkt = packet.Packet() self.assertFalse(pkt._data_is_binary([six.text_type('foo')])) self.assertFalse(pkt._data_is_binary([])) self.assertTrue(pkt._data_is_binary([b'foo'])) self.assertTrue(pkt._data_is_binary([six.text_type('foo'), b'bar']))
def test_decode_text_event_packet(self): pkt = packet.Packet(encoded_packet='2["foo"]') self.assertEqual(pkt.packet_type, packet.EVENT) self.assertEqual(pkt.data, ['foo']) self.assertEqual(pkt.encode(), '2["foo"]')
def test_encode_id(self): pkt = packet.Packet(packet_type=packet.EVENT, data=[six.text_type('foo')], id=123) self.assertEqual(pkt.id, 123) self.assertEqual(pkt.encode(), '2123["foo"]')
def test_decode_namespace_and_id(self): pkt = packet.Packet(encoded_packet='2/bar,123["foo"]') self.assertEqual(pkt.namespace, '/bar') self.assertEqual(pkt.id, 123) self.assertEqual(pkt.encode(), '2/bar,123["foo"]')
def test_encode_namespace_with_hyphens(self): pkt = packet.Packet(packet_type=packet.EVENT, data=[six.text_type('foo')], namespace='/b-a-r') self.assertEqual(pkt.namespace, '/b-a-r') self.assertEqual(pkt.encode(), '2/b-a-r,["foo"]')
def test_encode_event_with_hyphens(self): pkt = packet.Packet(packet_type=packet.EVENT, data=[six.text_type('f-o-o')]) self.assertEqual(pkt.namespace, None) self.assertEqual(pkt.encode(), '2["f-o-o"]')
def test_decode_namespace(self): pkt = packet.Packet(encoded_packet='2/bar,["foo"]') assert pkt.namespace == '/bar' assert pkt.encode() == '2/bar,["foo"]'
def test_send_packet_default_binary_py3(self): c = client.Client() c.eio.send = mock.MagicMock() c._send_packet(packet.Packet(packet.EVENT, 'foo')) c.eio.send.assert_called_once_with('2"foo"', binary=False)
def test_decode_binary_ack_packet(self): pkt = packet.Packet(encoded_packet='61-{"_placeholder":true,"num":0}') assert pkt.add_attachment(b'1234') assert pkt.packet_type == packet.BINARY_ACK assert pkt.data == b'1234'
def test_invalid_binary_packet(self): with pytest.raises(ValueError): packet.Packet(packet_type=packet.CONNECT_ERROR, data=b'123')
def test_decode_text_ack_packet(self): pkt = packet.Packet(encoded_packet='3["foo"]') assert pkt.packet_type == packet.ACK assert pkt.data == ['foo'] assert pkt.encode() == '3["foo"]'
def test_encode_text_ack_packet(self): pkt = packet.Packet(packet_type=packet.ACK, data=['foo']) assert pkt.packet_type == packet.ACK assert pkt.data == ['foo'] assert pkt.encode() == '3["foo"]'
def test_decode_empty_event_packet(self): pkt = packet.Packet(encoded_packet='1') assert pkt.packet_type == packet.DISCONNECT # same thing, but with a numeric payload pkt = packet.Packet(encoded_packet=1) assert pkt.packet_type == packet.DISCONNECT
def test_decode_binary_ack_packet(self): pkt = packet.Packet(encoded_packet='61-{"_placeholder":true,"num":0}') self.assertTrue(pkt.add_attachment(b'1234')) self.assertEqual(pkt.packet_type, packet.BINARY_ACK) self.assertEqual(pkt.data, b'1234')
def test_send_packet_default_binary_py3(self): c = asyncio_client.AsyncClient() c.eio.send = AsyncMock() _run(c._send_packet(packet.Packet(packet.EVENT, 'foo'))) c.eio.send.mock.assert_called_once_with('2"foo"', binary=False)
def test_decode_namespace_no_data(self): pkt = packet.Packet(encoded_packet='2/bar') self.assertEqual(pkt.namespace, '/bar') self.assertEqual(pkt.encode(), '2/bar')
def test_decode_namespace_with_query_string(self): # some Socket.IO clients mistakenly attach the query string to the # namespace pkt = packet.Packet(encoded_packet='2/bar?a=b,["foo"]') self.assertEqual(pkt.namespace, '/bar') self.assertEqual(pkt.encode(), '2/bar,["foo"]')
def test_decode_namespace_with_hyphens(self): pkt = packet.Packet(encoded_packet='2/b-a-r,["foo"]') self.assertEqual(pkt.namespace, '/b-a-r') self.assertEqual(pkt.encode(), '2/b-a-r,["foo"]')
def test_encode_namespace_no_data(self): pkt = packet.Packet(packet_type=packet.EVENT, namespace='/bar') self.assertEqual(pkt.encode(), '2/bar')
def test_decode_event_with_hyphens(self): pkt = packet.Packet(encoded_packet='2["f-o-o"]') self.assertEqual(pkt.namespace, None) self.assertEqual(pkt.encode(), '2["f-o-o"]')
def test_encode_id_no_data(self): pkt = packet.Packet(packet_type=packet.EVENT, id=123) self.assertEqual(pkt.id, 123) self.assertIsNone(pkt.data) self.assertEqual(pkt.encode(), '2123')
def test_decode_id(self): pkt = packet.Packet(encoded_packet='2123["foo"]') self.assertEqual(pkt.id, 123) self.assertEqual(pkt.encode(), '2123["foo"]')
def test_decode_id_no_data(self): pkt = packet.Packet(encoded_packet='2123') self.assertEqual(pkt.id, 123) self.assertIsNone(pkt.data) self.assertEqual(pkt.encode(), '2123')
def test_decode_default_packet(self): pkt = packet.Packet(encoded_packet='2') self.assertTrue(pkt.encode(), '2')
def test_encode_text_ack_packet(self): pkt = packet.Packet(packet_type=packet.ACK, data=[six.text_type('foo')]) self.assertEqual(pkt.packet_type, packet.ACK) self.assertEqual(pkt.data, ['foo']) self.assertEqual(pkt.encode(), '3["foo"]')
def test_encode_text_event_packet(self): pkt = packet.Packet(packet_type=packet.EVENT, data=[six.text_type('foo')]) self.assertEqual(pkt.packet_type, packet.EVENT) self.assertEqual(pkt.data, ['foo']) self.assertEqual(pkt.encode(), '2["foo"]')
def test_decode_text_ack_packet(self): pkt = packet.Packet(encoded_packet='3["foo"]') self.assertEqual(pkt.packet_type, packet.ACK) self.assertEqual(pkt.data, ['foo']) self.assertEqual(pkt.encode(), '3["foo"]')
def test_decode_empty_event_packet(self): pkt = packet.Packet(encoded_packet='1') self.assertEqual(pkt.packet_type, packet.DISCONNECT) # same thing, but with a numeric payload pkt = packet.Packet(encoded_packet=1) self.assertEqual(pkt.packet_type, packet.DISCONNECT)
def test_data_is_binary_dict(self): pkt = packet.Packet() assert not pkt._data_is_binary({'a': 'foo'}) assert not pkt._data_is_binary({}) assert pkt._data_is_binary({'a': b'foo'}) assert pkt._data_is_binary({'a': 'foo', 'b': b'bar'})