File: test_payload.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 (66 lines) | stat: -rw-r--r-- 2,418 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
import pytest

from engineio import packet
from engineio import payload


class TestPayload:
    def test_encode_empty_payload(self):
        p = payload.Payload()
        assert p.packets == []
        assert p.encode() == ''

    def test_decode_empty_payload(self):
        p = payload.Payload(encoded_payload='')
        assert p.encode() == ''

    def test_encode_payload_text(self):
        pkt = packet.Packet(packet.MESSAGE, data='abc')
        p = payload.Payload([pkt])
        assert p.packets == [pkt]
        assert p.encode() == '4abc'

    def test_encode_payload_text_multiple(self):
        pkt = packet.Packet(packet.MESSAGE, data='abc')
        pkt2 = packet.Packet(packet.MESSAGE, data='def')
        p = payload.Payload([pkt, pkt2])
        assert p.packets == [pkt, pkt2]
        assert p.encode() == '4abc\x1e4def'

    def test_encode_payload_binary(self):
        pkt = packet.Packet(packet.MESSAGE, data=b'\x00\x01\x02')
        p = payload.Payload([pkt])
        assert p.packets == [pkt]
        assert p.encode() == 'bAAEC'

    def test_encode_payload_binary_multiple(self):
        pkt = packet.Packet(packet.MESSAGE, data=b'\x00\x01\x02')
        pkt2 = packet.Packet(packet.MESSAGE, data=b'\x03\x04\x05\x06')
        p = payload.Payload([pkt, pkt2])
        assert p.packets == [pkt, pkt2]
        assert p.encode() == 'bAAEC\x1ebAwQFBg=='

    def test_encode_payload_text_binary_multiple(self):
        pkt = packet.Packet(packet.MESSAGE, data='abc')
        pkt2 = packet.Packet(packet.MESSAGE, data=b'\x03\x04\x05\x06')
        p = payload.Payload([pkt, pkt2, pkt2, pkt])
        assert p.packets == [pkt, pkt2, pkt2, pkt]
        assert p.encode() == '4abc\x1ebAwQFBg==\x1ebAwQFBg==\x1e4abc'

    def test_encode_jsonp_payload(self):
        pkt = packet.Packet(packet.MESSAGE, data='abc')
        p = payload.Payload([pkt])
        assert p.packets == [pkt]
        assert p.encode(jsonp_index=233) == '___eio[233]("4abc");'

    def test_decode_jsonp_payload(self):
        p = payload.Payload(encoded_payload='d=4abc')
        assert p.encode() == '4abc'

    def test_decode_invalid_payload(self):
        with pytest.raises(ValueError):
            payload.Payload(encoded_payload='bad payload')

    def test_decode_multi_payload_with_too_many_packets(self):
        with pytest.raises(ValueError):
            payload.Payload(encoded_payload='4abc\x1e4def\x1e' * 9 + '6')