本文整理匯總了Python中aiohttp.test_utils方法的典型用法代碼示例。如果您正苦於以下問題:Python aiohttp.test_utils方法的具體用法?Python aiohttp.test_utils怎麽用?Python aiohttp.test_utils使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類aiohttp
的用法示例。
在下文中一共展示了aiohttp.test_utils方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _http_request
# 需要導入模塊: import aiohttp [as 別名]
# 或者: from aiohttp import test_utils [as 別名]
def _http_request(
trace_config,
url: str,
method: str = "GET",
status_code: int = HTTPStatus.OK,
request_handler: typing.Callable = None,
**kwargs
) -> typing.Tuple[str, int]:
"""Helper to start an aiohttp test server and send an actual HTTP request to it."""
async def do_request():
async def default_handler(request):
assert "traceparent" in request.headers
return aiohttp.web.Response(status=int(status_code))
handler = request_handler or default_handler
app = aiohttp.web.Application()
parsed_url = urllib.parse.urlparse(url)
app.add_routes([aiohttp.web.get(parsed_url.path, handler)])
app.add_routes([aiohttp.web.post(parsed_url.path, handler)])
app.add_routes([aiohttp.web.patch(parsed_url.path, handler)])
with contextlib.suppress(aiohttp.ClientError):
async with aiohttp.test_utils.TestServer(app) as server:
netloc = (server.host, server.port)
async with aiohttp.test_utils.TestClient(
server, trace_configs=[trace_config]
) as client:
await client.start_server()
await client.request(
method, url, trace_request_ctx={}, **kwargs
)
return netloc
loop = asyncio.get_event_loop()
return loop.run_until_complete(do_request())