Skip to content

Commit

Permalink
Merge "HTTPBadRequest in v2 on malformed JSON request body."
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins authored and openstack-gerrit committed Aug 28, 2012
2 parents f57fc69 + f8467bf commit 24c0728
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion glance/common/wsgi.py
Expand Up @@ -455,7 +455,11 @@ def has_body(self, request):
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):
Expand Down
5 changes: 5 additions & 0 deletions glance/tests/unit/test_wsgi.py
Expand Up @@ -177,6 +177,11 @@ def test_from_json(self):
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)
Expand Down

0 comments on commit 24c0728

Please sign in to comment.