Enhance V3 version controller to provide JSON Home response

The V3 version controller would only respond with the normal version
JSON response. With this change, if the Accept header is
`application/json-home` it will respond with a JSON Home response
which includes all the registered JSON Home data. Note that there is
no JSON Home data yet so the response is empty.

bp json-home

Change-Id: Icfde89040b376bd6704d497966bd8ad109e32d08
This commit is contained in:
Brant Knudson 2014-08-02 19:16:52 -05:00
parent 47723ed130
commit ed2fb9d59e
3 changed files with 40 additions and 1 deletions

View File

@ -686,7 +686,10 @@ def render_response(body=None, status=None, headers=None, method=None):
content_type = content_types[0]
else:
content_type = None
if content_type is None or content_type == 'application/json':
JSON_ENCODE_CONTENT_TYPES = ('application/json',
'application/json-home',)
if content_type is None or content_type in JSON_ENCODE_CONTENT_TYPES:
body = jsonutils.dumps(body, cls=utils.SmarterEncoder)
if content_type is None:
headers.append(('Content-Type', 'application/json'))

View File

@ -143,9 +143,25 @@ class Version(wsgi.Application):
else:
raise exception.VersionNotFound(version='v2.0')
def _get_json_home_v3(self):
def all_resources():
for router in self._routers:
for resource in router.v3_resources:
yield resource
return {
'resources': dict(all_resources())
}
def get_version_v3(self, context):
versions = self._get_versions_list(context)
if 'v3' in _VERSIONS:
if context['headers'].get('Accept') == 'application/json-home':
return wsgi.render_response(
body=self._get_json_home_v3(),
headers=(('Content-Type', 'application/json-home'),))
return wsgi.render_response(body={
'version': versions['v3']
})

View File

@ -16,6 +16,7 @@
import random
import mock
from testtools import matchers as tt_matchers
from keystone import config
from keystone import controllers
@ -101,6 +102,8 @@ VERSIONS_RESPONSE = {
}
}
V3_JSON_HOME_RESOURCES = {}
class VersionTestCase(tests.TestCase):
def setUp(self):
@ -296,6 +299,23 @@ class VersionTestCase(tests.TestCase):
data = jsonutils.loads(resp.body)
self.assertEqual(data, v2_only_response)
def test_json_home_v3(self):
# If the request is /v3 and the Accept header is application/json-home
# then the server responds with a JSON Home document.
client = self.client(self.public_app)
resp = client.get('/v3', headers={'Accept': 'application/json-home'})
self.assertThat(resp.status, tt_matchers.Equals('200 OK'))
self.assertThat(resp.headers['Content-Type'],
tt_matchers.Equals('application/json-home'))
exp_json_home_data = {
'resources': V3_JSON_HOME_RESOURCES}
self.assertThat(jsonutils.loads(resp.body),
tt_matchers.Equals(exp_json_home_data))
class XmlVersionTestCase(tests.TestCase):