File: test_messaging.py

package info (click to toggle)
python-ws4py 0.4.2%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 688 kB
  • sloc: python: 4,500; makefile: 140; javascript: 96
file content (128 lines) | stat: -rw-r--r-- 4,840 bytes parent folder | download | duplicates (5)
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
# -*- coding: utf-8 -*-
import os
import unittest

from ws4py.framing import Frame, \
     OPCODE_CONTINUATION, OPCODE_TEXT, \
     OPCODE_BINARY, OPCODE_CLOSE, OPCODE_PING, OPCODE_PONG
from ws4py.messaging import *
from ws4py.compat import *

class WSMessagingTest(unittest.TestCase):
    def test_bytearray_text_message(self):
        m = TextMessage(bytearray(u'\xe9trange', 'utf-8'))
        self.assertFalse(m.is_binary)
        self.assertTrue(m.is_text)
        self.assertEqual(m.opcode, OPCODE_TEXT)
        self.assertEqual(m.encoding, 'utf-8')
        self.assertIsInstance(m.data, bytes)
        # length is compted on the unicode representation
        self.assertEqual(len(m), 7)
        # but once encoded it's actually taking 8 bytes in UTF-8
        self.assertEqual(len(m.data), 8)
        self.assertEqual(m.data, u'\xe9trange'.encode('utf-8'))

        f = m.single()
        self.assertIsInstance(f, bytes)
        self.assertEqual(len(f), 10)  # no masking

        f = m.single(mask=True)
        self.assertIsInstance(f, bytes)
        self.assertEqual(len(f), 14)  # mask takes 4 bytes

        self.assertEqual(m.fragment(first=True, last=True), m.single())

        m.extend(bytearray(' oui', 'utf-8'))
        self.assertEqual(m.data, u'\xe9trange oui'.encode('utf-8'))

    def test_bytes_text_message(self):
        m = TextMessage(u'\xe9trange'.encode('utf-8'))
        self.assertEqual(m.opcode, OPCODE_TEXT)
        self.assertEqual(m.encoding, 'utf-8')
        self.assertIsInstance(m.data, bytes)
        self.assertFalse(m.is_binary)
        self.assertTrue(m.is_text)
        # length is compted on the unicode representation
        self.assertEqual(len(m), 7)
        # but once encoded it's actually taking 8 bytes in UTF-8
        self.assertEqual(len(m.data), 8)
        self.assertEqual(m.data, u'\xe9trange'.encode('utf-8'))

        f = m.single()
        self.assertIsInstance(f, bytes)
        self.assertEqual(len(f), 10)  # no masking

        f = m.single(mask=True)
        self.assertIsInstance(f, bytes)
        self.assertEqual(len(f), 14)  # mask takes 4 bytes

        self.assertEqual(m.fragment(first=True, last=True), m.single())

        m.extend(b' oui')
        self.assertEqual(m.data, u'\xe9trange oui'.encode('utf-8'))

    def test_unicode_text_message(self):
        m = TextMessage(u'\xe9trange')
        self.assertEqual(m.opcode, OPCODE_TEXT)
        self.assertEqual(m.encoding, 'utf-8')
        self.assertIsInstance(m.data, bytes)
        self.assertFalse(m.is_binary)
        self.assertTrue(m.is_text)
        # length is compted on the unicode representation
        self.assertEqual(len(m), 7)
        # but once encoded it's actually taking 8 bytes in UTF-8
        self.assertEqual(len(m.data), 8)
        self.assertEqual(m.data, u'\xe9trange'.encode('utf-8'))

        f = m.single()
        self.assertIsInstance(f, bytes)
        self.assertEqual(len(f), 10)  # no masking

        f = m.single(mask=True)
        self.assertIsInstance(f, bytes)
        self.assertEqual(len(f), 14)  # mask takes 4 bytes

        self.assertEqual(m.fragment(first=True, last=True), m.single())

        m.extend(u' oui')
        self.assertEqual(m.data, u'\xe9trange oui'.encode('utf-8'))

    def test_unicode_text_message_with_no_encoding(self):
        self.assertRaises(TypeError, Message, OPCODE_TEXT, u'\xe9trange', encoding=None)

    def test_invalid_text_message_data_type(self):
        self.assertRaises(TypeError, TextMessage, ['something else'])
        m = TextMessage(u'\xe9trange')
        self.assertRaises(TypeError, m.extend, ["list aren't supported types"])

    def test_close_control_message(self):
        m = CloseControlMessage(reason=u'bye bye')
        self.assertEqual(m.opcode, OPCODE_CLOSE)
        self.assertEqual(m.encoding, 'utf-8')
        self.assertIsInstance(m.reason, bytes)
        self.assertEqual(len(m), 7)
        self.assertEqual(m.code, 1000)
        self.assertEqual(m.reason, b'bye bye')
        
    def test_ping_control_message(self):
        m = PingControlMessage(data=u'are you there?')
        self.assertEqual(m.opcode, OPCODE_PING)
        self.assertEqual(m.encoding, 'utf-8')
        self.assertIsInstance(m.data, bytes)
        self.assertEqual(len(m), 14)
        
    def test_pong_control_message(self):
        m = PongControlMessage(data=u'yes, I am')
        self.assertEqual(m.opcode, OPCODE_PONG)
        self.assertEqual(m.encoding, 'utf-8')
        self.assertIsInstance(m.data, bytes)
        self.assertEqual(len(m), 9)
        

if __name__ == '__main__':
    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    for testcase in [WSMessagingTest]:
        tests = loader.loadTestsFromTestCase(testcase)
        suite.addTests(tests)
    unittest.TextTestRunner(verbosity=2).run(suite)