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
This commit is contained in:
@@ -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(']')
|
||||
|
||||
@@ -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'))
|
||||
|
||||
Reference in New Issue
Block a user