本文整理汇总了Python中aiohttp.client.ClientResponse.read方法的典型用法代码示例。如果您正苦于以下问题:Python ClientResponse.read方法的具体用法?Python ClientResponse.read怎么用?Python ClientResponse.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aiohttp.client.ClientResponse
的用法示例。
在下文中一共展示了ClientResponse.read方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ClientResponseTests
# 需要导入模块: from aiohttp.client import ClientResponse [as 别名]
# 或者: from aiohttp.client.ClientResponse import read [as 别名]
class ClientResponseTests(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(None)
self.connection = unittest.mock.Mock()
self.stream = aiohttp.StreamParser(loop=self.loop)
self.response = ClientResponse('get', 'http://python.org')
def tearDown(self):
self.loop.close()
def test_del(self):
response = ClientResponse('get', 'http://python.org')
connection = unittest.mock.Mock()
response._setup_connection(connection)
with self.assertWarns(ResourceWarning):
del response
connection.close.assert_called_with()
def test_close(self):
self.response.connection = self.connection
self.response.close()
self.assertIsNone(self.response.connection)
self.assertTrue(self.connection.release.called)
self.response.close()
self.response.close()
def test_wait_for_100(self):
response = ClientResponse(
'get', 'http://python.org', continue100=object())
self.assertTrue(response.waiting_for_continue())
response = ClientResponse(
'get', 'http://python.org')
self.assertFalse(response.waiting_for_continue())
def test_repr(self):
self.response.status = 200
self.response.reason = 'Ok'
self.assertIn(
'<ClientResponse(http://python.org) [200 Ok]>',
repr(self.response))
def test_read_and_release_connection(self):
def side_effect(*args, **kwargs):
def second_call(*args, **kwargs):
raise aiohttp.EofStream
fut = asyncio.Future(loop=self.loop)
fut.set_result(b'payload')
content.read.side_effect = second_call
return fut
content = self.response.content = unittest.mock.Mock()
content.read.side_effect = side_effect
self.response.close = unittest.mock.Mock()
res = self.loop.run_until_complete(self.response.read())
self.assertEqual(res, b'payload')
self.assertTrue(self.response.close.called)
def test_read_and_release_connection_with_error(self):
content = self.response.content = unittest.mock.Mock()
content.read.return_value = asyncio.Future(loop=self.loop)
content.read.return_value.set_exception(ValueError)
self.response.close = unittest.mock.Mock()
self.assertRaises(
ValueError,
self.loop.run_until_complete, self.response.read())
self.response.close.assert_called_with(True)
def test_release(self):
fut = asyncio.Future(loop=self.loop)
fut.set_result(b'')
content = self.response.content = unittest.mock.Mock()
content.readany.return_value = fut
self.response.close = unittest.mock.Mock()
self.loop.run_until_complete(self.response.release())
self.assertTrue(self.response.close.called)
def test_read_and_close(self):
self.response.read = unittest.mock.Mock()
self.response.read.return_value = asyncio.Future(loop=self.loop)
self.response.read.return_value.set_result(b'data')
with self.assertWarns(DeprecationWarning):
res = self.loop.run_until_complete(self.response.read_and_close())
self.assertEqual(res, b'data')
self.assertTrue(self.response.read.called)
def test_read_decode_deprecated(self):
self.response._content = b'data'
self.response.json = unittest.mock.Mock()
self.response.json.return_value = asyncio.Future(loop=self.loop)
self.response.json.return_value.set_result('json')
with self.assertWarns(DeprecationWarning):
#.........这里部分代码省略.........