async def test_terminate(loop, conn) -> None: req = ClientRequest('get', URL('http://python.org'), loop=loop) resp = await req.send(conn) 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()
async def test_expect_100_continue_header(loop, conn) -> None: req = ClientRequest('get', URL('http://python.org/'), headers={'expect': '100-continue'}, loop=loop) resp = await req.send(conn) assert '100-continue' == req.headers['EXPECT'] assert req._continue is not None req.terminate() resp.close()
async def test_data_stream_continue(loop, buf, conn) -> None: @async_generator async def gen(): await yield_(b'binary data') await yield_(b' result') req = ClientRequest('POST', URL('http://python.org/'), data=gen(), expect100=True, loop=loop) assert req.chunked async def coro(): await asyncio.sleep(0.0001, loop=loop) req._continue.set_result(1) loop.create_task(coro()) resp = await req.send(conn) await req._writer assert buf.split(b'\r\n\r\n', 1)[1] == \ b'b\r\nbinary data\r\n7\r\n result\r\n0\r\n\r\n' await req.close() resp.close()
async def test_chunked_transfer_encoding(loop, conn) -> None: with pytest.raises(ValueError): ClientRequest('post', URL('http://python.org/'), headers={'TRANSFER-ENCODING': 'chunked'}, chunked=True, loop=loop)
async def test_chunked_length(loop, conn) -> None: with pytest.raises(ValueError): ClientRequest('post', URL('http://python.org/'), headers={'CONTENT-LENGTH': '1000'}, chunked=True, loop=loop)
async def test_data_stream_exc(loop, conn) -> None: fut = loop.create_future() @async_generator async def gen(): await yield_(b'binary data') await fut req = ClientRequest('POST', URL('http://python.org/'), data=gen(), loop=loop) assert req.chunked assert req.headers['TRANSFER-ENCODING'] == 'chunked' async def throw_exc(): await asyncio.sleep(0.01, loop=loop) fut.set_exception(ValueError) loop.create_task(throw_exc()) await req.send(conn) await req._writer # assert conn.close.called assert conn.protocol.set_exception.called await req.close()
async def test_data_stream_exc_chain(loop, conn) -> None: fut = loop.create_future() @async_generator async def gen(): await fut req = ClientRequest('POST', URL('http://python.org/'), data=gen(), loop=loop) inner_exc = ValueError() async def throw_exc(): await asyncio.sleep(0.01, loop=loop) fut.set_exception(inner_exc) loop.create_task(throw_exc()) await req.send(conn) await req._writer # assert connection.close.called assert conn.protocol.set_exception.called outer_exc = conn.protocol.set_exception.call_args[0][0] assert isinstance(outer_exc, ValueError) assert inner_exc is outer_exc assert inner_exc is outer_exc await req.close()
async def test_content_type_auto_header_bytes(loop, conn) -> None: req = ClientRequest('post', URL('http://python.org'), data=b'hey you', loop=loop) resp = await req.send(conn) assert 'application/octet-stream' == req.headers.get('CONTENT-TYPE') resp.close()
async def go(): nonlocal req, resp, writer req = ClientRequest('get', URL('http://python.org')) resp = await req.send(conn) assert req._writer is not None writer = req._writer = mock.Mock() await asyncio.sleep(0.05)
async def test_compress_and_content_encoding(loop, conn) -> None: with pytest.raises(ValueError): ClientRequest('post', URL('http://python.org/'), data='foo', headers={'content-encoding': 'deflate'}, compress='deflate', loop=loop)
async def test_pass_falsy_data(loop) -> None: 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({}) await req.close()
async def test_content_type_skip_auto_header_form(loop, conn) -> None: req = ClientRequest('post', URL('http://python.org'), data={'hey': 'you'}, loop=loop, skip_auto_headers={'Content-Type'}) resp = await req.send(conn) assert 'CONTENT-TYPE' not in req.headers resp.close()
async def test_content_type_auto_header_form(loop, conn) -> None: req = ClientRequest('post', URL('http://python.org'), data={'hey': 'you'}, loop=loop) resp = await req.send(conn) assert 'application/x-www-form-urlencoded' == \ req.headers.get('CONTENT-TYPE') resp.close()
async def test_get_with_data(loop) -> None: for meth in ClientRequest.GET_METHODS: req = ClientRequest(meth, URL('http://python.org/'), data={'life': '42'}, loop=loop) assert '/' == req.url.path assert b'life=42' == req.body._value await req.close()
async def test_chunked2(loop, conn) -> None: req = ClientRequest('post', URL('http://python.org/'), headers={'Transfer-encoding': 'chunked'}, loop=loop) resp = await req.send(conn) assert 'chunked' == req.headers['TRANSFER-ENCODING'] await req.close() resp.close()
async def test_urlencoded_formdata_charset(loop, conn) -> None: req = ClientRequest('post', URL('http://python.org'), data=aiohttp.FormData({'hey': 'you'}, charset='koi8-r'), loop=loop) await req.send(conn) assert 'application/x-www-form-urlencoded; charset=koi8-r' == \ req.headers.get('CONTENT-TYPE')
async def test_content_type_auto_header_content_length_no_skip(loop, conn) -> None: req = ClientRequest('post', URL('http://python.org'), data=io.BytesIO(b'hey'), skip_auto_headers={'Content-Length'}, loop=loop) resp = await req.send(conn) assert req.headers.get('CONTENT-LENGTH') == '3' resp.close()
async def test_file_upload_not_chunked(loop) -> None: here = os.path.dirname(__file__) fname = os.path.join(here, 'aiohttp.png') 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)) await req.close()
async def test_oserror_on_write_bytes(loop, conn) -> None: req = ClientRequest('POST', URL('http://python.org/'), loop=loop) writer = mock.Mock() writer.write.side_effect = OSError await req.write_bytes(writer, conn) assert conn.protocol.set_exception.called exc = conn.protocol.set_exception.call_args[0][0] assert isinstance(exc, aiohttp.ClientOSError)
async def test_content_encoding_dont_set_headers_if_no_body(loop, conn) -> None: req = ClientRequest('post', URL('http://python.org/'), compress='deflate', loop=loop) with mock.patch('aiohttp.client_reqrep.http'): resp = await req.send(conn) assert 'TRANSFER-ENCODING' not in req.headers assert 'CONTENT-ENCODING' not in req.headers await req.close() resp.close()
async def test_file_upload_force_chunked(loop) -> None: here = os.path.dirname(__file__) fname = os.path.join(here, 'aiohttp.png') 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 await req.close()
async def test_precompressed_data_stays_intact(loop) -> None: 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' await req.close()
async def test_pass_falsy_data_file(loop, tmpdir) -> None: 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 await req.close()
async def test_post_data(loop, conn) -> None: for meth in ClientRequest.POST_METHODS: req = ClientRequest(meth, URL('http://python.org/'), data={'life': '42'}, loop=loop) resp = await req.send(conn) assert '/' == req.url.path assert b'life=42' == req.body._value assert 'application/x-www-form-urlencoded' ==\ req.headers['CONTENT-TYPE'] await req.close() resp.close()
async def test_custom_response_class(loop, conn) -> None: class CustomResponse(ClientResponse): def read(self, decode=False): return 'customized!' req = ClientRequest('GET', URL('http://python.org/'), response_class=CustomResponse, loop=loop) resp = await req.send(conn) assert 'customized!' == resp.read() await req.close() resp.close()
async def test_chunked_explicit(loop, conn) -> None: req = ClientRequest('post', URL('http://python.org/'), chunked=True, loop=loop) with mock.patch('aiohttp.client_reqrep.StreamWriter') as m_writer: m_writer.return_value.write_headers = make_mocked_coro() resp = await req.send(conn) assert 'chunked' == req.headers['TRANSFER-ENCODING'] m_writer.return_value.enable_chunking.assert_called_with() await req.close() resp.close()
def test_request_comparison(): loop = asyncio.get_event_loop() import time start = time.time() for i in range(10000): ClientRequest("GET", _URL) print("aioclient completed: %s" % (time.time() - start)) for i in range(10000): BaseClientRequest("GET", _URL) print("aiohttp.client completed: %s" % (time.time() - start))
async def test_bytes_data(loop, conn) -> None: for meth in ClientRequest.POST_METHODS: req = ClientRequest(meth, URL('http://python.org/'), data=b'binary data', loop=loop) resp = await req.send(conn) assert '/' == req.url.path assert isinstance(req.body, payload.BytesPayload) assert b'binary data' == req.body._value assert 'application/octet-stream' == req.headers['CONTENT-TYPE'] await req.close() resp.close()
async def test_content_encoding_header(loop, conn) -> None: req = ClientRequest('post', URL('http://python.org/'), data='foo', headers={'Content-Encoding': 'deflate'}, loop=loop) with mock.patch('aiohttp.client_reqrep.StreamWriter') as m_writer: m_writer.return_value.write_headers = make_mocked_coro() resp = await req.send(conn) assert not m_writer.return_value.enable_compression.called assert not m_writer.return_value.enable_chunking.called await req.close() resp.close()
async def test_content_encoding(loop, conn) -> None: req = ClientRequest('post', URL('http://python.org/'), data='foo', compress='deflate', loop=loop) with mock.patch('aiohttp.client_reqrep.StreamWriter') as m_writer: m_writer.return_value.write_headers = make_mocked_coro() resp = await req.send(conn) assert req.headers['TRANSFER-ENCODING'] == 'chunked' assert req.headers['CONTENT-ENCODING'] == 'deflate' m_writer.return_value\ .enable_compression.assert_called_with('deflate') await req.close() resp.close()