From d31d167c78203732c74a97f5a98788462231e01f Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Fri, 30 Oct 2015 13:56:59 -0700 Subject: [PATCH] support packets with empty payload given as an integer --- socketio/packet.py | 6 +++++- tests/test_packet.py | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/socketio/packet.py b/socketio/packet.py index 642e179..9877a22 100644 --- a/socketio/packet.py +++ b/socketio/packet.py @@ -69,7 +69,11 @@ class Packet(object): necessary to fully decode the packet. """ ep = encoded_packet - self.packet_type = int(ep[0:1]) + try: + self.packet_type = int(ep[0:1]) + except TypeError: + self.packet_type = ep + ep = '' self.namespace = None self.data = None ep = ep[1:] diff --git a/tests/test_packet.py b/tests/test_packet.py index 511cee1..af77e8d 100644 --- a/tests/test_packet.py +++ b/tests/test_packet.py @@ -32,6 +32,13 @@ class TestPacket(unittest.TestCase): self.assertEqual(pkt.data, ['foo']) self.assertEqual(pkt.encode(), '2["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_encode_binary_event_packet(self): pkt = packet.Packet(packet_type=packet.EVENT, data=b'1234') self.assertEqual(pkt.packet_type, packet.BINARY_EVENT)