def _first_hook(req, resp, resource, params):
if resource.req_ids is None:
raise falcon.HTTPBadRequest(title='Append request id failed',
description='Append request id failed')
if((req.env['calplus.cloud'] != 'cloud1') or
('request-id' not in req.env)):
raise falcon.HTTPBadRequest(title='Process Request Error',
description='Problem when process request')
if not req.client_accepts_json:
raise falcon.HTTPNotAcceptable(
'This API only supports responses encoded as JSON.',
href='http://docs.examples.com/api/json')
if req.method in ('POST', 'PUT'):
if 'application/json' not in req.content_type:
raise falcon.HTTPUnsupportedMediaType(
'This API only supports requests encoded as JSON.',
href='http://docs.examples.com/api/json')
python类HTTPUnsupportedMediaType()的实例源码
def process_request(self, req, resp):
"""
:param req: Falcon request
:type req: falcon.request.Request
:param resp: Falcon response
:type resp: falcon.response.Response
"""
if not req.client_accepts_json:
raise falcon.HTTPNotAcceptable(
'This API only supports responses encoded as JSON.',
href='http://docs.examples.com/api/json')
if req.method in ('POST', 'PUT', 'PATCH'):
if req.content_type is None or 'application/json' not in req.content_type:
raise falcon.HTTPUnsupportedMediaType(
'This API only supports requests encoded as JSON.',
href='http://docs.examples.com/api/json')
def process_request(self, req, resp):
"""Performs content type enforcement on behalf of REST verbs."""
valid_content_types = ['application/x-yaml']
# GET and DELETE should never carry a message body, and have
# no content type. Check for content-length or
# transfer-encoding to determine if a content-type header
# is required.
requires_content_type = (
req.method not in ['GET', 'DELETE'] and (
(req.content_length is not None and
req.content_length > 0) or
req.get_header('transfer-encoding') is not None
)
)
if requires_content_type:
content_type = (req.content_type.split(';', 1)[0].strip()
if req.content_type else '')
if not content_type:
raise falcon.HTTPMissingHeader('Content-Type')
elif content_type not in valid_content_types:
message = (
"Unexpected content type: {type}. Expected content types "
"are: {expected}."
).format(
type=six.b(req.content_type).decode('utf-8'),
expected=valid_content_types
)
raise falcon.HTTPUnsupportedMediaType(description=message)
def process_request(self, req, resp):
if not req.client_accepts_json:
raise falcon.HTTPNotAcceptable(
'This API only supports responses encoded as JSON.',
href='http://docs.examples.com/api/json')
if req.method in ('POST', 'PUT'):
if 'application/json' not in req.content_type:
raise falcon.HTTPUnsupportedMediaType(
'This API only supports requests encoded as JSON.',
href='http://docs.examples.com/api/json')
def test_http_unsupported_media_type_no_title_and_desc_and_challenges():
try:
raise falcon.HTTPUnsupportedMediaType()
except falcon.HTTPUnsupportedMediaType as e:
assert e.description is None
def test_http_unsupported_media_type_with_title_and_desc_and_challenges():
try:
raise falcon.HTTPUnsupportedMediaType(description='boom')
except falcon.HTTPUnsupportedMediaType as e:
assert e.description == 'boom'
def test_misc(self, client):
self._misc_test(client, falcon.HTTPBadRequest, falcon.HTTP_400)
self._misc_test(client, falcon.HTTPNotAcceptable, falcon.HTTP_406,
needs_title=False)
self._misc_test(client, falcon.HTTPConflict, falcon.HTTP_409)
self._misc_test(client, falcon.HTTPPreconditionFailed, falcon.HTTP_412)
self._misc_test(client, falcon.HTTPUnsupportedMediaType, falcon.HTTP_415,
needs_title=False)
self._misc_test(client, falcon.HTTPUnprocessableEntity, falcon.HTTP_422)
self._misc_test(client, falcon.HTTPUnavailableForLegalReasons, falcon.HTTP_451,
needs_title=False)
self._misc_test(client, falcon.HTTPInternalServerError, falcon.HTTP_500)
self._misc_test(client, falcon.HTTPBadGateway, falcon.HTTP_502)
def _validate_content_type(req):
"""Validate content type.
Function validates request against correct content type.
If Content-Type cannot be established (i.e. header is missing),
:py:class:`falcon.HTTPMissingHeader` is thrown.
If Content-Type is not **application/json**(supported contents
types are define in SUPPORTED_CONTENT_TYPES variable),
:py:class:`falcon.HTTPUnsupportedMediaType` is thrown.
:param falcon.Request req: current request
:exception: :py:class:`falcon.HTTPMissingHeader`
:exception: :py:class:`falcon.HTTPUnsupportedMediaType`
"""
content_type = req.content_type
LOG.debug('Content-type is {0}'.format(content_type))
if content_type is None or len(content_type) == 0:
raise falcon.HTTPMissingHeader('Content-Type')
if content_type not in SUPPORTED_CONTENT_TYPES:
types = ','.join(SUPPORTED_CONTENT_TYPES)
details = ('Only [{0}] are accepted as events representation'.
format(types))
raise falcon.HTTPUnsupportedMediaType(description=details)
test_validation_middleware.py 文件源码
项目:monasca-events-api
作者: openstack
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def test_should_fail_unsupported_content_type(self):
req = FakeRequest('test/plain')
self.assertRaises(falcon.HTTPUnsupportedMediaType,
vm._validate_content_type,
req)
def process_request(self, req, resp):
if not req.client_accepts_json:
raise falcon.HTTPNotAcceptable(
'This API only supports requests encoded as JSON.',
href='http://docs.examples.com/api/json')
if req.method in ('POST', 'PUT'):
if 'application/json' not in req.content_type:
raise falcon.HTTPUnsupportedMediaType(
'This API only supports requests encoded as JSON.',
href='http://docs.examples.com/api/json')
def process_request(self, req, resp):
if not req.client_accepts_json:
raise falcon.HTTPNotAcceptable('This API only supports responses encoded as JSON.')
if req.method in ('POST', 'PUT') and req.content_length not in (None, 0):
if 'application/json' not in req.content_type:
raise falcon.HTTPUnsupportedMediaType('This API only supports requests encoded as JSON.')
def require_json(req, resp, resource, params):
if req.method in ('POST', 'PUT', 'PATCH'):
if req.content_type is None or 'application/json' not in req.content_type:
raise falcon.HTTPUnsupportedMediaType(
'This API only supports requests encoded as JSON.',
href='http://docs.examples.com/api/json')
def process_request(self, req, resp):
if not req.client_accepts_json:
raise falcon.HTTPNotAcceptable(
'This API only supports responses encoded as JSON.',
href='http://docs.examples.com/api/json')
if req.method in ('POST', 'PUT'):
if 'application/json' not in req.content_type:
raise falcon.HTTPUnsupportedMediaType(
'This API only supports requests encoded as JSON.',
href='http://docs.examples.com/api/json')