Skip to content
Myr

test_config.py

This file

Path

tests/test_config.py

223 lines · 6.6 KiB · Python

Last changed by

Remove and dissallow unused type ignores (#2470)Adrian Garcia Badaraccobefore adoption69e13cb

History of this file

Take it

The walk stopped after 200 commits: lines older than 67c2970 point at it.

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

lines 1–179why, one step back
1import os
2import ssl3import sys4from pathlib import Path56import certifi7import pytest89import httpx101112def test_load_ssl_config():13 context = httpx.create_ssl_context()14 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED15 assert context.check_hostname is True161718def test_load_ssl_config_verify_non_existing_path():19 with pytest.raises(IOError):20 httpx.create_ssl_context(verify="/path/to/nowhere")212223def test_load_ssl_config_verify_existing_file():24 context = httpx.create_ssl_context(verify=certifi.where())25 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED26 assert context.check_hostname is True272829@pytest.mark.parametrize("config", ("SSL_CERT_FILE", "SSL_CERT_DIR"))30def test_load_ssl_config_verify_env_file(31 https_server, ca_cert_pem_file, config, cert_authority32):33 os.environ[config] = (34 ca_cert_pem_file35 if config.endswith("_FILE")36 else str(Path(ca_cert_pem_file).parent)37 )38 context = httpx.create_ssl_context(trust_env=True)39 cert_authority.configure_trust(context)4041 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED42 assert context.check_hostname is True43 assert len(context.get_ca_certs()) == 1444546def test_load_ssl_config_verify_directory():47 path = Path(certifi.where()).parent48 context = httpx.create_ssl_context(verify=str(path))49 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED50 assert context.check_hostname is True515253def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):54 context = httpx.create_ssl_context(cert=(cert_pem_file, cert_private_key_file))55 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED56 assert context.check_hostname is True575859@pytest.mark.parametrize("password", [b"password", "password"])60def test_load_ssl_config_cert_and_encrypted_key(61 cert_pem_file, cert_encrypted_private_key_file, password62):63 context = httpx.create_ssl_context(64 cert=(cert_pem_file, cert_encrypted_private_key_file, password)65 )66 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED67 assert context.check_hostname is True686970def test_load_ssl_config_cert_and_key_invalid_password(71 cert_pem_file, cert_encrypted_private_key_file72):73 with pytest.raises(ssl.SSLError):74 httpx.create_ssl_context(75 cert=(cert_pem_file, cert_encrypted_private_key_file, "password1")76 )777879def test_load_ssl_config_cert_without_key_raises(cert_pem_file):80 with pytest.raises(ssl.SSLError):81 httpx.create_ssl_context(cert=cert_pem_file)828384def test_load_ssl_config_no_verify():85 context = httpx.create_ssl_context(verify=False)86 assert context.verify_mode == ssl.VerifyMode.CERT_NONE87 assert context.check_hostname is False888990def test_load_ssl_context():91 ssl_context = ssl.create_default_context()92 context = httpx.create_ssl_context(verify=ssl_context)9394 assert context is ssl_context959697def test_create_ssl_context_with_get_request(server, cert_pem_file):98 context = httpx.create_ssl_context(verify=cert_pem_file)99 response = httpx.get(server.url, verify=context)100 assert response.status_code == 200101102103def test_limits_repr():104 limits = httpx.Limits(max_connections=100)105 expected = "Limits(max_connections=100, max_keepalive_connections=None, keepalive_expiry=5.0)"106 assert repr(limits) == expected107108109def test_limits_eq():110 limits = httpx.Limits(max_connections=100)111 assert limits == httpx.Limits(max_connections=100)112113114def test_timeout_eq():115 timeout = httpx.Timeout(timeout=5.0)116 assert timeout == httpx.Timeout(timeout=5.0)117118119def test_timeout_all_parameters_set():120 timeout = httpx.Timeout(connect=5.0, read=5.0, write=5.0, pool=5.0)121 assert timeout == httpx.Timeout(timeout=5.0)122123124def test_timeout_from_nothing():125 timeout = httpx.Timeout(None)126 assert timeout.connect is None127 assert timeout.read is None128 assert timeout.write is None129 assert timeout.pool is None130131132def test_timeout_from_none():133 timeout = httpx.Timeout(timeout=None)134 assert timeout == httpx.Timeout(None)135136137def test_timeout_from_one_none_value():138 timeout = httpx.Timeout(None, read=None)139 assert timeout == httpx.Timeout(None)140141142def test_timeout_from_one_value():143 timeout = httpx.Timeout(None, read=5.0)144 assert timeout == httpx.Timeout(timeout=(None, 5.0, None, None))145146147def test_timeout_from_one_value_and_default():148 timeout = httpx.Timeout(5.0, pool=60.0)149 assert timeout == httpx.Timeout(timeout=(5.0, 5.0, 5.0, 60.0))150151152def test_timeout_missing_default():153 with pytest.raises(ValueError):154 httpx.Timeout(pool=60.0)155156157def test_timeout_from_tuple():158 timeout = httpx.Timeout(timeout=(5.0, 5.0, 5.0, 5.0))159 assert timeout == httpx.Timeout(timeout=5.0)160161162def test_timeout_from_config_instance():163 timeout = httpx.Timeout(timeout=5.0)164 assert httpx.Timeout(timeout) == httpx.Timeout(timeout=5.0)165166167def test_timeout_repr():168 timeout = httpx.Timeout(timeout=5.0)169 assert repr(timeout) == "Timeout(timeout=5.0)"170171 timeout = httpx.Timeout(None, read=5.0)172 assert repr(timeout) == "Timeout(connect=None, read=5.0, write=None, pool=None)"173174175@pytest.mark.skipif(176 not hasattr(ssl.SSLContext, "keylog_filename"),177 reason="requires OpenSSL 1.1.1 or higher",178)179@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")

use # pragma: no cover instead of # pragma: nocover (#2471)Adrian Garcia Badaraccobefore adoption16e2830

line 180why, one step back
180def test_ssl_config_support_for_keylog_file(tmpdir, monkeypatch): # pragma: no cover

Remove and dissallow unused type ignores (#2470)Adrian Garcia Badaraccobefore adoption69e13cb

lines 181–183why, one step back
181 if sys.version_info > (3, 8):
182 with monkeypatch.context() as m:183 m.delenv("SSLKEYLOGFILE", raising=False)

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

line 184why, one step back
184

Remove and dissallow unused type ignores (#2470)Adrian Garcia Badaraccobefore adoption69e13cb

line 185why, one step back
185 context = httpx.create_ssl_context(trust_env=True)

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

line 186why, one step back
186

Remove and dissallow unused type ignores (#2470)Adrian Garcia Badaraccobefore adoption69e13cb

line 187why, one step back
187 assert context.keylog_filename is None

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

line 188why, one step back
188

Remove and dissallow unused type ignores (#2470)Adrian Garcia Badaraccobefore adoption69e13cb

line 189why, one step back
189 filename = str(tmpdir.join("test.log"))

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

line 190why, one step back
190

Remove and dissallow unused type ignores (#2470)Adrian Garcia Badaraccobefore adoption69e13cb

lines 191–192why, one step back
191 with monkeypatch.context() as m:
192 m.setenv("SSLKEYLOGFILE", filename)

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

line 193why, one step back
193

Remove and dissallow unused type ignores (#2470)Adrian Garcia Badaraccobefore adoption69e13cb

line 194why, one step back
194 context = httpx.create_ssl_context(trust_env=True)

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

line 195why, one step back
195

Remove and dissallow unused type ignores (#2470)Adrian Garcia Badaraccobefore adoption69e13cb

line 196why, one step back
196 assert context.keylog_filename == filename

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

line 197why, one step back
197

Remove and dissallow unused type ignores (#2470)Adrian Garcia Badaraccobefore adoption69e13cb

line 198why, one step back
198 context = httpx.create_ssl_context(trust_env=False)

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

line 199why, one step back
199

Remove and dissallow unused type ignores (#2470)Adrian Garcia Badaraccobefore adoption69e13cb

line 200why, one step back
200 assert context.keylog_filename is None

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

lines 201–223why, one step back
201
202203def 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")