Skip to content

Commit

Permalink
Test client: pass packets through an encode/decode cycle (Fixes #1427)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Dec 13, 2020
1 parent 802e44b commit ffe66fe
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
8 changes: 8 additions & 0 deletions flask_socketio/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ class SocketIOTestClient(object):
def __init__(self, app, socketio, namespace=None, query_string=None,
headers=None, flask_test_client=None):
def _mock_send_packet(eio_sid, pkt):
# make sure the packet can be encoded and decoded
epkt = pkt.encode()
if not isinstance(epkt, list):
pkt = packet.Packet(encoded_packet=epkt)
else:
pkt = packet.Packet(encoded_packet=epkt[0])
for att in epkt[1:]:
pkt.add_attachment(att)
if pkt.packet_type == packet.EVENT or \
pkt.packet_type == packet.BINARY_EVENT:
if eio_sid not in self.queue:
Expand Down
56 changes: 50 additions & 6 deletions test_socketio.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,32 @@ def on_room_namespace_event(data):
send('room message', room=room)


@socketio.on('bad response')
def on_bad_response():
emit('my custom response', {'foo': socketio})


@socketio.on('bad callback')
def on_bad_callback():
return {'foo': socketio}


@socketio.on('changing response')
def on_changing_response():
data = {'foo': 'bar'}
emit('my custom response', data)
data['foo'] = 'baz'
return data


@socketio.on_error()
def error_handler(value):
if isinstance(value, AssertionError):
global error_testing
error_testing = True
else:
raise value
return value
return 'error'


@socketio.on('error testing')
Expand All @@ -173,7 +191,7 @@ def error_handler_namespace(value):
error_testing_namespace = True
else:
raise value
return value
return 'error/test'


@socketio.on("error testing", namespace='/test')
Expand All @@ -188,7 +206,7 @@ def error_handler_default(value):
error_testing_default = True
else:
raise value
return value
return 'error/default'


@socketio.on("error testing", namespace='/unused_namespace')
Expand Down Expand Up @@ -554,14 +572,14 @@ def test_error_handling_ack(self):
client2 = socketio.test_client(app, namespace='/test')
client3 = socketio.test_client(app, namespace='/unused_namespace')
errorack = client1.emit("error testing", "", callback=True)
self.assertIsNotNone(errorack)
self.assertEqual(errorack, 'error')
errorack_namespace = client2.emit("error testing", "",
namespace='/test', callback=True)
self.assertIsNotNone(errorack_namespace)
self.assertEqual(errorack_namespace, 'error/test')
errorack_default = client3.emit("error testing", "",
namespace='/unused_namespace',
callback=True)
self.assertIsNotNone(errorack_default)
self.assertEqual(errorack_default, 'error/default')

def test_on_event(self):
client = socketio.test_client(app)
Expand Down Expand Up @@ -671,6 +689,32 @@ def on_connect():
self.assertEqual(len(received), 1)
self.assertEqual(received[0]['args'], {'connected': 'foo'})

def test_encode_decode(self):
client = socketio.test_client(app)
client.get_received()
data = {'foo': 'bar', 'invalid': socketio}
self.assertRaises(TypeError, client.emit, 'my custom event', data,
callback=True)
data = {'foo': 'bar'}
ack = client.emit('my custom event', data, callback=True)
data['foo'] = 'baz'
received = client.get_received()
self.assertEqual(ack, {'foo': 'bar'})
self.assertEqual(len(received), 1)
self.assertEqual(received[0]['args'][0], {'foo': 'bar'})

def test_encode_decode_2(self):
client = socketio.test_client(app)
self.assertRaises(TypeError, client.emit, 'bad response')
self.assertRaises(TypeError, client.emit, 'bad callback',
callback=True)
client.get_received()
ack = client.emit('changing response', callback=True)
received = client.get_received()
self.assertEqual(len(received), 1)
self.assertEqual(received[0]['args'][0], {'foo': 'bar'})
self.assertEqual(ack, {'foo': 'baz'})


if __name__ == '__main__':
unittest.main()

0 comments on commit ffe66fe

Please sign in to comment.