summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@gmail.com>2014-04-11 07:32:41 -0700
committerJoshua Harlow <harlowja@gmail.com>2014-04-11 07:32:41 -0700
commitb9e3bb9f23db00703f56c92df8a02797fdcaf6f8 (patch)
tree9ba32f573e047509e50f1656c94aa6b05144bc02
parentf791605aea59a4800c6456b11fbfb9b6a3d59fc5 (diff)
downloadoslo-utils-b9e3bb9f23db00703f56c92df8a02797fdcaf6f8.tar.gz
Avoid raising index error when no host
Strings like 'http://' are not correctly parsing due to an index error raised when the first element of the address is parsed (which is empty). Avoid this by first checking if the address is non-empty before doing work on it. Change-Id: Ic50c9b31083dea82d068423f6809ec13c85da00a
-rw-r--r--openstack/common/network_utils.py6
-rw-r--r--tests/unit/test_network_utils.py7
2 files changed, 12 insertions, 1 deletions
diff --git a/openstack/common/network_utils.py b/openstack/common/network_utils.py
index 490f5a6..731bccb 100644
--- a/openstack/common/network_utils.py
+++ b/openstack/common/network_utils.py
@@ -42,8 +42,12 @@ def parse_host_port(address, default_port=None):
('::1', 1234)
>>> parse_host_port('2001:db8:85a3::8a2e:370:7334', default_port=1234)
('2001:db8:85a3::8a2e:370:7334', 1234)
-
+ >>> parse_host_port(None)
+ (None, None)
"""
+ if not address:
+ return (None, None)
+
if address[0] == '[':
# Escaped ipv6
_host, _port = address[1:].split(']')
diff --git a/tests/unit/test_network_utils.py b/tests/unit/test_network_utils.py
index 8dee0a5..27b87b8 100644
--- a/tests/unit/test_network_utils.py
+++ b/tests/unit/test_network_utils.py
@@ -20,6 +20,13 @@ from openstack.common import network_utils
class NetworkUtilsTest(test_base.BaseTestCase):
+ def test_no_host(self):
+ result = network_utils.urlsplit('http://')
+ self.assertEqual('', result.netloc)
+ self.assertEqual(None, result.port)
+ self.assertEqual(None, result.hostname)
+ self.assertEqual('http', result.scheme)
+
def test_parse_host_port(self):
self.assertEqual(('server01', 80),
network_utils.parse_host_port('server01:80'))