Merge "Fix the handling of content-type in HTTPClient"

This commit is contained in:
Zuul 2024-03-01 10:43:01 +00:00 committed by Gerrit Code Review
commit 804d179af6
3 changed files with 37 additions and 18 deletions

View File

@ -87,6 +87,8 @@ class HTTPClient(object):
if 'body' in kwargs:
kargs['body'] = kwargs['body']
if 'content_type' in kwargs:
kargs['content_type'] = kwargs['content_type']
if self.log_credentials:
log_kargs = kargs

View File

@ -107,7 +107,8 @@ class CLITestAuthNoAuth(testtools.TestCase):
mock_request.assert_called_once_with(
ENDPOINT_URL + '/resource',
'GET',
headers=headers)
headers=headers,
content_type=None)
self.assertEqual(self.client.endpoint_url, ENDPOINT_URL)
@ -153,7 +154,7 @@ class CLITestAuthKeystone(testtools.TestCase):
self.client.do_request('/resource', 'GET')
mock_request.assert_called_with(
ENDPOINT_URL + '/resource', 'GET',
headers=expected_headers)
headers=expected_headers, content_type=None)
self.assertEqual(self.client.endpoint_url, ENDPOINT_URL)
self.assertEqual(self.client.auth_token, TOKEN)
@ -174,7 +175,7 @@ class CLITestAuthKeystone(testtools.TestCase):
self.client.do_request('/resource', 'GET')
mock_request.assert_called_with(
ENDPOINT_URL + '/resource', 'GET',
headers=expected_headers)
headers=expected_headers, content_type=None)
@mock.patch('tackerclient.client.HTTPClient.request')
def test_refresh_token_no_auth_url(self, mock_request):
@ -192,7 +193,8 @@ class CLITestAuthKeystone(testtools.TestCase):
'GET')
expected_url = ENDPOINT_URL + '/resource'
mock_request.assert_called_with(expected_url, 'GET',
headers=expected_headers)
headers=expected_headers,
content_type=None)
def test_get_endpoint_url_with_invalid_auth_url(self):
# Handle the case when auth_url is not provided
@ -209,14 +211,14 @@ class CLITestAuthKeystone(testtools.TestCase):
self.client.do_request('/resource', 'GET')
mock_request.assert_called_with(
ENDPOINT_URL + '/resource', 'GET',
headers=expected_headers)
headers=expected_headers, content_type=None)
mock_request.return_value = (resp_200, '')
self.client.do_request('/resource', 'GET',
headers=headers)
mock_request.assert_called_with(
ENDPOINT_URL + '/resource', 'GET',
headers=headers)
headers=headers, content_type=None)
@mock.patch('tackerclient.client.HTTPClient.request')
def test_use_given_endpoint_url(self, mock_request):
@ -233,7 +235,7 @@ class CLITestAuthKeystone(testtools.TestCase):
headers=headers)
mock_request.assert_called_with(
ENDPOINT_OVERRIDE + '/resource', 'GET',
headers=headers)
headers=headers, content_type=None)
self.assertEqual(self.client.endpoint_url, ENDPOINT_OVERRIDE)
@mock.patch('tackerclient.client.HTTPClient.request')
@ -324,7 +326,8 @@ class CLITestAuthKeystone(testtools.TestCase):
expected_body = ('{"auth": {"tenantId": "testtenant_id",'
'"REDACTEDCredentials": {"REDACTED": "REDACTED",'
'"userId": "testuser_id"}}}')
_headers = {'headers': expected_headers, 'body': expected_body}
_headers = {'headers': expected_headers, 'body': expected_body,
'content_type': None}
mock_request.return_value = (resp_200, json.dumps(KS_TOKEN_RESULT))
self.client.do_request('/resource', 'GET', body=body)

View File

@ -239,8 +239,10 @@ class CLITestV10Base(testtools.TestCase):
# MyComparator does not decodes XML string correctly.
if self.format == 'json':
_body = MyComparator(body, self.client)
_content_type = 'application/json'
else:
_body = self.client.serialize(body)
_content_type = 'application/zip'
with mock.patch.object(self.client.httpclient, 'request') as mock_req:
mock_req.return_value = (MyResp(200), resstr)
args.extend(['--request-format', self.format])
@ -249,7 +251,8 @@ class CLITestV10Base(testtools.TestCase):
mock_req.assert_called_once_with(
end_url(path, format=self.format), 'POST',
body=_body,
headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN))
headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN),
content_type=_content_type)
self.assertEqual(get_client_called_count, mock_get.call_count)
_str = self.fake_stdout.make_string()
self.assertIn(myid, _str)
@ -273,7 +276,8 @@ class CLITestV10Base(testtools.TestCase):
mock_req.assert_called_once_with(
end_url(path, format=self.format), 'GET',
body=None,
headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN))
headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN),
content_type='application/json')
mock_get.assert_called_once_with()
def _test_list_resources(self, resources, cmd, detail=False, tags=[],
@ -368,7 +372,8 @@ class CLITestV10Base(testtools.TestCase):
self.client),
'GET',
body=None,
headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN))
headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN),
content_type='application/json')
_str = self.fake_stdout.make_string()
if response_contents is None:
self.assertIn('myid1', _str)
@ -465,7 +470,8 @@ class CLITestV10Base(testtools.TestCase):
mock_req.assert_called_once_with(
comparator, 'GET',
body=None,
headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN))
headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN),
content_type='application/json')
_str = self.fake_stdout.make_string()
if response_contents is None:
self.assertIn('myid1', _str)
@ -551,8 +557,10 @@ class CLITestV10Base(testtools.TestCase):
# MyComparator does not decodes XML string correctly.
if self.format == 'json':
_body = MyComparator(body, self.client)
_content_type = 'application/json'
else:
_body = self.client.serialize(body)
_content_type = 'application/zip'
with mock.patch.object(self.client.httpclient, 'request') as mock_req:
comparator = MyUrlComparator(
end_url(path % myid, format=self.format), self.client)
@ -564,7 +572,8 @@ class CLITestV10Base(testtools.TestCase):
comparator,
'PUT',
body=_body,
headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN))
headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN),
content_type=_content_type)
self.assertEqual(get_client_called_count, mock_get.call_count)
_str = self.fake_stdout.make_string()
@ -589,7 +598,8 @@ class CLITestV10Base(testtools.TestCase):
mock_req.assert_called_once_with(
end_url(path % myid, query, format=self.format), 'GET',
body=None,
headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN))
headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN),
content_type='application/json')
_str = self.fake_stdout.make_string()
mock_get.assert_called_once_with()
self.assertIn(myid, _str)
@ -611,12 +621,14 @@ class CLITestV10Base(testtools.TestCase):
mock_req.assert_called_once_with(
end_url(path % myid, format=self.format), 'DELETE',
body=body_str,
headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN))
headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN),
content_type='application/json')
else:
mock_req.assert_called_once_with(
end_url(path % myid, format=self.format), 'DELETE',
body=None,
headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN))
headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN),
content_type='application/json')
mock_get.assert_called_once_with()
_str = self.fake_stdout.make_string()
msg = 'All specified %(resource)s(s) %(msg)s successfully\n' % {
@ -638,7 +650,8 @@ class CLITestV10Base(testtools.TestCase):
mock_req.assert_called_once_with(
end_url(path % path_action, format=self.format), 'PUT',
body=MyComparator(body, self.client),
headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN))
headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN),
content_type='application/json')
_str = self.fake_stdout.make_string()
self.assertIn(myid, _str)
@ -660,7 +673,8 @@ class ClientV1TestJson(CLITestV10Base):
mock_req.assert_called_with(
expected_uri, 'PUT', body=expect_body,
headers={'X-Auth-Token': unicode_text,
'User-Agent': 'python-tackerclient'})
'User-Agent': 'python-tackerclient'},
content_type='application/json')
# test response with unicode
self.assertEqual(res_body, body)