Merge "Fix ZTA API to prevent HTTP 500 upon empty body"

This commit is contained in:
Jenkins 2016-09-12 11:14:23 +00:00 committed by Gerrit Code Review
commit 0a74abe155
2 changed files with 16 additions and 2 deletions

View File

@ -37,9 +37,13 @@ class Request(pecan.core.Request):
"""
if self.content_type in JSON_TYPES:
try:
return jsonutils.load(self.body_file)
json_dict = jsonutils.load(self.body_file)
if json_dict is None:
# NOTE(kiall): Somehow, json.load(fp) is returning None.
raise exceptions.EmptyRequestBody('Request Body is empty')
return json_dict
except ValueError as valueError:
if len(self.body) == 0:
if len(self.body) == 0 or self.body is None:
raise exceptions.EmptyRequestBody('Request Body is empty')
else:
raise exceptions.InvalidJson(six.text_type(valueError))

View File

@ -75,6 +75,16 @@ class ApiV2ZoneTransfersTest(ApiV2TestCase):
response.json['zone_id'])
self.assertIsNone(response.json['updated_at'])
def test_create_zone_transfer_request_empty_body(self):
# Send an empty ("None") body
response = self.client.post_json(
'/zones/%s/tasks/transfer_requests' % (self.zone.id),
None)
# Check the headers are what we expect
self.assertEqual(201, response.status_int)
self.assertEqual('application/json', response.content_type)
def test_get_zone_transfer_request(self):
initial = self.client.post_json(
'/zones/%s/tasks/transfer_requests' % (self.zone.id),