Exemple #1
0
    def test_execute_rpc_method_error(self, open_connection):
        """
        Tests that we can properly execute a method and return its response
        """

        writer_future = asyncio.Future()

        def _writer_func(request):
            writer_future.set_result(
                json.loads(
                    request.decode()
                )
            )

        mock_writer = asynctest.Mock(spec = asyncio.StreamWriter)
        mock_writer.write.side_effect = _writer_func

        mock_reader = asynctest.Mock(spec = asyncio.StreamReader)
        mock_reader.readline.return_value = json.dumps(
            {
                'jsonrpc': '2.0',
                'id': 1,
                'error': {
                    'code': -32,
                    'message': 'things',
                }
            }
        ).encode()

        open_connection.return_value = (mock_reader, mock_writer)

        client = RpcClient(self.host, self.port)
        client.start()

        client.execute_rpc_method(
            'testification',
            params = {
                'param0': 'test',
                'param1': 9001,
            },
        )
Exemple #2
0
    def test_execute_rpc_method(self, open_connection):
        """
        Tests that we can properly execute a method and return its response
        """

        writer_future = asyncio.Future()

        def _writer_func(request):
            writer_future.set_result(
                json.loads(
                    request.decode()
                )
            )

        mock_writer = asynctest.Mock(spec = asyncio.StreamWriter)
        mock_writer.write.side_effect = _writer_func

        mock_reader = asynctest.Mock(spec = asyncio.StreamReader)
        mock_reader.readline.return_value = json.dumps(
            {
                'jsonrpc': '2.0',
                'id': 1,
                'result': {
                    'the': 'thing',
                }
            }
        ).encode()

        open_connection.return_value = (mock_reader, mock_writer)

        client = RpcClient(self.host, self.port)
        client.start()

        response = client.execute_rpc_method(
            'testification',
            params = {
                'param0': 'test',
                'param1': 9001,
            },
        )

        client.stop()

        self.assertTrue(
            writer_future.done(),
            'Writer did not finish',
        )

        self.assertDictEqual(
            {
                'jsonrpc': '2.0',
                'method': 'testification',
                'params': {
                    'param0': 'test',
                    'param1': 9001,
                },
                'id': 1,
            },
            writer_future.result(),
            'Writer did not send the correct response',
        )

        self.assertDictEqual(
            {
                'the': 'thing',
            },
            response,
            'Client did not return the right response',
        )

        mock_writer.write_eof.assert_called_with()
        mock_writer.drain.assert_called_with()