From b5ca0d793826ac10ee41be84f18d64b09113aa80 Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Fri, 8 Jul 2011 17:40:56 -0400 Subject: refactor --- nova/api/openstack/wsgi.py | 25 ++++++++++++++----------- nova/tests/api/openstack/test_wsgi.py | 3 ++- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/nova/api/openstack/wsgi.py b/nova/api/openstack/wsgi.py index bd91aed60..7a8376722 100644 --- a/nova/api/openstack/wsgi.py +++ b/nova/api/openstack/wsgi.py @@ -52,9 +52,9 @@ class Request(webob.Request): content_type = self.content_type if content_type not in allowed_types: - return None - else: - return content_type + raise exception.InvalidContentType(content_type=content_type) + + return content_type class TextDeserializer(object): @@ -171,19 +171,22 @@ class RequestDeserializer(object): def deserialize_body(self, request, action): try: content_type = request.get_content_type() + except exception.InvalidContentType: + LOG.debug(_("Unrecognized Content-Type provided in request")) + return {} - if content_type is None: - LOG.debug(_("No Content-Type provided in request")) - return {} + if content_type is None: + LOG.debug(_("No Content-Type provided in request")) + return {} - if not len(request.body) > 0: - LOG.debug(_("Empty body provided in request")) - return {} + if not len(request.body) > 0: + LOG.debug(_("Empty body provided in request")) + return {} + try: deserializer = self.get_body_deserializer(content_type) - except exception.InvalidContentType: - LOG.debug(_("Unable to read body as provided Content-Type")) + LOG.debug(_("Unable to deserialize body as provided Content-Type")) raise return deserializer.deserialize(request.body, action) diff --git a/nova/tests/api/openstack/test_wsgi.py b/nova/tests/api/openstack/test_wsgi.py index 7ab39ed5a..938637207 100644 --- a/nova/tests/api/openstack/test_wsgi.py +++ b/nova/tests/api/openstack/test_wsgi.py @@ -18,7 +18,8 @@ class RequestTest(test.TestCase): request = wsgi.Request.blank('/tests/123', method='POST') request.headers["Content-Type"] = "text/html" request.body = "asdf
" - self.assertEqual(request.get_content_type(), None) + self.assertRaises(exception.InvalidContentType, + request.get_content_type) def test_content_type_with_charset(self): request = wsgi.Request.blank('/tests/123') -- cgit