Beispiel #1
0
from httpx import AsyncioBackend, HTTPVersionConfig, SSLConfig, TimeoutConfig
from httpx.concurrency.trio import TrioBackend


@pytest.mark.parametrize(
    "backend, get_cipher",
    [
        pytest.param(
            AsyncioBackend(),
            lambda stream: stream.stream_writer.get_extra_info("cipher",
                                                               default=None),
            marks=pytest.mark.asyncio,
        ),
        pytest.param(
            TrioBackend(),
            lambda stream:
            (stream.stream.cipher()
             if isinstance(stream.stream, trio.SSLStream) else None),
            marks=pytest.mark.trio,
        ),
    ],
)
async def test_start_tls_on_socket_stream(https_server, backend, get_cipher):
    """
    See that the concurrency backend can make a connection without TLS then
    start TLS on an existing connection.
    """
    if isinstance(backend, AsyncioBackend) and sys.version_info < (3, 7):
        pytest.xfail(
            reason="Requires Python 3.7+ for AbstractEventLoop.start_tls()")
Beispiel #2
0
def new_client() -> httpx.Client:
    return httpx.Client(
        backend=TrioBackend(),
        timeout=httpx.TimeoutConfig(const.TIMEOUT),
        pool_limits=httpx.PoolLimits(soft_limit=6, hard_limit=100),
    )
Beispiel #3
0
    RequestState,
    MAX_TRIES,
    TIMEOUT,
    SUCCESS,
    TOO_MANY_REQUESTS,
)

FORMAT = ("<green>{time:YYYY-MM-DD at HH:mm:ss}</green> "
          "| <lvl>{level}</lvl> | <lvl>{message}</lvl>")
logger.remove()
logger.add(sys.stdout, format=FORMAT, level="WARNING")

limit = trio.CapacityLimiter(4)

global_client = httpx.Client(
    backend=TrioBackend(),
    timeout=TimeoutConfig(TIMEOUT),
    pool_limits=PoolLimits(soft_limit=6, hard_limit=100),
)

ConnectErrors = (
    socket.gaierror,
    trio.BrokenResourceError,
    ConnectTimeout,
    StreamClosedError,
    ssl.SSLError,
    ReadTimeout,
)


class Crawler:
Beispiel #4
0
        # We know we're at the end of the response when we've received the body plus
        # the terminating CRLFs.
        if should_contain in response and response.endswith(b"\r\n\r\n"):
            ended = True
            break

    assert ended
    return response


@pytest.mark.parametrize(
    "backend, get_cipher",
    [
        pytest.param(
            AsyncioBackend(), get_asyncio_cipher, marks=pytest.mark.asyncio),
        pytest.param(TrioBackend(), get_trio_cipher, marks=pytest.mark.trio),
    ],
)
async def test_start_tls_on_tcp_socket_stream(https_server, backend,
                                              get_cipher):
    ctx = SSLConfig().load_ssl_context_no_verify()
    timeout = TimeoutConfig(5)

    stream = await backend.open_tcp_stream(https_server.url.host,
                                           https_server.url.port, None,
                                           timeout)

    try:
        assert stream.is_connection_dropped() is False
        assert get_cipher(stream) is None