fix tests to use new prepared_request method
Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/6287bf1b Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/6287bf1b Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/6287bf1b Branch: refs/heads/trunk Commit: 6287bf1bf6b6927960cbfb9443b1b145b7ad8b2f Parents: c078b68 Author: Anthony Shaw <anthonys...@apache.org> Authored: Thu Jan 5 19:19:36 2017 +1100 Committer: Anthony Shaw <anthonys...@apache.org> Committed: Thu Jan 5 19:19:36 2017 +1100 ---------------------------------------------------------------------- libcloud/httplib_ssl.py | 17 +++++++++-------- libcloud/test/__init__.py | 6 ++++++ libcloud/test/storage/test_cloudfiles.py | 2 +- libcloud/test/test_connection.py | 14 ++++++++++---- libcloud/utils/loggingconnection.py | 14 ++++---------- 5 files changed, 30 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/6287bf1b/libcloud/httplib_ssl.py ---------------------------------------------------------------------- diff --git a/libcloud/httplib_ssl.py b/libcloud/httplib_ssl.py index 1955855..168cb68 100644 --- a/libcloud/httplib_ssl.py +++ b/libcloud/httplib_ssl.py @@ -24,7 +24,6 @@ import socket import requests import libcloud.security -from libcloud.utils.py3 import httplib from libcloud.utils.py3 import urlparse @@ -187,17 +186,19 @@ class LibcloudConnection(LibcloudBaseConnection): verify=self.ca_cert if self.ca_cert is not None else self.verify ) - def prepared_request(self, method, url, body=None, headers=None, raw=False): - req = requests.Request(method, ''.join([self.host, url]), data=body, headers=headers) - + def prepared_request(self, method, url, body=None, + headers=None, raw=False): + req = requests.Request(method, ''.join([self.host, url]), + data=body, headers=headers) + prepped = self.session.prepare_request(req) prepped.body = body - - self.response = self.session.send(prepped, + + self.response = self.session.send( + prepped, stream=raw, - verify=self.ca_cert if self.ca_cert is not None else self.verify - ) + verify=self.ca_cert if self.ca_cert is not None else self.verify) def getresponse(self): return self.response http://git-wip-us.apache.org/repos/asf/libcloud/blob/6287bf1b/libcloud/test/__init__.py ---------------------------------------------------------------------- diff --git a/libcloud/test/__init__.py b/libcloud/test/__init__.py index 3d11111..701530a 100644 --- a/libcloud/test/__init__.py +++ b/libcloud/test/__init__.py @@ -107,6 +107,9 @@ class MockResponse(object): def getheaders(self): return list(self.headers.items()) + def iter_content(self, chunk_size): + return self.body_iter + def msg(self): raise NotImplemented @@ -275,6 +278,9 @@ class MockHttpTestCase(MockHttp, unittest.TestCase): class StorageMockHttp(MockHttp): + def prepared_request(self, method, url, body=None, headers=None, raw=False): + pass + def putrequest(self, method, action, skip_host=0, skip_accept_encoding=0): pass http://git-wip-us.apache.org/repos/asf/libcloud/blob/6287bf1b/libcloud/test/storage/test_cloudfiles.py ---------------------------------------------------------------------- diff --git a/libcloud/test/storage/test_cloudfiles.py b/libcloud/test/storage/test_cloudfiles.py index 1489de9..1c11a61 100644 --- a/libcloud/test/storage/test_cloudfiles.py +++ b/libcloud/test/storage/test_cloudfiles.py @@ -398,7 +398,7 @@ class CloudFilesTests(unittest.TestCase): extra = {} def func(*args, **kwargs): - self.assertEqual(kwargs['headers']['Content-Length'], 0) + self.assertEqual(kwargs['headers']['Content-Length'], '0') func.called = True return old_request(*args, **kwargs) http://git-wip-us.apache.org/repos/asf/libcloud/blob/6287bf1b/libcloud/test/test_connection.py ---------------------------------------------------------------------- diff --git a/libcloud/test/test_connection.py b/libcloud/test/test_connection.py index 53cd979..def4201 100644 --- a/libcloud/test/test_connection.py +++ b/libcloud/test/test_connection.py @@ -163,15 +163,21 @@ class ConnectionClassTestCase(unittest.TestCase): for method in ['POST', 'PUT', 'post', 'put']: con.request('/test', method=method, data=None, headers={'Content-Length': '42'}, raw=True) - putheader_call_list = con.connection.putheader.call_args_list - self.assertIn(call('Content-Length', '42'), putheader_call_list) + request_prepared_call_list = con.connection.prepared_request.call_args_list + expected_call = call(body=None, headers={'Host': '127.0.0.1', 'Content-Length': '42', + 'Accept-Encoding': 'gzip,deflate', + 'User-Agent': con._user_agent()}, url='/test', method=method) + self.assertIn(expected_call, request_prepared_call_list) # '' as data, raw request, do not touch Content-Length if present for method in ['POST', 'PUT', 'post', 'put']: con.request('/test', method=method, data=None, headers={'Content-Length': '42'}, raw=True) - putheader_call_list = con.connection.putheader.call_args_list - self.assertIn(call('Content-Length', '42'), putheader_call_list) + request_prepared_call_list = con.connection.prepared_request.call_args_list + expected_call = call(body=None, headers={'Host': '127.0.0.1', 'Content-Length': '42', + 'Accept-Encoding': 'gzip,deflate', + 'User-Agent': con._user_agent()}, url='/test', method=method) + self.assertIn(expected_call, request_prepared_call_list) # 'a' as data, content length should be present for method in ['POST', 'PUT', 'post', 'put']: http://git-wip-us.apache.org/repos/asf/libcloud/blob/6287bf1b/libcloud/utils/loggingconnection.py ---------------------------------------------------------------------- diff --git a/libcloud/utils/loggingconnection.py b/libcloud/utils/loggingconnection.py index 8577da4..4e4174a 100644 --- a/libcloud/utils/loggingconnection.py +++ b/libcloud/utils/loggingconnection.py @@ -39,7 +39,7 @@ from libcloud.utils.misc import lowercase_keys from libcloud.utils.compression import decompress_data -class LoggingBaseConnection(LibcloudConnection): +class LoggingConnection(LibcloudConnection): """ Debug class to log all HTTP(s) requests as they could be made with the curl command. @@ -47,6 +47,9 @@ class LoggingBaseConnection(LibcloudConnection): :cvar log: file-like object that logs entries are written to. """ + protocol = 'https' + port = None + log = None http_proxy_used = False @@ -168,15 +171,6 @@ class LoggingBaseConnection(LibcloudConnection): self.port, url))]) return " ".join(cmd) - -class LoggingConnection(LoggingBaseConnection): - """ - Utility Class for logging HTTPS connections - """ - - protocol = 'https' - port = None - def getresponse(self): r = LibcloudConnection.getresponse(self) if self.log is not None: