libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anthonys...@apache.org
Subject [06/10] libcloud git commit: fix morph_action_hook and create a list of tests to make sure every scenarion works as expected
Date Fri, 13 Jan 2017 03:30:33 GMT
fix morph_action_hook and create a list of tests to make sure every scenarion works as expected


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/fedace00
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/fedace00
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/fedace00

Branch: refs/heads/trunk
Commit: fedace00261bcbb0a45bda003b968101a897d4ed
Parents: 57e8b62
Author: Anthony Shaw <anthonyshaw@apache.org>
Authored: Fri Jan 13 14:04:28 2017 +1100
Committer: Anthony Shaw <anthonyshaw@apache.org>
Committed: Fri Jan 13 14:04:28 2017 +1100

----------------------------------------------------------------------
 libcloud/common/base.py          |  7 ++++++-
 libcloud/test/test_connection.py | 37 +++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/fedace00/libcloud/common/base.py
----------------------------------------------------------------------
diff --git a/libcloud/common/base.py b/libcloud/common/base.py
index c045cd2..c5759f3 100644
--- a/libcloud/common/base.py
+++ b/libcloud/common/base.py
@@ -674,7 +674,12 @@ class Connection(object):
         return response
 
     def morph_action_hook(self, action):
-        return urlparse.urljoin(self.request_path, action)
+        url = urlparse.urljoin(self.request_path.lstrip('/').rstrip('/') +
+                               '/', action.lstrip('/'))
+        if not url.startswith('/'):
+            return '/' + url
+        else:
+            return url
 
     def add_default_params(self, params):
         """

http://git-wip-us.apache.org/repos/asf/libcloud/blob/fedace00/libcloud/test/test_connection.py
----------------------------------------------------------------------
diff --git a/libcloud/test/test_connection.py b/libcloud/test/test_connection.py
index d4bca52..8a4e9b6 100644
--- a/libcloud/test/test_connection.py
+++ b/libcloud/test/test_connection.py
@@ -123,6 +123,43 @@ class BaseConnectionClassTestCase(unittest.TestCase):
             response = conn.request('/test')
         self.assertEqual(response.body, 'data')
 
+    def test_morph_action_hook(self):
+        conn = Connection(url="http://test.com")
+
+        conn.request_path = ''
+        self.assertEqual(conn.morph_action_hook('/test'), '/test')
+        self.assertEqual(conn.morph_action_hook('test'), '/test')
+
+        conn.request_path = '/v1'
+        self.assertEqual(conn.morph_action_hook('/test'), '/v1/test')
+        self.assertEqual(conn.morph_action_hook('test'), '/v1/test')
+
+        conn.request_path = '/v1'
+        self.assertEqual(conn.morph_action_hook('/test'), '/v1/test')
+        self.assertEqual(conn.morph_action_hook('test'), '/v1/test')
+
+        conn.request_path = 'v1'
+        self.assertEqual(conn.morph_action_hook('/test'), '/v1/test')
+        self.assertEqual(conn.morph_action_hook('test'), '/v1/test')
+
+        conn.request_path = 'v1/'
+        self.assertEqual(conn.morph_action_hook('/test'), '/v1/test')
+        self.assertEqual(conn.morph_action_hook('test'), '/v1/test')
+
+    def test_connect_with_prefix(self):
+        """
+        Test that a connection with a base path (e.g. /v1/) will
+        add the base path to requests
+        """
+        conn = Connection(url='http://test.com/')
+        conn.connect()
+        conn.request_path = '/v1'
+        self.assertEqual(conn.connection.host, 'http://test.com')
+        with requests_mock.mock() as m:
+            m.get('http://test.com/v1/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


Mime
View raw message