本文整理汇总了Python中aiohttp.test_utils.TestServer方法的典型用法代码示例。如果您正苦于以下问题:Python test_utils.TestServer方法的具体用法?Python test_utils.TestServer怎么用?Python test_utils.TestServer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aiohttp.test_utils
的用法示例。
在下文中一共展示了test_utils.TestServer方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: aiohttp_server
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestServer [as 别名]
def aiohttp_server():
"""Factory to create a TestServer instance, given an app.
aiohttp_server(app, **kwargs)
"""
servers = []
async def go(app, *, port=None, **kwargs): # type: ignore
server = AIOHTTPTestServer(app, port=port)
await server.start_server(**kwargs)
servers.append(server)
return server
yield go
while servers:
await servers.pop().close()
# Adding debug logs to websocket tests
示例2: test_client
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestServer [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())
示例3: client
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestServer [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 TestServer [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 TestServer [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 TestServer [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 TestServer [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: server
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestServer [as 别名]
def server(aiohttp_server) -> TestServer:
app = create_app()
server = await aiohttp_server(app, port=TEST_PORT)
return server
示例9: aiohttp_client
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestServer [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
示例10: fake_zipkin
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestServer [as 别名]
def fake_zipkin(loop):
zipkin = FakeZipkin(loop=loop)
server = TestServer(zipkin.app, loop=loop)
await server.start_server()
zipkin.port = server.port
await yield_(zipkin)
await server.close()
示例11: _get_client_and_server
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestServer [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)
示例12: server
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestServer [as 别名]
def server(aiohttp_server: Any, db_path: Path) -> _TestServer:
app = await init_app(db_path)
return await aiohttp_server(app)
示例13: client
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestServer [as 别名]
def client(server: _TestServer) -> AsyncIterator[Client]:
async with Client(server.make_url("/"), "test_user") as client:
yield client
示例14: fake_cloud
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import TestServer [as 别名]
def fake_cloud(aiohttp_server, ssl_ctx):
"""Defines the testserver funcarg"""
loop = asyncio.new_event_loop()
stop_threads = False
t = Thread(
name="aio_fake_cloud",
target=start_background_loop,
args=(lambda: stop_threads, loop),
)
t.setDaemon(True)
t.start()
aio_server = AsyncFakeCloudServer()
app = web.Application()
app.router.add_route("GET", "/{tail:.*}", aio_server)
app.router.add_route("POST", "/{tail:.*}", aio_server)
server = TestServer(app)
asyncio.run_coroutine_threadsafe(
server.start_server(loop=loop, ssl=ssl_ctx), loop
).result()
aio_server.url = str(server._root)
server.url = aio_server.url
yield server
asyncio.run_coroutine_threadsafe(server.close(), loop).result()
stop_threads = True