def test_closing_connection(self): trans = StringTransport() handler = TestHandler() proto = ws.WebSocketProtocol13(handler, trans, 'localhost', '/test') proto.security_key = b'dGhlIHNhbXBsZSBub25jZQ==' data = u'HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n'.encode( 'utf-8') data += b'\x88\x00' proto.dataReceived(data) self.assertTrue(handler.closing)
def test_receive_frame(self): trans = StringTransport() handler = TestHandler() proto = ws.WebSocketProtocol13(handler, trans, 'localhost', '/test') proto.security_key = b'dGhlIHNhbXBsZSBub25jZQ==' data = 'HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n'.encode( 'utf-8') data += b'\x81\x03\x01\x02\x03' proto.dataReceived(data) self.assertEqual(b'\x01\x02\x03', handler.payload)
def test_invalid_sec_key(self): trans = StringTransport() handler = TestHandler() proto = ws.WebSocketProtocol13(handler, trans, 'localhost', '/test') proto.security_key = b'dGhlIHNhbXBsZSBub25jZQ==' data = u'HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: s2pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n'.encode( 'utf-8') try: proto.dataReceived(data) self.fail('Invalid security key should results in exception') except ws.WebSocketError: pass
def test_invalid_opcode(self): trans = StringTransport() handler = TestHandler() proto = ws.WebSocketProtocol13(handler, trans, 'localhost', '/test') proto.security_key = b'dGhlIHNhbXBsZSBub25jZQ==' data = 'HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n'.encode( 'utf-8') data += b'\x83\x03\x01\x02\x03' try: proto.dataReceived(data) self.fail('Opcode 3 should raises an exception.') except ws.WebSocketError: pass
def test_send_frame(self): trans = StringTransport() proto = ws.WebSocketProtocol13(None, trans, 'localhost', '/test') proto.send_frame(True, 2, b'1234') data = trans.value() self.assertEquals(0x82, ord(data[0:1])) self.assertEquals(0x84, ord(data[1:2])) # test payload mask = data[2:6] pload = data[6:] unmasked = array( 'B', [ord(pload[i]) ^ ord(mask[i % 4]) for i in range(len(pload))]).tostring() self.assertEquals(b'1234', unmasked)