test_config.py
This file
Path
tests/test_config.py
223 lines · 6.5 KiB · Python
Last changed by
Prep for introducing SyncClient (#713)Tom Christiebefore adoptionf17ab37
History of this fileTake it
The walk stopped after 200 commits: lines older than 1b82a2a 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 socketFix invalid f-string (#375)秋葉before adoption1b82a2a
lines 3–4why, one step back3import ssl4import sysAdd support for SSL_CERT_FILE and SSL_CERT_DIR (#307)Can Sarıgölbefore adoptionc9810a7
line 5why, one step back5from pathlib import PathFix invalid f-string (#375)秋葉before adoption1b82a2a
line 6why, one step back6Minor config cleanup (#621)Tom Christiebefore adoption3124a38
line 7why, one step back7import certifiFix invalid f-string (#375)秋葉before adoption1b82a2a
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 SSLConfigFix invalid f-string (#375)秋葉before adoption1b82a2a
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()Load SSL Context on init (#709)Tom Christiebefore adoption79a9748
line 16why, one step back16 context = ssl_config.ssl_contextFix invalid f-string (#375)秋葉before adoption1b82a2a
lines 17–22why, one step back17 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED18 assert context.check_hostname is True192021def test_load_ssl_config_verify_non_existing_path():22 with pytest.raises(IOError):Load SSL Context on init (#709)Tom Christiebefore adoption79a9748
line 23why, one step back23 SSLConfig(verify="/path/to/nowhere")Fix invalid f-string (#375)秋葉before adoption1b82a2a
lines 24–26why, one step back242526def test_load_ssl_config_verify_existing_file():Minor config cleanup (#621)Tom Christiebefore adoption3124a38
line 27why, one step back27 ssl_config = SSLConfig(verify=certifi.where())Load SSL Context on init (#709)Tom Christiebefore adoption79a9748
line 28why, one step back28 context = ssl_config.ssl_contextFix invalid f-string (#375)秋葉before adoption1b82a2a
lines 29–30why, one step back29 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED30 assert context.check_hostname is TrueAdd support for SSL_CERT_FILE and SSL_CERT_DIR (#307)Can Sarıgölbefore adoptionc9810a7
lines 31–39why, one step back313233@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 )Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 40why, one step back40 ssl_config = SSLConfig(trust_env=True)Load SSL Context on init (#709)Tom Christiebefore adoption79a9748
line 41why, one step back41 context = ssl_config.ssl_contextAdd support for SSL_CERT_FILE and SSL_CERT_DIR (#307)Can Sarıgölbefore adoptionc9810a7
lines 42–54why, one step back42 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()) == 1Fix invalid f-string (#375)秋葉before adoption1b82a2a
lines 55–57why, one step back555657def test_load_ssl_config_verify_directory():Minor config cleanup (#621)Tom Christiebefore adoption3124a38
line 58why, one step back58 path = Path(certifi.where()).parentTighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 59why, one step back59 ssl_config = SSLConfig(verify=path)Load SSL Context on init (#709)Tom Christiebefore adoption79a9748
line 60why, one step back60 context = ssl_config.ssl_contextFix invalid f-string (#375)秋葉before adoption1b82a2a
lines 61–65why, one step back61 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):Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 66why, one step back66 ssl_config = SSLConfig(cert=(cert_pem_file, cert_private_key_file))Load SSL Context on init (#709)Tom Christiebefore adoption79a9748
line 67why, one step back67 context = ssl_config.ssl_contextFix invalid f-string (#375)秋葉before adoption1b82a2a
lines 68–75why, one step back68 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):Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 76why, one step back76 ssl_config = SSLConfig(Fix invalid f-string (#375)秋葉before adoption1b82a2a
lines 77–78why, one step back77 cert=(cert_pem_file, cert_encrypted_private_key_file, password)78 )Load SSL Context on init (#709)Tom Christiebefore adoption79a9748
line 79why, one step back79 context = ssl_config.ssl_contextFix invalid f-string (#375)秋葉before adoption1b82a2a
lines 80–87why, one step back80 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 with pytest.raises(ssl.SSLError):Load SSL Context on init (#709)Tom Christiebefore adoption79a9748
line 88why, one step back88 SSLConfig(cert=(cert_pem_file, cert_encrypted_private_key_file, "password1"))Fix invalid f-string (#375)秋葉before adoption1b82a2a
lines 89–92why, one step back899091def test_load_ssl_config_cert_without_key_raises(cert_pem_file):92 with pytest.raises(ssl.SSLError):Load SSL Context on init (#709)Tom Christiebefore adoption79a9748
line 93why, one step back93 SSLConfig(cert=cert_pem_file)Fix invalid f-string (#375)秋葉before adoption1b82a2a
lines 94–96why, one step back949596def test_load_ssl_config_no_verify():Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 97why, one step back97 ssl_config = SSLConfig(verify=False)Load SSL Context on init (#709)Tom Christiebefore adoption79a9748
line 98why, one step back98 context = ssl_config.ssl_contextFix invalid f-string (#375)秋葉before adoption1b82a2a
lines 99–104why, one step back99 assert context.verify_mode == ssl.VerifyMode.CERT_NONE100 assert context.check_hostname is False101102103def test_load_ssl_context():104 ssl_context = ssl.create_default_context()Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 105why, one step back105 ssl_config = SSLConfig(verify=ssl_context)Fix invalid f-string (#375)秋葉before adoption1b82a2a
lines 106–110why, one step back106107 assert ssl_config.ssl_context is ssl_context108109110def test_ssl_repr():Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 111why, one step back111 ssl = SSLConfig(verify=False)Fix invalid f-string (#375)秋葉before adoption1b82a2a
lines 112–114why, one step back112 assert repr(ssl) == "SSLConfig(cert=None, verify=False)"113114Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
lines 115–119why, one step back115def test_ssl_eq():116 ssl = SSLConfig(verify=False)117 assert ssl == SSLConfig(verify=False)118119Fix invalid f-string (#375)秋葉before adoption1b82a2a
lines 120–121why, one step back120def test_limits_repr():121 limits = httpx.PoolLimits(hard_limit=100)Pool timeouts should be on the TimeoutConfig, not PoolLimits (#563)Tom Christiebefore adoption364378a
line 122why, one step back122 assert repr(limits) == "PoolLimits(soft_limit=None, hard_limit=100)"Fix invalid f-string (#375)秋葉before adoption1b82a2a
lines 123–124why, one step back123124Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
lines 125–129why, one step back125def test_limits_eq():126 limits = httpx.PoolLimits(hard_limit=100)127 assert limits == httpx.PoolLimits(hard_limit=100)128129Fix invalid f-string (#375)秋葉before adoption1b82a2a
line 130why, one step back130def test_timeout_eq():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 131–132why, one step back131 timeout = httpx.Timeout(timeout=5.0)132 assert timeout == httpx.Timeout(timeout=5.0)Fix invalid f-string (#375)秋葉before adoption1b82a2a
lines 133–134why, one step back133134Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
line 135why, one step back135def test_timeout_from_nothing():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 136why, one step back136 timeout = httpx.Timeout()Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
lines 137–139why, one step back137 assert timeout.connect_timeout is None138 assert timeout.read_timeout is None139 assert timeout.write_timeout is NonePool timeouts should be on the TimeoutConfig, not PoolLimits (#563)Tom Christiebefore adoption364378a
line 140why, one step back140 assert timeout.pool_timeout is NoneFix invalid f-string (#375)秋葉before adoption1b82a2a
lines 141–142why, one step back141142Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
line 143why, one step back143def test_timeout_from_none():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 144–145why, one step back144 timeout = httpx.Timeout(timeout=None)145 assert timeout == httpx.Timeout()Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
lines 146–148why, one step back146147148def test_timeout_from_one_none_value():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 149–150why, one step back149 timeout = httpx.Timeout(read_timeout=None)150 assert timeout == httpx.Timeout()Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
lines 151–152why, one step back151152Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 153–162why, one step back153def test_timeout_from_one_value():154 timeout = httpx.Timeout(read_timeout=5.0)155 assert timeout == httpx.Timeout(timeout=(None, 5.0, None, None))156157158def test_timeout_from_one_value_and_default():159 timeout = httpx.Timeout(5.0, pool_timeout=60.0)160 assert timeout == httpx.Timeout(timeout=(5.0, 5.0, 5.0, 60.0))161162Fix invalid f-string (#375)秋葉before adoption1b82a2a
line 163why, one step back163def test_timeout_from_tuple():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 164–165why, one step back164 timeout = httpx.Timeout(timeout=(5.0, 5.0, 5.0, 5.0))165 assert timeout == httpx.Timeout(timeout=5.0)Fix invalid f-string (#375)秋葉before adoption1b82a2a
lines 166–168why, one step back166167168def test_timeout_from_config_instance():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 169–170why, one step back169 timeout = httpx.Timeout(timeout=5.0)170 assert httpx.Timeout(timeout) == httpx.Timeout(timeout=5.0)Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
lines 171–173why, one step back171172173def test_timeout_repr():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 174–175why, one step back174 timeout = httpx.Timeout(timeout=5.0)175 assert repr(timeout) == "Timeout(timeout=5.0)"Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
line 176why, one step back176TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 177why, one step back177 timeout = httpx.Timeout(read_timeout=5.0)Pool timeouts should be on the TimeoutConfig, not PoolLimits (#563)Tom Christiebefore adoption364378a
line 178why, one step back178 assert repr(timeout) == (TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 179why, one step back179 "Timeout(connect_timeout=None, read_timeout=5.0, "Pool timeouts should be on the TimeoutConfig, not PoolLimits (#563)Tom Christiebefore adoption364378a
line 180why, one step back180 "write_timeout=None, pool_timeout=None)"Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
line 181why, one step back181 )Fix invalid f-string (#375)秋葉before adoption1b82a2a
lines 182–188why, one step back182183184@pytest.mark.skipif(185 not hasattr(ssl.SSLContext, "keylog_filename"),186 reason="requires OpenSSL 1.1.1 or higher",187)188@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")Minor test fixes (#583)Tom Christiebefore adoption12d00b2
line 189why, one step back189def test_ssl_config_support_for_keylog_file(tmpdir, monkeypatch): # pragma: nocoverFix invalid f-string (#375)秋葉before adoption1b82a2a
lines 190–192why, one step back190 with monkeypatch.context() as m:191 m.delenv("SSLKEYLOGFILE", raising=False)192Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 193why, one step back193 ssl_config = SSLConfig(trust_env=True)Fix invalid f-string (#375)秋葉before adoption1b82a2a
lines 194–201why, one step back194195 assert ssl_config.ssl_context.keylog_filename is None196197 filename = str(tmpdir.join("test.log"))198199 with monkeypatch.context() as m:200 m.setenv("SSLKEYLOGFILE", filename)201Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 202why, one step back202 ssl_config = SSLConfig(trust_env=True)Fix invalid f-string (#375)秋葉before adoption1b82a2a
lines 203–205why, one step back203204 assert ssl_config.ssl_context.keylog_filename == filename205Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 206why, one step back206 ssl_config = SSLConfig(trust_env=False)Fix invalid f-string (#375)秋葉before adoption1b82a2a
lines 207–208why, one step back207208 assert ssl_config.ssl_context.keylog_filename is NonePrep for introducing SyncClient (#713)Tom Christiebefore adoptionf17ab37
lines 209–223why, one step back209210211def test_proxy_from_url():212 proxy = httpx.Proxy("https://example.com")213 assert repr(proxy) == "Proxy(url='https://example.com', headers={}, mode='DEFAULT')"214215216def test_invalid_proxy_scheme():217 with pytest.raises(ValueError):218 httpx.Proxy("invalid://example.com")219220221def test_invalid_proxy_mode():222 with pytest.raises(ValueError):223 httpx.Proxy("https://example.com", mode="INVALID")