def test_nonfin_control_frame(self): reader = asyncio.StreamReader(loop=self.loop) parser = WebSocketParser(reader) task = asyncio.Task(parser.parse_frame(reader), loop=self.loop) frame = FrameBuilder.build(fin=False, opcode=OpCode.close, payload=b'', masked=True) self.loop.call_soon(lambda: reader.feed_data(frame)) self.assertRaises(WebSocketFormatException, self.loop.run_until_complete, task)
def test_close_frame_no_payload(self): reader = asyncio.StreamReader(loop=self.loop) parser = WebSocketParser(reader) task = asyncio.Task(parser.parse_frame(reader), loop=self.loop) self.loop.call_soon(lambda: reader.feed_data(FrameBuilder.close())) frame = self.loop.run_until_complete(task) self.assertEqual(frame.opcode, OpCode.close)
def test_eof_when_reading_payload(self): reader = asyncio.StreamReader(loop=self.loop) parser = WebSocketParser(reader) frame = FrameBuilder.text('hello')[:6] self.loop.call_soon(feed_data_and_eof, reader, frame) task = asyncio.Task(parser.get_message(), loop=self.loop) self.assertIsNone(self.loop.run_until_complete(task))
def test_reject_unmasked_frames(self): reader = asyncio.StreamReader(loop=self.loop) parser = WebSocketParser(reader) frame = FrameBuilder.close(masked=False) self.loop.call_soon(lambda: reader.feed_data(frame)) task = asyncio.Task(parser.get_message(), loop=self.loop) self.assertRaises(WebSocketFormatException, self.loop.run_until_complete, task)
def test_control_frame_too_long(self): reader = asyncio.StreamReader(loop=self.loop) parser = WebSocketParser(reader) frame = FrameBuilder.close(1000, payload=' '*126) self.loop.call_soon(lambda: reader.feed_data(frame)) task = asyncio.Task(parser.get_message(), loop=self.loop) self.assertRaises(WebSocketFormatException, self.loop.run_until_complete, task)
def test_return_none_on_eof(self): reader = asyncio.StreamReader(loop=self.loop) parser = WebSocketParser(reader) self.loop.call_soon(lambda: reader.feed_eof()) task = asyncio.Task(parser.get_message(), loop=self.loop) msg = self.loop.run_until_complete(task) self.assertIsNone(msg)
def test_invalid_opcode(self): reader = asyncio.StreamReader(loop=self.loop) parser = WebSocketParser(reader) frame = FrameBuilder.close() frame = b'\xff' + frame[1:] self.loop.call_soon(lambda: reader.feed_data(frame)) task = asyncio.Task(parser.get_message(), loop=self.loop) self.assertRaises(WebSocketFormatException, self.loop.run_until_complete, task)
def test_eight_byte_length(self): reader = asyncio.StreamReader(loop=self.loop) parser = WebSocketParser(reader) frame = FrameBuilder.text(" "*67000, fin=True) self.loop.call_soon(lambda: reader.feed_data(frame)) task = asyncio.Task(parser.get_message(), loop=self.loop) msg = self.loop.run_until_complete(task) self.assertEqual(len(msg.payload), 67000)
def test_raise_on_first_continuation(self): reader = asyncio.StreamReader(loop=self.loop) parser = WebSocketParser(reader) frame = FrameBuilder.continuation(" world!", fin=True) self.loop.call_soon(lambda: reader.feed_data(frame)) task = asyncio.Task(parser.get_message(), loop=self.loop) self.assertRaises(WebSocketFormatException, self.loop.run_until_complete, task)
def test_unmasking(self): data = b'\x81\x97\xea\x84N\xe4\xbf\xf7+\x96\xca\xa3*\x80\x8e\xa3n\x81\x84\xf0+\x96\x8f\xe0n\x87\x82\xe5:' reader = asyncio.StreamReader(loop=self.loop) parser = WebSocketParser(reader) task = asyncio.Task(parser.parse_frame(reader), loop=self.loop) self.loop.call_soon(lambda: reader.feed_data(data)) frame = self.loop.run_until_complete(task) self.assertEqual(frame.fin, 1) self.assertEqual(frame.opcode, OpCode.text) self.assertEqual(frame.payload, b"User 'ddd' entered chat")
def test_binary_message(self): reader = asyncio.StreamReader(loop=self.loop) parser = WebSocketParser(reader) task = asyncio.Task(parser.get_message(), loop=self.loop) frame = FrameBuilder.binary(b'binary') self.loop.call_soon(lambda: reader.feed_data(frame)) frame = self.loop.run_until_complete(task) self.assertEqual(frame.opcode, OpCode.binary) self.assertEqual(frame.payload, b'binary')
def test_raise_on_text_after_text(self): reader = asyncio.StreamReader(loop=self.loop) parser = WebSocketParser(reader) frame1 = FrameBuilder.text("Hello", fin=False) frame2 = FrameBuilder.text(" world!", fin=True) self.loop.call_soon(lambda: reader.feed_data(b''.join((frame1, frame2)))) task = asyncio.Task(parser.get_message(), loop=self.loop) self.assertRaises(WebSocketFormatException, self.loop.run_until_complete, task)
def test_eof_when_reading_length(self): reader = asyncio.StreamReader(loop=self.loop) parser = WebSocketParser(reader) frame = FrameBuilder.text(' ' * 129)[:2] self.loop.call_soon(feed_data_and_eof, reader, frame) task = asyncio.Task(parser.get_message(), loop=self.loop) self.assertIsNone(self.loop.run_until_complete(task)) reader._eof = False frame = FrameBuilder.text(' ' * 67000)[:2] self.loop.call_soon(feed_data_and_eof, reader, frame) task = asyncio.Task(parser.get_message(), loop=self.loop) self.assertIsNone(self.loop.run_until_complete(task))
def test_ping_frame_between_text_frames(self): reader = asyncio.StreamReader(loop=self.loop) parser = WebSocketParser(reader) first = FrameBuilder.text("Hello", fin=False) ping = FrameBuilder.ping() second = FrameBuilder.continuation(" world!", fin=True) frame = FrameBuilder.build(fin=False, opcode=OpCode.close, payload=b'', masked=True) self.loop.call_soon(lambda: reader.feed_data(b''.join((first, ping, second)))) task = asyncio.Task(parser.get_message(), loop=self.loop) message = self.loop.run_until_complete(task) self.assertEqual(message.opcode, OpCode.ping) task = asyncio.Task(parser.get_message(), loop=self.loop) message = self.loop.run_until_complete(task) self.assertEqual(message.opcode, OpCode.text) self.assertEqual(message.payload, 'Hello world!')