From 378eeb55f3ee47f3389993bf495d17ed9da3f85b Mon Sep 17 00:00:00 2001 From: Aleksandr Kurlov Date: Mon, 23 Jan 2017 04:02:15 +0500 Subject: [PATCH 1/2] Fixed hyphens namcespace bug --- socketio/packet.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/socketio/packet.py b/socketio/packet.py index 2834aee..70a403c 100644 --- a/socketio/packet.py +++ b/socketio/packet.py @@ -78,12 +78,21 @@ class Packet(object): self.namespace = None self.data = None ep = ep[1:] - dash = (ep + '-').find('-') - comma = (ep + ',').find(',') attachment_count = 0 - if dash < comma: - attachment_count = int(ep[0:dash]) - ep = ep[dash + 1:] + if ep and ep[0].isdigit(): + # build number so it's either attachment count or id + ind = 0 + ep_len = len(ep) + while ind < ep_len and ep[ind].isdigit(): + ind += 1 + num = int(ep[0:ind]) + ep = ep[ind:] + if ep: + if ep[0] == '-': + attachment_count = num + ep = ep[1:] + else: + self.id = num if ep and ep[0:1] == '/': sep = ep.find(',') if sep == -1: From c091a177955931a92eb6aa6c853561028872a449 Mon Sep 17 00:00:00 2001 From: Aleksandr Kurlov Date: Mon, 23 Jan 2017 04:11:14 +0500 Subject: [PATCH 2/2] Added test cases for namespace with hyphens --- tests/test_packet.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/test_packet.py b/tests/test_packet.py index 039c2a5..c89ef94 100644 --- a/tests/test_packet.py +++ b/tests/test_packet.py @@ -106,6 +106,17 @@ class TestPacket(unittest.TestCase): self.assertEqual(pkt.namespace, '/bar') self.assertEqual(pkt.encode(), '2/bar') + def test_encode_namespace_with_hyphens(self): + pkt = packet.Packet(packet_type=packet.EVENT, + data=[six.text_type('foo')], namespace='/b-a-r') + self.assertEqual(pkt.namespace, '/b-a-r') + self.assertEqual(pkt.encode(), '2/b-a-r,["foo"]') + + def test_decode_namespace_with_hyphens(self): + pkt = packet.Packet(encoded_packet='2/b-a-r,["foo"]') + self.assertEqual(pkt.namespace, '/b-a-r') + self.assertEqual(pkt.encode(), '2/b-a-r,["foo"]') + def test_encode_id(self): pkt = packet.Packet(packet_type=packet.EVENT, data=[six.text_type('foo')], id=123)