Skip to content

Commit

Permalink
send binary packets as such in the sync client (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed May 29, 2019
1 parent 612815f commit 076232e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
6 changes: 5 additions & 1 deletion engineio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,11 @@ def _write_loop(self):
# websocket
try:
for pkt in packets:
self.ws.send(pkt.encode())
encoded_packet = pkt.encode(always_bytes=False)
if pkt.binary:
self.ws.send_binary(encoded_packet)
else:
self.ws.send(encoded_packet)
self.queue.task_done()
except websocket.WebSocketConnectionClosedException:
self.logger.warning(
Expand Down
22 changes: 22 additions & 0 deletions tests/asyncio/test_asyncio_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,28 @@ def test_write_loop_websocket_three_packets(self):
self.assertEqual(c.ws.send.mock.call_args_list[1][0][0], '2')
self.assertEqual(c.ws.send.mock.call_args_list[2][0][0], '6')

def test_write_loop_websocket_one_packet_binary(self):
c = asyncio_client.AsyncClient()
c.state = 'connected'
c.ping_interval = 1
c.ping_timeout = 2
c.current_transport = 'websocket'
c.queue = mock.MagicMock()
c.queue.Empty = RuntimeError
c.queue.get = AsyncMock(side_effect=[
packet.Packet(packet.MESSAGE, b'foo'),
RuntimeError
])
c.queue.get_nowait = mock.MagicMock(side_effect=[
RuntimeError
])
c.ws = mock.MagicMock()
c.ws.send = AsyncMock()
_run(c._write_loop())
self.assertEqual(c.queue.task_done.call_count, 1)
self.assertEqual(c.ws.send.mock.call_count, 1)
c.ws.send.mock.assert_called_once_with(b'\x04foo')

def test_write_loop_websocket_bad_connection(self):
c = asyncio_client.AsyncClient()
c.state = 'connected'
Expand Down
30 changes: 26 additions & 4 deletions tests/common/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,8 @@ def test_write_loop_websocket_one_packet(self):
c._write_loop()
self.assertEqual(c.queue.task_done.call_count, 1)
self.assertEqual(c.ws.send.call_count, 1)
c.ws.send.assert_called_once_with(b'4{"foo":"bar"}')
self.assertEqual(c.ws.send_binary.call_count, 0)
c.ws.send.assert_called_once_with('4{"foo":"bar"}')

def test_write_loop_websocket_three_packets(self):
c = client.Client()
Expand All @@ -1099,9 +1100,30 @@ def test_write_loop_websocket_three_packets(self):
c._write_loop()
self.assertEqual(c.queue.task_done.call_count, 3)
self.assertEqual(c.ws.send.call_count, 3)
self.assertEqual(c.ws.send.call_args_list[0][0][0], b'4{"foo":"bar"}')
self.assertEqual(c.ws.send.call_args_list[1][0][0], b'2')
self.assertEqual(c.ws.send.call_args_list[2][0][0], b'6')
self.assertEqual(c.ws.send_binary.call_count, 0)
self.assertEqual(c.ws.send.call_args_list[0][0][0], '4{"foo":"bar"}')
self.assertEqual(c.ws.send.call_args_list[1][0][0], '2')
self.assertEqual(c.ws.send.call_args_list[2][0][0], '6')

def test_write_loop_websocket_one_packet_binary(self):
c = client.Client()
c.state = 'connected'
c.ping_interval = 1
c.ping_timeout = 2
c.current_transport = 'websocket'
c.queue = mock.MagicMock()
c.queue.Empty = RuntimeError
c.queue.get.side_effect = [
packet.Packet(packet.MESSAGE, b'foo'),
RuntimeError,
RuntimeError
]
c.ws = mock.MagicMock()
c._write_loop()
self.assertEqual(c.queue.task_done.call_count, 1)
self.assertEqual(c.ws.send.call_count, 0)
self.assertEqual(c.ws.send_binary.call_count, 1)
c.ws.send_binary.assert_called_once_with(b'\x04foo')

def test_write_loop_websocket_bad_connection(self):
c = client.Client()
Expand Down

0 comments on commit 076232e

Please sign in to comment.