Skip to content
Myr

test_config.py

This file

Path

tests/test_config.py

133 lines · 4.0 KiB · Python

Last changed by

Use pathlib.Path instead of os.path functions (#188)Stephen Brown IIbefore adoptionfde30b8

History of this file

Take it

1import ssl23import pytest45import httpx678@pytest.mark.asyncio9async def test_load_ssl_config():10    ssl_config = httpx.SSLConfig()11    context = await ssl_config.load_ssl_context()12    assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED13    assert context.check_hostname is True141516@pytest.mark.asyncio17async def test_load_ssl_config_verify_non_existing_path():18    ssl_config = httpx.SSLConfig(verify="/path/to/nowhere")19    with pytest.raises(IOError):20        await ssl_config.load_ssl_context()212223@pytest.mark.asyncio24async def test_load_ssl_config_verify_existing_file():25    ssl_config = httpx.SSLConfig(verify=httpx.config.DEFAULT_CA_BUNDLE_PATH)26    context = await ssl_config.load_ssl_context()27    assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED28    assert context.check_hostname is True293031@pytest.mark.asyncio32async def test_load_ssl_config_verify_directory():33    path = httpx.config.DEFAULT_CA_BUNDLE_PATH.parent34    ssl_config = httpx.SSLConfig(verify=path)35    context = await ssl_config.load_ssl_context()36    assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED37    assert context.check_hostname is True383940@pytest.mark.asyncio41async def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):42    ssl_config = httpx.SSLConfig(cert=(cert_pem_file, cert_private_key_file))43    context = await ssl_config.load_ssl_context()44    assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED45    assert context.check_hostname is True464748@pytest.mark.asyncio49@pytest.mark.parametrize("password", [b"password", "password"])50async def test_load_ssl_config_cert_and_encrypted_key(51    cert_pem_file, cert_encrypted_private_key_file, password52):53    ssl_config = httpx.SSLConfig(54        cert=(cert_pem_file, cert_encrypted_private_key_file, password)55    )56    context = await ssl_config.load_ssl_context()57    assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED58    assert context.check_hostname is True596061@pytest.mark.asyncio62async def test_load_ssl_config_cert_and_key_invalid_password(63    cert_pem_file, cert_encrypted_private_key_file64):65    ssl_config = httpx.SSLConfig(66        cert=(cert_pem_file, cert_encrypted_private_key_file, "password1")67    )6869    with pytest.raises(ssl.SSLError):70        await ssl_config.load_ssl_context()717273@pytest.mark.asyncio74async def test_load_ssl_config_cert_without_key_raises(cert_pem_file):75    ssl_config = httpx.SSLConfig(cert=cert_pem_file)76    with pytest.raises(ssl.SSLError):77        await ssl_config.load_ssl_context()787980@pytest.mark.asyncio81async def test_load_ssl_config_no_verify():82    ssl_config = httpx.SSLConfig(verify=False)83    context = await ssl_config.load_ssl_context()84    assert context.verify_mode == ssl.VerifyMode.CERT_NONE85    assert context.check_hostname is False868788def test_ssl_repr():89    ssl = httpx.SSLConfig(verify=False)90    assert repr(ssl) == "SSLConfig(cert=None, verify=False)"919293def test_timeout_repr():94    timeout = httpx.TimeoutConfig(timeout=5.0)95    assert repr(timeout) == "TimeoutConfig(timeout=5.0)"9697    timeout = httpx.TimeoutConfig(read_timeout=5.0)98    assert (99        repr(timeout)100        == "TimeoutConfig(connect_timeout=None, read_timeout=5.0, write_timeout=None)"101    )102103104def test_limits_repr():105    limits = httpx.PoolLimits(hard_limit=100)106    assert (107        repr(limits) == "PoolLimits(soft_limit=None, hard_limit=100, pool_timeout=None)"108    )109110111def test_ssl_eq():112    ssl = httpx.SSLConfig(verify=False)113    assert ssl == httpx.SSLConfig(verify=False)114115116def test_timeout_eq():117    timeout = httpx.TimeoutConfig(timeout=5.0)118    assert timeout == httpx.TimeoutConfig(timeout=5.0)119120121def test_limits_eq():122    limits = httpx.PoolLimits(hard_limit=100)123    assert limits == httpx.PoolLimits(hard_limit=100)124125126def test_timeout_from_tuple():127    timeout = httpx.TimeoutConfig(timeout=(5.0, 5.0, 5.0))128    assert timeout == httpx.TimeoutConfig(timeout=5.0)129130131def test_timeout_from_config_instance():132    timeout = httpx.TimeoutConfig(timeout=5.0)133    assert httpx.TimeoutConfig(timeout) == httpx.TimeoutConfig(timeout=5.0)