Skip to content

Commit

Permalink
Created new exception for handling malformed requests
Browse files Browse the repository at this point in the history
Wrote tests
Raise httpBadRequest on malformed request bodies
  • Loading branch information
ameade committed Jun 14, 2011
1 parent 035e43f commit a3ddb45
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
16 changes: 13 additions & 3 deletions nova/api/openstack/wsgi.py
Expand Up @@ -2,6 +2,7 @@
import json
import webob
from xml.dom import minidom
from xml.parsers.expat import ExpatError

from nova import exception
from nova import log as logging
Expand Down Expand Up @@ -71,7 +72,10 @@ def default(self, datastring):
class JSONDeserializer(TextDeserializer):

def default(self, datastring):
return utils.loads(datastring)
try:
return utils.loads(datastring)
except ValueError:
raise exception.MalformedRequestBody()


class XMLDeserializer(TextDeserializer):
Expand All @@ -86,8 +90,12 @@ def __init__(self, metadata=None):

def default(self, datastring):
plurals = set(self.metadata.get('plurals', {}))
node = minidom.parseString(datastring).childNodes[0]
return {node.nodeName: self._from_xml_node(node, plurals)}

try:
node = minidom.parseString(datastring).childNodes[0]
return {node.nodeName: self._from_xml_node(node, plurals)}
except ExpatError:
raise exception.MalformedRequestBody()

def _from_xml_node(self, node, listnames):
"""Convert a minidom node to a simple Python type.
Expand Down Expand Up @@ -353,6 +361,8 @@ def __call__(self, request):
request)
except exception.InvalidContentType:
return webob.exc.HTTPBadRequest(_("Unsupported Content-Type"))
except exception.MalformedRequestBody:
return webob.exc.HTTPBadRequest(_("Malformed request"))

action_result = self.dispatch(request, action, action_args)

Expand Down
4 changes: 4 additions & 0 deletions nova/exception.py
Expand Up @@ -585,3 +585,7 @@ class InstanceExists(Duplicate):

class MigrationError(NovaException):
message = _("Migration error") + ": %(reason)s"


class MalformedRequestBody(NovaException):
message = _("Malformed message body") + ": %(reason)s"
21 changes: 21 additions & 0 deletions nova/tests/api/openstack/test_api.py
Expand Up @@ -15,6 +15,8 @@
# License for the specific language governing permissions and limitations
# under the License.

import json

import webob.exc
import webob.dec

Expand All @@ -23,6 +25,7 @@
from nova import test
from nova.api import openstack
from nova.api.openstack import faults
from nova.tests.api.openstack import fakes


class APITest(test.TestCase):
Expand All @@ -31,6 +34,24 @@ def _wsgi_app(self, inner_app):
# simpler version of the app than fakes.wsgi_app
return openstack.FaultWrapper(inner_app)

def test_malformed_json(self):
req = webob.Request.blank('/')
req.method = 'POST'
req.body = '{'
req.headers["content-type"] = "application/json"

res = req.get_response(fakes.wsgi_app())
self.assertEqual(res.status_int, 400)

def test_malformed_xml(self):
req = webob.Request.blank('/')
req.method = 'POST'
req.body = '<hi im not xml>'
req.headers["content-type"] = "application/xml"

res = req.get_response(fakes.wsgi_app())
self.assertEqual(res.status_int, 400)

def test_exceptions_are_converted_to_faults(self):

@webob.dec.wsgify
Expand Down

0 comments on commit a3ddb45

Please sign in to comment.