test_config.py
This file
Path
tests/test_config.py
133 lines · 4.0 KiB · Python
Last changed by
Accept SSLContext into SSLConfig(verify=...) (#215)Seth Michael Larsonbefore adoptiondf8874b
History of this fileTake it
1import ssl23import pytest45import httpx678def test_load_ssl_config():9 ssl_config = httpx.SSLConfig()10 context = ssl_config.load_ssl_context()11 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED12 assert context.check_hostname is True131415def test_load_ssl_config_verify_non_existing_path():16 ssl_config = httpx.SSLConfig(verify="/path/to/nowhere")17 with pytest.raises(IOError):18 ssl_config.load_ssl_context()192021def test_load_ssl_config_verify_existing_file():22 ssl_config = httpx.SSLConfig(verify=httpx.config.DEFAULT_CA_BUNDLE_PATH)23 context = ssl_config.load_ssl_context()24 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED25 assert context.check_hostname is True262728def test_load_ssl_config_verify_directory():29 path = httpx.config.DEFAULT_CA_BUNDLE_PATH.parent30 ssl_config = httpx.SSLConfig(verify=path)31 context = ssl_config.load_ssl_context()32 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED33 assert context.check_hostname is True343536def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):37 ssl_config = httpx.SSLConfig(cert=(cert_pem_file, cert_private_key_file))38 context = ssl_config.load_ssl_context()39 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED40 assert context.check_hostname is True414243@pytest.mark.parametrize("password", [b"password", "password"])44def test_load_ssl_config_cert_and_encrypted_key(45 cert_pem_file, cert_encrypted_private_key_file, password46):47 ssl_config = httpx.SSLConfig(48 cert=(cert_pem_file, cert_encrypted_private_key_file, password)49 )50 context = ssl_config.load_ssl_context()51 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED52 assert context.check_hostname is True535455def test_load_ssl_config_cert_and_key_invalid_password(56 cert_pem_file, cert_encrypted_private_key_file57):58 ssl_config = httpx.SSLConfig(59 cert=(cert_pem_file, cert_encrypted_private_key_file, "password1")60 )6162 with pytest.raises(ssl.SSLError):63 ssl_config.load_ssl_context()646566def test_load_ssl_config_cert_without_key_raises(cert_pem_file):67 ssl_config = httpx.SSLConfig(cert=cert_pem_file)68 with pytest.raises(ssl.SSLError):69 ssl_config.load_ssl_context()707172def test_load_ssl_config_no_verify():73 ssl_config = httpx.SSLConfig(verify=False)74 context = ssl_config.load_ssl_context()75 assert context.verify_mode == ssl.VerifyMode.CERT_NONE76 assert context.check_hostname is False777879def test_load_ssl_context():80 ssl_context = ssl.create_default_context()81 ssl_config = httpx.SSLConfig(verify=ssl_context)8283 assert ssl_config.verify is True84 assert ssl_config.ssl_context is ssl_context85 assert repr(ssl_config) == "SSLConfig(cert=None, verify=True)"868788def 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)