diff --git a/glance/api/versions.py b/glance/api/versions.py index 1d2eea8768..308dd6e29b 100644 --- a/glance/api/versions.py +++ b/glance/api/versions.py @@ -17,6 +17,7 @@ from oslo_config import cfg from oslo_log import log as logging from oslo_serialization import jsonutils from six.moves import http_client +from six.moves import urllib import webob.dec from glance.common import wsgi @@ -59,14 +60,17 @@ class Controller(object): def index(self, req, explicit=False): """Respond to a request for all OpenStack API versions.""" def build_version_object(version, path, status): - url = CONF.public_endpoint or req.host_url + url = CONF.public_endpoint or req.application_url + # Always add '/' to url end for urljoin href url + url = url.rstrip('/') + '/' + href = urllib.parse.urljoin(url, path).rstrip('/') + '/' return { 'id': 'v%s' % version, 'status': status, 'links': [ { 'rel': 'self', - 'href': '%s/%s/' % (url, path), + 'href': '%s' % href, }, ], } diff --git a/glance/tests/unit/test_versions.py b/glance/tests/unit/test_versions.py index d8cd12b179..d7bf9e9946 100644 --- a/glance/tests/unit/test_versions.py +++ b/glance/tests/unit/test_versions.py @@ -135,6 +135,17 @@ class VersionsTest(base.IsolatedUnitTest): expected = self._get_versions_list(ssl_url) self.assertEqual(expected, results) + def test_get_version_list_for_external_app(self): + url = 'http://customhost:9292/app/api' + req = webob.Request.blank('/', base_url=url) + self.config(bind_host='127.0.0.1', bind_port=9292) + res = versions.Controller().index(req) + self.assertEqual(http.MULTIPLE_CHOICES, res.status_int) + self.assertEqual('application/json', res.content_type) + results = jsonutils.loads(res.body)['versions'] + expected = self._get_versions_list(url) + self.assertEqual(expected, results) + class VersionNegotiationTest(base.IsolatedUnitTest):