Пример #1
0
    def test_read_headers_no_colon(self):
        # Setup: Create a reader with a buffer that contains the control sequence but does not
        #        match the header format
        test_buffer = bytearray(b'1234567890\r\n\r\n')
        reader = JSONRPCReader(None, logger=utils.get_mock_logger())
        reader._buffer = test_buffer
        reader._buffer_end_offset = len(reader._buffer)
        reader._read_offset = 1

        # If: I look for a header block in the buffer
        # Then:
        # ... I should get an exception b/c of the malformed header
        with self.assertRaises(KeyError):
            reader._try_read_headers()

        # ... The current reading position of the buffer should be reset to 0
        self.assertEqual(reader._read_offset, 0)

        # ... The buffer should have been trashed
        self.assertIsNot(reader._buffer, test_buffer)
Пример #2
0
    def test_read_headers_bad_format(self):
        # Setup: Create a reader with a header block that contains invalid content-length
        test_buffer = bytearray(b'Content-Length: abc\r\n\r\n')
        reader = JSONRPCReader(None, logger=utils.get_mock_logger())
        reader._buffer = test_buffer
        reader._buffer_end_offset = len(reader._buffer)
        reader._read_offset = 1

        # If: I look for a header block in the buffer
        # Then:
        # ... I should get an exception from there not being a content-length header
        with self.assertRaises(LookupError):
            reader._try_read_headers()

        # ... The current reading position of the buffer should be reset to 0
        self.assertEqual(reader._read_offset, 0)

        # ... The buffer should have been trashed
        self.assertIsNot(reader._buffer, test_buffer)

        # ... The headers should have been trashed
        self.assertEqual(len(reader._headers), 0)
Пример #3
0
    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))
Пример #4
0
    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))