diff --git a/SoftLayer/tests/functional_tests.py b/SoftLayer/tests/functional_tests.py index 025e59ed1..d121f9958 100644 --- a/SoftLayer/tests/functional_tests.py +++ b/SoftLayer/tests/functional_tests.py @@ -37,10 +37,10 @@ def test_no_hostname(self): # This test will fail if 'notvalidsoftlayer.com' becomes a thing SoftLayer.transports.make_xml_rpc_api_call( 'http://notvalidsoftlayer.com', 'getObject') - except SoftLayer.SoftLayerAPIError as e: - self.assertEqual(e.faultCode, 0) - self.assertIn('not known', e.faultString) - self.assertIn('not known', e.reason) + except SoftLayer.SoftLayerAPIError as ex: + self.assertIn('not known', str(ex)) + self.assertIn('not known', ex.faultString) + self.assertEqual(ex.faultCode, 0) else: self.fail('No Exception Raised') diff --git a/SoftLayer/tests/transport_tests.py b/SoftLayer/tests/transport_tests.py index 196795dbc..a0e82f558 100644 --- a/SoftLayer/tests/transport_tests.py +++ b/SoftLayer/tests/transport_tests.py @@ -15,7 +15,8 @@ class TestXmlRpcAPICall(testing.TestCase): def set_up(self): - self.send_content = ''' + self.response = mock.MagicMock() + self.response.content = ''' @@ -26,9 +27,9 @@ def set_up(self): ''' - @mock.patch('SoftLayer.transports.requests.Session.send') - def test_call(self, send): - send().content = self.send_content + @mock.patch('requests.request') + def test_call(self, request): + request.return_value = self.response data = ''' @@ -46,33 +47,40 @@ def test_call(self, send): ''' resp = transports.make_xml_rpc_api_call( 'http://something.com/path/to/resource', 'getObject') - args = send.call_args + args = request.call_args self.assertIsNotNone(args) args, kwargs = args - send.assert_called_with(mock.ANY, proxies=None, timeout=None) + request.assert_called_with('POST', + 'http://something.com/path/to/resource', + headers=None, + proxies=None, + data=data, + timeout=None) self.assertEqual(resp, []) - self.assertEqual(args[0].body, data) def test_proxy_without_protocol(self): self.assertRaises( - SoftLayer.TransportError, + SoftLayer.TransportError, # NOQA transports.make_xml_rpc_api_call, 'http://something.com/path/to/resource', 'getObject', 'localhost:3128') - @mock.patch('SoftLayer.transports.requests.Session.send') - def test_valid_proxy(self, send): - send().content = self.send_content + @mock.patch('requests.request') + def test_valid_proxy(self, request): + request.return_value = self.response transports.make_xml_rpc_api_call( 'http://something.com/path/to/resource', 'getObject', proxy='http://localhost:3128') - send.assert_called_with( + request.assert_called_with( + 'POST', mock.ANY, + headers=None, proxies={'https': 'http://localhost:3128', 'http': 'http://localhost:3128'}, + data=mock.ANY, timeout=None) diff --git a/SoftLayer/transports.py b/SoftLayer/transports.py index 364c60f24..44fc9812c 100644 --- a/SoftLayer/transports.py +++ b/SoftLayer/transports.py @@ -42,17 +42,16 @@ def make_xml_rpc_api_call(uri, method, args=None, headers=None, payload = utils.xmlrpc_client.dumps(tuple(largs), methodname=method, allow_none=True) - session = requests.Session() - req = requests.Request('POST', uri, data=payload, - headers=http_headers).prepare() LOGGER.debug("=== REQUEST ===") LOGGER.info('POST %s', uri) - LOGGER.debug(req.headers) + LOGGER.debug(http_headers) LOGGER.debug(payload) - response = session.send(req, - timeout=timeout, - proxies=_proxies_dict(proxy)) + response = requests.request('POST', uri, + data=payload, + headers=http_headers, + timeout=timeout, + proxies=_proxies_dict(proxy)) LOGGER.debug("=== RESPONSE ===") LOGGER.debug(response.headers) LOGGER.debug(response.content) @@ -91,14 +90,18 @@ def make_rest_api_call(method, url, :param dict http_headers: HTTP headers to use for the request :param int timeout: number of seconds to use as a timeout """ + LOGGER.debug("=== REQUEST ===") LOGGER.info('%s %s', method, url) + LOGGER.debug(http_headers) try: resp = requests.request(method, url, headers=http_headers, timeout=timeout, proxies=_proxies_dict(proxy)) - resp.raise_for_status() + LOGGER.debug("=== RESPONSE ===") + LOGGER.debug(resp.headers) LOGGER.debug(resp.content) + resp.raise_for_status() if url.endswith('.json'): return json.loads(resp.content) else: diff --git a/tox.ini b/tox.ini index 8e68e57e4..5abdf9e0d 100644 --- a/tox.ini +++ b/tox.ini @@ -21,10 +21,11 @@ deps = hacking pylint commands = - flake8 --max-complexity=36 --statistics \ + flake8 --max-complexity=36 \ --ignore=H401,H402,H404,H405 \ SoftLayer pylint SoftLayer \ + -r n \ # Don't show the long report --ignore=tests,testing \ -d R0903 \ # Too few public methods -d R0914 \ # Too many local variables