test_config.py
This file
Path
tests/test_config.py
231 lines · 7.0 KiB · Python
Last changed by
Add support for SSL_CERT_FILE and SSL_CERT_DIR (#307)Can Sarıgölbefore adoptionc9810a7
History of this fileTake it
The walk stopped after 200 commits: lines older than d8950ef 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 socketDrop prepare_requestTom Christiebefore adoptiond8950ef
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 PathDrop prepare_requestTom Christiebefore adoptiond8950ef
lines 6–8why, one step back67import pytest8Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 9why, one step back9import httpxDrop prepare_requestTom Christiebefore adoptiond8950ef
lines 10–11why, one step back1011Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 12why, one step back12def test_load_ssl_config():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
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()Drop prepare_requestTom Christiebefore adoptiond8950ef
line 15why, one step back15 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIREDSupport client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
line 16why, one step back16 assert context.check_hostname is TrueDrop prepare_requestTom Christiebefore adoptiond8950ef
lines 17–18why, one step back1718Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6
line 19why, one step back19def test_load_ssl_config_verify_non_existing_path():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 20why, one step back20 ssl_config = httpx.SSLConfig(verify="/path/to/nowhere")Drop prepare_requestTom Christiebefore adoptiond8950ef
line 21why, one step back21 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()Drop prepare_requestTom Christiebefore adoptiond8950ef
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():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
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()Drop prepare_requestTom Christiebefore adoptiond8950ef
line 28why, one step back28 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIREDSupport client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
line 29why, one step back29 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()) == 1Drop prepare_requestTom Christiebefore adoptiond8950ef
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.parentRenaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
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()Drop prepare_requestTom Christiebefore adoptiond8950ef
line 60why, one step back60 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIREDSupport client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
line 61why, one step back61 assert context.check_hostname is TrueDrop prepare_requestTom Christiebefore adoptiond8950ef
lines 62–63why, one step back6263Run 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):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
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()Drop prepare_requestTom Christiebefore adoptiond8950ef
line 67why, one step back67 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIREDSupport client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
line 68why, one step back68 assert context.check_hostname is TrueDrop prepare_requestTom Christiebefore adoptiond8950ef
lines 69–70why, one step back6970Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
line 71why, one step back71@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(Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
lines 73–74why, one step back73 cert_pem_file, cert_encrypted_private_key_file, password74):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 75why, one step back75 ssl_config = httpx.SSLConfig(Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
lines 76–77why, one step back76 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()Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
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(Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
lines 84–85why, one step back84 cert_pem_file, cert_encrypted_private_key_file85):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 86why, one step back86 ssl_config = httpx.SSLConfig(Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
lines 87–89why, one step back87 cert=(cert_pem_file, cert_encrypted_private_key_file, "password1")88 )89Drop prepare_requestTom Christiebefore adoptiond8950ef
line 90why, one step back90 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()Drop prepare_requestTom Christiebefore adoptiond8950ef
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):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 95why, one step back95 ssl_config = httpx.SSLConfig(cert=cert_pem_file)Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
line 96why, one step back96 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()Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
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():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
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()Drop prepare_requestTom Christiebefore adoptiond8950ef
line 103why, one step back103 assert context.verify_mode == ssl.VerifyMode.CERT_NONESupport client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
line 104why, one step back104 assert context.check_hostname is FalseDrop prepare_requestTom Christiebefore adoptiond8950ef
lines 105–106why, one step back105106Accept 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)"114115Drop prepare_requestTom Christiebefore adoptiond8950ef
line 116why, one step back116def test_ssl_repr():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 117why, one step back117 ssl = httpx.SSLConfig(verify=False)Drop prepare_requestTom Christiebefore adoptiond8950ef
lines 118–120why, one step back118 assert repr(ssl) == "SSLConfig(cert=None, verify=False)"119120Merge pull request #241 from encode/protocol-supportTom Christiebefore adoptione0b523d
lines 121–145why, one step back121def test_http_versions_repr():122 http_versions = httpx.HTTPVersionConfig()123 assert repr(http_versions) == "HTTPVersionConfig(['HTTP/1.1', 'HTTP/2'])"124125126def test_http_versions_from_string():127 http_versions = httpx.HTTPVersionConfig("HTTP/1.1")128 assert repr(http_versions) == "HTTPVersionConfig(['HTTP/1.1'])"129130131def test_http_versions_from_list():132 http_versions = httpx.HTTPVersionConfig(["http/1.1"])133 assert repr(http_versions) == "HTTPVersionConfig(['HTTP/1.1'])"134135136def test_http_versions_from_config():137 http_versions = httpx.HTTPVersionConfig(httpx.HTTPVersionConfig("HTTP/1.1"))138 assert repr(http_versions) == "HTTPVersionConfig(['HTTP/1.1'])"139140141def test_invalid_http_version():142 with pytest.raises(ValueError):143 httpx.HTTPVersionConfig("HTTP/9")144145Raise TypeError if invalid parameter for HTTPVersionConfig (#253)Can Sarıgölbefore adoption4ea4af6
lines 146–155why, one step back146def test_invalid_http_version_type():147 with pytest.raises(TypeError):148 httpx.HTTPVersionConfig(123)149150151def test_invalid_http_version_list_type():152 with pytest.raises(ValueError):153 httpx.HTTPVersionConfig([123])154155Merge pull request #241 from encode/protocol-supportTom Christiebefore adoptione0b523d
lines 156–160why, one step back156def test_empty_http_version():157 with pytest.raises(ValueError):158 httpx.HTTPVersionConfig([])159160Drop prepare_requestTom Christiebefore adoptiond8950ef
line 161why, one step back161def test_timeout_repr():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 162why, one step back162 timeout = httpx.TimeoutConfig(timeout=5.0)Drop prepare_requestTom Christiebefore adoptiond8950ef
lines 163–164why, one step back163 assert repr(timeout) == "TimeoutConfig(timeout=5.0)"164Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 165why, one step back165 timeout = httpx.TimeoutConfig(read_timeout=5.0)Drop prepare_requestTom Christiebefore adoptiond8950ef
lines 166–172why, one step back166 assert (167 repr(timeout)168 == "TimeoutConfig(connect_timeout=None, read_timeout=5.0, write_timeout=None)"169 )170171172def test_limits_repr():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 173why, one step back173 limits = httpx.PoolLimits(hard_limit=100)Drop prepare_requestTom Christiebefore adoptiond8950ef
lines 174–179why, one step back174 assert (175 repr(limits) == "PoolLimits(soft_limit=None, hard_limit=100, pool_timeout=None)"176 )177178179def test_ssl_eq():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
lines 180–181why, one step back180 ssl = httpx.SSLConfig(verify=False)181 assert ssl == httpx.SSLConfig(verify=False)Drop prepare_requestTom Christiebefore adoptiond8950ef
lines 182–184why, one step back182183184def test_timeout_eq():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
lines 185–186why, one step back185 timeout = httpx.TimeoutConfig(timeout=5.0)186 assert timeout == httpx.TimeoutConfig(timeout=5.0)Drop prepare_requestTom Christiebefore adoptiond8950ef
lines 187–189why, one step back187188189def test_limits_eq():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
lines 190–191why, one step back190 limits = httpx.PoolLimits(hard_limit=100)191 assert limits == httpx.PoolLimits(hard_limit=100)Drop prepare_requestTom Christiebefore adoptiond8950ef
lines 192–194why, one step back192193194def test_timeout_from_tuple():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
lines 195–196why, one step back195 timeout = httpx.TimeoutConfig(timeout=(5.0, 5.0, 5.0))196 assert timeout == httpx.TimeoutConfig(timeout=5.0)Drop prepare_requestTom Christiebefore adoptiond8950ef
lines 197–199why, one step back197198199def test_timeout_from_config_instance():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
lines 200–201why, one step back200 timeout = httpx.TimeoutConfig(timeout=5.0)201 assert httpx.TimeoutConfig(timeout) == httpx.TimeoutConfig(timeout=5.0)Add support for SSLKEYLOGFILE (#301)Tomás Faríasbefore adoptiondb7f2d0
lines 202–231why, one step back202203204@pytest.mark.skipif(205 not hasattr(ssl.SSLContext, "keylog_filename"),206 reason="requires OpenSSL 1.1.1 or higher",207)208@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")209def test_ssl_config_support_for_keylog_file(tmpdir, monkeypatch):210 with monkeypatch.context() as m:211 m.delenv("SSLKEYLOGFILE", raising=False)212213 ssl_config = httpx.SSLConfig(trust_env=True)214 ssl_config.load_ssl_context()215216 assert ssl_config.ssl_context.keylog_filename is None217218 filename = str(tmpdir.join("test.log"))219220 with monkeypatch.context() as m:221 m.setenv("SSLKEYLOGFILE", filename)222223 ssl_config = httpx.SSLConfig(trust_env=True)224 ssl_config.load_ssl_context()225226 assert ssl_config.ssl_context.keylog_filename == filename227228 ssl_config = httpx.SSLConfig(trust_env=False)229 ssl_config.load_ssl_context()230231 assert ssl_config.ssl_context.keylog_filename is None