Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions SoftLayer/tests/functional_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
32 changes: 20 additions & 12 deletions SoftLayer/tests/transport_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
class TestXmlRpcAPICall(testing.TestCase):

def set_up(self):
self.send_content = '''<?xml version="1.0" encoding="utf-8"?>
self.response = mock.MagicMock()
self.response.content = '''<?xml version="1.0" encoding="utf-8"?>
<params>
<param>
<value>
Expand All @@ -26,9 +27,9 @@ def set_up(self):
</param>
</params>'''

@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 = '''<?xml version='1.0'?>
<methodCall>
Expand All @@ -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)


Expand Down
19 changes: 11 additions & 8 deletions SoftLayer/transports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down