From a2406d9510f2a77450c22591a669dc7081a9add4 Mon Sep 17 00:00:00 2001 From: Ayumu Ueha Date: Fri, 9 Feb 2024 10:54:35 +0000 Subject: [PATCH] Fix the handling of content-type in HTTPClient If content-type other than application/json is used, an error occur because the processing is lacked in HTTPClient to pass content-type. So, this patch fixes the handling of `content-type` in HTTPClient. Closes-Bug: #2052768 Change-Id: Ief823dbdec9cceea3c7129860381b9ab34d0e438 --- tackerclient/client.py | 2 ++ tackerclient/tests/unit/test_auth.py | 19 ++++++++------- tackerclient/tests/unit/test_cli10.py | 34 +++++++++++++++++++-------- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/tackerclient/client.py b/tackerclient/client.py index a62ae33b..c565248c 100644 --- a/tackerclient/client.py +++ b/tackerclient/client.py @@ -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 diff --git a/tackerclient/tests/unit/test_auth.py b/tackerclient/tests/unit/test_auth.py index 3da14406..4bc2d93b 100644 --- a/tackerclient/tests/unit/test_auth.py +++ b/tackerclient/tests/unit/test_auth.py @@ -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) diff --git a/tackerclient/tests/unit/test_cli10.py b/tackerclient/tests/unit/test_cli10.py index 9183b3f9..3c533e7b 100644 --- a/tackerclient/tests/unit/test_cli10.py +++ b/tackerclient/tests/unit/test_cli10.py @@ -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)