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
The walk stopped after 200 commits: lines older than a0aff8d point at it.
Add support for SSL_CERT_FILE and SSL_CERT_DIR (#307)Can Sarıgölbefore adoptionc9810a7
lines 1–2why, one step back1import os2import socketfixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
line 3why, one step back3import sslAdd support for SSLKEYLOGFILE (#301)Tomás Faríasbefore adoptiondb7f2d0
line 4why, one step back4import sysAdd support for SSL_CERT_FILE and SSL_CERT_DIR (#307)Can Sarıgölbefore adoptionc9810a7
line 5why, one step back5from pathlib import Pathfixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
line 6why, one step back6Minor config cleanup (#621)Tom Christiebefore adoption3124a38
line 7why, one step back7import certififixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
lines 8–10why, one step back8import pytest910import httpxTighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 11why, one step back11from httpx.config import SSLConfigfixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
lines 12–14why, one step back121314def test_load_ssl_config():Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 15why, one step back15 ssl_config = SSLConfig()fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
lines 16–21why, one step back16 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():Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 22why, one step back22 ssl_config = SSLConfig(verify="/path/to/nowhere")fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
lines 23–27why, one step back23 with pytest.raises(IOError):24 ssl_config.load_ssl_context()252627def test_load_ssl_config_verify_existing_file():Minor config cleanup (#621)Tom Christiebefore adoption3124a38
line 28why, one step back28 ssl_config = SSLConfig(verify=certifi.where())fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
lines 29–31why, one step back29 context = ssl_config.load_ssl_context()30 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED31 assert context.check_hostname is TrueAdd support for SSL_CERT_FILE and SSL_CERT_DIR (#307)Can Sarıgölbefore adoptionc9810a7
lines 32–40why, one step back323334@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 )Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 41why, one step back41 ssl_config = SSLConfig(trust_env=True)Add support for SSL_CERT_FILE and SSL_CERT_DIR (#307)Can Sarıgölbefore adoptionc9810a7
lines 42–55why, one step back42 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()) == 1fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
lines 56–58why, one step back565758def test_load_ssl_config_verify_directory():Minor config cleanup (#621)Tom Christiebefore adoption3124a38
line 59why, one step back59 path = Path(certifi.where()).parentTighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 60why, one step back60 ssl_config = SSLConfig(verify=path)fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
lines 61–66why, one step back61 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):Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 67why, one step back67 ssl_config = SSLConfig(cert=(cert_pem_file, cert_private_key_file))fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
lines 68–76why, one step back68 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):Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 77why, one step back77 ssl_config = SSLConfig(fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
lines 78–87why, one step back78 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):Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 88why, one step back88 ssl_config = SSLConfig(fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
lines 89–96why, one step back89 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):Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 97why, one step back97 ssl_config = SSLConfig(cert=cert_pem_file)fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
lines 98–102why, one step back98 with pytest.raises(ssl.SSLError):99 ssl_config.load_ssl_context()100101102def test_load_ssl_config_no_verify():Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 103why, one step back103 ssl_config = SSLConfig(verify=False)fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
lines 104–110why, one step back104 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()Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 111why, one step back111 ssl_config = SSLConfig(verify=ssl_context)fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
lines 112–118why, one step back112113 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():Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 119why, one step back119 ssl = SSLConfig(verify=False)fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
lines 120–122why, one step back120 assert repr(ssl) == "SSLConfig(cert=None, verify=False)"121122Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
lines 123–127why, one step back123def test_ssl_eq():124 ssl = SSLConfig(verify=False)125 assert ssl == SSLConfig(verify=False)126127fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
lines 128–129why, one step back128def test_limits_repr():129 limits = httpx.PoolLimits(hard_limit=100)Pool timeouts should be on the TimeoutConfig, not PoolLimits (#563)Tom Christiebefore adoption364378a
line 130why, one step back130 assert repr(limits) == "PoolLimits(soft_limit=None, hard_limit=100)"fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
lines 131–132why, one step back131132Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
lines 133–137why, one step back133def test_limits_eq():134 limits = httpx.PoolLimits(hard_limit=100)135 assert limits == httpx.PoolLimits(hard_limit=100)136137fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
line 138why, one step back138def test_timeout_eq():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 139–140why, one step back139 timeout = httpx.Timeout(timeout=5.0)140 assert timeout == httpx.Timeout(timeout=5.0)fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
lines 141–142why, one step back141142Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
line 143why, one step back143def test_timeout_from_nothing():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 144why, one step back144 timeout = httpx.Timeout()Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
lines 145–147why, one step back145 assert timeout.connect_timeout is None146 assert timeout.read_timeout is None147 assert timeout.write_timeout is NonePool timeouts should be on the TimeoutConfig, not PoolLimits (#563)Tom Christiebefore adoption364378a
line 148why, one step back148 assert timeout.pool_timeout is Nonefixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
lines 149–150why, one step back149150Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
line 151why, one step back151def test_timeout_from_none():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 152–153why, one step back152 timeout = httpx.Timeout(timeout=None)153 assert timeout == httpx.Timeout()Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
lines 154–156why, one step back154155156def test_timeout_from_one_none_value():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 157–158why, one step back157 timeout = httpx.Timeout(read_timeout=None)158 assert timeout == httpx.Timeout()Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
lines 159–160why, one step back159160Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 161–170why, one step back161def 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))169170fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
line 171why, one step back171def test_timeout_from_tuple():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 172–173why, one step back172 timeout = httpx.Timeout(timeout=(5.0, 5.0, 5.0, 5.0))173 assert timeout == httpx.Timeout(timeout=5.0)fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
lines 174–176why, one step back174175176def test_timeout_from_config_instance():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 177–178why, one step back177 timeout = httpx.Timeout(timeout=5.0)178 assert httpx.Timeout(timeout) == httpx.Timeout(timeout=5.0)Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
lines 179–181why, one step back179180181def test_timeout_repr():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 182–183why, one step back182 timeout = httpx.Timeout(timeout=5.0)183 assert repr(timeout) == "Timeout(timeout=5.0)"Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
line 184why, one step back184TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 185why, one step back185 timeout = httpx.Timeout(read_timeout=5.0)Pool timeouts should be on the TimeoutConfig, not PoolLimits (#563)Tom Christiebefore adoption364378a
line 186why, one step back186 assert repr(timeout) == (TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 187why, one step back187 "Timeout(connect_timeout=None, read_timeout=5.0, "Pool timeouts should be on the TimeoutConfig, not PoolLimits (#563)Tom Christiebefore adoption364378a
line 188why, one step back188 "write_timeout=None, pool_timeout=None)"Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
line 189why, one step back189 )Add support for SSLKEYLOGFILE (#301)Tomás Faríasbefore adoptiondb7f2d0
lines 190–196why, one step back190191192@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")Minor test fixes (#583)Tom Christiebefore adoption12d00b2
line 197why, one step back197def test_ssl_config_support_for_keylog_file(tmpdir, monkeypatch): # pragma: nocoverAdd support for SSLKEYLOGFILE (#301)Tomás Faríasbefore adoptiondb7f2d0
lines 198–200why, one step back198 with monkeypatch.context() as m:199 m.delenv("SSLKEYLOGFILE", raising=False)200Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 201why, one step back201 ssl_config = SSLConfig(trust_env=True)Add support for SSLKEYLOGFILE (#301)Tomás Faríasbefore adoptiondb7f2d0
lines 202–210why, one step back202 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)210Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 211why, one step back211 ssl_config = SSLConfig(trust_env=True)Add support for SSLKEYLOGFILE (#301)Tomás Faríasbefore adoptiondb7f2d0
lines 212–215why, one step back212 ssl_config.load_ssl_context()213214 assert ssl_config.ssl_context.keylog_filename == filename215Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 216why, one step back216 ssl_config = SSLConfig(trust_env=False)Add support for SSLKEYLOGFILE (#301)Tomás Faríasbefore adoptiondb7f2d0
lines 217–219why, one step back217 ssl_config.load_ssl_context()218219 assert ssl_config.ssl_context.keylog_filename is None