File: test_packet.py

package info (click to toggle)
python-engineio 4.12.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 944 kB
  • sloc: python: 10,594; makefile: 15; sh: 6
file content (142 lines) | stat: -rw-r--r-- 5,080 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import pytest

from engineio import packet


class TestPacket:
    def test_encode_default_packet(self):
        pkt = packet.Packet()
        assert pkt.packet_type == packet.NOOP
        assert pkt.data is None
        assert not pkt.binary
        assert pkt.encode() == '6'

    def test_decode_default_packet(self):
        pkt = packet.Packet(encoded_packet='6')
        assert pkt.encode() == '6'

    def test_encode_text_packet(self):
        data = 'text'
        pkt = packet.Packet(packet.MESSAGE, data=data)
        assert pkt.packet_type == packet.MESSAGE
        assert pkt.data == data
        assert not pkt.binary
        assert pkt.encode() == '4text'

    def test_decode_text_packet(self):
        pkt = packet.Packet(encoded_packet=b'4text')
        assert pkt.encode() == b'4text'

    def test_encode_empty_text_packet(self):
        data = ''
        pkt = packet.Packet(packet.MESSAGE, data=data)
        assert pkt.packet_type == packet.MESSAGE
        assert pkt.data == data
        assert not pkt.binary
        assert pkt.encode() == '4'

    def test_decode_empty_text_packet(self):
        pkt = packet.Packet(encoded_packet=b'4')
        assert pkt.encode() == b'4'

    def test_encode_binary_packet(self):
        pkt = packet.Packet(packet.MESSAGE, data=b'\x01\x02\x03')
        assert pkt.packet_type == packet.MESSAGE
        assert pkt.data == b'\x01\x02\x03'
        assert pkt.binary
        assert pkt.encode() == b'\x01\x02\x03'

    def test_encode_binary_bytearray_packet(self):
        pkt = packet.Packet(packet.MESSAGE, data=bytearray(b'\x01\x02\x03'))
        assert pkt.packet_type == packet.MESSAGE
        assert pkt.data == b'\x01\x02\x03'
        assert pkt.binary
        assert pkt.encode() == b'\x01\x02\x03'

    def test_encode_binary_b64_packet(self):
        pkt = packet.Packet(packet.MESSAGE, data=b'\x01\x02\x03\x04')
        assert pkt.packet_type == packet.MESSAGE
        assert pkt.data == b'\x01\x02\x03\x04'
        assert pkt.binary
        assert pkt.encode(b64=True) == 'bAQIDBA=='

    def test_encode_empty_binary_packet(self):
        pkt = packet.Packet(packet.MESSAGE, data=b'')
        assert pkt.packet_type == packet.MESSAGE
        assert pkt.data == b''
        assert pkt.binary
        assert pkt.encode() == b''

    def test_decode_binary_packet(self):
        pkt = packet.Packet(encoded_packet=b'\x04\x01\x02\x03')
        assert pkt.encode() == b'\x04\x01\x02\x03'

    def test_decode_binary_bytearray_packet(self):
        pkt = packet.Packet(encoded_packet=bytearray(b'\x04\x01\x02\x03'))
        assert pkt.encode() == b'\x04\x01\x02\x03'

    def test_decode_binary_b64_packet(self):
        pkt = packet.Packet(encoded_packet='bBAECAw==')
        assert pkt.encode() == b'\x04\x01\x02\x03'

    def test_decode_empty_binary_packet(self):
        pkt = packet.Packet(encoded_packet=b'')
        assert pkt.encode() == b''

    def test_encode_json_packet(self):
        pkt = packet.Packet(packet.MESSAGE, data={'a': 123, 'b': '456'})
        assert pkt.packet_type == packet.MESSAGE
        assert pkt.data == {'a': 123, 'b': '456'}
        assert not pkt.binary
        assert pkt.encode() in [
            '4{"a":123,"b":"456"}',
            '4{"b":"456","a":123}',
        ]

    def test_decode_json_packet(self):
        pkt = packet.Packet(encoded_packet='4{"a":123,"b":"456"}')
        assert pkt.encode() in [
            '4{"a":123,"b":"456"}',
            '4{"b":"456","a":123}',
        ]

    def test_decode_json_packet_long_int(self):
        pkt = packet.Packet(encoded_packet='4{"a":' + '1' * 100 + '}')
        assert pkt.packet_type == packet.MESSAGE
        assert pkt.data == {'a': int('1' * 100)}
        pkt = packet.Packet(encoded_packet='4{"a":' + '1' * 101 + '}')
        assert pkt.packet_type == packet.MESSAGE
        assert pkt.data == '{"a":' + '1' * 101 + '}'

    def test_encode_number_packet(self):
        pkt = packet.Packet(packet.MESSAGE, data=123)
        assert pkt.packet_type == packet.MESSAGE
        assert pkt.data == 123
        assert not pkt.binary
        assert pkt.encode() == '4123'

    def test_decode_number_packet(self):
        pkt = packet.Packet(encoded_packet='4123')
        assert pkt.packet_type == packet.MESSAGE
        # integer payloads are parsed as strings, see
        # https://github.com/miguelgrinberg/python-engineio/issues/75
        # for background on this decision
        assert pkt.data == '123'
        assert not pkt.binary
        assert pkt.encode() == '4123'

    def test_binary_non_message_packet(self):
        with pytest.raises(ValueError):
            packet.Packet(packet.NOOP, b'\x01\x02\x03')

    def test_decode_invalid_empty_text_packet(self):
        with pytest.raises(ValueError):
            packet.Packet(encoded_packet='')

    def test_encode_cache(self):
        pkt = packet.Packet(packet.MESSAGE, data=123)
        assert pkt.encode() == '4123'
        pkt.data = 456
        assert pkt.encode() == '4123'
        pkt.encode_cache = None
        assert pkt.encode() == '4456'