test_config.py
This file
Path
tests/test_config.py
218 lines · 6.6 KiB · Python
Last changed by
Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
History of this fileTake it
1import os2import socket3import ssl4import sys5from pathlib import Path67import pytest89import httpx10from httpx.config import SSLConfig111213def test_load_ssl_config():14 ssl_config = SSLConfig()15 context = ssl_config.load_ssl_context()16 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED17 assert context.check_hostname is True181920def test_load_ssl_config_verify_non_existing_path():21 ssl_config = SSLConfig(verify="/path/to/nowhere")22 with pytest.raises(IOError):23 ssl_config.load_ssl_context()242526def test_load_ssl_config_verify_existing_file():27 ssl_config = SSLConfig(verify=httpx.config.DEFAULT_CA_BUNDLE_PATH)28 context = ssl_config.load_ssl_context()29 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED30 assert context.check_hostname is True313233@pytest.mark.parametrize("config", ("SSL_CERT_FILE", "SSL_CERT_DIR"))34def test_load_ssl_config_verify_env_file(https_server, ca_cert_pem_file, config):35 os.environ[config] = (36 ca_cert_pem_file37 if config.endswith("_FILE")38 else str(Path(ca_cert_pem_file).parent)39 )40 ssl_config = SSLConfig(trust_env=True)41 context = ssl_config.load_ssl_context()42 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED43 assert context.check_hostname is True44 assert ssl_config.verify == os.environ[config]4546 # Skipping 'SSL_CERT_DIR' functional test for now because47 # we're unable to get the certificate within the directory to48 # load into the SSLContext. :(49 if config == "SSL_CERT_FILE":50 host = https_server.url.host51 port = https_server.url.port52 conn = socket.create_connection((host, port))53 context.wrap_socket(conn, server_hostname=host)54 assert len(context.get_ca_certs()) == 1555657def test_load_ssl_config_verify_directory():58 path = httpx.config.DEFAULT_CA_BUNDLE_PATH.parent59 ssl_config = SSLConfig(verify=path)60 context = ssl_config.load_ssl_context()61 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED62 assert context.check_hostname is True636465def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):66 ssl_config = SSLConfig(cert=(cert_pem_file, cert_private_key_file))67 context = ssl_config.load_ssl_context()68 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED69 assert context.check_hostname is True707172@pytest.mark.parametrize("password", [b"password", "password"])73def test_load_ssl_config_cert_and_encrypted_key(74 cert_pem_file, cert_encrypted_private_key_file, password75):76 ssl_config = SSLConfig(77 cert=(cert_pem_file, cert_encrypted_private_key_file, password)78 )79 context = ssl_config.load_ssl_context()80 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED81 assert context.check_hostname is True828384def test_load_ssl_config_cert_and_key_invalid_password(85 cert_pem_file, cert_encrypted_private_key_file86):87 ssl_config = SSLConfig(88 cert=(cert_pem_file, cert_encrypted_private_key_file, "password1")89 )9091 with pytest.raises(ssl.SSLError):92 ssl_config.load_ssl_context()939495def test_load_ssl_config_cert_without_key_raises(cert_pem_file):96 ssl_config = SSLConfig(cert=cert_pem_file)97 with pytest.raises(ssl.SSLError):98 ssl_config.load_ssl_context()99100101def test_load_ssl_config_no_verify():102 ssl_config = SSLConfig(verify=False)103 context = ssl_config.load_ssl_context()104 assert context.verify_mode == ssl.VerifyMode.CERT_NONE105 assert context.check_hostname is False106107108def test_load_ssl_context():109 ssl_context = ssl.create_default_context()110 ssl_config = SSLConfig(verify=ssl_context)111112 assert ssl_config.verify is True113 assert ssl_config.ssl_context is ssl_context114 assert repr(ssl_config) == "SSLConfig(cert=None, verify=True)"115116117def test_ssl_repr():118 ssl = SSLConfig(verify=False)119 assert repr(ssl) == "SSLConfig(cert=None, verify=False)"120121122def test_ssl_eq():123 ssl = SSLConfig(verify=False)124 assert ssl == SSLConfig(verify=False)125126127def test_limits_repr():128 limits = httpx.PoolLimits(hard_limit=100)129 assert repr(limits) == "PoolLimits(soft_limit=None, hard_limit=100)"130131132def test_limits_eq():133 limits = httpx.PoolLimits(hard_limit=100)134 assert limits == httpx.PoolLimits(hard_limit=100)135136137def test_timeout_eq():138 timeout = httpx.Timeout(timeout=5.0)139 assert timeout == httpx.Timeout(timeout=5.0)140141142def test_timeout_from_nothing():143 timeout = httpx.Timeout()144 assert timeout.connect_timeout is None145 assert timeout.read_timeout is None146 assert timeout.write_timeout is None147 assert timeout.pool_timeout is None148149150def test_timeout_from_none():151 timeout = httpx.Timeout(timeout=None)152 assert timeout == httpx.Timeout()153154155def test_timeout_from_one_none_value():156 timeout = httpx.Timeout(read_timeout=None)157 assert timeout == httpx.Timeout()158159160def test_timeout_from_one_value():161 timeout = httpx.Timeout(read_timeout=5.0)162 assert timeout == httpx.Timeout(timeout=(None, 5.0, None, None))163164165def test_timeout_from_one_value_and_default():166 timeout = httpx.Timeout(5.0, pool_timeout=60.0)167 assert timeout == httpx.Timeout(timeout=(5.0, 5.0, 5.0, 60.0))168169170def test_timeout_from_tuple():171 timeout = httpx.Timeout(timeout=(5.0, 5.0, 5.0, 5.0))172 assert timeout == httpx.Timeout(timeout=5.0)173174175def test_timeout_from_config_instance():176 timeout = httpx.Timeout(timeout=5.0)177 assert httpx.Timeout(timeout) == httpx.Timeout(timeout=5.0)178179180def test_timeout_repr():181 timeout = httpx.Timeout(timeout=5.0)182 assert repr(timeout) == "Timeout(timeout=5.0)"183184 timeout = httpx.Timeout(read_timeout=5.0)185 assert repr(timeout) == (186 "Timeout(connect_timeout=None, read_timeout=5.0, "187 "write_timeout=None, pool_timeout=None)"188 )189190191@pytest.mark.skipif(192 not hasattr(ssl.SSLContext, "keylog_filename"),193 reason="requires OpenSSL 1.1.1 or higher",194)195@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")196def test_ssl_config_support_for_keylog_file(tmpdir, monkeypatch): # pragma: nocover197 with monkeypatch.context() as m:198 m.delenv("SSLKEYLOGFILE", raising=False)199200 ssl_config = SSLConfig(trust_env=True)201 ssl_config.load_ssl_context()202203 assert ssl_config.ssl_context.keylog_filename is None204205 filename = str(tmpdir.join("test.log"))206207 with monkeypatch.context() as m:208 m.setenv("SSLKEYLOGFILE", filename)209210 ssl_config = SSLConfig(trust_env=True)211 ssl_config.load_ssl_context()212213 assert ssl_config.ssl_context.keylog_filename == filename214215 ssl_config = SSLConfig(trust_env=False)216 ssl_config.load_ssl_context()217218 assert ssl_config.ssl_context.keylog_filename is None