test_config.py
This file
Path
tests/test_config.py
223 lines · 6.5 KiB · Python
Last changed by
Ruff linter: Use the default line-length (#2922)T-256before adoptionfd60b18
History of this fileTake it
The walk stopped after 200 commits: lines older than 40a0da0 point at it.
Add back in URL.raw with NamedTuple (#2481)Demetribefore adoption40a0da0
lines 1–103why, one step back1import os2import ssl3from pathlib import Path45import certifi6import pytest78import httpx91011def test_load_ssl_config():12 context = httpx.create_ssl_context()13 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED14 assert context.check_hostname is True151617def test_load_ssl_config_verify_non_existing_path():18 with pytest.raises(IOError):19 httpx.create_ssl_context(verify="/path/to/nowhere")202122def test_load_ssl_config_verify_existing_file():23 context = httpx.create_ssl_context(verify=certifi.where())24 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED25 assert context.check_hostname is True262728@pytest.mark.parametrize("config", ("SSL_CERT_FILE", "SSL_CERT_DIR"))29def test_load_ssl_config_verify_env_file(30 https_server, ca_cert_pem_file, config, cert_authority31):32 os.environ[config] = (33 ca_cert_pem_file34 if config.endswith("_FILE")35 else str(Path(ca_cert_pem_file).parent)36 )37 context = httpx.create_ssl_context(trust_env=True)38 cert_authority.configure_trust(context)3940 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED41 assert context.check_hostname is True42 assert len(context.get_ca_certs()) == 1434445def test_load_ssl_config_verify_directory():46 path = Path(certifi.where()).parent47 context = httpx.create_ssl_context(verify=str(path))48 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED49 assert context.check_hostname is True505152def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):53 context = httpx.create_ssl_context(cert=(cert_pem_file, cert_private_key_file))54 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED55 assert context.check_hostname is True565758@pytest.mark.parametrize("password", [b"password", "password"])59def test_load_ssl_config_cert_and_encrypted_key(60 cert_pem_file, cert_encrypted_private_key_file, password61):62 context = httpx.create_ssl_context(63 cert=(cert_pem_file, cert_encrypted_private_key_file, password)64 )65 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED66 assert context.check_hostname is True676869def test_load_ssl_config_cert_and_key_invalid_password(70 cert_pem_file, cert_encrypted_private_key_file71):72 with pytest.raises(ssl.SSLError):73 httpx.create_ssl_context(74 cert=(cert_pem_file, cert_encrypted_private_key_file, "password1")75 )767778def test_load_ssl_config_cert_without_key_raises(cert_pem_file):79 with pytest.raises(ssl.SSLError):80 httpx.create_ssl_context(cert=cert_pem_file)818283def test_load_ssl_config_no_verify():84 context = httpx.create_ssl_context(verify=False)85 assert context.verify_mode == ssl.VerifyMode.CERT_NONE86 assert context.check_hostname is False878889def test_load_ssl_context():90 ssl_context = ssl.create_default_context()91 context = httpx.create_ssl_context(verify=ssl_context)9293 assert context is ssl_context949596def test_create_ssl_context_with_get_request(server, cert_pem_file):97 context = httpx.create_ssl_context(verify=cert_pem_file)98 response = httpx.get(server.url, verify=context)99 assert response.status_code == 200100101102def test_limits_repr():103 limits = httpx.Limits(max_connections=100)Ruff linter: Use the default line-length (#2922)T-256before adoptionfd60b18
lines 104–107why, one step back104 expected = (105 "Limits(max_connections=100, max_keepalive_connections=None,"106 " keepalive_expiry=5.0)"107 )Add back in URL.raw with NamedTuple (#2481)Demetribefore adoption40a0da0
lines 108–181why, one step back108 assert repr(limits) == expected109110111def test_limits_eq():112 limits = httpx.Limits(max_connections=100)113 assert limits == httpx.Limits(max_connections=100)114115116def test_timeout_eq():117 timeout = httpx.Timeout(timeout=5.0)118 assert timeout == httpx.Timeout(timeout=5.0)119120121def test_timeout_all_parameters_set():122 timeout = httpx.Timeout(connect=5.0, read=5.0, write=5.0, pool=5.0)123 assert timeout == httpx.Timeout(timeout=5.0)124125126def test_timeout_from_nothing():127 timeout = httpx.Timeout(None)128 assert timeout.connect is None129 assert timeout.read is None130 assert timeout.write is None131 assert timeout.pool is None132133134def test_timeout_from_none():135 timeout = httpx.Timeout(timeout=None)136 assert timeout == httpx.Timeout(None)137138139def test_timeout_from_one_none_value():140 timeout = httpx.Timeout(None, read=None)141 assert timeout == httpx.Timeout(None)142143144def test_timeout_from_one_value():145 timeout = httpx.Timeout(None, read=5.0)146 assert timeout == httpx.Timeout(timeout=(None, 5.0, None, None))147148149def test_timeout_from_one_value_and_default():150 timeout = httpx.Timeout(5.0, pool=60.0)151 assert timeout == httpx.Timeout(timeout=(5.0, 5.0, 5.0, 60.0))152153154def test_timeout_missing_default():155 with pytest.raises(ValueError):156 httpx.Timeout(pool=60.0)157158159def test_timeout_from_tuple():160 timeout = httpx.Timeout(timeout=(5.0, 5.0, 5.0, 5.0))161 assert timeout == httpx.Timeout(timeout=5.0)162163164def test_timeout_from_config_instance():165 timeout = httpx.Timeout(timeout=5.0)166 assert httpx.Timeout(timeout) == httpx.Timeout(timeout=5.0)167168169def test_timeout_repr():170 timeout = httpx.Timeout(timeout=5.0)171 assert repr(timeout) == "Timeout(timeout=5.0)"172173 timeout = httpx.Timeout(None, read=5.0)174 assert repr(timeout) == "Timeout(connect=None, read=5.0, write=None, pool=None)"175176177@pytest.mark.skipif(178 not hasattr(ssl.SSLContext, "keylog_filename"),179 reason="requires OpenSSL 1.1.1 or higher",180)181def test_ssl_config_support_for_keylog_file(tmpdir, monkeypatch): # pragma: no coverDrop support for Python 3.7 (#2813)Iurii Plinerbefore adoptionb40c04d
lines 182–183why, one step back182 with monkeypatch.context() as m:183 m.delenv("SSLKEYLOGFILE", raising=False)Add back in URL.raw with NamedTuple (#2481)Demetribefore adoption40a0da0
line 184why, one step back184Drop support for Python 3.7 (#2813)Iurii Plinerbefore adoptionb40c04d
line 185why, one step back185 context = httpx.create_ssl_context(trust_env=True)Add back in URL.raw with NamedTuple (#2481)Demetribefore adoption40a0da0
line 186why, one step back186Drop support for Python 3.7 (#2813)Iurii Plinerbefore adoptionb40c04d
line 187why, one step back187 assert context.keylog_filename is NoneAdd back in URL.raw with NamedTuple (#2481)Demetribefore adoption40a0da0
line 188why, one step back188Drop support for Python 3.7 (#2813)Iurii Plinerbefore adoptionb40c04d
line 189why, one step back189 filename = str(tmpdir.join("test.log"))Add back in URL.raw with NamedTuple (#2481)Demetribefore adoption40a0da0
line 190why, one step back190Drop support for Python 3.7 (#2813)Iurii Plinerbefore adoptionb40c04d
lines 191–192why, one step back191 with monkeypatch.context() as m:192 m.setenv("SSLKEYLOGFILE", filename)Add back in URL.raw with NamedTuple (#2481)Demetribefore adoption40a0da0
line 193why, one step back193Drop support for Python 3.7 (#2813)Iurii Plinerbefore adoptionb40c04d
line 194why, one step back194 context = httpx.create_ssl_context(trust_env=True)Add back in URL.raw with NamedTuple (#2481)Demetribefore adoption40a0da0
line 195why, one step back195Drop support for Python 3.7 (#2813)Iurii Plinerbefore adoptionb40c04d
line 196why, one step back196 assert context.keylog_filename == filenameAdd back in URL.raw with NamedTuple (#2481)Demetribefore adoption40a0da0
line 197why, one step back197Drop support for Python 3.7 (#2813)Iurii Plinerbefore adoptionb40c04d
line 198why, one step back198 context = httpx.create_ssl_context(trust_env=False)Add back in URL.raw with NamedTuple (#2481)Demetribefore adoption40a0da0
line 199why, one step back199Drop support for Python 3.7 (#2813)Iurii Plinerbefore adoptionb40c04d
line 200why, one step back200 assert context.keylog_filename is NoneAdd back in URL.raw with NamedTuple (#2481)Demetribefore adoption40a0da0
lines 201–223why, one step back201202203def test_proxy_from_url():204 proxy = httpx.Proxy("https://example.com")205206 assert str(proxy.url) == "https://example.com"207 assert proxy.auth is None208 assert proxy.headers == {}209 assert repr(proxy) == "Proxy('https://example.com')"210211212def test_proxy_with_auth_from_url():213 proxy = httpx.Proxy("https://username:password@example.com")214215 assert str(proxy.url) == "https://example.com"216 assert proxy.auth == ("username", "password")217 assert proxy.headers == {}218 assert repr(proxy) == "Proxy('https://example.com', auth=('username', '********'))"219220221def test_invalid_proxy_scheme():222 with pytest.raises(ValueError):223 httpx.Proxy("invalid://example.com")