Ensure that dataWrapper feature is checked before using the 'data' value of a

reponse.

Reviewed: https://codereview.appspot.com/6737065/
Fixes: http://code.google.com/p/google-api-python-client/issues/detail?id=208
This commit is contained in:
Ali Afshar
2012-10-23 11:14:28 -07:00
parent 1a0d5cdd20
commit 81fde8e0f1
3 changed files with 27 additions and 3 deletions

View File

@@ -261,7 +261,7 @@ class JsonModel(BaseModel):
def deserialize(self, content):
body = simplejson.loads(content)
if isinstance(body, dict) and 'data' in body:
if self._data_wrapper and isinstance(body, dict) and 'data' in body:
body = body['data']
return body

View File

@@ -157,7 +157,6 @@ class Model(unittest.TestCase):
except HttpError, e:
self.assertTrue('not authorized' in str(e))
def test_good_response(self):
model = JsonModel(data_wrapper=True)
resp = httplib2.Response({'status': '200'})
@@ -242,6 +241,31 @@ class Model(unittest.TestCase):
'--response-end--')
apiclient.model.logging = old_logging
def test_no_data_wrapper_deserialize(self):
model = JsonModel(data_wrapper=False)
resp = httplib2.Response({'status': '200'})
resp.reason = 'OK'
content = '{"data": "is good"}'
content = model.response(resp, content)
self.assertEqual(content, {'data': 'is good'})
def test_data_wrapper_deserialize(self):
model = JsonModel(data_wrapper=True)
resp = httplib2.Response({'status': '200'})
resp.reason = 'OK'
content = '{"data": "is good"}'
content = model.response(resp, content)
self.assertEqual(content, 'is good')
def test_data_wrapper_deserialize_nodata(self):
model = JsonModel(data_wrapper=True)
resp = httplib2.Response({'status': '200'})
resp.reason = 'OK'
content = '{"atad": "is good"}'
content = model.response(resp, content)
self.assertEqual(content, {'atad': 'is good'})
if __name__ == '__main__':
unittest.main()

View File

@@ -52,7 +52,7 @@ class Mocks(unittest.TestCase):
def test_simple_response(self):
requestBuilder = RequestMockBuilder({
'plus.activities.get': (None, '{"data": {"foo": "bar"}}')
'plus.activities.get': (None, '{"foo": "bar"}')
})
plus = build('plus', 'v1', http=self.http, requestBuilder=requestBuilder)