def test_has_body_has_content_type_malformed(self): request = wsgi.Request.blank('/') request.method = 'POST' request.body = encodeutils.safe_encode('asdf') self.assertIn('Content-Length', request.headers) request.headers['Content-Type'] = 'application/json' self.assertFalse(wsgi.JSONRequestDeserializer().has_body(request))
def test_has_body_has_content_length_plain_content_type(self): request = wsgi.Request.blank('/') request.method = 'POST' request.body = encodeutils.safe_encode('{"key": "value"}') self.assertIn('Content-Length', request.headers) request.headers['Content-Type'] = 'text/plain' self.assertTrue(wsgi.JSONRequestDeserializer().has_body(request))
def test_from_json_exceeds_max_json_mb(self): cfg.CONF.set_override('max_json_body_size', 10) body = json.dumps(['a'] * cfg.CONF.max_json_body_size) self.assertTrue(len(body) > cfg.CONF.max_json_body_size) error = self.assertRaises(exception.RequestLimitExceeded, wsgi.JSONRequestDeserializer().from_json, body) msg = 'Request limit exceeded: JSON body size ' + \ '(%s bytes) exceeds maximum allowed size (%s bytes).' % \ (len(body), cfg.CONF.max_json_body_size) self.assertEqual(msg, six.text_type(error))
def test_resource_client_exceptions_dont_log_error(self): class Controller(object): def __init__(self, excpetion_to_raise): self.excpetion_to_raise = excpetion_to_raise def raise_exception(self, req, body): raise self.excpetion_to_raise() actions = {'action': 'raise_exception', 'body': 'data'} env = {'wsgiorg.routing_args': [None, actions]} request = wsgi.Request.blank('/tests/123', environ=env) request.body = encodeutils.safe_encode('{"foo": "value"}') resource = wsgi.Resource(Controller(self.exception), wsgi.JSONRequestDeserializer(), None) e = self.assertRaises(self.exception_catch, resource, request) e = e.exc if hasattr(e, 'exc') else e self.assertNotIn(six.text_type(e), self.LOG.output)
def test_resource_call_error_handle(self): class Controller(object): def delete(self, req, identity): return (req, identity) actions = {'action': 'delete', 'id': 12, 'body': 'data'} env = {'wsgiorg.routing_args': [None, actions]} request = wsgi.Request.blank('/tests/123', environ=env) request.body = encodeutils.safe_encode('{"foo" : "value"}') resource = wsgi.Resource(Controller(), wsgi.JSONRequestDeserializer(), None) # The Resource does not throw webob.HTTPExceptions, since they # would be considered responses by wsgi and the request flow would end, # instead they are wrapped so they can reach the fault application # where they are converted to a JSON response e = self.assertRaises(exception.HTTPExceptionDisguise, resource, request) self.assertIsInstance(e.exc, webob.exc.HTTPBadRequest)
def test_resource_call_error_handle_localized(self): class Controller(object): def delete(self, req, identity): return (req, identity) actions = {'action': 'delete', 'id': 12, 'body': 'data'} env = {'wsgiorg.routing_args': [None, actions]} request = wsgi.Request.blank('/tests/123', environ=env) request.body = encodeutils.safe_encode('{"foo" : "value"}') message_es = "No Encontrado" translated_ex = webob.exc.HTTPBadRequest(message_es) resource = wsgi.Resource(Controller(), wsgi.JSONRequestDeserializer(), None) def fake_translate_exception(ex, locale): return translated_ex self.stubs.SmartSet(wsgi, 'translate_exception', fake_translate_exception) e = self.assertRaises(exception.HTTPExceptionDisguise, resource, request) self.assertEqual(message_es, six.text_type(e.exc))
def create_resource(options): '''Clusters resource factory method.''' return wsgi.Resource(ClusterController(options), wsgi.JSONRequestDeserializer(), serializers.JSONResponseSerializer())
def create_resource(options): '''Profile types resource factory method.''' return wsgi.Resource(ProfileTypeController(options), wsgi.JSONRequestDeserializer(), serializers.JSONResponseSerializer())
def test_default_no_body(self): request = wsgi.Request.blank('/') actual = wsgi.JSONRequestDeserializer().default(request) expected = {} self.assertEqual(expected, actual)
def test_from_json_malformed(self): fixture = 'kjasdklfjsklajf' self.assertRaises(webob.exc.HTTPBadRequest, wsgi.JSONRequestDeserializer().from_json, fixture)
def test_from_json(self): fixture = '{"key": "value"}' expected = {"key": "value"} actual = wsgi.JSONRequestDeserializer().from_json(fixture) self.assertEqual(expected, actual)
def test_no_body_no_content_length(self): request = wsgi.Request.blank('/') self.assertFalse(wsgi.JSONRequestDeserializer().has_body(request))
def test_has_body_content_type_with_get(self): request = wsgi.Request.blank('/') request.method = 'GET' request.body = encodeutils.safe_encode('{"key": "value"}') self.assertIn('Content-Length', request.headers) self.assertTrue(wsgi.JSONRequestDeserializer().has_body(request))
def create_resource(options): """Trigger resource factory method.""" return wsgi.Resource(TriggerController(options), wsgi.JSONRequestDeserializer(), serializers.JSONResponseSerializer())