Esempio n. 1
0
    def test_build_request_object_passes_arguments_to_request_constructor(
        self,
        mock_get_required_headers,
        mock_request_constructor
    ):
        session = mock.Mock()

        service = mock.Mock()
        service.get_hosts.return_value = ['http://host1.biz']

        endpoint = mock.Mock()
        endpoint.default_method = 'POST'
        endpoint.get_formatted_path.return_value = '/foo'
        endpoint.required_headers = {'header': 'value'}
        endpoint.default_params = {}
        endpoint.required_params = set()

        path_kwargs = {'foo': 'bar'}
        params = {'baz': 'qux'}
        endpoint.get_merged_params.return_value = params
        data = 'I am a data'
        headers = {'Accept': 'stuff'}
        cookies = {'chocolate-chip': 'yes'}
        auth = mock.Mock()

        mock_get_required_headers.return_value = {'header': 'value'}
        expected_headers = {}
        expected_headers.update(headers)
        expected_headers.update(endpoint.required_headers)

        with mock.patch.object(session, 'prepare_request') as mock_prepare_request:
            ServiceCaller.build_request_object(
                session,
                service,
                endpoint,
                path_kwargs=path_kwargs,
                params=params,
                data=data,
                headers=headers,
                cookies=cookies,
                auth=auth,
            )

            mock_request_constructor.assert_called_once_with(
                url='http://host1.biz/foo',
                method=endpoint.default_method,
                headers=expected_headers,
                cookies=cookies,
                params=params,
                data=data,
                auth=auth,
            )

            assert 1 == mock_prepare_request.call_count
Esempio n. 2
0
    def test_build_request_object_raises_no_host_exception(self):
        service = mock.Mock()
        service.get_hosts.return_value = []

        with pytest.raises(NoHostsAvailableException):
            ServiceCaller.build_request_object(None, service, None)