feat(wsgi): check for client media type support
Change-Id: I2c473bb158c68fe87671e4dea19f9063e33b3f58 Fixes: bug #1177947
This commit is contained in:
parent
296b5add2a
commit
4fc292513d
47
marconi/tests/transport/wsgi/test_media_type.py
Normal file
47
marconi/tests/transport/wsgi/test_media_type.py
Normal 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)
|
@ -38,6 +38,16 @@ WSGI_CFG = config.namespace('drivers:transport:wsgi').from_options(**OPTIONS)
|
|||||||
LOG = logging.getLogger(__name__)
|
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):
|
def _extract_project_id(req, resp, params):
|
||||||
params['project_id'] = req.get_header('X-PROJECT-ID')
|
params['project_id'] = req.get_header('X-PROJECT-ID')
|
||||||
|
|
||||||
@ -52,7 +62,7 @@ class Driver(transport.DriverBase):
|
|||||||
|
|
||||||
def _init_routes(self):
|
def _init_routes(self):
|
||||||
"""Initialize URI routes to resources."""
|
"""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
|
queue_controller = self.storage.queue_controller
|
||||||
message_controller = self.storage.message_controller
|
message_controller = self.storage.message_controller
|
||||||
|
Loading…
Reference in New Issue
Block a user