Fix 500 error if request body is not JSON object
In `JsonBodyMiddleware` we expect POST body to be a JSON dictionary, and failed with 500 error if body was a valid JSON, but not a dictionary. Additional check was added along with a test for described case. Change-Id: I08ae3c8fa4eb53b67604d8b8791ca19d9c1682e6 Closes-Bug: #1316657
This commit is contained in:
parent
21bf6c7fda
commit
5a7d4e24bb
|
@ -131,6 +131,11 @@ class JsonBodyMiddleware(wsgi.Middleware):
|
|||
if not params_parsed:
|
||||
params_parsed = {}
|
||||
|
||||
if not isinstance(params_parsed, dict):
|
||||
e = exception.ValidationError(attribute='valid JSON object',
|
||||
target='request body')
|
||||
return wsgi.render_exception(e, request=request)
|
||||
|
||||
params = {}
|
||||
for k, v in six.iteritems(params_parsed):
|
||||
if k in ('self', 'context'):
|
||||
|
|
|
@ -90,6 +90,14 @@ class JsonBodyMiddlewareTest(tests.TestCase):
|
|||
resp = middleware.JsonBodyMiddleware(None).process_request(req)
|
||||
self.assertEqual(400, resp.status_int)
|
||||
|
||||
def test_not_dict_body(self):
|
||||
req = make_request(body='42',
|
||||
content_type='application/json',
|
||||
method='POST')
|
||||
resp = middleware.JsonBodyMiddleware(None).process_request(req)
|
||||
self.assertEqual(400, resp.status_int)
|
||||
self.assertTrue('valid JSON object' in resp.json['error']['message'])
|
||||
|
||||
def test_no_content_type(self):
|
||||
req = make_request(body='{"arg1": "one", "arg2": ["a"]}',
|
||||
method='POST')
|
||||
|
|
Loading…
Reference in New Issue