Exemple #1
0
    def test_send_message(self):
        with io.BytesIO(b'') as stream:
            # If:
            # ... I create a JSON RPC writer
            writer = JSONRPCWriter(stream, logger=utils.get_mock_logger())

            # ... and I send a message
            message = JSONRPCMessage.create_request('123', 'test/test', {})
            writer.send_message(message)

            # Then:
            # ... The content-length header should be present
            stream.seek(0)
            header = stream.readline().decode('ascii')
            self.assertRegex(
                header,
                re.compile('^Content-Length: [0-9]+\r\n$', re.IGNORECASE))

            # ... There should be a blank line to signify the end of the headers
            blank_line = stream.readline().decode('ascii')
            self.assertEqual(blank_line, '\r\n')

            # ... The JSON message as a dictionary should match the dictionary of the message
            message_str = str.join(
                os.linesep, [x.decode('UTF-8') for x in stream.readlines()])
            message_dict = json.loads(message_str)
            self.assertDictEqual(message_dict, message.dictionary)
    def test_dispatch_request_no_handler():
        # If: I dispatch a message that has no handler
        logger = utils.get_mock_logger()
        message = JSONRPCMessage.create_request('123', 'non_existent', {})
        server = JSONRPCServer(None, None, logger=logger)
        server._dispatch_message(message)

        # Then:
        # ... Nothing should have happened
        # TODO: Capture that an error was sent
        # ... A warning should have been logged
        logger.warn.assert_called_once()
    def send_request(self, method, params):
        """
        Add a new request to the output queue
        :param method: Method string of the request
        :param params: Data to send along with the request
        """
        message_id = str(uuid.uuid4())

        # Create the message
        message = JSONRPCMessage.create_request(message_id, method, params)

        # TODO: Add support for handlers for the responses
        # Add the message to the output queue
        self._output_queue.put(message)
    def test_dispatch_request_normal(self):
        # Setup: Create a server with a single handler that has none for the deserialization class
        config = IncomingMessageConfiguration('test/test', _TestParams)
        handler = mock.MagicMock()
        server = JSONRPCServer(None, None, logger=utils.get_mock_logger())
        server.set_request_handler(config, handler)

        # If: I dispatch a message that has none set for the deserialization class
        params = {}
        message = JSONRPCMessage.create_request('123', 'test/test', params)
        server._dispatch_message(message)

        # Then:
        # ... The handler should have been called
        handler.assert_called_once()

        # ... The parameters to the handler should have been a request context and params
        self.assertIsInstance(handler.mock_calls[0][1][0], RequestContext)
        self.assertIs(handler.mock_calls[0][1][0]._queue, server._output_queue)
        self.assertIs(handler.mock_calls[0][1][0]._message, message)
        self.assertIsInstance(handler.mock_calls[0][1][1], _TestParams)
    def test_create_request(self):
        # If: I create a request
        message = JSONRPCMessage.create_request(10, "test/test", {})

        # Then:
        # ... The message should have all the properties I defined
        self.assertIsNotNone(message)
        self.assertEqual(message.message_id, 10)
        self.assertEqual(message.message_method, "test/test")
        self.assertDictEqual(message.message_params, {})
        self.assertIsNone(message.message_result)
        self.assertIsNone(message.message_error)
        self.assertEqual(message.message_type, JSONRPCMessageType.Request)

        # ... The dictionary should have the same values stored
        dictionary = message.dictionary
        self.assertIsNotNone(dictionary)
        self.assertDictEqual(dictionary, {
            'jsonrpc': '2.0',
            'method': 'test/test',
            'params': {},
            'id': 10
        })