Repository: libcloud Updated Branches: refs/heads/trunk 5be8b04fe -> 8a4de206e
fix a bug where creating a connection from URL would not work when request actions start with /. Urlparse instead of ''.join Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/e02a8f0e Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/e02a8f0e Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/e02a8f0e Branch: refs/heads/trunk Commit: e02a8f0e3f8af56dfc64c547c4c44b15250b447d Parents: f4c00ea Author: Anthony Shaw <anthonys...@apache.org> Authored: Fri Jan 13 13:07:05 2017 +1100 Committer: Anthony Shaw <anthonys...@apache.org> Committed: Fri Jan 13 13:07:05 2017 +1100 ---------------------------------------------------------------------- libcloud/common/base.py | 4 +--- libcloud/httplib_ssl.py | 12 ++++++++++-- libcloud/test/test_connection.py | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/e02a8f0e/libcloud/common/base.py ---------------------------------------------------------------------- diff --git a/libcloud/common/base.py b/libcloud/common/base.py index d33872e..c045cd2 100644 --- a/libcloud/common/base.py +++ b/libcloud/common/base.py @@ -674,9 +674,7 @@ class Connection(object): return response def morph_action_hook(self, action): - if not action.startswith("/"): - action = "/" + action - return self.request_path + action + return urlparse.urljoin(self.request_path, action) def add_default_params(self, params): """ http://git-wip-us.apache.org/repos/asf/libcloud/blob/e02a8f0e/libcloud/httplib_ssl.py ---------------------------------------------------------------------- diff --git a/libcloud/httplib_ssl.py b/libcloud/httplib_ssl.py index 8bd9d89..913ad6c 100644 --- a/libcloud/httplib_ssl.py +++ b/libcloud/httplib_ssl.py @@ -182,16 +182,24 @@ class LibcloudConnection(LibcloudBaseConnection): self.set_http_proxy(proxy_url=proxy_url) self.session.timeout = kwargs.get('timeout', 60) + @property + def verification(self): + """ + The option for SSL verification given to underlying requests + """ + return self.ca_cert if self.ca_cert is not None else self.verify + def request(self, method, url, body=None, headers=None, raw=False, stream=False): + url = urlparse.urljoin(self.host, url) self.response = self.session.request( method=method.lower(), - url=''.join([self.host, url]), + url=url, data=body, headers=headers, allow_redirects=1, stream=stream, - verify=self.ca_cert if self.ca_cert is not None else self.verify + verify=self.verification ) def prepared_request(self, method, url, body=None, http://git-wip-us.apache.org/repos/asf/libcloud/blob/e02a8f0e/libcloud/test/test_connection.py ---------------------------------------------------------------------- diff --git a/libcloud/test/test_connection.py b/libcloud/test/test_connection.py index d58ff3f..d4bca52 100644 --- a/libcloud/test/test_connection.py +++ b/libcloud/test/test_connection.py @@ -21,6 +21,8 @@ import ssl from mock import Mock, patch +import requests_mock + from libcloud.test import unittest from libcloud.common.base import Connection from libcloud.httplib_ssl import LibcloudBaseConnection @@ -109,6 +111,18 @@ class BaseConnectionClassTestCase(unittest.TestCase): conn = LibcloudConnection(host='localhost', port=80) self.assertEqual(conn.host, 'http://localhost') + def test_connection_url_merging(self): + """ + Test that the connection class will parse URLs correctly + """ + conn = Connection(url='http://test.com/') + conn.connect() + self.assertEqual(conn.connection.host, 'http://test.com') + with requests_mock.mock() as m: + m.get('http://test.com/test', text='data') + response = conn.request('/test') + self.assertEqual(response.body, 'data') + def test_secure_connection_unusual_port(self): """ Test that the connection class will default to secure (https) even