From 102c917be984079e53e729cc50554c96ba24f248 Mon Sep 17 00:00:00 2001 From: Mark McClain Date: Mon, 9 Apr 2012 10:08:00 -0400 Subject: [PATCH] return 404 for invalid api version request fixes bug: 934115 This fix returns 404 for all non-root requests that route via the versions app. For root requests ('/') the available api version info is returned. Change-Id: I701389d9239cb40426f7a47206642b56c7eeeae1 --- quantum/api/versions.py | 3 +++ quantum/tests/unit/test_api.py | 37 ++++++++++++++++++++++++++++--- quantum/tests/unit/testlib_api.py | 5 ++--- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/quantum/api/versions.py b/quantum/api/versions.py index db271f700..63f6e0203 100644 --- a/quantum/api/versions.py +++ b/quantum/api/versions.py @@ -40,6 +40,9 @@ class Versions(wsgi.Application): }, ] + if req.path != '/': + return webob.exc.HTTPNotFound() + builder = versions_view.get_view_builder(req) versions = [builder.build(version) for version in version_objs] response = dict(versions=versions) diff --git a/quantum/tests/unit/test_api.py b/quantum/tests/unit/test_api.py index ea49ae0e6..7bdf76ae1 100644 --- a/quantum/tests/unit/test_api.py +++ b/quantum/tests/unit/test_api.py @@ -1,6 +1,6 @@ # vim: tabstop=4 shiftwidth=4 softtabstop=4 -# Copyright 2010-2011 ???? +# Copyright 2010-2012 ???? # All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may @@ -16,13 +16,16 @@ # under the License. # @author: Salvatore Orlando, Citrix Systems - +import json import logging -from webob import exc +import unittest +from lxml import etree +from webob import exc, request import quantum.api.attachments as atts import quantum.api.networks as nets import quantum.api.ports as ports +import quantum.api.versions as versions import quantum.tests.unit._test_api as test_api import quantum.tests.unit.testlib_api as testlib @@ -360,3 +363,31 @@ class APIFiltersTest(test_api.AbstractAPITest): # Check port count: should return 2 self.assertEqual(len(port_data['ports']), 2) LOG.debug("test_port_multiple_filters - END") + + +class APIRootTest(unittest.TestCase): + def setUp(self): + self.app = versions.Versions() + + def _test_root_responds_with_versions(self, content_type): + req = testlib.create_request('/', '', content_type) + response = self.app(req) + self.assertEquals(response.status_int, 200) + return response.body + + def test_root_responds_with_versions_json(self): + body = self._test_root_responds_with_versions('application/json') + data = json.loads(body) + self.assertEquals('versions', data.keys()[0]) + + def test_root_responds_with_versions_xml(self): + body = self._test_root_responds_with_versions('application/xml') + root = etree.fromstring(body) + self.assertEquals(root.tag, 'versions') + + def test_invalid_version(self): + req = testlib.create_request('/v99.99/tenants/tenantX/networks', + '', + 'application/json') + response = self.app(req) + self.assertEquals(response.status_int, 404) diff --git a/quantum/tests/unit/testlib_api.py b/quantum/tests/unit/testlib_api.py index 8a43895dd..0a8ac8a63 100644 --- a/quantum/tests/unit/testlib_api.py +++ b/quantum/tests/unit/testlib_api.py @@ -1,5 +1,4 @@ -import webob - +from quantum import wsgi from quantum.common.serializer import Serializer @@ -8,7 +7,7 @@ def create_request(path, body, content_type, method='GET', query_string=None): url = "%s?%s" % (path, query_string) else: url = path - req = webob.Request.blank(url) + req = wsgi.Request.blank(url) req.method = method req.headers = {} req.headers['Accept'] = content_type -- 2.45.2