test_config.py
This file
Path
tests/test_config.py
219 lines · 6.5 KiB · Python
Last changed by
Minor config cleanup (#621)Tom Christiebefore adoption3124a38
History of this fileTake it
1import os2import socket3import ssl4import sys5from pathlib import Path67import certifi8import pytest910import httpx11from httpx.config import SSLConfig121314def test_load_ssl_config():15 ssl_config = SSLConfig()16 context = ssl_config.load_ssl_context()17 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED18 assert context.check_hostname is True192021def test_load_ssl_config_verify_non_existing_path():22 ssl_config = SSLConfig(verify="/path/to/nowhere")23 with pytest.raises(IOError):24 ssl_config.load_ssl_context()252627def test_load_ssl_config_verify_existing_file():28 ssl_config = SSLConfig(verify=certifi.where())29 context = ssl_config.load_ssl_context()30 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED31 assert context.check_hostname is True323334@pytest.mark.parametrize("config", ("SSL_CERT_FILE", "SSL_CERT_DIR"))35def test_load_ssl_config_verify_env_file(https_server, ca_cert_pem_file, config):36 os.environ[config] = (37 ca_cert_pem_file38 if config.endswith("_FILE")39 else str(Path(ca_cert_pem_file).parent)40 )41 ssl_config = SSLConfig(trust_env=True)42 context = ssl_config.load_ssl_context()43 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED44 assert context.check_hostname is True45 assert ssl_config.verify == os.environ[config]4647 # Skipping 'SSL_CERT_DIR' functional test for now because48 # we're unable to get the certificate within the directory to49 # load into the SSLContext. :(50 if config == "SSL_CERT_FILE":51 host = https_server.url.host52 port = https_server.url.port53 conn = socket.create_connection((host, port))54 context.wrap_socket(conn, server_hostname=host)55 assert len(context.get_ca_certs()) == 1565758def test_load_ssl_config_verify_directory():59 path = Path(certifi.where()).parent60 ssl_config = SSLConfig(verify=path)61 context = ssl_config.load_ssl_context()62 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED63 assert context.check_hostname is True646566def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):67 ssl_config = SSLConfig(cert=(cert_pem_file, cert_private_key_file))68 context = ssl_config.load_ssl_context()69 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED70 assert context.check_hostname is True717273@pytest.mark.parametrize("password", [b"password", "password"])74def test_load_ssl_config_cert_and_encrypted_key(75 cert_pem_file, cert_encrypted_private_key_file, password76):77 ssl_config = SSLConfig(78 cert=(cert_pem_file, cert_encrypted_private_key_file, password)79 )80 context = ssl_config.load_ssl_context()81 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED82 assert context.check_hostname is True838485def test_load_ssl_config_cert_and_key_invalid_password(86 cert_pem_file, cert_encrypted_private_key_file87):88 ssl_config = SSLConfig(89 cert=(cert_pem_file, cert_encrypted_private_key_file, "password1")90 )9192 with pytest.raises(ssl.SSLError):93 ssl_config.load_ssl_context()949596def test_load_ssl_config_cert_without_key_raises(cert_pem_file):97 ssl_config = SSLConfig(cert=cert_pem_file)98 with pytest.raises(ssl.SSLError):99 ssl_config.load_ssl_context()100101102def test_load_ssl_config_no_verify():103 ssl_config = SSLConfig(verify=False)104 context = ssl_config.load_ssl_context()105 assert context.verify_mode == ssl.VerifyMode.CERT_NONE106 assert context.check_hostname is False107108109def test_load_ssl_context():110 ssl_context = ssl.create_default_context()111 ssl_config = SSLConfig(verify=ssl_context)112113 assert ssl_config.verify is True114 assert ssl_config.ssl_context is ssl_context115 assert repr(ssl_config) == "SSLConfig(cert=None, verify=True)"116117118def test_ssl_repr():119 ssl = SSLConfig(verify=False)120 assert repr(ssl) == "SSLConfig(cert=None, verify=False)"121122123def test_ssl_eq():124 ssl = SSLConfig(verify=False)125 assert ssl == SSLConfig(verify=False)126127128def test_limits_repr():129 limits = httpx.PoolLimits(hard_limit=100)130 assert repr(limits) == "PoolLimits(soft_limit=None, hard_limit=100)"131132133def test_limits_eq():134 limits = httpx.PoolLimits(hard_limit=100)135 assert limits == httpx.PoolLimits(hard_limit=100)136137138def test_timeout_eq():139 timeout = httpx.Timeout(timeout=5.0)140 assert timeout == httpx.Timeout(timeout=5.0)141142143def test_timeout_from_nothing():144 timeout = httpx.Timeout()145 assert timeout.connect_timeout is None146 assert timeout.read_timeout is None147 assert timeout.write_timeout is None148 assert timeout.pool_timeout is None149150151def test_timeout_from_none():152 timeout = httpx.Timeout(timeout=None)153 assert timeout == httpx.Timeout()154155156def test_timeout_from_one_none_value():157 timeout = httpx.Timeout(read_timeout=None)158 assert timeout == httpx.Timeout()159160161def test_timeout_from_one_value():162 timeout = httpx.Timeout(read_timeout=5.0)163 assert timeout == httpx.Timeout(timeout=(None, 5.0, None, None))164165166def test_timeout_from_one_value_and_default():167 timeout = httpx.Timeout(5.0, pool_timeout=60.0)168 assert timeout == httpx.Timeout(timeout=(5.0, 5.0, 5.0, 60.0))169170171def test_timeout_from_tuple():172 timeout = httpx.Timeout(timeout=(5.0, 5.0, 5.0, 5.0))173 assert timeout == httpx.Timeout(timeout=5.0)174175176def test_timeout_from_config_instance():177 timeout = httpx.Timeout(timeout=5.0)178 assert httpx.Timeout(timeout) == httpx.Timeout(timeout=5.0)179180181def test_timeout_repr():182 timeout = httpx.Timeout(timeout=5.0)183 assert repr(timeout) == "Timeout(timeout=5.0)"184185 timeout = httpx.Timeout(read_timeout=5.0)186 assert repr(timeout) == (187 "Timeout(connect_timeout=None, read_timeout=5.0, "188 "write_timeout=None, pool_timeout=None)"189 )190191192@pytest.mark.skipif(193 not hasattr(ssl.SSLContext, "keylog_filename"),194 reason="requires OpenSSL 1.1.1 or higher",195)196@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")197def test_ssl_config_support_for_keylog_file(tmpdir, monkeypatch): # pragma: nocover198 with monkeypatch.context() as m:199 m.delenv("SSLKEYLOGFILE", raising=False)200201 ssl_config = SSLConfig(trust_env=True)202 ssl_config.load_ssl_context()203204 assert ssl_config.ssl_context.keylog_filename is None205206 filename = str(tmpdir.join("test.log"))207208 with monkeypatch.context() as m:209 m.setenv("SSLKEYLOGFILE", filename)210211 ssl_config = SSLConfig(trust_env=True)212 ssl_config.load_ssl_context()213214 assert ssl_config.ssl_context.keylog_filename == filename215216 ssl_config = SSLConfig(trust_env=False)217 ssl_config.load_ssl_context()218219 assert ssl_config.ssl_context.keylog_filename is None