diff --git a/socketio/packet.py b/socketio/packet.py index 570708b..e1e080b 100644 --- a/socketio/packet.py +++ b/socketio/packet.py @@ -88,9 +88,9 @@ class Packet(object): self.namespace = None self.data = None ep = ep[1:] - dash = (ep + '-').find('-') + dash = ep.find('-') attachment_count = 0 - if ep[0:dash].isdigit(): + if dash > 0 and ep[0:dash].isdigit(): attachment_count = int(ep[0:dash]) ep = ep[dash + 1:] if ep and ep[0:1] == '/': @@ -106,7 +106,7 @@ class Packet(object): self.namespace = self.namespace[0:q] if ep and ep[0].isdigit(): self.id = 0 - while ep[0].isdigit(): + while ep and ep[0].isdigit(): self.id = self.id * 10 + int(ep[0]) ep = ep[1:] if ep: diff --git a/tests/test_packet.py b/tests/test_packet.py index 9602789..90044bb 100644 --- a/tests/test_packet.py +++ b/tests/test_packet.py @@ -146,6 +146,18 @@ class TestPacket(unittest.TestCase): self.assertEqual(pkt.id, 123) self.assertEqual(pkt.encode(), '2123["foo"]') + 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_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_encode_namespace_and_id(self): pkt = packet.Packet(packet_type=packet.EVENT, data=[six.text_type('foo')], namespace='/bar',