Here are the examples of the python api tests.mocks.getaddrinfo_async_mock taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
5 Examples
3
Source : conftest.py
with Apache License 2.0
from romis2012
with Apache License 2.0
from romis2012
def patch_anyio_getaddrinfo():
import anyio
with mock.patch(
'anyio._core._sockets.getaddrinfo',
new=getaddrinfo_async_mock(anyio.getaddrinfo),
):
yield None
@pytest.fixture(scope='session', autouse=True)
0
Source : test_proxy_async_aio_v2.py
with Apache License 2.0
from romis2012
with Apache License 2.0
from romis2012
async def make_request(proxy: AsyncioProxy, url: str, resolve_host=False, timeout=None):
loop = asyncio.get_event_loop()
with patch.object(
loop,
attribute='getaddrinfo',
new=getaddrinfo_async_mock(loop.getaddrinfo),
):
url = URL(url)
dest_host = url.host
if resolve_host:
resolver = Resolver(loop=loop)
_, dest_host = await resolver.resolve(url.host)
ssl_context = None
if url.scheme == 'https':
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS)
ssl_context.verify_mode = ssl.CERT_REQUIRED
ssl_context.load_verify_locations(TEST_HOST_PEM_FILE)
stream = await proxy.connect(
dest_host=dest_host, dest_port=url.port, dest_ssl=ssl_context, timeout=timeout
)
# fmt: off
request = (
'GET {rel_url} HTTP/1.1\r\n'
'Host: {host}\r\n'
'Connection: close\r\n\r\n'
)
# fmt: on
request = request.format(rel_url=url.path_qs, host=url.host)
request = request.encode('ascii')
await stream.write_all(request)
response = await stream.read(1024)
status_line = response.split(b'\r\n', 1)[0]
version, status_code, *reason = status_line.split()
await stream.close()
return int(status_code)
@pytest.mark.parametrize('url', (TEST_URL_IPV4, TEST_URL_IPV4_HTTPS))
0
Source : test_proxy_async_anyio.py
with Apache License 2.0
from romis2012
with Apache License 2.0
from romis2012
async def make_request(
proxy: AnyioProxy,
url: str,
resolve_host=False,
timeout=None,
):
# import anyio
with patch(
'anyio._core._sockets.getaddrinfo',
new=getaddrinfo_async_mock(anyio.getaddrinfo),
):
url = URL(url)
dest_host = url.host
if resolve_host:
resolver = Resolver()
_, dest_host = await resolver.resolve(url.host)
ssl_context = None
if url.scheme == 'https':
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS)
ssl_context.verify_mode = ssl.CERT_REQUIRED
ssl_context.load_verify_locations(TEST_HOST_PEM_FILE)
stream = await proxy.connect(
dest_host=dest_host,
dest_port=url.port,
dest_ssl=ssl_context,
timeout=timeout,
)
# fmt: off
request = (
'GET {rel_url} HTTP/1.1\r\n'
'Host: {host}\r\n'
'Connection: close\r\n\r\n'
)
# fmt: on
request = request.format(rel_url=url.path_qs, host=url.host)
request = request.encode('ascii')
await stream.write_all(request)
response = await stream.read(1024)
status_line = response.split(b'\r\n', 1)[0]
version, status_code, *reason = status_line.split()
await stream.close()
return int(status_code)
@pytest.mark.parametrize('url', (TEST_URL_IPV4, TEST_URL_IPV4_HTTPS))
0
Source : test_proxy_async_curio.py
with Apache License 2.0
from romis2012
with Apache License 2.0
from romis2012
async def make_request(
proxy: CurioProxy,
url: str,
resolve_host=False,
timeout=None,
):
with patch(
'curio.socket.getaddrinfo',
new=getaddrinfo_async_mock(curio.socket.getaddrinfo),
):
url = URL(url)
dest_host = url.host
if resolve_host:
resolver = Resolver()
_, dest_host = await resolver.resolve(url.host)
sock: curio.io.Socket = await proxy.connect(
dest_host=dest_host, dest_port=url.port, timeout=timeout
)
ssl_context: Optional[curiossl.CurioSSLContext] = None
if url.scheme == 'https':
_ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS)
_ssl_context.verify_mode = ssl.CERT_REQUIRED
_ssl_context.load_verify_locations(TEST_HOST_PEM_FILE)
ssl_context = curiossl.CurioSSLContext(_ssl_context)
if ssl_context is not None:
sock = await ssl_context.wrap_socket(
sock, do_handshake_on_connect=False, server_hostname=url.host
)
await sock.do_handshake()
stream = sock.as_stream()
# fmt: off
request = (
'GET {rel_url} HTTP/1.1\r\n'
'Host: {host}\r\n'
'Connection: close\r\n\r\n'
)
# fmt: on
request = request.format(rel_url=url.path_qs, host=url.host)
request = request.encode('ascii')
await stream.write(request)
response = await stream.read(1024)
status_line = response.split(b'\r\n', 1)[0]
status_line = status_line.decode('utf-8', 'surrogateescape')
version, status_code, *reason = status_line.split()
return int(status_code)
@pytest.mark.parametrize('url', (TEST_URL_IPV4, TEST_URL_IPV4_HTTPS))
0
Source : test_proxy_async_trio_v2.py
with Apache License 2.0
from romis2012
with Apache License 2.0
from romis2012
async def make_request(
proxy: TrioProxy,
url: str,
resolve_host=False,
timeout=None,
):
with patch(
'trio._highlevel_open_tcp_stream.getaddrinfo',
new=getaddrinfo_async_mock(trio.socket.getaddrinfo),
):
url = URL(url)
dest_host = url.host
if resolve_host:
resolver = Resolver()
_, dest_host = await resolver.resolve(url.host)
ssl_context = None
if url.scheme == 'https':
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS)
ssl_context.verify_mode = ssl.CERT_REQUIRED
ssl_context.load_verify_locations(TEST_HOST_PEM_FILE)
stream = await proxy.connect(
dest_host=dest_host,
dest_port=url.port,
dest_ssl=ssl_context,
timeout=timeout,
)
# fmt: off
request = (
'GET {rel_url} HTTP/1.1\r\n'
'Host: {host}\r\n'
'Connection: close\r\n\r\n'
)
# fmt: on
request = request.format(rel_url=url.path_qs, host=url.host)
request = request.encode('ascii')
await stream.write_all(request)
response = await stream.read(1024)
status_line = response.split(b'\r\n', 1)[0]
version, status_code, *reason = status_line.split()
await stream.close()
return int(status_code)
@pytest.mark.parametrize('url', (TEST_URL_IPV4, TEST_URL_IPV4_HTTPS))