Skip to content

Commit

Permalink
return 404 for invalid api version request
Browse files Browse the repository at this point in the history
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
  • Loading branch information
markmcclain authored and Gary Kotton committed Jun 17, 2012
1 parent 3b52fd3 commit 18c043c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
3 changes: 3 additions & 0 deletions quantum/api/versions.py
Expand Up @@ -40,6 +40,9 @@ def __call__(self, req):
},
]

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)
Expand Down
37 changes: 34 additions & 3 deletions 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
Expand All @@ -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

Expand Down Expand Up @@ -360,3 +363,31 @@ def test_port_multiple_filters(self):
# 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)
5 changes: 2 additions & 3 deletions quantum/tests/unit/testlib_api.py
@@ -1,5 +1,4 @@
import webob

from quantum import wsgi
from quantum.common.serializer import Serializer


Expand All @@ -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
Expand Down

0 comments on commit 18c043c

Please sign in to comment.