Return valid url when querying versions endpoint

Use rpartition instead of replace to do a more targeted stripping of
path to render a correct base_uri.

Change-Id: Idc2a5a6bd0e9e2675883f2188d4856b01ba5fdb8
Story: 2006147
Task: 35643
This commit is contained in:
Bharat Kunwar 2019-07-25 10:30:56 +01:00
parent 29e25e6e29
commit 2f2593fcce
2 changed files with 22 additions and 13 deletions

View File

@ -85,7 +85,7 @@ def _get_common_links(req):
self_uri = req.uri
if six.PY2:
self_uri = self_uri.decode(rest_utils.ENCODING)
base_uri = self_uri.replace(req.path, '')
base_uri = self_uri.rpartition(req.path)[0]
return [
{
'rel': 'self',
@ -106,7 +106,7 @@ def _parse_version(version_id, req):
self_uri = req.uri
if six.PY2:
self_uri = self_uri.decode(rest_utils.ENCODING)
base_uri = self_uri.replace(req.path, '')
base_uri = self_uri.rpartition(req.path)[0]
# need to get template dict, consecutive calls
# needs to operate on unmodified instance

View File

@ -27,6 +27,7 @@ class TestApiVersions(base.BaseApiTestCase):
def setUp(self):
super(TestApiVersions, self).setUp()
self.versions = versions.Versions()
self.app.add_route("/", self.versions)
self.app.add_route("/version/", self.versions)
self.app.add_route("/version/{version_id}", self.versions)
@ -73,23 +74,31 @@ class TestApiVersions(base.BaseApiTestCase):
self.assertIn('rel', link)
key = link.get('rel')
self.assertIn(key, expected_links_keys)
href = link.get('href')
self.assertTrue(href.startswith(expected_url))
expected_versions = 'v2.0', 'v3.0'
expected_links_keys = 'self', 'version', 'healthcheck'
expected_protocol = 'http'
expected_host = 'fakehost.com'
expected_url = '{}://{}'.format(expected_protocol, expected_host)
res = self.simulate_request(
path='/version',
method='GET',
headers={
'Content-Type': 'application/json'
}
)
self.assertEqual(falcon.HTTP_200, res.status)
for expected_path in ['/', '/version']:
res = self.simulate_request(
path=expected_path,
protocol=expected_protocol,
host=expected_host,
method='GET',
headers={
'Content-Type': 'application/json'
}
)
self.assertEqual(falcon.HTTP_200, res.status)
response = res.json
response = res.json
_check_elements()
_check_global_links()
_check_elements()
_check_global_links()
def test_should_return_expected_version_id(self):
expected_versions = 'v2.0', 'v3.0'