本文整理汇总了Python中aiohttp.client.ClientResponse类的典型用法代码示例。如果您正苦于以下问题:Python ClientResponse类的具体用法?Python ClientResponse怎么用?Python ClientResponse使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ClientResponse类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_https_connect_runtime_error
def test_https_connect_runtime_error(self, ClientRequestMock):
loop_mock = unittest.mock.Mock()
proxy_req = ClientRequest('GET', 'http://proxy.example.com',
loop=loop_mock)
ClientRequestMock.return_value = proxy_req
proxy_resp = ClientResponse('get', 'http://proxy.example.com')
proxy_resp._loop = loop_mock
proxy_req.send = send_mock = unittest.mock.Mock()
send_mock.return_value = proxy_resp
proxy_resp.start = start_mock = unittest.mock.Mock()
self._fake_coroutine(start_mock, unittest.mock.Mock(status=200))
connector = aiohttp.ProxyConnector(
'http://proxy.example.com', loop=loop_mock)
tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
tr.get_extra_info.return_value = None
self._fake_coroutine(loop_mock.create_connection, (tr, proto))
req = ClientRequest('GET', 'https://www.python.org', loop=self.loop)
with self.assertRaisesRegex(
RuntimeError, "Transport does not expose socket instance"):
self.loop.run_until_complete(connector._create_connection(req))
proxy_req.close()
proxy_resp.close()
req.close()
示例2: test_wait_for_100
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())
示例3: test_https_connect_http_proxy_error
def test_https_connect_http_proxy_error(self, ClientRequestMock):
loop_mock = unittest.mock.Mock()
proxy_req = ClientRequest('GET', 'http://proxy.example.com',
loop=loop_mock)
ClientRequestMock.return_value = proxy_req
proxy_resp = ClientResponse('get', 'http://proxy.example.com')
proxy_resp._loop = loop_mock
proxy_req.send = send_mock = unittest.mock.Mock()
send_mock.return_value = proxy_resp
proxy_resp.start = start_mock = unittest.mock.Mock()
self._fake_coroutine(
start_mock, unittest.mock.Mock(status=400, reason='bad request'))
connector = aiohttp.ProxyConnector(
'http://proxy.example.com', loop=loop_mock)
tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
tr.get_extra_info.return_value = None
self._fake_coroutine(loop_mock.create_connection, (tr, proto))
req = ClientRequest('GET', 'https://www.python.org', loop=self.loop)
with self.assertRaisesRegex(
aiohttp.HttpProxyError, "400, message='bad request'"):
self.loop.run_until_complete(connector._create_connection(req))
proxy_req.close()
proxy_resp.close()
req.close()
示例4: test_https_connect
def test_https_connect(self, ClientRequestMock):
loop_mock = unittest.mock.Mock()
proxy_req = ClientRequest('GET', 'http://proxy.example.com',
loop=loop_mock)
ClientRequestMock.return_value = proxy_req
proxy_resp = ClientResponse('get', 'http://proxy.example.com')
proxy_resp._loop = loop_mock
proxy_req.send = send_mock = unittest.mock.Mock()
send_mock.return_value = proxy_resp
proxy_resp.start = start_mock = unittest.mock.Mock()
self._fake_coroutine(start_mock, unittest.mock.Mock(status=200))
connector = aiohttp.ProxyConnector(
'http://proxy.example.com', loop=loop_mock)
tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
self._fake_coroutine(loop_mock.create_connection, (tr, proto))
req = ClientRequest('GET', 'https://www.python.org', loop=self.loop)
self.loop.run_until_complete(connector._create_connection(req))
self.assertEqual(req.path, '/')
self.assertEqual(proxy_req.method, 'CONNECT')
self.assertEqual(proxy_req.path, 'www.python.org:443')
tr.pause_reading.assert_called_once_with()
tr.get_extra_info.assert_called_with('socket', default=None)
proxy_req.close()
proxy_resp.close()
req.close()
示例5: test_del
def test_del(self):
response = ClientResponse('get', 'http://python.org')
connection = unittest.mock.Mock()
response._setup_connection(connection)
del response
connection.close.assert_called_with()
示例6: setUp
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')
示例7: setUp
def setUp(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(None)
self.transport = unittest.mock.Mock()
self.stream = aiohttp.StreamParser()
self.response = ClientResponse('get', 'http://base-conn.org')
self.response._post_init(self.loop)
示例8: cb
def cb():
def read():
fut = asyncio.Future(loop=self._loop)
with open(filename) as f:
fut.set_result(f.read().encode("utf-8"))
return fut
resp = ClientResponse("GET", uri)
resp.headers = {"Content-Type": "application/json"}
resp.status = 200
resp.content = Mock()
resp.content.read.side_effect = read
resp.close = Mock()
fut = asyncio.Future(loop=self._loop)
fut.set_result(resp)
return fut
示例9: TestBaseConnector
class TestBaseConnector(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(None)
self.transport = unittest.mock.Mock()
self.stream = aiohttp.StreamParser()
self.response = ClientResponse('get', 'http://base-conn.org')
self.response._post_init(self.loop)
def tearDown(self):
self.response.close()
self.loop.close()
gc.collect()
def test_del(self):
conn = aiohttp.BaseConnector(loop=self.loop)
transp = unittest.mock.Mock()
conn._conns['a'] = [(transp, 'proto', 123)]
conns_impl = conn._conns
exc_handler = unittest.mock.Mock()
self.loop.set_exception_handler(exc_handler)
with self.assertWarns(ResourceWarning):
del conn
gc.collect()
self.assertFalse(conns_impl)
transp.close.assert_called_with()
msg = {'connector': unittest.mock.ANY, # conn was deleted
'connections': unittest.mock.ANY,
'message': 'Unclosed connector'}
if self.loop.get_debug():
msg['source_traceback'] = unittest.mock.ANY
exc_handler.assert_called_with(self.loop, msg)
def test_del_with_scheduled_cleanup(self):
conn = aiohttp.BaseConnector(loop=self.loop, keepalive_timeout=0.01)
transp = unittest.mock.Mock()
conn._conns['a'] = [(transp, 'proto', 123)]
conns_impl = conn._conns
conn._start_cleanup_task()
exc_handler = unittest.mock.Mock()
self.loop.set_exception_handler(exc_handler)
with self.assertWarns(ResourceWarning):
del conn
yield from asyncio.sleep(0.01)
gc.collect()
self.assertFalse(conns_impl)
transp.close.assert_called_with()
msg = {'connector': unittest.mock.ANY, # conn was deleted
'message': 'Unclosed connector'}
if self.loop.get_debug():
msg['source_traceback'] = unittest.mock.ANY
exc_handler.assert_called_with(self.loop, msg)
def test_del_with_closed_loop(self):
conn = aiohttp.BaseConnector(loop=self.loop)
transp = unittest.mock.Mock()
conn._conns['a'] = [(transp, 'proto', 123)]
conns_impl = conn._conns
conn._start_cleanup_task()
exc_handler = unittest.mock.Mock()
self.loop.set_exception_handler(exc_handler)
self.loop.close()
with self.assertWarns(ResourceWarning):
del conn
gc.collect()
self.assertFalse(conns_impl)
self.assertFalse(transp.close.called)
self.assertTrue(exc_handler.called)
def test_del_empty_conector(self):
conn = aiohttp.BaseConnector(loop=self.loop)
exc_handler = unittest.mock.Mock()
self.loop.set_exception_handler(exc_handler)
del conn
self.assertFalse(exc_handler.called)
def test_create_conn(self):
def go():
conn = aiohttp.BaseConnector(loop=self.loop)
with self.assertRaises(NotImplementedError):
yield from conn._create_connection(object())
self.loop.run_until_complete(go())
@unittest.mock.patch('aiohttp.connector.asyncio')
#.........这里部分代码省略.........
示例10: ClientResponseTests
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):
#.........这里部分代码省略.........
示例11: ClientResponseTests
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)
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_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'payload')
self.response.close = unittest.mock.Mock()
res = self.loop.run_until_complete(self.response.read_and_close())
self.assertEqual(res, b'payload')
self.assertTrue(self.response.read.called)
self.assertTrue(self.response.close.called)
def test_read_and_close_with_error(self):
self.response.read = unittest.mock.Mock()
self.response.read.return_value = asyncio.Future(loop=self.loop)
self.response.read.return_value.set_exception(ValueError)
self.response.close = unittest.mock.Mock()
self.assertRaises(
ValueError,
self.loop.run_until_complete, self.response.read_and_close())
self.assertTrue(self.response.read.called)
self.response.close.assert_called_with(True)
示例12: TestBaseConnector
class TestBaseConnector(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(None)
self.transport = unittest.mock.Mock()
self.stream = aiohttp.StreamParser()
self.response = ClientResponse('get', 'http://base-conn.org')
self.response._post_init(self.loop)
def tearDown(self):
self.response.close()
self.loop.close()
gc.collect()
@unittest.skipUnless(PY_341, "Requires Python 3.4.1+")
def test_del(self):
conn = aiohttp.BaseConnector(loop=self.loop)
transp = unittest.mock.Mock()
conn._conns['a'] = [(transp, 'proto', 123)]
conns_impl = conn._conns
exc_handler = unittest.mock.Mock()
self.loop.set_exception_handler(exc_handler)
with self.assertWarns(ResourceWarning):
del conn
gc.collect()
self.assertFalse(conns_impl)
transp.close.assert_called_with()
msg = {'connector': unittest.mock.ANY, # conn was deleted
'message': 'Unclosed connector'}
if self.loop.get_debug():
msg['source_traceback'] = unittest.mock.ANY
exc_handler.assert_called_with(self.loop, msg)
@unittest.skipUnless(PY_341, "Requires Python 3.4.1+")
def test_del_with_scheduled_cleanup(self):
conn = aiohttp.BaseConnector(loop=self.loop, keepalive_timeout=0.01)
transp = unittest.mock.Mock()
conn._conns['a'] = [(transp, 'proto', 123)]
conns_impl = conn._conns
conn._start_cleanup_task()
exc_handler = unittest.mock.Mock()
self.loop.set_exception_handler(exc_handler)
with self.assertWarns(ResourceWarning):
del conn
yield from asyncio.sleep(0.01)
gc.collect()
self.assertFalse(conns_impl)
transp.close.assert_called_with()
msg = {'connector': unittest.mock.ANY, # conn was deleted
'message': 'Unclosed connector'}
if self.loop.get_debug():
msg['source_traceback'] = unittest.mock.ANY
exc_handler.assert_called_with(self.loop, msg)
@unittest.skipUnless(PY_341, "Requires Python 3.4.1+")
def test_del_with_closed_loop(self):
conn = aiohttp.BaseConnector(loop=self.loop)
transp = unittest.mock.Mock()
conn._conns['a'] = [(transp, 'proto', 123)]
conns_impl = conn._conns
conn._start_cleanup_task()
exc_handler = unittest.mock.Mock()
self.loop.set_exception_handler(exc_handler)
self.loop.close()
with self.assertWarns(ResourceWarning):
del conn
gc.collect()
self.assertFalse(conns_impl)
self.assertFalse(transp.close.called)
self.assertTrue(exc_handler.called)
@unittest.skipUnless(PY_341, "Requires Python 3.4.1+")
def test_del_empty_conector(self):
conn = aiohttp.BaseConnector(loop=self.loop)
exc_handler = unittest.mock.Mock()
self.loop.set_exception_handler(exc_handler)
del conn
self.assertFalse(exc_handler.called)
def test_create_conn(self):
def go():
conn = aiohttp.BaseConnector(loop=self.loop)
with self.assertRaises(NotImplementedError):
yield from conn._create_connection(object())
#.........这里部分代码省略.........
示例13: TestBaseConnector
class TestBaseConnector(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(None)
self.transport = unittest.mock.Mock()
self.stream = aiohttp.StreamParser()
self.response = ClientResponse('get', 'http://base-conn.org')
self.response._post_init(self.loop)
def tearDown(self):
self.response.close()
self.loop.close()
gc.collect()
@unittest.skipUnless(PY_341, "Requires Python 3.4.1+")
def test_del(self):
conn = aiohttp.BaseConnector(loop=self.loop)
transp = unittest.mock.Mock()
conn._conns['a'] = [(transp, 'proto', 123)]
conns_impl = conn._conns
exc_handler = unittest.mock.Mock()
self.loop.set_exception_handler(exc_handler)
with self.assertWarns(ResourceWarning):
del conn
gc.collect()
self.assertFalse(conns_impl)
transp.close.assert_called_with()
msg = {'connector': unittest.mock.ANY, # conn was deleted
'message': 'Unclosed connector'}
if self.loop.get_debug():
msg['source_traceback'] = unittest.mock.ANY
exc_handler.assert_called_with(self.loop, msg)
@unittest.skipUnless(PY_341, "Requires Python 3.4.1+")
def test_del_with_scheduled_cleanup(self):
conn = aiohttp.BaseConnector(loop=self.loop, keepalive_timeout=0.01)
transp = unittest.mock.Mock()
conn._conns['a'] = [(transp, 'proto', 123)]
conns_impl = conn._conns
conn._start_cleanup_task()
exc_handler = unittest.mock.Mock()
self.loop.set_exception_handler(exc_handler)
with self.assertWarns(ResourceWarning):
del conn
yield from asyncio.sleep(0.01)
gc.collect()
self.assertFalse(conns_impl)
transp.close.assert_called_with()
msg = {'connector': unittest.mock.ANY, # conn was deleted
'message': 'Unclosed connector'}
if self.loop.get_debug():
msg['source_traceback'] = unittest.mock.ANY
exc_handler.assert_called_with(self.loop, msg)
@unittest.skipUnless(PY_341, "Requires Python 3.4.1+")
def test_del_with_closed_loop(self):
conn = aiohttp.BaseConnector(loop=self.loop)
transp = unittest.mock.Mock()
conn._conns['a'] = [(transp, 'proto', 123)]
conns_impl = conn._conns
conn._start_cleanup_task()
exc_handler = unittest.mock.Mock()
self.loop.set_exception_handler(exc_handler)
self.loop.close()
with self.assertWarns(ResourceWarning):
del conn
gc.collect()
self.assertFalse(conns_impl)
self.assertFalse(transp.close.called)
self.assertTrue(exc_handler.called)
@unittest.skipUnless(PY_341, "Requires Python 3.4.1+")
def test_del_empty_conector(self):
conn = aiohttp.BaseConnector(loop=self.loop)
exc_handler = unittest.mock.Mock()
self.loop.set_exception_handler(exc_handler)
del conn
self.assertFalse(exc_handler.called)
def test_create_conn(self):
def go():
conn = aiohttp.BaseConnector(loop=self.loop)
with self.assertRaises(NotImplementedError):
yield from conn._create_connection(object())
#.........这里部分代码省略.........