Skip to content

Commit

Permalink
Support binary data in the test client
Browse files Browse the repository at this point in the history
Fixes #601
  • Loading branch information
miguelgrinberg committed Dec 8, 2017
1 parent 96cf8ee commit ad0001d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 7 additions & 2 deletions flask_socketio/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,15 @@ def emit(self, event, *args, **kwargs):
self.callback_counter += 1
id = self.callback_counter
pkt = packet.Packet(packet.EVENT, data=[event] + list(args),
namespace=namespace, id=id, binary=False)
namespace=namespace, id=id)
self.ack = None
with self.app.app_context():
self.socketio.server._handle_eio_message(self.sid, pkt.encode())
encoded_pkt = pkt.encode()
if isinstance(encoded_pkt, list):
for epkt in encoded_pkt:
self.socketio.server._handle_eio_message(self.sid, epkt)
else:
self.socketio.server._handle_eio_message(self.sid, encoded_pkt)
if self.ack is not None:
return self.ack['args'][0] if len(self.ack['args']) == 1 \
else self.ack['args']
Expand Down
10 changes: 10 additions & 0 deletions test_socketio.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,16 @@ def test_emit(self):
self.assertEqual(received[0]['name'], 'my custom response')
self.assertEqual(received[0]['args'][0]['a'], 'b')

def test_emit_binary(self):
client = socketio.test_client(app)
client.get_received()
client.emit('my custom event', {u'a': b'\x01\x02\x03'})
received = client.get_received()
self.assertEqual(len(received), 1)
self.assertEqual(len(received[0]['args']), 1)
self.assertEqual(received[0]['name'], 'my custom response')
self.assertEqual(received[0]['args'][0]['a'], b'\x01\x02\x03')

def test_request_event_data(self):
client = socketio.test_client(app)
client.get_received()
Expand Down

0 comments on commit ad0001d

Please sign in to comment.