def test_request_headers(self):

        request = ClientRequest()
        request.add_header("a", 1)
        request.add_headers({'b':2, 'c':3})

        self.assertEqual(request.headers, {'a':1, 'b':2, 'c':3})
    def test_request_data(self):

        request = ClientRequest()
        data = "Lots of dataaaa"
        request.add_content(data)

        self.assertEqual(request.data, json.dumps(data))
        self.assertEqual(request.headers.get('Content-Length'), 17)
    def test_request_url_with_params(self):

        request = ClientRequest()
        request.url = "a/b/c?t=y"
        request.format_parameters({'g': 'h'})

        self.assertIn(request.url, [
            'a/b/c?g=h&t=y',
            'a/b/c?t=y&g=h'
        ])
Example #4
0
    def test_request_headers(self):

        request = ClientRequest()
        request.add_header("a", 1)
        request.add_headers({'b':2, 'c':3})

        self.assertEqual(request.headers, {'a':1, 'b':2, 'c':3})
Example #5
0
    def test_client_formdata_send(self):

        mock_client = mock.create_autospec(ServiceClient)
        mock_client._format_data.return_value = "formatted"
        request = ClientRequest('GET')
        ServiceClient.send_formdata(mock_client, request)
        mock_client.send.assert_called_with(request, None, None, files={})

        ServiceClient.send_formdata(mock_client, request, {'id': '1234'},
                                    {'Test': 'Data'})
        mock_client.send.assert_called_with(request, {'id': '1234'},
                                            None,
                                            files={'Test': 'formatted'})

        ServiceClient.send_formdata(mock_client, request,
                                    {'Content-Type': '1234'}, {
                                        '1': '1',
                                        '2': '2'
                                    })
        mock_client.send.assert_called_with(request, {},
                                            None,
                                            files={
                                                '1': 'formatted',
                                                '2': 'formatted'
                                            })

        ServiceClient.send_formdata(mock_client, request,
                                    {'Content-Type': '1234'}, {
                                        '1': '1',
                                        '2': None
                                    })
        mock_client.send.assert_called_with(request, {},
                                            None,
                                            files={'1': 'formatted'})

        ServiceClient.send_formdata(
            mock_client, request,
            {'Content-Type': 'application/x-www-form-urlencoded'}, {
                '1': '1',
                '2': '2'
            })
        mock_client.send.assert_called_with(request, {}, None)
        self.assertEqual(request.data, {'1': '1', '2': '2'})

        ServiceClient.send_formdata(
            mock_client, request,
            {'Content-Type': 'application/x-www-form-urlencoded'}, {
                '1': '1',
                '2': None
            })
        mock_client.send.assert_called_with(request, {}, None)
        self.assertEqual(request.data, {'1': '1'})
Example #6
0
    def test_client_send(self):

        mock_client = mock.create_autospec(ServiceClient)
        mock_client.config = self.cfg
        mock_client.creds = self.creds
        mock_client._log = self.cfg._log
        mock_client._configure_session.return_value = {}
        session = mock.create_autospec(requests.Session)
        mock_client.creds.signed_session.return_value = session
        mock_client.creds.refresh_session.return_value = session

        request = ClientRequest('GET')
        ServiceClient.send(mock_client, request)
        session.request.call_count = 0
        mock_client._configure_session.assert_called_with(session)
        session.request.assert_called_with('GET', None, data=[], headers={})
        session.close.assert_called_with()

        ServiceClient.send(mock_client, request, headers={'id':'1234'}, content={'Test':'Data'})
        mock_client._configure_session.assert_called_with(session)
        session.request.assert_called_with('GET', None, data='{"Test": "Data"}', headers={'Content-Length': 16, 'id':'1234'})
        self.assertEqual(session.request.call_count, 1)
        session.request.call_count = 0
        session.close.assert_called_with()

        session.request.side_effect = requests.RequestException("test")
        with self.assertRaises(ClientRequestError):
            ServiceClient.send(mock_client, request, headers={'id':'1234'}, content={'Test':'Data'}, test='value')
        mock_client._configure_session.assert_called_with(session, test='value')
        session.request.assert_called_with('GET', None, data='{"Test": "Data"}', headers={'Content-Length': 16, 'id':'1234'})
        self.assertEqual(session.request.call_count, 1)
        session.request.call_count = 0
        session.close.assert_called_with()

        session.request.side_effect = oauth2.rfc6749.errors.InvalidGrantError("test")
        with self.assertRaises(TokenExpiredError):
            ServiceClient.send(mock_client, request, headers={'id':'1234'}, content={'Test':'Data'}, test='value')
        self.assertEqual(session.request.call_count, 2)
        session.request.call_count = 0
        session.close.assert_called_with()

        session.request.side_effect = ValueError("test")
        with self.assertRaises(ValueError):
            ServiceClient.send(mock_client, request, headers={'id':'1234'}, content={'Test':'Data'}, test='value')
        session.close.assert_called_with()
    def _get_resource_locations(self, all_host_types):
        # Check local client's cached Options first
        if all_host_types:
            if self._all_host_types_locations is not None:
                return self._all_host_types_locations
        elif self._locations is not None:
            return self._locations

        # Next check for options cached on disk
        if not all_host_types and OPTIONS_FILE_CACHE[self.normalized_url]:
            try:
                logger.debug('File cache hit for options on: %s',
                             self.normalized_url)
                self._locations = self._base_deserialize.deserialize_data(
                    OPTIONS_FILE_CACHE[self.normalized_url],
                    '[ApiResourceLocation]')
                return self._locations
            except DeserializationError as ex:
                logger.debug(ex, exc_info=True)
        else:
            logger.debug('File cache miss for options on: %s',
                         self.normalized_url)

        # Last resort, make the call to the server
        options_uri = self._combine_url(self.config.base_url, '_apis')
        request = ClientRequest()
        request.url = self._client.format_url(options_uri)
        if all_host_types:
            query_parameters = {'allHostTypes': True}
            request.format_parameters(query_parameters)
        request.method = 'OPTIONS'
        headers = {'Accept': 'application/json'}
        if self._suppress_fedauth_redirect:
            headers['X-TFS-FedAuthRedirect'] = 'Suppress'
        response = self._send_request(request, headers=headers)
        wrapper = self._base_deserialize('VssJsonCollectionWrapper', response)
        if wrapper is None:
            raise VstsClientRequestError(
                "Failed to retrieve resource locations from: {}".format(
                    options_uri))
        collection = wrapper.value
        returned_locations = self._base_deserialize('[ApiResourceLocation]',
                                                    collection)
        if all_host_types:
            self._all_host_types_locations = returned_locations
        else:
            self._locations = returned_locations
            try:
                OPTIONS_FILE_CACHE[self.normalized_url] = wrapper.value
            except SerializationError as ex:
                logger.debug(ex, exc_info=True)
        return returned_locations
 def _create_request_message(self, http_method, location_id, route_values=None,
                             query_parameters=None):
     location = self._get_resource_location(location_id)
     if location is None:
         raise ValueError('API resource location ' + location_id + ' is not registered on '
                          + self.config.base_url + '.')
     if route_values is None:
         route_values = {}
     route_values['area'] = location.area
     route_values['resource'] = location.resource_name
     route_template = self._remove_optional_route_parameters(location.route_template,
                                                             route_values)
     logging.debug('Route template: %s', location.route_template)
     url = self._client.format_url(route_template, **route_values)
     request = ClientRequest()
     request.url = self._client.format_url(url)
     if query_parameters:
         request.format_parameters(query_parameters)
     request.method = http_method
     return request
    def test_client_formdata_add(self):

        client = mock.create_autospec(ServiceClient)
        client._format_data.return_value = "formatted"

        request = ClientRequest('GET')
        ServiceClient._add_formdata(client, request)
        assert request.files == {}

        request = ClientRequest('GET')
        ServiceClient._add_formdata(client, request, {'Test': 'Data'})
        assert request.files == {'Test': 'formatted'}

        request = ClientRequest('GET')
        request.headers = {'Content-Type': '1234'}
        ServiceClient._add_formdata(client, request, {'1': '1', '2': '2'})
        assert request.files == {'1': 'formatted', '2': 'formatted'}

        request = ClientRequest('GET')
        request.headers = {'Content-Type': '1234'}
        ServiceClient._add_formdata(client, request, {'1': '1', '2': None})
        assert request.files == {'1': 'formatted'}

        request = ClientRequest('GET')
        request.headers = {'Content-Type': 'application/x-www-form-urlencoded'}
        ServiceClient._add_formdata(client, request, {'1': '1', '2': '2'})
        assert request.files == []
        assert request.data == {'1': '1', '2': '2'}

        request = ClientRequest('GET')
        request.headers = {'Content-Type': 'application/x-www-form-urlencoded'}
        ServiceClient._add_formdata(client, request, {'1': '1', '2': None})
        assert request.files == []
        assert request.data == {'1': '1'}
    def test_client_send(self):
        class MockHTTPDriver(object):
            def configure_session(self, **config):
                pass

            def send(self, request, **config):
                pass

        client = ServiceClient(self.creds, self.cfg)
        client.config.keep_alive = True
        session = mock.create_autospec(requests.Session)
        client._http_driver.session = session
        session.adapters = {
            "http://": HTTPAdapter(),
            "https://": HTTPAdapter(),
        }
        client.creds.signed_session.return_value = session
        client.creds.refresh_session.return_value = session
        # Be sure the mock does not trick me
        assert not hasattr(session.resolve_redirects, 'is_mrest_patched')

        request = ClientRequest('GET')
        client.send(request, stream=False)
        session.request.call_count = 0
        session.request.assert_called_with(
            'GET',
            None,
            allow_redirects=True,
            cert=None,
            headers={
                'User-Agent': self.cfg.user_agent,
                'Test': 'true'  # From global config
            },
            stream=False,
            timeout=100,
            verify=True)
        assert session.resolve_redirects.is_mrest_patched

        client.send(request,
                    headers={'id': '1234'},
                    content={'Test': 'Data'},
                    stream=False)
        session.request.assert_called_with(
            'GET',
            None,
            data='{"Test": "Data"}',
            allow_redirects=True,
            cert=None,
            headers={
                'User-Agent': self.cfg.user_agent,
                'Content-Length': '16',
                'id': '1234',
                'Test': 'true'  # From global config
            },
            stream=False,
            timeout=100,
            verify=True)
        self.assertEqual(session.request.call_count, 1)
        session.request.call_count = 0
        assert session.resolve_redirects.is_mrest_patched

        session.request.side_effect = requests.RequestException("test")
        with self.assertRaises(ClientRequestError):
            client.send(request,
                        headers={'id': '1234'},
                        content={'Test': 'Data'},
                        test='value',
                        stream=False)
        session.request.assert_called_with(
            'GET',
            None,
            data='{"Test": "Data"}',
            allow_redirects=True,
            cert=None,
            headers={
                'User-Agent': self.cfg.user_agent,
                'Content-Length': '16',
                'id': '1234',
                'Test': 'true'  # From global config
            },
            stream=False,
            timeout=100,
            verify=True)
        self.assertEqual(session.request.call_count, 1)
        session.request.call_count = 0
        assert session.resolve_redirects.is_mrest_patched

        session.request.side_effect = oauth2.rfc6749.errors.InvalidGrantError(
            "test")
        with self.assertRaises(TokenExpiredError):
            client.send(request,
                        headers={'id': '1234'},
                        content={'Test': 'Data'},
                        test='value')
        self.assertEqual(session.request.call_count, 2)
        session.request.call_count = 0

        session.request.side_effect = ValueError("test")
        with self.assertRaises(ValueError):
            client.send(request,
                        headers={'id': '1234'},
                        content={'Test': 'Data'},
                        test='value')
    def test_request_xml(self):
        request = ClientRequest()
        data = ET.Element("root")
        request.add_content(data)

        assert request.data == b"<?xml version='1.0' encoding='utf8'?>\n<root />"