test_config.py
This file
Path
tests/test_config.py
217 lines · 6.6 KiB · Python
Last changed by
Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
History of this fileTake it
The walk stopped after 200 commits: lines older than fb17459 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 multipart header params encoding (#167)Nikbefore adoptionfb17459
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 multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 6–11why, one step back67import pytest89import httpx1011Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 12why, one step back12def test_load_ssl_config():Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
line 13why, one step back13 ssl_config = httpx.SSLConfig()Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 14why, one step back14 context = ssl_config.load_ssl_context()Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 15–18why, one step back15 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED16 assert context.check_hostname is True1718Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 19why, one step back19def test_load_ssl_config_verify_non_existing_path():Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 20–21why, one step back20 ssl_config = httpx.SSLConfig(verify="/path/to/nowhere")21 with pytest.raises(IOError):Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 22why, one step back22 ssl_config.load_ssl_context()Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 23–24why, one step back2324Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 25why, one step back25def test_load_ssl_config_verify_existing_file():Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
line 26why, one step back26 ssl_config = httpx.SSLConfig(verify=httpx.config.DEFAULT_CA_BUNDLE_PATH)Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 27why, one step back27 context = ssl_config.load_ssl_context()Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 28–29why, one step back28 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED29 assert context.check_hostname is TrueAdd support for SSL_CERT_FILE and SSL_CERT_DIR (#307)Can Sarıgölbefore adoptionc9810a7
lines 30–53why, one step back303132@pytest.mark.parametrize("config", ("SSL_CERT_FILE", "SSL_CERT_DIR"))33def test_load_ssl_config_verify_env_file(https_server, ca_cert_pem_file, config):34 os.environ[config] = (35 ca_cert_pem_file36 if config.endswith("_FILE")37 else str(Path(ca_cert_pem_file).parent)38 )39 ssl_config = httpx.SSLConfig(trust_env=True)40 context = ssl_config.load_ssl_context()41 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED42 assert context.check_hostname is True43 assert ssl_config.verify == os.environ[config]4445 # Skipping 'SSL_CERT_DIR' functional test for now because46 # we're unable to get the certificate within the directory to47 # load into the SSLContext. :(48 if config == "SSL_CERT_FILE":49 host = https_server.url.host50 port = https_server.url.port51 conn = socket.create_connection((host, port))52 context.wrap_socket(conn, server_hostname=host)53 assert len(context.get_ca_certs()) == 1Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 54–55why, one step back5455Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 56why, one step back56def test_load_ssl_config_verify_directory():Use pathlib.Path instead of os.path functions (#188)Stephen Brown IIbefore adoptionfde30b8
line 57why, one step back57 path = httpx.config.DEFAULT_CA_BUNDLE_PATH.parentFixed multipart header params encoding (#167)Nikbefore adoptionfb17459
line 58why, one step back58 ssl_config = httpx.SSLConfig(verify=path)Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 59why, one step back59 context = ssl_config.load_ssl_context()Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 60–63why, one step back60 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED61 assert context.check_hostname is True6263Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 64why, one step back64def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
line 65why, one step back65 ssl_config = httpx.SSLConfig(cert=(cert_pem_file, cert_private_key_file))Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 66why, one step back66 context = ssl_config.load_ssl_context()Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 67–71why, one step back67 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED68 assert context.check_hostname is True697071@pytest.mark.parametrize("password", [b"password", "password"])Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 72why, one step back72def test_load_ssl_config_cert_and_encrypted_key(Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 73–77why, one step back73 cert_pem_file, cert_encrypted_private_key_file, password74):75 ssl_config = httpx.SSLConfig(76 cert=(cert_pem_file, cert_encrypted_private_key_file, password)77 )Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 78why, one step back78 context = ssl_config.load_ssl_context()Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 79–82why, one step back79 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED80 assert context.check_hostname is True8182Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 83why, one step back83def test_load_ssl_config_cert_and_key_invalid_password(Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 84–90why, one step back84 cert_pem_file, cert_encrypted_private_key_file85):86 ssl_config = httpx.SSLConfig(87 cert=(cert_pem_file, cert_encrypted_private_key_file, "password1")88 )8990 with pytest.raises(ssl.SSLError):Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 91why, one step back91 ssl_config.load_ssl_context()Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 92–93why, one step back9293Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 94why, one step back94def test_load_ssl_config_cert_without_key_raises(cert_pem_file):Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 95–96why, one step back95 ssl_config = httpx.SSLConfig(cert=cert_pem_file)96 with pytest.raises(ssl.SSLError):Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 97why, one step back97 ssl_config.load_ssl_context()Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 98–99why, one step back9899Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 100why, one step back100def test_load_ssl_config_no_verify():Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
line 101why, one step back101 ssl_config = httpx.SSLConfig(verify=False)Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 102why, one step back102 context = ssl_config.load_ssl_context()Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 103–106why, one step back103 assert context.verify_mode == ssl.VerifyMode.CERT_NONE104 assert context.check_hostname is False105106Accept SSLContext into SSLConfig(verify=...) (#215)Seth Michael Larsonbefore adoptiondf8874b
lines 107–115why, one step back107def test_load_ssl_context():108 ssl_context = ssl.create_default_context()109 ssl_config = httpx.SSLConfig(verify=ssl_context)110111 assert ssl_config.verify is True112 assert ssl_config.ssl_context is ssl_context113 assert repr(ssl_config) == "SSLConfig(cert=None, verify=True)"114115Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 116–122why, one step back116def test_ssl_repr():117 ssl = httpx.SSLConfig(verify=False)118 assert repr(ssl) == "SSLConfig(cert=None, verify=False)"119120121def test_limits_repr():122 limits = httpx.PoolLimits(hard_limit=100)Pool timeouts should be on the TimeoutConfig, not PoolLimits (#563)Tom Christiebefore adoption364378a
line 123why, one step back123 assert repr(limits) == "PoolLimits(soft_limit=None, hard_limit=100)"Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 124–130why, one step back124125126def test_ssl_eq():127 ssl = httpx.SSLConfig(verify=False)128 assert ssl == httpx.SSLConfig(verify=False)129130Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
lines 131–135why, one step back131def test_limits_eq():132 limits = httpx.PoolLimits(hard_limit=100)133 assert limits == httpx.PoolLimits(hard_limit=100)134135Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
line 136why, one step back136def test_timeout_eq():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 137–138why, one step back137 timeout = httpx.Timeout(timeout=5.0)138 assert timeout == httpx.Timeout(timeout=5.0)Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 139–140why, one step back139140Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
line 141why, one step back141def test_timeout_from_nothing():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 142why, one step back142 timeout = httpx.Timeout()Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
lines 143–145why, one step back143 assert timeout.connect_timeout is None144 assert timeout.read_timeout is None145 assert timeout.write_timeout is NonePool timeouts should be on the TimeoutConfig, not PoolLimits (#563)Tom Christiebefore adoption364378a
line 146why, one step back146 assert timeout.pool_timeout is NoneFixed multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 147–148why, one step back147148Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
line 149why, one step back149def test_timeout_from_none():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 150–151why, one step back150 timeout = httpx.Timeout(timeout=None)151 assert timeout == httpx.Timeout()Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
lines 152–154why, one step back152153154def test_timeout_from_one_none_value():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 155–156why, one step back155 timeout = httpx.Timeout(read_timeout=None)156 assert timeout == httpx.Timeout()Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
lines 157–158why, one step back157158Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 159–168why, one step back159def test_timeout_from_one_value():160 timeout = httpx.Timeout(read_timeout=5.0)161 assert timeout == httpx.Timeout(timeout=(None, 5.0, None, None))162163164def test_timeout_from_one_value_and_default():165 timeout = httpx.Timeout(5.0, pool_timeout=60.0)166 assert timeout == httpx.Timeout(timeout=(5.0, 5.0, 5.0, 60.0))167168Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
line 169why, one step back169def test_timeout_from_tuple():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 170–171why, one step back170 timeout = httpx.Timeout(timeout=(5.0, 5.0, 5.0, 5.0))171 assert timeout == httpx.Timeout(timeout=5.0)Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 172–174why, one step back172173174def test_timeout_from_config_instance():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 175–176why, one step back175 timeout = httpx.Timeout(timeout=5.0)176 assert httpx.Timeout(timeout) == httpx.Timeout(timeout=5.0)Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
lines 177–179why, one step back177178179def test_timeout_repr():TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 180–181why, one step back180 timeout = httpx.Timeout(timeout=5.0)181 assert repr(timeout) == "Timeout(timeout=5.0)"Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
line 182why, one step back182TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 183why, one step back183 timeout = httpx.Timeout(read_timeout=5.0)Pool timeouts should be on the TimeoutConfig, not PoolLimits (#563)Tom Christiebefore adoption364378a
line 184why, one step back184 assert repr(timeout) == (TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 185why, one step back185 "Timeout(connect_timeout=None, read_timeout=5.0, "Pool timeouts should be on the TimeoutConfig, not PoolLimits (#563)Tom Christiebefore adoption364378a
line 186why, one step back186 "write_timeout=None, pool_timeout=None)"Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0
line 187why, one step back187 )Add support for SSLKEYLOGFILE (#301)Tomás Faríasbefore adoptiondb7f2d0
lines 188–194why, one step back188189190@pytest.mark.skipif(191 not hasattr(ssl.SSLContext, "keylog_filename"),192 reason="requires OpenSSL 1.1.1 or higher",193)194@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")Minor test fixes (#583)Tom Christiebefore adoption12d00b2
line 195why, one step back195def test_ssl_config_support_for_keylog_file(tmpdir, monkeypatch): # pragma: nocoverAdd support for SSLKEYLOGFILE (#301)Tomás Faríasbefore adoptiondb7f2d0
lines 196–217why, one step back196 with monkeypatch.context() as m:197 m.delenv("SSLKEYLOGFILE", raising=False)198199 ssl_config = httpx.SSLConfig(trust_env=True)200 ssl_config.load_ssl_context()201202 assert ssl_config.ssl_context.keylog_filename is None203204 filename = str(tmpdir.join("test.log"))205206 with monkeypatch.context() as m:207 m.setenv("SSLKEYLOGFILE", filename)208209 ssl_config = httpx.SSLConfig(trust_env=True)210 ssl_config.load_ssl_context()211212 assert ssl_config.ssl_context.keylog_filename == filename213214 ssl_config = httpx.SSLConfig(trust_env=False)215 ssl_config.load_ssl_context()216217 assert ssl_config.ssl_context.keylog_filename is None