feat(wsgi): check for client media type support

Change-Id: I2c473bb158c68fe87671e4dea19f9063e33b3f58
Fixes: bug #1177947
This commit is contained in:
Zhihao Yuan 2013-07-15 14:08:27 -04:00 committed by Zhihao Yuan
parent 296b5add2a
commit 4fc292513d
2 changed files with 58 additions and 1 deletions

View File

@ -0,0 +1,47 @@
# Copyright (c) 2013 Rackspace, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import falcon
from falcon import testing
from marconi.tests.transport.wsgi import base
class TestWSGIMediaType(base.TestBase):
config_filename = 'wsgi_sqlite.conf'
def test_json_only_endpoints(self):
headers = {'Client-ID': '30387f00',
'Accept': 'application/xml'}
endpoints = [
('GET', '/v1/queues'),
('GET', '/v1/queues/nonexistent'),
('GET', '/v1/queues/nonexistent/stats'),
('POST', '/v1/queues/nonexistent/messages'),
('GET', '/v1/queues/nonexistent/messages/deadbeaf'),
('POST', '/v1/queues/nonexistent/claims'),
('GET', '/v1/queues/nonexistent/claims/0ad'),
('GET', '/v1/health'),
]
for method, endpoint in endpoints:
env = testing.create_environ(endpoint,
method=method,
headers=headers)
self.app(env, self.srmock)
self.assertEquals(self.srmock.status, falcon.HTTP_406)

View File

@ -38,6 +38,16 @@ WSGI_CFG = config.namespace('drivers:transport:wsgi').from_options(**OPTIONS)
LOG = logging.getLogger(__name__)
def _check_media_type(req, resp, params):
if not req.client_accepts('application/json'):
raise falcon.HTTPNotAcceptable(
'''
Endpoint only serves `application/json`; specify client-side
media type support with the "Accept" header.''',
href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html",
href_text='"14.1 Accept", Hypertext Transfer Protocol -- HTTP/1.1')
def _extract_project_id(req, resp, params):
params['project_id'] = req.get_header('X-PROJECT-ID')
@ -52,7 +62,7 @@ class Driver(transport.DriverBase):
def _init_routes(self):
"""Initialize URI routes to resources."""
self.app = falcon.API(before=_extract_project_id)
self.app = falcon.API(before=[_check_media_type, _extract_project_id])
queue_controller = self.storage.queue_controller
message_controller = self.storage.message_controller