Normalize url without port with schema default port

In function get_bare_url, it will assert url startswith base_url,
if base_url is 'http://neutron.openstack.svc.cluster.local:80/',
while url is 'http://neutron.openstack.svc.cluster.local/v2.0/router/...',
it will raise error. here need to add default schema port 80 to url to
fix this problem.

Change-Id: I44d623d3d0d96711f7ca93fc24b40c024dd86446
Closes-Bug: #1829962
This commit is contained in:
zheng.yong 2019-05-22 14:16:14 +08:00 committed by yong.zheng
parent 22c547853e
commit 74e760a46e
2 changed files with 25 additions and 2 deletions

View File

@ -1111,8 +1111,10 @@ class BaseSearchCriteriaTest(BaseNetworkTest):
def get_bare_url(self, url):
base_url = self.client.base_url
self.assertTrue(url.startswith(base_url))
return url[len(base_url):]
base_url_normalized = utils.normalize_url(base_url)
url_normalized = utils.normalize_url(url)
self.assertTrue(url_normalized.startswith(base_url_normalized))
return url_normalized[len(base_url_normalized):]
@classmethod
def _extract_resources(cls, body):

View File

@ -21,9 +21,18 @@
import functools
import threading
import time
try:
import urlparse
except ImportError:
from urllib import parse as urlparse
import eventlet
SCHEMA_PORT_MAPPING = {
"http": 80,
"https": 443,
}
class classproperty(object):
def __init__(self, f):
@ -102,3 +111,15 @@ def override_class(overriden_class, overrider_class):
bases = (overrider_class, overriden_class)
overriden_class = type(name, bases, {})
return overriden_class
def normalize_url(url):
"""Normalize url without port with schema default port
"""
parse_result = urlparse.urlparse(url)
(scheme, netloc, url, params, query, fragment) = parse_result
port = parse_result.port
if scheme in SCHEMA_PORT_MAPPING and not port:
netloc = netloc + ":" + str(SCHEMA_PORT_MAPPING[scheme])
return urlparse.urlunparse((scheme, netloc, url, params, query, fragment))