Example #1
0
async def test_start_tls_on_uds_socket_stream(https_uds_server):
    backend = lookup_backend()
    ctx = SSLConfig().load_ssl_context_no_verify()
    timeout = Timeout(5)

    stream = await backend.open_uds_stream(https_uds_server.config.uds,
                                           https_uds_server.url.host, None,
                                           timeout)

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

        stream = await stream.start_tls(https_uds_server.url.host, ctx,
                                        timeout)
        assert stream.is_connection_dropped() is False
        assert get_cipher(stream) is not None

        await stream.write(b"GET / HTTP/1.1\r\n\r\n", timeout)

        response = await read_response(stream,
                                       timeout,
                                       should_contain=b"Hello, world")
        assert response.startswith(b"HTTP/1.1 200 OK\r\n")

    finally:
        await stream.close()
Example #2
0
def test_lookup_backend():
    assert isinstance(lookup_backend("asyncio"), AsyncioBackend)
    assert isinstance(lookup_backend("trio"), TrioBackend)
    assert isinstance(lookup_backend(AsyncioBackend()), AsyncioBackend)

    async def get_backend_from_auto():
        auto_backend = lookup_backend("auto")
        return auto_backend.backend

    loop = asyncio.get_event_loop()
    backend = loop.run_until_complete(get_backend_from_auto())
    assert isinstance(backend, AsyncioBackend)

    backend = trio.run(get_backend_from_auto)
    assert isinstance(backend, TrioBackend)

    with pytest.raises(Exception, match="unknownio"):
        lookup_backend("unknownio")
Example #3
0
async def test_concurrent_read(server):
    """
    Regression test for: https://github.com/encode/httpx/issues/527
    """
    backend = lookup_backend()
    stream = await backend.open_tcp_stream(server.url.host,
                                           server.url.port,
                                           ssl_context=None,
                                           timeout=Timeout(5))
    timeout = Timeout(5)
    try:
        await stream.write(b"GET / HTTP/1.1\r\n\r\n", timeout)
        await run_concurrently(lambda: stream.read(10, timeout),
                               lambda: stream.read(10, timeout))
    finally:
        await stream.close()
Example #4
0
 def __init__(self, data_to_send=b"", backend="auto"):
     self.backend = lookup_backend(backend)
     self.data_to_send = data_to_send
     self.received_data = []
     self.stream = MockRawSocketStream(self)
Example #5
0
 def __init__(self, app, backend="auto"):
     self.app = app
     self.backend = lookup_backend(backend)
     self.server = None
Example #6
0
 async def get_backend_from_auto():
     auto_backend = lookup_backend("auto")
     return auto_backend.backend
Example #7
0
 async def restart(server):
     backend = lookup_backend()
     await backend.run_in_threadpool(asyncio_backend.run, server.restart)