test_config.py
This file
Path
tests/test_config.py
173 lines · 5.1 KiB · Python
Last changed by
Raise TypeError if invalid parameter for HTTPVersionConfig (#253)Can Sarıgölbefore adoption4ea4af6
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_http_versions_repr():94 http_versions = httpx.HTTPVersionConfig()95 assert repr(http_versions) == "HTTPVersionConfig(['HTTP/1.1', 'HTTP/2'])"969798def test_http_versions_from_string():99 http_versions = httpx.HTTPVersionConfig("HTTP/1.1")100 assert repr(http_versions) == "HTTPVersionConfig(['HTTP/1.1'])"101102103def test_http_versions_from_list():104 http_versions = httpx.HTTPVersionConfig(["http/1.1"])105 assert repr(http_versions) == "HTTPVersionConfig(['HTTP/1.1'])"106107108def test_http_versions_from_config():109 http_versions = httpx.HTTPVersionConfig(httpx.HTTPVersionConfig("HTTP/1.1"))110 assert repr(http_versions) == "HTTPVersionConfig(['HTTP/1.1'])"111112113def test_invalid_http_version():114 with pytest.raises(ValueError):115 httpx.HTTPVersionConfig("HTTP/9")116117118def test_invalid_http_version_type():119 with pytest.raises(TypeError):120 httpx.HTTPVersionConfig(123)121122123def test_invalid_http_version_list_type():124 with pytest.raises(ValueError):125 httpx.HTTPVersionConfig([123])126127128def test_empty_http_version():129 with pytest.raises(ValueError):130 httpx.HTTPVersionConfig([])131132133def test_timeout_repr():134 timeout = httpx.TimeoutConfig(timeout=5.0)135 assert repr(timeout) == "TimeoutConfig(timeout=5.0)"136137 timeout = httpx.TimeoutConfig(read_timeout=5.0)138 assert (139 repr(timeout)140 == "TimeoutConfig(connect_timeout=None, read_timeout=5.0, write_timeout=None)"141 )142143144def test_limits_repr():145 limits = httpx.PoolLimits(hard_limit=100)146 assert (147 repr(limits) == "PoolLimits(soft_limit=None, hard_limit=100, pool_timeout=None)"148 )149150151def test_ssl_eq():152 ssl = httpx.SSLConfig(verify=False)153 assert ssl == httpx.SSLConfig(verify=False)154155156def test_timeout_eq():157 timeout = httpx.TimeoutConfig(timeout=5.0)158 assert timeout == httpx.TimeoutConfig(timeout=5.0)159160161def test_limits_eq():162 limits = httpx.PoolLimits(hard_limit=100)163 assert limits == httpx.PoolLimits(hard_limit=100)164165166def test_timeout_from_tuple():167 timeout = httpx.TimeoutConfig(timeout=(5.0, 5.0, 5.0))168 assert timeout == httpx.TimeoutConfig(timeout=5.0)169170171def test_timeout_from_config_instance():172 timeout = httpx.TimeoutConfig(timeout=5.0)173 assert httpx.TimeoutConfig(timeout) == httpx.TimeoutConfig(timeout=5.0)