Repository: encode/httpx
File: tests/test_utils.py (Python)

Code before:

@pytest.mark.asyncio
@pytest.mark.parametrize("httpx_debug", ["0", "1", "True", "False"])
async def test_httpx_debug_enabled_stderr_logging(server, capsys, httpx_debug):
    os.environ["HTTPX_DEBUG"] = httpx_debug

    # Force a reload on the logging handlers
    utils._LOGGER_INITIALIZED = False
    utils.get_logger("httpx")

    async with httpx.AsyncClient() as client:
        await client.get(server.url)

    if httpx_debug in ("1", "True"):
        assert "httpx.dispatch.connection_pool" in capsys.readouterr().err
    else:
        assert "httpx.dispatch.connection_pool" not in capsys.readouterr().err

    # Reset the logger so we don't have verbose output in all unit tests
    logging.getLogger("httpx").handlers = []


def test_get_ssl_cert_file():
    os.environ["SSL_CERT_FILE"] = "tests/test_utils.py"
    os.environ["SSL_CERT_DIR"] = "tests/"
    assert get_ca_bundle_from_env() == "tests/test_utils.py"

    os.environ["SSL_CERT_FILE"] = "wrongpath"
    assert get_ca_bundle_from_env() == "tests"

    os.environ["SSL_CERT_DIR"] = "wrongpath"
    assert get_ca_bundle_from_env() is None


Review comment (bug):
I'd say let’s add tests at the top here for the cases when `SSL_CERT_FILE` and `SSL_CERT_DIR` are not set. Ideally we should be able to catch the potential bugs I mention in my other comment. :)

Code after:
def test_parse_header_links(value, expected):
    assert parse_header_links(value) == expected


@pytest.mark.asyncio
@pytest.mark.parametrize("httpx_debug", ["0", "1", "True", "False"])
async def test_httpx_debug_enabled_stderr_logging(server, capsys, httpx_debug):
    os.environ["HTTPX_DEBUG"] = httpx_debug

    # Force a reload on the logging handlers
    utils._LOGGER_INITIALIZED = False
    utils.get_logger("httpx")

    async with httpx.AsyncClient() as client:
        await client.get(server.url)

    if httpx_debug in ("1", "True"):
        assert "httpx.dispatch.connection_pool" in capsys.readouterr().err
    else:
        assert "httpx.dispatch.connection_pool" not in capsys.readouterr().err

    # Reset the logger so we don't have verbose output in all unit tests
    logging.getLogger("httpx").handlers = []


def test_get_ssl_cert_file():
    # Two environments is not set.
    assert get_ca_bundle_from_env() is None

    os.environ["SSL_CERT_DIR"] = "tests/"
    # SSL_CERT_DIR is correctly set, SSL_CERT_FILE is not set.
    assert get_ca_bundle_from_env() == "tests"

    del os.environ["SSL_CERT_DIR"]
    os.environ["SSL_CERT_FILE"] = "tests/test_utils.py"
    # SSL_CERT_FILE is correctly set, SSL_CERT_DIR is not set.
    assert get_ca_bundle_from_env() == "tests/test_utils.py"

    os.environ["SSL_CERT_FILE"] = "wrongfile"
    # SSL_CERT_FILE is set with wrong file,  SSL_CERT_DIR is not set.
    assert get_ca_bundle_from_env() is None

    del os.environ["SSL_CERT_FILE"]
    os.environ["SSL_CERT_DIR"] = "wrongpath"
    # SSL_CERT_DIR is set with wrong path,  SSL_CERT_FILE is not set.
    assert get_ca_bundle_from_env() is None

    os.environ["SSL_CERT_DIR"] = "tests/"
    os.environ["SSL_CERT_FILE"] = "tests/test_utils.py"
    # Two environments is correctly set.
    assert get_ca_bundle_from_env() == "tests/test_utils.py"