Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_using_masking_key_when_unexpected(self):
f = Frame(opcode=OPCODE_TEXT, body=b'hello', fin=1, masking_key=os.urandom(4)).build()
s = Stream(expect_masking=False)
s.parser.send(f)
next(s.parser)
self.assertNotEqual(s.errors, [])
self.assertIsInstance(s.errors[0], CloseControlMessage)
self.assertEqual(s.errors[0].code, 1002)
def test_invalid_encoded_bytes_on_continuation(self):
s = Stream()
f = Frame(opcode=OPCODE_TEXT, body=b'hello',
fin=0, masking_key=os.urandom(4)).build()
s.parser.send(f)
next(s.parser)
f = Frame(opcode=OPCODE_CONTINUATION, body=b'h\xc3llo',
fin=1, masking_key=os.urandom(4)).build()
s.parser.send(f)
next(s.parser)
self.assertNotEqual(s.errors, [])
self.assertIsInstance(s.errors[0], CloseControlMessage)
self.assertEqual(s.errors[0].code, 1007)
def test_empty_close_message(self):
f = Frame(opcode=OPCODE_CLOSE, body=b'', fin=1, masking_key=os.urandom(4)).build()
s = Stream()
self.assertEqual(s.closing, None)
s.parser.send(f)
self.assertEqual(type(s.closing), CloseControlMessage)
def test_invalid_sized_close_message(self):
payload = b'boom'
f = Frame(opcode=OPCODE_CLOSE, body=payload,
fin=1, masking_key=os.urandom(4)).build()
s = Stream()
self.assertEqual(len(s.errors), 0)
self.assertEqual(s.closing, None)
s.parser.send(f)
self.assertEqual(type(s.closing), CloseControlMessage)
self.assertEqual(s.closing.code, 1002)
def test_invalid_close_message_reason_encoding(self):
payload = struct.pack("!H", 1000) + b'h\xc3llo'
f = Frame(opcode=OPCODE_CLOSE, body=payload,
fin=1, masking_key=os.urandom(4)).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, 1007)
def test_binary_message_received(self):
msg = os.urandom(16)
f = Frame(opcode=OPCODE_BINARY, body=msg, fin=1, masking_key=os.urandom(4)).build()
s = Stream()
self.assertEqual(s.has_message, False)
s.parser.send(f)
self.assertEqual(s.message.completed, True)
def test_helper_ping_message(self):
s = Stream()
m = s.ping('sos')
self.assertIsInstance(m, bytes)
self.assertEqual(len(m), 5)
def test_too_large_close_message(self):
payload = struct.pack("!H", 1000) + b'*' * 330
f = Frame(opcode=OPCODE_CLOSE, body=payload,
fin=1, masking_key=os.urandom(4)).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(len(s.errors), 1)
self.assertEqual(type(s.errors[0]), CloseControlMessage)
self.assertEqual(s.errors[0].code, 1002)
def __init__(self, sock, protocols=None, extensions=None, environ=None, heartbeat_freq=None):
""" The ``sock`` is an opened connection
resulting from the websocket handshake.
If ``protocols`` is provided, it is a list of protocols
negotiated during the handshake as is ``extensions``.
If ``environ`` is provided, it is a copy of the WSGI environ
dictionnary from the underlying WSGI server.
"""
self.stream = Stream(always_mask=False)
"""
Underlying websocket stream that performs the websocket
parsing to high level objects. By default this stream
never masks its messages. Clients using this class should
set the ``stream.always_mask`` fields to ``True``
and ``stream.expect_masking`` fields to ``False``.
"""
self.protocols = protocols
"""
List of protocols supported by this endpoint.
Unused for now.
"""
self.extensions = extensions
"""
def __init__(self, sock, protocols=None, extensions=None, environ=None, heartbeat_freq=None):
""" The ``sock`` is an opened connection
resulting from the websocket handshake.
If ``protocols`` is provided, it is a list of protocols
negotiated during the handshake as is ``extensions``.
If ``environ`` is provided, it is a copy of the WSGI environ
dictionnary from the underlying WSGI server.
"""
self.stream = Stream(always_mask=False)
"""
Underlying websocket stream that performs the websocket
parsing to high level objects. By default this stream
never masks its messages. Clients using this class should
set the ``stream.always_mask`` fields to ``True``
and ``stream.expect_masking`` fields to ``False``.
"""
self.protocols = protocols
"""
List of protocols supported by this endpoint.
Unused for now.
"""
self.extensions = extensions
"""