def test_63_bit_length(self): f = Frame(opcode=OPCODE_TEXT, body=b"*" * 65536, fin=1) self.assertEqual(len(f.build()), 65546) mask = os.urandom(4) f = Frame(opcode=OPCODE_TEXT, body=b"*" * 65536, masking_key=mask, fin=1) self.assertEqual(len(f.build()), 65550)
def test_63_bit_length(self): f = Frame(opcode=OPCODE_TEXT, body=b'*' * 65536, fin=1) self.assertEqual(len(f.build()), 65546) mask = os.urandom(4) f = Frame(opcode=OPCODE_TEXT, body=b'*' * 65536, masking_key=mask, fin=1) self.assertEqual(len(f.build()), 65550)
def test_7_bit_length(self): f = Frame(opcode=OPCODE_TEXT, body=b'', fin=1) self.assertEqual(len(f.build()), 2) f = Frame(opcode=OPCODE_TEXT, body=b'*' * 125, fin=1) self.assertEqual(len(f.build()), 127) mask = os.urandom(4) f = Frame(opcode=OPCODE_TEXT, body=b'', masking_key=mask, fin=1) self.assertEqual(len(f.build()), 6) f = Frame(opcode=OPCODE_TEXT, body=b'*' * 125, masking_key=mask, fin=1) self.assertEqual(len(f.build()), 131)
def test_16_bit_length(self): f = Frame(opcode=OPCODE_TEXT, body=b"*" * 126, fin=1) self.assertEqual(len(f.build()), 130) f = Frame(opcode=OPCODE_TEXT, body=b"*" * 65535, fin=1) self.assertEqual(len(f.build()), 65539) mask = os.urandom(4) f = Frame(opcode=OPCODE_TEXT, body=b"*" * 126, masking_key=mask, fin=1) self.assertEqual(len(f.build()), 134) f = Frame(opcode=OPCODE_TEXT, body=b"*" * 65535, masking_key=mask, fin=1) self.assertEqual(len(f.build()), 65543)
def test_7_bit_length(self): f = Frame(opcode=OPCODE_TEXT, body=b"", fin=1) self.assertEqual(len(f.build()), 2) f = Frame(opcode=OPCODE_TEXT, body=b"*" * 125, fin=1) self.assertEqual(len(f.build()), 127) mask = os.urandom(4) f = Frame(opcode=OPCODE_TEXT, body=b"", masking_key=mask, fin=1) self.assertEqual(len(f.build()), 6) f = Frame(opcode=OPCODE_TEXT, body=b"*" * 125, masking_key=mask, fin=1) self.assertEqual(len(f.build()), 131)
def test_16_bit_length(self): f = Frame(opcode=OPCODE_TEXT, body=b'*' * 126, fin=1) self.assertEqual(len(f.build()), 130) f = Frame(opcode=OPCODE_TEXT, body=b'*' * 65535, fin=1) self.assertEqual(len(f.build()), 65539) mask = os.urandom(4) f = Frame(opcode=OPCODE_TEXT, body=b'*' * 126, masking_key=mask, fin=1) self.assertEqual(len(f.build()), 134) f = Frame(opcode=OPCODE_TEXT, body=b'*' * 65535, masking_key=mask, fin=1) self.assertEqual(len(f.build()), 65543)
def test_masking(self): mask = "7\xfa!=" f = Frame(opcode=OPCODE_TEXT, body='Hello', masking_key=mask, fin=1) spec_says = '\x81\x857\xfa!=\x7f\x9fMQX' self.assertEqual(f.build(), spec_says)
def test_masking(self): if py3k: mask = b"7\xfa!=" else: mask = "7\xfa!=" f = Frame(opcode=OPCODE_TEXT, body=b'Hello', masking_key=mask, fin=1) if py3k: spec_says = b'\x81\x857\xfa!=\x7f\x9fMQX' else: spec_says = '\x81\x857\xfa!=\x7f\x9fMQX' self.assertEqual(f.build(), spec_says)
def test_opcodes(self): for opcode in [OPCODE_CONTINUATION, OPCODE_TEXT, OPCODE_BINARY, OPCODE_CLOSE, OPCODE_PING, OPCODE_PONG]: f = Frame(opcode=opcode, body=b"", fin=1) byte = ord(f.build()[0]) self.assertTrue(byte & opcode == opcode) f = Frame(opcode=0x3, body=b"", fin=1) self.assertRaises(ValueError, f.build)
def test_masking(self): if py3k: mask = b"7\xfa!=" else: mask = "7\xfa!=" f = Frame(opcode=OPCODE_TEXT, body=enc('Hello'), masking_key=mask, fin=1) if py3k: spec_says = b'\x81\x857\xfa!=\x7f\x9fMQX' else: spec_says = '\x81\x857\xfa!=\x7f\x9fMQX' self.assertEqual(f.build(), spec_says)
def test_opcodes(self): for opcode in [ OPCODE_CONTINUATION, OPCODE_TEXT, OPCODE_BINARY, OPCODE_CLOSE, OPCODE_PING, OPCODE_PONG ]: f = Frame(opcode=opcode, body=b'', fin=1) byte = ord(f.build()[0]) self.assertTrue(byte & opcode == opcode) f = Frame(opcode=0x3, body=b'', fin=1) self.assertRaises(ValueError, f.build)
def received_message(self, m): # Assuming type of arraybuffer xx = N.fromstring(m.data, N.int16, count=5) # xx = N.fromstring(m.data, N.float32, count=4) print xx xx = xx*2; f = Frame(OPCODE_BINARY, xx.tostring(), fin=1) b = f.build() yy = BinaryMessage(b) for conn in SUBSCRIBERS: conn.send(yy)
def test_protocol_exception_from_frame_parsing(self): payload = struct.pack("!H", 1000) + b'hello' f = Frame(opcode=OPCODE_CLOSE, body=payload, fin=1, masking_key=os.urandom(4)) f.rsv1 = 1 f = f.build() s = Stream() self.assertEqual(len(s.errors), 0) self.assertEqual(s.closing, None) s.parser.send(f) self.assertEqual(s.closing, None) self.assertEqual(type(s.errors[0]), CloseControlMessage) self.assertEqual(s.errors[0].code, 1002)
def test_passing_encoded_string(self): # once encoded the u'\xe9' character will be of length 2 f = Frame(opcode=OPCODE_TEXT, body=u'\xe9trange'.encode('utf-8'), fin=1) self.assertEqual(len(f.build()), 10)
def test_passing_encoded_string(self): # once encoded the u'\xe9' character will be of length 2 f = Frame(opcode=OPCODE_TEXT, body=u'\xe9trange'.encode('utf-8'), fin=1) self.assertEqual(len(f.build()), 10)