本文整理汇总了Python中aiohttp.test_utils.TestClient方法的典型用法代码示例。如果您正苦于以下问题:Python test_utils.TestClient方法的具体用法?Python test_utils.TestClient怎么用?Python test_utils.TestClient使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aiohttp.test_utils
的用法示例。
在下文中一共展示了test_utils.TestClient方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_client
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestClient [as 别名]
def test_client(loop, client_class):
"""Factory to create a TestClient instance.
test_client(app, **kwargs)
"""
clients = []
async def go(app, server_kwargs=None, **kwargs):
if isinstance(app, web.Application):
server_kwargs = server_kwargs or {}
server = test_utils.TestServer(app, loop=loop, **server_kwargs)
else:
server = app
client = client_class(server, loop=loop, **kwargs)
await client.start_server()
clients.append(client)
return client
yield go
async def finalize():
while clients:
await clients.pop().close()
loop.run_until_complete(finalize())
示例2: test_get_post
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestClient [as 别名]
def test_get_post(client: _TestClient, db: aiosqlite.Connection) -> None:
async with db.execute(
"INSERT INTO posts (title, text, owner, editor) VALUES (?, ?, ?, ?)",
["title", "text", "user", "user"],
) as cursor:
post_id = cursor.lastrowid
await db.commit()
resp = await client.get(f"/api/{post_id}")
assert resp.status == 200
data = await resp.json()
assert data == {
"data": {
"editor": "user",
"id": "1",
"owner": "user",
"text": "text",
"title": "title",
},
"status": "ok",
}
示例3: client
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestClient [as 别名]
def client(app):
client = TestClient(TestServer(app))
await client.start_server()
yield client
await client.close()
示例4: working_client
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestClient [as 别名]
def working_client():
loop = asyncio.get_event_loop()
app = web.Application()
app.router.add_get('/.well-known/openid-configuration', respond_json({'token_endpoint': '/token'}))
app.router.add_post('/token', respond_json({'id-token': 'id-token-data', 'refresh-token': 'refresh-token-data'}))
with patch('kubernetes_asyncio.config.openid.aiohttp.ClientSession') as _client_session:
client = _TestClient(_TestServer(app, loop=loop), loop=loop)
_client_session.return_value = client
yield client
示例5: fail_well_known_client
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestClient [as 别名]
def fail_well_known_client():
loop = asyncio.get_event_loop()
app = web.Application()
app.router.add_get('/.well-known/openid-configuration', make_responder(web.Response(status=500)))
with patch('kubernetes_asyncio.config.openid.aiohttp.ClientSession') as _client_session:
client = _TestClient(_TestServer(app, loop=loop), loop=loop)
_client_session.return_value = client
yield client
示例6: fail_token_request_client
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestClient [as 别名]
def fail_token_request_client():
loop = asyncio.get_event_loop()
app = web.Application()
app.router.add_get('/.well-known/openid-configuration', respond_json({'token_endpoint': '/token'}))
app.router.add_post('/token', make_responder(web.Response(status=500)))
with patch('kubernetes_asyncio.config.openid.aiohttp.ClientSession') as _client_session:
client = _TestClient(_TestServer(app, loop=loop), loop=loop)
_client_session.return_value = client
yield client
示例7: test_rpc_client
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestClient [as 别名]
def test_rpc_client(loop):
test_client = None
rpc_client = None
async def _create_from_app_factory(app_factory, *args, **kwargs):
nonlocal test_client, rpc_client
app = app_factory(loop, *args, **kwargs)
test_client = TestClient(TestServer(app), loop=loop)
await test_client.start_server()
rpc_client = ServerProxy(
'',
loop=loop,
client=test_client
)
return rpc_client
yield _create_from_app_factory
if rpc_client:
loop.run_until_complete(rpc_client.close())
rpc_client = None
if test_client:
loop.run_until_complete(test_client.close())
test_client = None
示例8: client
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestClient [as 别名]
def client(aiohttp_client, server) -> TestClient:
return await aiohttp_client(server)
示例9: xtest_render_html_basic
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestClient [as 别名]
def xtest_render_html_basic(event_loop):
expected = BS(open(os.path.join(RESPONSES_DIR, 'httpbin.org.html.txt')).read()).prettify()
client = tc(setup_app(loop=event_loop), loop=event_loop)
await client.start_server()
resp = await client.get('/render.html?url={}'.format(quote('{}/html'.format(HTTPBIN_HOST))))
assert resp.status == 200
text = await resp.text()
assert expected == text
示例10: xtest_render_html_with_js_profile
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestClient [as 别名]
def xtest_render_html_with_js_profile(event_loop):
expected = BS(open(os.path.join(RESPONSES_DIR, 'httpbin.org.html.after_profile.txt')).read()).prettify()
profile_name = 'httpbin-org-html'
profile_path = os.path.join(PROJECT_ROOT, 'tests/resources/js/profiles/{}'.format(profile_name))
client = tc(setup_app(loop=event_loop, js_profiles_path=profile_path), loop=event_loop)
await client.start_server()
resp = await client.get('/render.html?url={}&js={}'.format(
quote('{}/html'.format(HTTPBIN_HOST)),
profile_name))
assert resp.status == 200
text = await resp.text()
assert expected == text
示例11: aiohttp_client
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestClient [as 别名]
def aiohttp_client(self, app: asyncworker.App) -> TestClient:
routes = app.routes_registry.http_routes
http_app = web.Application()
for route in routes:
for route_def in route.aiohttp_routes():
route_def.register(http_app.router)
self.server = TestServer(
http_app, port=int(os.getenv("TEST_ASYNCWORKER_HTTP_PORT") or 0)
)
client = TestClient(self.server)
await self.server.start_server()
return client
示例12: http_client
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestClient [as 别名]
def http_client(http_server):
client = TestClient(http_server)
asyncio.ensure_future(client.start_server())
await asyncio.sleep(0.01)
return client
示例13: test_client_is_passed_to_test
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestClient [as 别名]
def test_client_is_passed_to_test(self):
@http_client(self.app)
async def my_test_case(client):
return client
client = await my_test_case()
self.assertTrue(isinstance(client, TestClient))
示例14: test_decorated_method_can_have_its_own_params
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestClient [as 别名]
def test_decorated_method_can_have_its_own_params(self):
@http_client(self.app)
async def my_method(a, b, http_client):
return (http_client, a, b)
rv = await my_method(42, 10)
self.assertTrue(isinstance(rv[0], TestClient))
self.assertEqual([42, 10], [rv[1], rv[2]])
示例15: _get_client_and_server
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestClient [as 别名]
def _get_client_and_server(app: App) -> Tuple[TestClient, TestServer]:
routes = app.routes_registry.http_routes
http_app = web.Application()
for route in routes:
for route_def in route.aiohttp_routes():
route_def.register(http_app.router)
port = int(os.getenv("TEST_ASYNCWORKER_HTTP_PORT") or 0)
server = TestServer(http_app, port=port)
client = TestClient(server)
await server.start_server()
return (client, server)