Merge "HTTPBadRequest in v2 on malformed JSON request body."

This commit is contained in:
Jenkins 2012-08-28 23:58:31 +00:00 committed by Gerrit Code Review
commit 24c072881a
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)