Be robust in what you accept, which might be a JSON object w/o an outer wrapper 'data' object.
This commit is contained in:
@@ -111,7 +111,10 @@ class JsonModel(object):
|
|||||||
if resp.status == 204:
|
if resp.status == 204:
|
||||||
# A 204: No Content response should be treated differently to all the other success states
|
# A 204: No Content response should be treated differently to all the other success states
|
||||||
return simplejson.loads('{}')
|
return simplejson.loads('{}')
|
||||||
return simplejson.loads(content)['data']
|
body = simplejson.loads(content)
|
||||||
|
if isinstance(body, dict) and 'data' in body:
|
||||||
|
body = body['data']
|
||||||
|
return body
|
||||||
else:
|
else:
|
||||||
logging.debug('Content from bad request was: %s' % content)
|
logging.debug('Content from bad request was: %s' % content)
|
||||||
if resp.get('content-type', '').startswith('application/json'):
|
if resp.get('content-type', '').startswith('application/json'):
|
||||||
|
|||||||
@@ -140,5 +140,23 @@ class Model(unittest.TestCase):
|
|||||||
content = model.response(resp, content)
|
content = model.response(resp, content)
|
||||||
self.assertEqual(content, 'is good')
|
self.assertEqual(content, 'is good')
|
||||||
|
|
||||||
|
def test_good_response_wo_data(self):
|
||||||
|
model = JsonModel()
|
||||||
|
resp = httplib2.Response({'status': '200'})
|
||||||
|
resp.reason = 'OK'
|
||||||
|
content = '{"foo": "is good"}'
|
||||||
|
|
||||||
|
content = model.response(resp, content)
|
||||||
|
self.assertEqual(content, {'foo': 'is good'})
|
||||||
|
|
||||||
|
def test_good_response_wo_data_str(self):
|
||||||
|
model = JsonModel()
|
||||||
|
resp = httplib2.Response({'status': '200'})
|
||||||
|
resp.reason = 'OK'
|
||||||
|
content = '"data goes here"'
|
||||||
|
|
||||||
|
content = model.response(resp, content)
|
||||||
|
self.assertEqual(content, 'data goes here')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user