test_config.py
This file
Path
tests/test_config.py
204 lines · 6.0 KiB · Python
Last changed by
Add support for SSLKEYLOGFILE (#301)Tomás Faríasbefore adoptiondb7f2d0
History of this fileTake it
1import ssl2import sys34import pytest56import httpx789def test_load_ssl_config():10 ssl_config = httpx.SSLConfig()11 context = ssl_config.load_ssl_context()12 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED13 assert context.check_hostname is True141516def test_load_ssl_config_verify_non_existing_path():17 ssl_config = httpx.SSLConfig(verify="/path/to/nowhere")18 with pytest.raises(IOError):19 ssl_config.load_ssl_context()202122def test_load_ssl_config_verify_existing_file():23 ssl_config = httpx.SSLConfig(verify=httpx.config.DEFAULT_CA_BUNDLE_PATH)24 context = ssl_config.load_ssl_context()25 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED26 assert context.check_hostname is True272829def test_load_ssl_config_verify_directory():30 path = httpx.config.DEFAULT_CA_BUNDLE_PATH.parent31 ssl_config = httpx.SSLConfig(verify=path)32 context = ssl_config.load_ssl_context()33 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED34 assert context.check_hostname is True353637def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):38 ssl_config = httpx.SSLConfig(cert=(cert_pem_file, cert_private_key_file))39 context = ssl_config.load_ssl_context()40 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED41 assert context.check_hostname is True424344@pytest.mark.parametrize("password", [b"password", "password"])45def test_load_ssl_config_cert_and_encrypted_key(46 cert_pem_file, cert_encrypted_private_key_file, password47):48 ssl_config = httpx.SSLConfig(49 cert=(cert_pem_file, cert_encrypted_private_key_file, password)50 )51 context = ssl_config.load_ssl_context()52 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED53 assert context.check_hostname is True545556def test_load_ssl_config_cert_and_key_invalid_password(57 cert_pem_file, cert_encrypted_private_key_file58):59 ssl_config = httpx.SSLConfig(60 cert=(cert_pem_file, cert_encrypted_private_key_file, "password1")61 )6263 with pytest.raises(ssl.SSLError):64 ssl_config.load_ssl_context()656667def test_load_ssl_config_cert_without_key_raises(cert_pem_file):68 ssl_config = httpx.SSLConfig(cert=cert_pem_file)69 with pytest.raises(ssl.SSLError):70 ssl_config.load_ssl_context()717273def test_load_ssl_config_no_verify():74 ssl_config = httpx.SSLConfig(verify=False)75 context = ssl_config.load_ssl_context()76 assert context.verify_mode == ssl.VerifyMode.CERT_NONE77 assert context.check_hostname is False787980def test_load_ssl_context():81 ssl_context = ssl.create_default_context()82 ssl_config = httpx.SSLConfig(verify=ssl_context)8384 assert ssl_config.verify is True85 assert ssl_config.ssl_context is ssl_context86 assert repr(ssl_config) == "SSLConfig(cert=None, verify=True)"878889def test_ssl_repr():90 ssl = httpx.SSLConfig(verify=False)91 assert repr(ssl) == "SSLConfig(cert=None, verify=False)"929394def test_http_versions_repr():95 http_versions = httpx.HTTPVersionConfig()96 assert repr(http_versions) == "HTTPVersionConfig(['HTTP/1.1', 'HTTP/2'])"979899def test_http_versions_from_string():100 http_versions = httpx.HTTPVersionConfig("HTTP/1.1")101 assert repr(http_versions) == "HTTPVersionConfig(['HTTP/1.1'])"102103104def test_http_versions_from_list():105 http_versions = httpx.HTTPVersionConfig(["http/1.1"])106 assert repr(http_versions) == "HTTPVersionConfig(['HTTP/1.1'])"107108109def test_http_versions_from_config():110 http_versions = httpx.HTTPVersionConfig(httpx.HTTPVersionConfig("HTTP/1.1"))111 assert repr(http_versions) == "HTTPVersionConfig(['HTTP/1.1'])"112113114def test_invalid_http_version():115 with pytest.raises(ValueError):116 httpx.HTTPVersionConfig("HTTP/9")117118119def test_invalid_http_version_type():120 with pytest.raises(TypeError):121 httpx.HTTPVersionConfig(123)122123124def test_invalid_http_version_list_type():125 with pytest.raises(ValueError):126 httpx.HTTPVersionConfig([123])127128129def test_empty_http_version():130 with pytest.raises(ValueError):131 httpx.HTTPVersionConfig([])132133134def test_timeout_repr():135 timeout = httpx.TimeoutConfig(timeout=5.0)136 assert repr(timeout) == "TimeoutConfig(timeout=5.0)"137138 timeout = httpx.TimeoutConfig(read_timeout=5.0)139 assert (140 repr(timeout)141 == "TimeoutConfig(connect_timeout=None, read_timeout=5.0, write_timeout=None)"142 )143144145def test_limits_repr():146 limits = httpx.PoolLimits(hard_limit=100)147 assert (148 repr(limits) == "PoolLimits(soft_limit=None, hard_limit=100, pool_timeout=None)"149 )150151152def test_ssl_eq():153 ssl = httpx.SSLConfig(verify=False)154 assert ssl == httpx.SSLConfig(verify=False)155156157def test_timeout_eq():158 timeout = httpx.TimeoutConfig(timeout=5.0)159 assert timeout == httpx.TimeoutConfig(timeout=5.0)160161162def test_limits_eq():163 limits = httpx.PoolLimits(hard_limit=100)164 assert limits == httpx.PoolLimits(hard_limit=100)165166167def test_timeout_from_tuple():168 timeout = httpx.TimeoutConfig(timeout=(5.0, 5.0, 5.0))169 assert timeout == httpx.TimeoutConfig(timeout=5.0)170171172def test_timeout_from_config_instance():173 timeout = httpx.TimeoutConfig(timeout=5.0)174 assert httpx.TimeoutConfig(timeout) == httpx.TimeoutConfig(timeout=5.0)175176177@pytest.mark.skipif(178 not hasattr(ssl.SSLContext, "keylog_filename"),179 reason="requires OpenSSL 1.1.1 or higher",180)181@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")182def test_ssl_config_support_for_keylog_file(tmpdir, monkeypatch):183 with monkeypatch.context() as m:184 m.delenv("SSLKEYLOGFILE", raising=False)185186 ssl_config = httpx.SSLConfig(trust_env=True)187 ssl_config.load_ssl_context()188189 assert ssl_config.ssl_context.keylog_filename is None190191 filename = str(tmpdir.join("test.log"))192193 with monkeypatch.context() as m:194 m.setenv("SSLKEYLOGFILE", filename)195196 ssl_config = httpx.SSLConfig(trust_env=True)197 ssl_config.load_ssl_context()198199 assert ssl_config.ssl_context.keylog_filename == filename200201 ssl_config = httpx.SSLConfig(trust_env=False)202 ssl_config.load_ssl_context()203204 assert ssl_config.ssl_context.keylog_filename is None