Here are the examples of the python api aiohttp.client_reqrep.ClientRequest taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
59 Examples
3
Example 1
@asyncio.coroutine
def test_chunked_explicit_size(loop):
req = ClientRequest(
'get', URL('http://python.org/'), chunked=1024, loop=loop)
with mock.patch('aiohttp.client_reqrep.aiohttp') as m_http:
resp = req.send(mock.Mock(), mock.Mock())
assert 'chunked' == req.headers['TRANSFER-ENCODING']
m_http.Request.return_value\
.add_chunking_filter.assert_called_with(1024)
yield from req.close()
resp.close()
3
Example 2
@mock.patch('aiosocks.connector.create_connection')
def test_connect_remote_resolve(self, cr_conn_mock):
tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')
cr_conn_mock.side_effect = \
fake_coroutine((tr, proto)).side_effect
req = ClientRequest('GET', 'http://python.org', loop=self.loop)
connector = SocksConnector(aiosocks.Socks5Addr('127.0.0.1'),
None, loop=self.loop, remote_resolve=True)
connector._resolve_host = fake_coroutine([mock.MagicMock()])
conn = self.loop.run_until_complete(connector.connect(req))
self.assertEqual(connector._resolve_host.call_count, 1)
conn.close()
3
Example 3
@asyncio.coroutine
def test_file_upload_not_chunked_seek(loop):
here = os.path.dirname(__file__)
fname = os.path.join(here, 'sample.key')
with open(fname, 'rb') as f:
f.seek(100)
req = ClientRequest(
'post', URL('http://python.org/'),
data=f,
loop=loop)
assert req.headers['CONTENT-LENGTH'] == \
str(os.path.getsize(fname) - 100)
yield from req.close()
3
Example 4
@asyncio.coroutine
def test_custom_response_class(loop):
class CustomResponse(ClientResponse):
def read(self, decode=False):
return 'customized!'
req = ClientRequest(
'GET', URL('http://python.org/'), response_class=CustomResponse,
loop=loop)
resp = req.send(mock.Mock(), mock.Mock())
assert 'customized!' == resp.read()
yield from req.close()
resp.close()
3
Example 5
@asyncio.coroutine
def test_content_encoding(loop):
req = ClientRequest('get', URL('http://python.org/'), data='foo',
compress='deflate', loop=loop)
with mock.patch('aiohttp.client_reqrep.aiohttp') as m_http:
resp = req.send(mock.Mock(), mock.Mock())
assert req.headers['TRANSFER-ENCODING'] == 'chunked'
assert req.headers['CONTENT-ENCODING'] == 'deflate'
m_http.Request.return_value\
.add_compression_filter.assert_called_with('deflate')
yield from req.close()
resp.close()
3
Example 6
def test_content_type_skip_auto_header_form(loop):
req = ClientRequest('post', URL('http://python.org'),
data={'hey': 'you'}, loop=loop,
skip_auto_headers={'Content-Type'})
resp = req.send(mock.Mock(), mock.Mock())
assert 'CONTENT-TYPE' not in req.headers
resp.close()
3
Example 7
def test_auth_utf8(self):
proxy_req = ClientRequest(
'GET', URL('http://proxy.example.com'),
auth=aiohttp.helpers.BasicAuth('юзер', 'пасс', 'utf-8'),
loop=self.loop)
self.assertIn('AUTHORIZATION', proxy_req.headers)
3
Example 8
@mock.patch('aiosocks.connector.create_connection')
def test_connect_locale_resolve(self, cr_conn_mock):
tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')
cr_conn_mock.side_effect = \
fake_coroutine((tr, proto)).side_effect
req = ClientRequest('GET', 'http://python.org', loop=self.loop)
connector = SocksConnector(aiosocks.Socks5Addr('proxy.example'),
None, loop=self.loop, remote_resolve=False)
connector._resolve_host = fake_coroutine([mock.MagicMock()])
conn = self.loop.run_until_complete(connector.connect(req))
self.assertTrue(connector._resolve_host.is_called)
self.assertEqual(connector._resolve_host.call_count, 2)
conn.close()
3
Example 9
@mock.patch('aiosocks.connector.create_connection')
def test_proxy_negotiate_fail(self, cr_conn_mock):
loop_mock = mock.Mock()
cr_conn_mock.side_effect = \
fake_coroutine(aiosocks.SocksError()).side_effect
req = ClientRequest('GET', 'http://python.org', loop=self.loop)
connector = SocksConnector(aiosocks.Socks5Addr('127.0.0.1'),
None, loop=loop_mock)
loop_mock.getaddrinfo = fake_coroutine([mock.MagicMock()])
with self.assertRaises(aiosocks.SocksError):
self.loop.run_until_complete(connector.connect(req))
3
Example 10
def test_content_type_auto_header_form(loop):
req = ClientRequest('post', URL('http://python.org'),
data={'hey': 'you'}, loop=loop)
resp = req.send(mock.Mock(), mock.Mock())
assert 'application/x-www-form-urlencoded' == \
req.headers.get('CONTENT-TYPE')
resp.close()
3
Example 11
@asyncio.coroutine
def test_chunked(loop):
req = ClientRequest(
'get', URL('http://python.org/'),
headers={'TRANSFER-ENCODING': 'gzip'}, loop=loop)
resp = req.send(mock.Mock(), mock.Mock())
assert 'gzip' == req.headers['TRANSFER-ENCODING']
yield from req.close()
resp.close()
3
Example 12
@asyncio.coroutine
def test_content_encoding_dont_set_headers_if_no_body(loop):
req = ClientRequest('get', URL('http://python.org/'),
compress='deflate', loop=loop)
with mock.patch('aiohttp.client_reqrep.aiohttp'):
resp = req.send(mock.Mock(), mock.Mock())
assert 'TRANSFER-ENCODING' not in req.headers
assert 'CONTENT-ENCODING' not in req.headers
yield from req.close()
resp.close()
3
Example 13
@asyncio.coroutine
def test_chunked2(loop):
req = ClientRequest(
'get', URL('http://python.org/'),
headers={'Transfer-encoding': 'chunked'}, loop=loop)
resp = req.send(mock.Mock(), mock.Mock())
assert 'chunked' == req.headers['TRANSFER-ENCODING']
yield from req.close()
resp.close()
3
Example 14
def test_terminate_with_closed_loop(loop):
req = ClientRequest('get', URL('http://python.org'), loop=loop)
resp = req.send(mock.Mock(), mock.Mock())
assert req._writer is not None
writer = req._writer = mock.Mock()
loop.close()
req.terminate()
assert req._writer is None
assert not writer.cancel.called
resp.close()
3
Example 15
@asyncio.coroutine
def test_no_content_length2(loop):
req = ClientRequest('head', URL('http://python.org'), loop=loop)
resp = req.send(mock.Mock(), mock.Mock())
assert '0' == req.headers.get('CONTENT-LENGTH')
yield from req.close()
resp.close()
3
Example 16
def test_proxy_connection_error(self):
connector = aiohttp.TCPConnector(loop=self.loop)
connector._resolve_host = make_mocked_coro(
raise_exception=OSError('dont take it serious'))
req = ClientRequest(
'GET', URL('http://www.python.org'),
proxy=URL('http://proxy.example.com'),
loop=self.loop,
)
expected_headers = dict(req.headers)
with self.assertRaises(aiohttp.ProxyConnectionError):
self.loop.run_until_complete(connector.connect(req))
self.assertEqual(req.url.path, '/')
self.assertEqual(dict(req.headers), expected_headers)
3
Example 17
def test_content_type_auto_header_content_length_no_skip(loop):
req = ClientRequest('get', URL('http://python.org'),
data=io.BytesIO(b'hey'),
skip_auto_headers={'Content-Length'},
loop=loop)
resp = req.send(mock.Mock(), mock.Mock())
assert req.headers.get('CONTENT-LENGTH') == '3'
resp.close()
3
Example 18
@asyncio.coroutine
def test_precompressed_data_stays_intact(loop):
data = zlib.compress(b'foobar')
req = ClientRequest(
'post', URL('http://python.org/'),
data=data,
headers={'CONTENT-ENCODING': 'deflate'},
compress=False,
loop=loop)
assert not req.compress
assert not req.chunked
assert req.headers['CONTENT-ENCODING'] == 'deflate'
yield from req.close()
3
Example 19
def test_proxy_auth(self):
with self.assertRaises(ValueError) as ctx:
ClientRequest(
'GET', URL('http://python.org'),
proxy=URL('http://proxy.example.com'),
proxy_auth=('user', 'pass'),
loop=mock.Mock())
self.assertEqual(
ctx.exception.args[0],
"proxy_auth must be None or BasicAuth() tuple",
)
3
Example 20
@mock.patch('aiosocks.connector.create_connection')
def test_proxy_connect_fail(self, cr_conn_mock):
loop_mock = mock.Mock()
cr_conn_mock.side_effect = \
fake_coroutine(aiosocks.SocksConnectionError()).side_effect
req = ClientRequest('GET', 'http://python.org', loop=self.loop)
connector = SocksConnector(aiosocks.Socks5Addr('127.0.0.1'),
None, loop=loop_mock)
loop_mock.getaddrinfo = fake_coroutine([mock.MagicMock()])
with self.assertRaises(aiohttp.ProxyConnectionError):
self.loop.run_until_complete(connector.connect(req))
3
Example 21
def test_expect100(loop):
req = ClientRequest('get', URL('http://python.org/'),
expect100=True, loop=loop)
resp = req.send(mock.Mock(), mock.Mock())
assert '100-continue' == req.headers['EXPECT']
assert req._continue is not None
req.terminate()
resp.close()
3
Example 22
def test_expect_100_continue_header(loop):
req = ClientRequest('get', URL('http://python.org/'),
headers={'expect': '100-continue'}, loop=loop)
resp = req.send(mock.Mock(), mock.Mock())
assert '100-continue' == req.headers['EXPECT']
assert req._continue is not None
req.terminate()
resp.close()
3
Example 23
@asyncio.coroutine
def test_pass_falsy_data_file(loop, tmpdir):
testfile = tmpdir.join('tmpfile').open('w+b')
testfile.write(b'data')
testfile.seek(0)
skip = frozenset([hdrs.CONTENT_TYPE])
req = ClientRequest(
'post', URL('http://python.org/'),
data=testfile,
skip_auto_headers=skip,
loop=loop)
assert req.headers.get('CONTENT-LENGTH', None) is not None
yield from req.close()
3
Example 24
@asyncio.coroutine
def test_data_file(loop):
req = ClientRequest(
'POST', URL('http://python.org/'),
data=io.BufferedReader(io.BytesIO(b'*' * 2)),
loop=loop)
assert req.chunked
assert isinstance(req.body, io.IOBase)
assert req.headers['TRANSFER-ENCODING'] == 'chunked'
transport = mock.Mock()
resp = req.send(transport, mock.Mock())
assert isinstance(req._writer, asyncio.Future)
yield from resp.wait_for_close()
assert req._writer is None
assert transport.write.mock_calls[-2:] == [
mock.call(b'2\r\n' + b'*' * 2 + b'\r\n'),
mock.call(b'0\r\n\r\n')]
yield from req.close()
3
Example 25
@asyncio.coroutine
def test_no_content_length(loop):
req = ClientRequest('get', URL('http://python.org'), loop=loop)
resp = req.send(mock.Mock(), mock.Mock())
assert '0' == req.headers.get('CONTENT-LENGTH')
yield from req.close()
resp.close()
3
Example 26
def test_content_type_skip_auto_header_bytes(loop):
req = ClientRequest('post', URL('http://python.org'), data=b'hey you',
skip_auto_headers={'Content-Type'},
loop=loop)
resp = req.send(mock.Mock(), mock.Mock())
assert 'CONTENT-TYPE' not in req.headers
resp.close()
3
Example 27
@asyncio.coroutine
def test_data_stream_not_bytes(loop):
@asyncio.coroutine
def gen():
yield object()
req = ClientRequest(
'POST', URL('http://python.org/'), data=gen(), loop=loop)
protocol = mock.Mock()
resp = req.send(mock.Mock(), protocol)
yield from req._writer
assert protocol.set_exception.called
yield from req.close()
resp.close()
3
Example 28
@asyncio.coroutine
def test_pass_falsy_data(loop):
with mock.patch(
'aiohttp.client_reqrep.ClientRequest.update_body_from_data'):
req = ClientRequest(
'post', URL('http://python.org/'),
data={}, loop=loop)
req.update_body_from_data.assert_called_once_with({}, frozenset())
yield from req.close()
3
Example 29
@asyncio.coroutine
def test_close(loop):
@asyncio.coroutine
def gen():
yield from asyncio.sleep(0.00001, loop=loop)
return b'result'
req = ClientRequest(
'POST', URL('http://python.org/'), data=gen(), loop=loop)
transport = mock.Mock()
resp = req.send(transport, mock.Mock())
yield from req.close()
assert transport.write.mock_calls[-2:] == [
mock.call(b'6\r\nresult\r\n'),
mock.call(b'0\r\n\r\n')]
yield from req.close()
resp.close()
3
Example 30
@asyncio.coroutine
def test_file_upload_not_chunked(loop):
here = os.path.dirname(__file__)
fname = os.path.join(here, 'sample.key')
with open(fname, 'rb') as f:
req = ClientRequest(
'post', URL('http://python.org/'),
data=f,
loop=loop)
assert not req.chunked
assert req.headers['CONTENT-LENGTH'] == str(os.path.getsize(fname))
yield from req.close()
3
Example 31
@asyncio.coroutine
def test_terminate(loop):
req = ClientRequest('get', URL('http://python.org'), loop=loop)
resp = req.send(mock.Mock(), mock.Mock())
assert req._writer is not None
writer = req._writer = mock.Mock()
req.terminate()
assert req._writer is None
writer.cancel.assert_called_with()
resp.close()
3
Example 32
@asyncio.coroutine
def test_chunked_explicit(loop):
req = ClientRequest(
'get', URL('http://python.org/'), chunked=True, loop=loop)
with mock.patch('aiohttp.client_reqrep.aiohttp') as m_http:
resp = req.send(mock.Mock(), mock.Mock())
assert 'chunked' == req.headers['TRANSFER-ENCODING']
m_http.Request.return_value\
.add_chunking_filter.assert_called_with(8192)
yield from req.close()
resp.close()
3
Example 33
def test_terminate_without_writer(loop):
req = ClientRequest('get', URL('http://python.org'), loop=loop)
assert req._writer is None
req.terminate()
assert req._writer is None
3
Example 34
@asyncio.coroutine
def test_chunked_length(loop):
req = ClientRequest(
'get', URL('http://python.org/'),
headers={'CONTENT-LENGTH': '1000'}, chunked=1024, loop=loop)
resp = req.send(mock.Mock(), mock.Mock())
assert req.headers['TRANSFER-ENCODING'] == 'chunked'
assert 'CONTENT-LENGTH' not in req.headers
yield from req.close()
resp.close()
3
Example 35
@asyncio.coroutine
def test_file_upload_force_chunked(loop):
here = os.path.dirname(__file__)
fname = os.path.join(here, 'sample.key')
with open(fname, 'rb') as f:
req = ClientRequest(
'post', URL('http://python.org/'),
data=f,
chunked=True,
loop=loop)
assert req.chunked
assert 'CONTENT-LENGTH' not in req.headers
yield from req.close()
3
Example 36
@pytest.yield_fixture
def make_request(loop):
request = None
def maker(method, url, *args, **kwargs):
nonlocal request
request = ClientRequest(method, URL(url), *args, loop=loop, **kwargs)
return request
yield maker
if request is not None:
loop.run_until_complete(request.close())
3
Example 37
def test_content_type_auto_header_bytes(loop):
req = ClientRequest('post', URL('http://python.org'), data=b'hey you',
loop=loop)
resp = req.send(mock.Mock(), mock.Mock())
assert 'application/octet-stream' == req.headers.get('CONTENT-TYPE')
resp.close()
3
Example 38
@asyncio.coroutine
def test_content_encoding_header(loop):
req = ClientRequest(
'get', URL('http://python.org/'), data='foo',
headers={'Content-Encoding': 'deflate'}, loop=loop)
with mock.patch('aiohttp.client_reqrep.aiohttp') as m_http:
resp = req.send(mock.Mock(), mock.Mock())
assert req.headers['TRANSFER-ENCODING'] == 'chunked'
assert req.headers['CONTENT-ENCODING'] == 'deflate'
m_http.Request.return_value\
.add_compression_filter.assert_called_with('deflate')
m_http.Request.return_value\
.add_chunking_filter.assert_called_with(8192)
yield from req.close()
resp.close()
0
Example 39
@asyncio.coroutine
def test_data_stream_continue(loop):
def gen():
yield b'binary data'
return b' result'
req = ClientRequest(
'POST', URL('http://python.org/'), data=gen(),
expect100=True, loop=loop)
assert req.chunked
assert inspect.isgenerator(req.body)
def coro():
yield from asyncio.sleep(0.0001, loop=loop)
req._continue.set_result(1)
helpers.ensure_future(coro(), loop=loop)
transport = mock.Mock()
resp = req.send(transport, mock.Mock())
yield from req._writer
assert transport.write.mock_calls[-2:] == [
mock.call(b'12\r\nbinary data result\r\n'),
mock.call(b'0\r\n\r\n')]
yield from req.close()
resp.close()
0
Example 40
@asyncio.coroutine
def test_data_continue(loop):
req = ClientRequest(
'POST', URL('http://python.org/'), data=b'data',
expect100=True, loop=loop)
def coro():
yield from asyncio.sleep(0.0001, loop=loop)
req._continue.set_result(1)
helpers.ensure_future(coro(), loop=loop)
transport = mock.Mock()
resp = req.send(transport, mock.Mock())
assert 1 == len(transport.write.mock_calls)
yield from req._writer
assert transport.write.mock_calls[-1] == mock.call(b'data')
yield from req.close()
resp.close()
0
Example 41
@mock.patch('aiohttp.connector.ClientRequest')
def test_connect(self, ClientRequestMock):
req = ClientRequest(
'GET', URL('http://www.python.org'),
proxy=URL('http://proxy.example.com'),
loop=self.loop
)
self.assertEqual(str(req.proxy), 'http://proxy.example.com')
# mock all the things!
connector = aiohttp.TCPConnector(loop=self.loop)
connector._resolve_host = make_mocked_coro([mock.MagicMock()])
tr, proto = mock.Mock(), mock.Mock()
self.loop.create_connection = make_mocked_coro((tr, proto))
conn = self.loop.run_until_complete(connector.connect(req))
self.assertEqual(req.path, 'http://www.python.org')
self.assertIs(conn._transport, tr)
self.assertIs(conn._protocol, proto)
ClientRequestMock.assert_called_with(
'GET', URL('http://proxy.example.com'),
auth=None,
headers={'Host': 'www.python.org'},
loop=self.loop)
0
Example 42
@mock.patch('aiohttp.connector.ClientRequest')
def test_auth(self, ClientRequestMock):
proxy_req = ClientRequest(
'GET', URL('http://proxy.example.com'),
auth=aiohttp.helpers.BasicAuth('user', 'pass'),
loop=self.loop
)
ClientRequestMock.return_value = proxy_req
self.assertIn('AUTHORIZATION', proxy_req.headers)
self.assertNotIn('PROXY-AUTHORIZATION', proxy_req.headers)
connector = aiohttp.TCPConnector(loop=self.loop)
connector._resolve_host = make_mocked_coro([mock.MagicMock()])
tr, proto = mock.Mock(), mock.Mock()
self.loop.create_connection = make_mocked_coro((tr, proto))
req = ClientRequest(
'GET', URL('http://www.python.org'),
proxy=URL('http://proxy.example.com'),
proxy_auth=aiohttp.helpers.BasicAuth('user', 'pass'),
loop=self.loop,
)
self.assertNotIn('AUTHORIZATION', req.headers)
self.assertNotIn('PROXY-AUTHORIZATION', req.headers)
conn = self.loop.run_until_complete(connector.connect(req))
self.assertEqual(req.path, 'http://www.python.org')
self.assertNotIn('AUTHORIZATION', req.headers)
self.assertIn('PROXY-AUTHORIZATION', req.headers)
self.assertNotIn('AUTHORIZATION', proxy_req.headers)
self.assertNotIn('PROXY-AUTHORIZATION', proxy_req.headers)
ClientRequestMock.assert_called_with(
'GET', URL('http://proxy.example.com'),
auth=aiohttp.helpers.BasicAuth('user', 'pass'),
loop=mock.ANY, headers=mock.ANY)
conn.close()
0
Example 43
def test_default_loop(loop):
asyncio.set_event_loop(loop)
req = ClientRequest('get', URL('http://python.org/'))
assert req.loop is loop
0
Example 44
def test_content_type_auto_header_get(loop):
req = ClientRequest('get', URL('http://python.org'), loop=loop)
resp = req.send(mock.Mock(), mock.Mock())
assert 'CONTENT-TYPE' not in req.headers
resp.close()
0
Example 45
@mock.patch('aiohttp.connector.ClientRequest')
def test_auth_from_url(self, ClientRequestMock):
proxy_req = ClientRequest('GET',
URL('http://user:[email protected]'),
loop=self.loop)
ClientRequestMock.return_value = proxy_req
self.assertIn('AUTHORIZATION', proxy_req.headers)
self.assertNotIn('PROXY-AUTHORIZATION', proxy_req.headers)
connector = aiohttp.TCPConnector(loop=self.loop)
connector._resolve_host = make_mocked_coro([mock.MagicMock()])
tr, proto = mock.Mock(), mock.Mock()
self.loop.create_connection = make_mocked_coro((tr, proto))
req = ClientRequest(
'GET', URL('http://www.python.org'),
proxy=URL('http://user:[email protected]'),
loop=self.loop,
)
self.assertNotIn('AUTHORIZATION', req.headers)
self.assertNotIn('PROXY-AUTHORIZATION', req.headers)
conn = self.loop.run_until_complete(connector.connect(req))
self.assertEqual(req.path, 'http://www.python.org')
self.assertNotIn('AUTHORIZATION', req.headers)
self.assertIn('PROXY-AUTHORIZATION', req.headers)
self.assertNotIn('AUTHORIZATION', proxy_req.headers)
self.assertNotIn('PROXY-AUTHORIZATION', proxy_req.headers)
ClientRequestMock.assert_called_with(
'GET', URL('http://user:[email protected]'),
auth=None, loop=mock.ANY, headers=mock.ANY)
conn.close()
0
Example 46
@mock.patch('aiohttp.connector.ClientRequest')
def test_auth__not_modifying_request(self, ClientRequestMock):
proxy_req = ClientRequest('GET',
URL('http://user:[email protected]'),
loop=self.loop)
ClientRequestMock.return_value = proxy_req
proxy_req_headers = dict(proxy_req.headers)
connector = aiohttp.TCPConnector(loop=self.loop)
connector._resolve_host = make_mocked_coro(
raise_exception=OSError('nothing personal'))
req = ClientRequest(
'GET', URL('http://www.python.org'),
proxy=URL('http://user:[email protected]'),
loop=self.loop,
)
req_headers = dict(req.headers)
with self.assertRaises(aiohttp.ProxyConnectionError):
self.loop.run_until_complete(connector.connect(req))
self.assertEqual(req.headers, req_headers)
self.assertEqual(req.url.path, '/')
self.assertEqual(proxy_req.headers, proxy_req_headers)
0
Example 47
@mock.patch('aiohttp.connector.ClientRequest')
def test_https_connect(self, ClientRequestMock):
proxy_req = ClientRequest('GET', URL('http://proxy.example.com'),
loop=self.loop)
ClientRequestMock.return_value = proxy_req
proxy_resp = ClientResponse('get', URL('http://proxy.example.com'))
proxy_resp._loop = self.loop
proxy_req.send = send_mock = mock.Mock()
send_mock.return_value = proxy_resp
proxy_resp.start = make_mocked_coro(mock.Mock(status=200))
connector = aiohttp.TCPConnector(loop=self.loop)
connector._resolve_host = make_mocked_coro(
[{'hostname': 'hostname', 'host': '127.0.0.1', 'port': 80,
'family': socket.AF_INET, 'proto': 0, 'flags': 0}])
tr, proto = mock.Mock(), mock.Mock()
self.loop.create_connection = make_mocked_coro((tr, proto))
req = ClientRequest(
'GET', URL('https://www.python.org'),
proxy=URL('http://proxy.example.com'),
loop=self.loop,
)
self.loop.run_until_complete(connector._create_connection(req))
self.assertEqual(req.url.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)
self.loop.run_until_complete(proxy_req.close())
proxy_resp.close()
self.loop.run_until_complete(req.close())
0
Example 48
@asyncio.coroutine
def test_data_stream(loop):
def gen():
yield b'binary data'
return b' result'
req = ClientRequest(
'POST', URL('http://python.org/'), data=gen(), loop=loop)
assert req.chunked
assert inspect.isgenerator(req.body)
assert req.headers['TRANSFER-ENCODING'] == 'chunked'
transport = mock.Mock()
resp = req.send(transport, mock.Mock())
assert isinstance(req._writer, asyncio.Future)
yield from resp.wait_for_close()
assert req._writer is None
assert transport.write.mock_calls[-2:] == [
mock.call(b'12\r\nbinary data result\r\n'),
mock.call(b'0\r\n\r\n')]
yield from req.close()
0
Example 49
@asyncio.coroutine
def test_data_stream_exc(loop):
fut = helpers.create_future(loop)
def gen():
yield b'binary data'
yield from fut
req = ClientRequest(
'POST', URL('http://python.org/'), data=gen(), loop=loop)
assert req.chunked
assert inspect.isgenerator(req.body)
assert req.headers['TRANSFER-ENCODING'] == 'chunked'
@asyncio.coroutine
def exc():
yield from asyncio.sleep(0.01, loop=loop)
fut.set_exception(ValueError)
helpers.ensure_future(exc(), loop=loop)
protocol = mock.Mock()
resp = req.send(mock.Mock(), protocol)
connection = mock.Mock()
resp._connection = connection
yield from req._writer
assert connection.close.called
assert protocol.set_exception.called
yield from req.close()
0
Example 50
@asyncio.coroutine
def test_data_stream_exc_chain(loop):
fut = helpers.create_future(loop)
def gen():
yield from fut
req = ClientRequest('POST', URL('http://python.org/'),
data=gen(), loop=loop)
inner_exc = ValueError()
@asyncio.coroutine
def exc():
yield from asyncio.sleep(0.01, loop=loop)
fut.set_exception(inner_exc)
helpers.ensure_future(exc(), loop=loop)
protocol = mock.Mock()
resp = req.send(mock.Mock(), protocol)
connection = mock.Mock()
resp._connection = connection
yield from req._writer
assert connection.close.called
assert protocol.set_exception.called
outer_exc = protocol.set_exception.call_args[0][0]
assert isinstance(outer_exc, aiohttp.ClientRequestError)
assert inner_exc is outer_exc.__context__
assert inner_exc is outer_exc.__cause__
yield from req.close()