Merge "Prevent StopIteration exception during parse response"

This commit is contained in:
Jenkins
2016-01-07 17:59:26 +00:00
committed by Gerrit Code Review
2 changed files with 8 additions and 1 deletions

View File

@@ -461,7 +461,9 @@ class RestClient(object):
# } # }
try: try:
# Ensure there are not more than one top-level keys # Ensure there are not more than one top-level keys
if len(body.keys()) > 1: # NOTE(freerunner): Ensure, that JSON is not nullable to
# to prevent StopIteration Exception
if len(body.keys()) != 1:
return body return body
# Just return the "wrapped" element # Just return the "wrapped" element
first_key, first_item = six.next(six.iteritems(body)) first_key, first_item = six.next(six.iteritems(body))

View File

@@ -244,6 +244,7 @@ class TestRestClientParseRespJSON(BaseRestClientTestClass):
keys[0]: values[0], keys[0]: values[0],
keys[1]: values[1], keys[1]: values[1],
}} }}
null_dict = {}
def setUp(self): def setUp(self):
self.fake_http = fake_http.fake_httplib2() self.fake_http = fake_http.fake_httplib2()
@@ -273,6 +274,10 @@ class TestRestClientParseRespJSON(BaseRestClientTestClass):
body = self.rest_client._parse_resp(json.dumps(data)) body = self.rest_client._parse_resp(json.dumps(data))
self.assertEqual(data, body) self.assertEqual(data, body)
def test_parse_nullable_dict(self):
body = self.rest_client._parse_resp(json.dumps(self.null_dict))
self.assertEqual(self.null_dict, body)
class TestRestClientErrorCheckerJSON(base.TestCase): class TestRestClientErrorCheckerJSON(base.TestCase):
c_type = "application/json" c_type = "application/json"