def test_read_content_success(self): # Setup: Create a reader that has read in headers and has all of a message buffered test_buffer = bytearray(b"message") reader = JSONRPCReader(None, logger=utils.get_mock_logger()) reader._buffer = test_buffer reader._buffer_end_offset = len(reader._buffer) reader._read_offset = 0 reader._read_state = JSONRPCReader.ReadState.Content reader._expected_content_length = 5 # If: I read a message from the buffer output = [''] result = reader._try_read_content(output) # Then: # ... The message should be successfully read self.assertTrue(result) self.assertEqual(output[0], 'messa') # ... The state of the reader should have been updated self.assertEqual(reader._read_state, JSONRPCReader.ReadState.Header) self.assertEqual(reader._read_offset, 5) self.assertEqual(reader._buffer_end_offset, len(reader._buffer))
def test_read_content_not_enough_buffer(self): # Setup: Create a reader that has read in headers and has part of a message buffered test_buffer = bytearray(b'message') reader = JSONRPCReader(None, logger=utils.get_mock_logger()) reader._buffer = test_buffer reader._buffer_end_offset = len(reader._buffer) reader._read_offset = 0 reader._read_state = JSONRPCReader.ReadState.Content reader._expected_content_length = 15 # If: I read a message from the buffer output = [''] result = reader._try_read_content(output) # Then: # ... The result should be false and the output should be empty self.assertFalse(result) self.assertEqual(output[0], '') # ... The state of the reader should stay the same self.assertEqual(reader._read_state, JSONRPCReader.ReadState.Content) self.assertEqual(reader._read_offset, 0) self.assertEqual(reader._expected_content_length, 15) self.assertEqual(reader._buffer_end_offset, len(reader._buffer))