Пример #1
0
    def test_call_with_explicit_encoding(self):
        service = mock.Mock()
        service.get_hosts.return_value = ['http://host1.biz']
        service.required_headers = {}

        endpoint = mock.Mock()
        endpoint.required_headers = {}
        endpoint.get_formatted_path.return_value = '/foo/'
        del endpoint.stub_response

        mock_logger = mock.Mock()
        mock_response = mock.Mock()
        mock_response.history = []

        session = mock.Mock()
        session.send.return_value = mock_response

        ServiceCaller.call(
            service,
            endpoint,
            session=session,
            logger=mock_logger,
            encoding='FAKE-CODEC'
        )

        assert 'FAKE-CODEC' == mock_response.encoding
Пример #2
0
    def test_call_with_existing_session(self, mock_get_adapted_session):
        service = mock.Mock()
        service.get_hosts.return_value = ['http://host1.biz']
        service.required_headers = {}

        endpoint = mock.Mock()
        endpoint.required_headers = {}
        endpoint.get_formatted_path.return_value = '/foo/'
        del endpoint.stub_response

        mock_logger = mock.Mock()
        mock_response = mock.Mock()
        mock_response.history = []

        session = mock.Mock()
        session.send.return_value = mock_response

        ServiceCaller.call(
            service,
            endpoint,
            session=session,
            logger=mock_logger,
        )

        assert not mock_get_adapted_session.called
        assert not session.close.called
Пример #3
0
    def test_call_stub_dynamic_params(self, MockSession, MockAdapter, mock_build_request_object, mock_get_adapted_session, mock_timeout):
        """
        Test getting a response for a ``StubEndpoint`` using a dynamic response with parameters
        """
        service = mock.Mock()
        service.get_hosts.return_value = ['http://host1.biz']

        stub_endpoint = mock.Mock()
        def stub_response(**kwargs):
            response_map = {
                'param_value': 'correct',
                'default': 'incorrect',
            }
            data_key = kwargs['params'].setdefault('param_key', 'default')
            return response_map[data_key]

        stub_endpoint.stub_response = stub_response

        actual_response = ServiceCaller.call(
            service,
            stub_endpoint,
            params={'param_key': 'param_value'}
        )

        expected_response = 'correct'

        assert expected_response == actual_response
Пример #4
0
    def test_call_stub(self, MockSession, MockAdapter, mock_build_request_object, mock_get_adapted_session, mock_timeout):
        """
        Test getting a response for a ``StubEndpoint``
        """
        service = mock.Mock()
        service.get_hosts.return_value = ['http://host1.biz']

        stub_endpoint = mock.Mock()
        stub_endpoint.stub_response = {'stub': 'response'}

        actual_response = ServiceCaller.call(service, stub_endpoint)

        expected_response = {'stub': 'response'}

        assert expected_response == actual_response
Пример #5
0
    def test_call_stub_dynamic(self, MockSession, MockAdapter, mock_build_request_object, mock_get_adapted_session, mock_timeout):
        """
        Test getting a response for a ``StubEndpoint`` using a dynamic response
        """
        service = mock.Mock()
        service.get_hosts.return_value = ['http://host1.biz']

        stub_endpoint = mock.Mock()
        def stub_response(**kwargs):
            return 'stub response'
        stub_endpoint.stub_response = stub_response

        actual_response = ServiceCaller.call(service, stub_endpoint)

        expected_response = 'stub response'

        assert expected_response == actual_response
Пример #6
0
    def test_call(self, MockSession, MockAdapter, mock_build_request_object, mock_get_adapted_session, mock_timeout):
        service = mock.Mock()
        service.get_hosts.return_value = ['http://host1.biz']

        endpoint = mock.Mock()
        endpoint.default_method = 'GET'
        endpoint.get_formatted_path.return_value = '/foo/'
        endpoint.required_headers = {}
        endpoint.streaming = True
        del endpoint.stub_response

        request = mock.Mock()
        request.url = 'http://host1.biz/foo/'
        mock_build_request_object.return_value = request

        mock_logger = mock.Mock()

        mock_response = mock.Mock()
        mock_response.status_code = 200
        mock_response.url = 'http://host1.biz/foo/'
        mock_response.history = []

        mock_session = MockSession()
        mock_session.send.return_value = mock_response
        mock_get_adapted_session.return_value = mock_session

        ServiceCaller.call(
            service,
            endpoint,
            timeout_spec=mock_timeout,
            logger=mock_logger,
        )

        mock_get_adapted_session.assert_called_once_with(MockAdapter())
        mock_session.send.assert_called_once_with(
            request,
            timeout=(mock_timeout.connection_timeout, mock_timeout.read_timeout),
            stream=endpoint.streaming,
        )

        mock_logger.info.assert_any_call('GET http://host1.biz/foo/')
        mock_logger.info.assert_any_call('200 http://host1.biz/foo/')

        endpoint.default_method = 'POST'
        request.method = 'POST'

        ServiceCaller.call(
            service,
            endpoint,
            session=mock_session,
            timeout_spec=mock_timeout,
            logger=mock_logger,
        )

        mock_session.send.assert_any_call(
            request,
            timeout=(mock_timeout.connection_timeout, mock_timeout.read_timeout),
            stream=endpoint.streaming,
        )

        mock_logger.info.assert_any_call('GET http://host1.biz/foo/')
        mock_logger.info.assert_any_call('200 http://host1.biz/foo/')

        request.method = 'PUT'

        ServiceCaller.call(
            service,
            endpoint,
            method='PUT',
            session=mock_session,
            timeout_spec=mock_timeout,
            logger=mock_logger,
        )

        mock_session.send.assert_any_call(
            request,
            timeout=(mock_timeout.connection_timeout, mock_timeout.read_timeout),
            stream=endpoint.streaming,
        )