Use requests.Response.text ato avoid handling encoding here explicitly
This commit is contained in:
Родитель
000b073a37
Коммит
44aede0ade
|
@ -803,15 +803,11 @@ class Deserializer(object):
|
|||
|
||||
if hasattr(raw_data, 'content'):
|
||||
# This is a requests.Response
|
||||
if not raw_data.content:
|
||||
if not raw_data.text:
|
||||
return None
|
||||
|
||||
if isinstance(raw_data.content, bytes):
|
||||
data = raw_data.content.decode(encoding=raw_data.encoding)
|
||||
else:
|
||||
data = raw_data.content
|
||||
try:
|
||||
return json.loads(data)
|
||||
return json.loads(raw_data.text)
|
||||
except (ValueError, TypeError):
|
||||
return data
|
||||
|
||||
|
|
|
@ -125,7 +125,8 @@ class TestModelDeserialization(unittest.TestCase):
|
|||
}
|
||||
|
||||
resp = mock.create_autospec(Response)
|
||||
resp.content = json.dumps(data)
|
||||
resp.content = True # Hack, should not been read
|
||||
resp.text = json.dumps(data)
|
||||
resp.encoding = 'utf-8'
|
||||
model = self.d('GenericResource', resp)
|
||||
self.assertEqual(model.properties['platformFaultDomainCount'], 3)
|
||||
|
@ -705,37 +706,38 @@ class TestRuntimeDeserialized(unittest.TestCase):
|
|||
"""
|
||||
response_data = mock.create_autospec(Response)
|
||||
response_data.encoding = 'utf-8'
|
||||
response_data.content = True # Hack, should not been read
|
||||
|
||||
response_data.content = json.dumps({})
|
||||
response_data.text = ''
|
||||
response = self.d("[str]", response_data)
|
||||
self.assertIsNone(response)
|
||||
|
||||
response_data.content = ""
|
||||
response_data.text = json.dumps({})
|
||||
response = self.d("[str]", response_data)
|
||||
self.assertIsNone(response)
|
||||
|
||||
response_data.content = None
|
||||
response_data.text = ""
|
||||
response = self.d("[str]", response_data)
|
||||
self.assertIsNone(response)
|
||||
|
||||
message = ["a","b","b"]
|
||||
response_data.content = json.dumps(message)
|
||||
response_data.text = json.dumps(message)
|
||||
response = self.d("[str]", response_data)
|
||||
self.assertEqual(response, message)
|
||||
|
||||
response_data.content = json.dumps(12345)
|
||||
response_data.text = json.dumps(12345)
|
||||
with self.assertRaises(DeserializationError):
|
||||
response = self.d("[str]", response_data)
|
||||
|
||||
response_data.content = True
|
||||
response_data.text = 'true'
|
||||
response = self.d('bool', response_data)
|
||||
self.assertEqual(response, True)
|
||||
|
||||
response_data.content = json.dumps(1)
|
||||
response_data.text = json.dumps(1)
|
||||
response = self.d('bool', response_data)
|
||||
self.assertEqual(response, True)
|
||||
|
||||
response_data.content = json.dumps("true1")
|
||||
response_data.text = json.dumps("true1")
|
||||
with self.assertRaises(DeserializationError):
|
||||
response = self.d('bool', response_data)
|
||||
|
||||
|
@ -803,7 +805,8 @@ class TestRuntimeDeserialized(unittest.TestCase):
|
|||
|
||||
response_data.status_code = None
|
||||
response_data.headers = {'client-request-id':None, 'etag':None}
|
||||
response_data.content = None
|
||||
response_data.content = True # Should not been read
|
||||
response_data.text = ''
|
||||
|
||||
response = self.d(self.TestObj, response_data)
|
||||
self.assertIsNone(response)
|
||||
|
@ -815,20 +818,22 @@ class TestRuntimeDeserialized(unittest.TestCase):
|
|||
response_data = mock.create_autospec(Response)
|
||||
response_data.status_code = 200
|
||||
response_data.headers = {'client-request-id':"123", 'etag':456.3}
|
||||
response_data.content = None
|
||||
response_data.content = True # Should not been read
|
||||
response_data.text = ''
|
||||
|
||||
response = self.d(self.TestObj, response_data)
|
||||
self.assertIsNone(response)
|
||||
|
||||
message = {'AttrB':'1234'}
|
||||
response_data.content = json.dumps(message)
|
||||
response_data.content = True # Hack, should not read this
|
||||
response_data.text = json.dumps(message)
|
||||
response_data.encoding = 'utf-8'
|
||||
response = self.d(self.TestObj, response_data)
|
||||
self.assertTrue(hasattr(response, 'attr_b'))
|
||||
self.assertEqual(response.attr_b, int(message['AttrB']))
|
||||
|
||||
with self.assertRaises(DeserializationError):
|
||||
response_data.content = json.dumps({'AttrB':'NotANumber'})
|
||||
response_data.text = json.dumps({'AttrB':'NotANumber'})
|
||||
response = self.d(self.TestObj, response_data)
|
||||
|
||||
def test_attr_str(self):
|
||||
|
@ -839,7 +844,8 @@ class TestRuntimeDeserialized(unittest.TestCase):
|
|||
response_data = mock.create_autospec(Response)
|
||||
response_data.status_code = 200
|
||||
response_data.headers = {'client-request-id': 'a', 'etag': 'b'}
|
||||
response_data.content = json.dumps(message)
|
||||
response_data.content = True # Hack, shoud not been read
|
||||
response_data.text = json.dumps(message)
|
||||
response_data.encoding = 'utf-8'
|
||||
|
||||
response = self.d(self.TestObj, response_data)
|
||||
|
@ -847,16 +853,16 @@ class TestRuntimeDeserialized(unittest.TestCase):
|
|||
self.assertEqual(response.attr_a, message['id'])
|
||||
|
||||
message = {'id':1234}
|
||||
response_data.content = json.dumps(message)
|
||||
response_data.text = json.dumps(message)
|
||||
response = self.d(self.TestObj, response_data)
|
||||
self.assertEqual(response.attr_a, str(message['id']))
|
||||
|
||||
message = {'id':list()}
|
||||
response_data.content = json.dumps(message)
|
||||
response_data.text = json.dumps(message)
|
||||
response = self.d(self.TestObj, response_data)
|
||||
self.assertEqual(response.attr_a, str(message['id']))
|
||||
|
||||
response_data.content = json.dumps({'id':None})
|
||||
response_data.text = json.dumps({'id':None})
|
||||
response = self.d(self.TestObj, response_data)
|
||||
self.assertEqual(response.attr_a, None)
|
||||
|
||||
|
@ -867,7 +873,8 @@ class TestRuntimeDeserialized(unittest.TestCase):
|
|||
response_data = mock.create_autospec(Response)
|
||||
response_data.status_code = 200
|
||||
response_data.headers = {'client-request-id': 'a', 'etag': 'b'}
|
||||
response_data.content = json.dumps({'Key_C':True})
|
||||
response_data.content = True # Hack, should not been read
|
||||
response_data.text = json.dumps({'Key_C':True})
|
||||
response_data.encoding = 'utf-8'
|
||||
|
||||
response = self.d(self.TestObj, response_data)
|
||||
|
@ -875,15 +882,15 @@ class TestRuntimeDeserialized(unittest.TestCase):
|
|||
self.assertTrue(hasattr(response, 'attr_c'))
|
||||
self.assertEqual(response.attr_c, True)
|
||||
|
||||
response_data.content = json.dumps({'Key_C':[]})
|
||||
response_data.text = json.dumps({'Key_C':[]})
|
||||
with self.assertRaises(DeserializationError):
|
||||
response = self.d(self.TestObj, response_data)
|
||||
|
||||
response_data.content = json.dumps({'Key_C':0})
|
||||
response_data.text = json.dumps({'Key_C':0})
|
||||
response = self.d(self.TestObj, response_data)
|
||||
self.assertEqual(response.attr_c, False)
|
||||
|
||||
response_data.content = json.dumps({'Key_C':"value"})
|
||||
response_data.text = json.dumps({'Key_C':"value"})
|
||||
with self.assertRaises(DeserializationError):
|
||||
response = self.d(self.TestObj, response_data)
|
||||
|
||||
|
@ -894,7 +901,8 @@ class TestRuntimeDeserialized(unittest.TestCase):
|
|||
response_data = mock.create_autospec(Response)
|
||||
response_data.status_code = 200
|
||||
response_data.headers = {'client-request-id': 'a', 'etag': 'b'}
|
||||
response_data.content = json.dumps({'AttrD': []})
|
||||
response_data.content = True # Hack, should not been read
|
||||
response_data.text = json.dumps({'AttrD': []})
|
||||
response_data.encoding = 'utf-8'
|
||||
|
||||
response = self.d(self.TestObj, response_data)
|
||||
|
@ -902,23 +910,23 @@ class TestRuntimeDeserialized(unittest.TestCase):
|
|||
self.assertEqual(deserialized_list, [])
|
||||
|
||||
message = {'AttrD': [1,2,3]}
|
||||
response_data.content = json.dumps(message)
|
||||
response_data.text = json.dumps(message)
|
||||
response = self.d(self.TestObj, response_data)
|
||||
deserialized_list = [d for d in response.attr_d]
|
||||
self.assertEqual(deserialized_list, message['AttrD'])
|
||||
|
||||
message = {'AttrD': ["1","2","3"]}
|
||||
response_data.content = json.dumps(message)
|
||||
response_data.text = json.dumps(message)
|
||||
response = self.d(self.TestObj, response_data)
|
||||
deserialized_list = [d for d in response.attr_d]
|
||||
self.assertEqual(deserialized_list, [int(i) for i in message['AttrD']])
|
||||
|
||||
response_data.content = json.dumps({'AttrD': ["test","test2","test3"]})
|
||||
response_data.text = json.dumps({'AttrD': ["test","test2","test3"]})
|
||||
with self.assertRaises(DeserializationError):
|
||||
response = self.d(self.TestObj, response_data)
|
||||
deserialized_list = [d for d in response.attr_d]
|
||||
|
||||
response_data.content = json.dumps({'AttrD': "NotAList"})
|
||||
response_data.text = json.dumps({'AttrD': "NotAList"})
|
||||
with self.assertRaises(DeserializationError):
|
||||
response = self.d(self.TestObj, response_data)
|
||||
deserialized_list = [d for d in response.attr_d]
|
||||
|
@ -930,20 +938,21 @@ class TestRuntimeDeserialized(unittest.TestCase):
|
|||
response_data = mock.create_autospec(Response)
|
||||
response_data.status_code = 200
|
||||
response_data.headers = {'client-request-id': 'a', 'etag': 'b'}
|
||||
response_data.content = json.dumps({'AttrF':[]})
|
||||
response_data.content = True # Hack, should not been read
|
||||
response_data.text = json.dumps({'AttrF':[]})
|
||||
response_data.encoding = 'utf-8'
|
||||
|
||||
response = self.d(self.TestObj, response_data)
|
||||
self.assertTrue(hasattr(response, 'attr_f'))
|
||||
self.assertEqual(response.attr_f, [])
|
||||
|
||||
response_data.content = json.dumps({'AttrF':None})
|
||||
response_data.text = json.dumps({'AttrF':None})
|
||||
|
||||
response = self.d(self.TestObj, response_data)
|
||||
self.assertTrue(hasattr(response, 'attr_f'))
|
||||
self.assertEqual(response.attr_f, None)
|
||||
|
||||
response_data.content = json.dumps({})
|
||||
response_data.text = json.dumps({})
|
||||
|
||||
response = self.d(self.TestObj, response_data)
|
||||
|
||||
|
@ -951,21 +960,21 @@ class TestRuntimeDeserialized(unittest.TestCase):
|
|||
self.assertEqual(response.attr_f, None)
|
||||
|
||||
message = {'AttrF':[[]]}
|
||||
response_data.content = json.dumps(message)
|
||||
response_data.text = json.dumps(message)
|
||||
|
||||
response = self.d(self.TestObj, response_data)
|
||||
self.assertTrue(hasattr(response, 'attr_f'))
|
||||
self.assertEqual(response.attr_f, message['AttrF'])
|
||||
|
||||
message = {'AttrF':[[1,2,3], ['a','b','c']]}
|
||||
response_data.content = json.dumps(message)
|
||||
response_data.text = json.dumps(message)
|
||||
|
||||
response = self.d(self.TestObj, response_data)
|
||||
self.assertTrue(hasattr(response, 'attr_f'))
|
||||
self.assertEqual(response.attr_f, [[str(i) for i in k] for k in message['AttrF']])
|
||||
|
||||
with self.assertRaises(DeserializationError):
|
||||
response_data.content = json.dumps({'AttrF':[1,2,3]})
|
||||
response_data.text = json.dumps({'AttrF':[1,2,3]})
|
||||
response = self.d(self.TestObj, response_data)
|
||||
|
||||
def test_attr_list_complex(self):
|
||||
|
@ -983,7 +992,8 @@ class TestRuntimeDeserialized(unittest.TestCase):
|
|||
response_data = mock.create_autospec(Response)
|
||||
response_data.status_code = 200
|
||||
response_data.headers = {'client-request-id': 'a', 'etag': 'b'}
|
||||
response_data.content = json.dumps({"id":[{"ABC": "123"}]})
|
||||
response_data.content = True # Hack, should not been read
|
||||
response_data.text = json.dumps({"id":[{"ABC": "123"}]})
|
||||
response_data.encoding = 'utf-8'
|
||||
|
||||
d = Deserializer({'ListObj':ListObj})
|
||||
|
|
Загрузка…
Ссылка в новой задаче