Merge "Add server-supported min and max API version to HTTPNotAcceptable(406)"

This commit is contained in:
Jenkins 2015-03-06 13:56:51 +00:00 committed by Gerrit Code Review
commit 5b8e9bd5c2
2 changed files with 14 additions and 11 deletions

View File

@ -165,32 +165,34 @@ class Controller(rest.RestController):
# the request object to make the links.
return V1.convert()
def _check_version(self, version):
def _check_version(self, version, headers=None):
if not headers:
headers = {}
# ensure that major version in the URL matches the header
if version.major != BASE_VERSION:
raise exc.HTTPNotAcceptable(_(
"Mutually exclusive versions requested. Version %(ver)s "
"requested but not supported by this service.")
% {'ver': version})
"requested but not supported by this service. The supported "
"version range is: [%(min)s, %(max)s].") % {'ver': version,
'min': MIN_VER_STR, 'max': MAX_VER_STR}, headers=headers)
# ensure the minor version is within the supported range
if version < MIN_VER or version > MAX_VER:
raise exc.HTTPNotAcceptable(_(
"Unsupported minor version requested. This API service "
"supports the following version range: "
"[%(min)s, %(max)s].") % {'min': MIN_VER_STR,
'max': MAX_VER_STR})
"Version %(ver)s was requested but the minor version is not "
"supported by this service. The supported version range is: "
"[%(min)s, %(max)s].") % {'ver': version, 'min': MIN_VER_STR,
'max': MAX_VER_STR}, headers=headers)
@pecan.expose()
def _route(self, args):
v = base.Version(pecan.request.headers, MIN_VER_STR, MAX_VER_STR)
# Always set the min and max headers
# FIXME: these are not being sent if _check_version raises an exception
pecan.response.headers[base.Version.min_string] = MIN_VER_STR
pecan.response.headers[base.Version.max_string] = MAX_VER_STR
# assert that requested version is supported
self._check_version(v)
self._check_version(v, pecan.response.headers)
pecan.response.headers[base.Version.string] = str(v)
pecan.request.version = v

View File

@ -58,10 +58,11 @@ class TestCheckVersions(test_base.TestCase):
def test_check_version_too_high(self):
self.version.major = v1_api.BASE_VERSION
self.version.minor = v1_api.MAX_VER.minor + 1
self.assertRaises(
e = self.assertRaises(
webob_exc.HTTPNotAcceptable,
v1_api.Controller()._check_version,
self.version)
self.version, {'fake-headers': v1_api.MAX_VER.minor})
self.assertEqual(v1_api.MAX_VER.minor, e.headers['fake-headers'])
def test_check_version_ok(self):
self.version.major = v1_api.BASE_VERSION