From 6f0cc4821621abff5137223b2fc4f254579f3a87 Mon Sep 17 00:00:00 2001 From: Jan Hutar Date: Thu, 29 Mar 2018 10:38:36 +0200 Subject: [PATCH 1/5] Do not return bytes, but str --- nailgun/entities.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nailgun/entities.py b/nailgun/entities.py index c74b7bbd..1c262bad 100644 --- a/nailgun/entities.py +++ b/nailgun/entities.py @@ -4383,7 +4383,8 @@ def download_debug_certificate(self, synchronous=True, **kwargs): kwargs.update(self._server_config.get_client_kwargs()) response = client.get( self.path('download_debug_certificate'), **kwargs) - return _handle_response(response, self._server_config, synchronous) + return str(_handle_response( + response, self._server_config, synchronous)) class OSDefaultTemplate( From 4c0ba10b5d04b8b6434dcace98cdeb7853973fdd Mon Sep 17 00:00:00 2001 From: Jan Hutar Date: Thu, 29 Mar 2018 22:09:58 +0200 Subject: [PATCH 2/5] Fix issue reviewers found. Thanks! --- nailgun/entities.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nailgun/entities.py b/nailgun/entities.py index 1c262bad..83ee620c 100644 --- a/nailgun/entities.py +++ b/nailgun/entities.py @@ -4383,8 +4383,8 @@ def download_debug_certificate(self, synchronous=True, **kwargs): kwargs.update(self._server_config.get_client_kwargs()) response = client.get( self.path('download_debug_certificate'), **kwargs) - return str(_handle_response( - response, self._server_config, synchronous)) + return _handle_response(response, self._server_config, synchronous) \ + .decode('utf-8') class OSDefaultTemplate( From 45ce0a3499bc0a943832264c8178636c0f016e65 Mon Sep 17 00:00:00 2001 From: Jan Hutar Date: Thu, 29 Mar 2018 22:45:21 +0200 Subject: [PATCH 3/5] Alter tests to pass for download_debug_certificate --- tests/test_entities.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/test_entities.py b/tests/test_entities.py index cc925d35..6b437a3d 100644 --- a/tests/test_entities.py +++ b/tests/test_entities.py @@ -1661,6 +1661,8 @@ def setUpClass(cls): entity_obj3.method2, 'put', ) + ``decoded_responses`` is a tuple of methods whose result was decoded + into string. """ cfg = config.ServerConfig('http://example.com') generic = {'server_config': cfg, 'id': 1} @@ -1742,6 +1744,9 @@ def setUpClass(cls): (entities.SyncPlan(**sync_plan).add_products, 'put'), (entities.SyncPlan(**sync_plan).remove_products, 'put'), ) + cls.decoded_responses = ( + entities.Organization(**generic).download_debug_certificate, + ) def test_generic(self): """Check that a variety of helper methods are sane. @@ -1769,7 +1774,10 @@ def test_generic(self): self.assertEqual(len(client_request.call_args[0]), 1) self.assertEqual(client_request.call_args[1], kwargs) self.assertEqual(handlr.call_count, 1) - self.assertEqual(handlr.return_value, response) + if method in self.decoded_responses: + self.assertEqual(handlr.return_value.decode('utf-8'), response) + else: + self.assertEqual(handlr.return_value, response) class AbstractDockerContainerTestCase(TestCase): From 32c5b56a1d40323050cfb6654ac76f6b1382fa49 Mon Sep 17 00:00:00 2001 From: Jan Hutar Date: Thu, 29 Mar 2018 22:57:43 +0200 Subject: [PATCH 4/5] Fix too long line --- tests/test_entities.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_entities.py b/tests/test_entities.py index 6b437a3d..d1ea8b7a 100644 --- a/tests/test_entities.py +++ b/tests/test_entities.py @@ -1775,7 +1775,8 @@ def test_generic(self): self.assertEqual(client_request.call_args[1], kwargs) self.assertEqual(handlr.call_count, 1) if method in self.decoded_responses: - self.assertEqual(handlr.return_value.decode('utf-8'), response) + self.assertEqual(handlr.return_value.decode('utf-8'), + response) else: self.assertEqual(handlr.return_value, response) From ebce060156a0a51b596ac7d58f601f18978a79ea Mon Sep 17 00:00:00 2001 From: Lukas Pramuk Date: Tue, 3 Apr 2018 15:20:15 +0200 Subject: [PATCH 5/5] This makes it easier - thank you lpramuk --- nailgun/entities.py | 5 +++-- tests/test_entities.py | 11 +---------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/nailgun/entities.py b/nailgun/entities.py index 83ee620c..794c2dc0 100644 --- a/nailgun/entities.py +++ b/nailgun/entities.py @@ -122,6 +122,8 @@ def _handle_response(response, server_config, synchronous=False, timeout=None): return if 'application/json' in response.headers.get('content-type', '').lower(): return response.json() + elif isinstance(response.content, bytes): + return response.content.decode('utf-8') else: return response.content @@ -4383,8 +4385,7 @@ def download_debug_certificate(self, synchronous=True, **kwargs): kwargs.update(self._server_config.get_client_kwargs()) response = client.get( self.path('download_debug_certificate'), **kwargs) - return _handle_response(response, self._server_config, synchronous) \ - .decode('utf-8') + return _handle_response(response, self._server_config, synchronous) class OSDefaultTemplate( diff --git a/tests/test_entities.py b/tests/test_entities.py index d1ea8b7a..cc925d35 100644 --- a/tests/test_entities.py +++ b/tests/test_entities.py @@ -1661,8 +1661,6 @@ def setUpClass(cls): entity_obj3.method2, 'put', ) - ``decoded_responses`` is a tuple of methods whose result was decoded - into string. """ cfg = config.ServerConfig('http://example.com') generic = {'server_config': cfg, 'id': 1} @@ -1744,9 +1742,6 @@ def setUpClass(cls): (entities.SyncPlan(**sync_plan).add_products, 'put'), (entities.SyncPlan(**sync_plan).remove_products, 'put'), ) - cls.decoded_responses = ( - entities.Organization(**generic).download_debug_certificate, - ) def test_generic(self): """Check that a variety of helper methods are sane. @@ -1774,11 +1769,7 @@ def test_generic(self): self.assertEqual(len(client_request.call_args[0]), 1) self.assertEqual(client_request.call_args[1], kwargs) self.assertEqual(handlr.call_count, 1) - if method in self.decoded_responses: - self.assertEqual(handlr.return_value.decode('utf-8'), - response) - else: - self.assertEqual(handlr.return_value, response) + self.assertEqual(handlr.return_value, response) class AbstractDockerContainerTestCase(TestCase):