HTTPBadRequest in v2 on malformed JSON request body.

Fixes bug 1039901

Change-Id: I79ff2059f8f4de60cc92f8cf89cd80be74529e3c
This commit is contained in:
Alex Meade 2012-08-23 11:10:28 -04:00
parent 66c10d434a
commit f8467bfcef
2 changed files with 10 additions and 1 deletions

View File

@ -455,7 +455,11 @@ class JSONRequestDeserializer(object):
return False
def from_json(self, datastring):
return json.loads(datastring)
try:
return json.loads(datastring)
except ValueError:
msg = _('Malformed JSON in request body.')
raise webob.exc.HTTPBadRequest(explanation=msg)
def default(self, request):
if self.has_body(request):

View File

@ -177,6 +177,11 @@ class JSONRequestDeserializerTest(test_utils.BaseTestCase):
actual = wsgi.JSONRequestDeserializer().from_json(fixture)
self.assertEqual(actual, expected)
def test_from_json_malformed(self):
fixture = 'kjasdklfjsklajf'
self.assertRaises(webob.exc.HTTPBadRequest,
wsgi.JSONRequestDeserializer().from_json, fixture)
def test_default_no_body(self):
request = wsgi.Request.blank('/')
actual = wsgi.JSONRequestDeserializer().default(request)