Skip to content
Myr

test_config.py

This file

Path

tests/test_config.py

220 lines · 6.4 KiB · Python

Last changed by

Drop support for Python 3.7 (#2813)Iurii Plinerbefore adoptionb40c04d

History of this file

Take it

1import 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)104    expected = "Limits(max_connections=100, max_keepalive_connections=None, keepalive_expiry=5.0)"105    assert repr(limits) == expected106107108def test_limits_eq():109    limits = httpx.Limits(max_connections=100)110    assert limits == httpx.Limits(max_connections=100)111112113def test_timeout_eq():114    timeout = httpx.Timeout(timeout=5.0)115    assert timeout == httpx.Timeout(timeout=5.0)116117118def test_timeout_all_parameters_set():119    timeout = httpx.Timeout(connect=5.0, read=5.0, write=5.0, pool=5.0)120    assert timeout == httpx.Timeout(timeout=5.0)121122123def test_timeout_from_nothing():124    timeout = httpx.Timeout(None)125    assert timeout.connect is None126    assert timeout.read is None127    assert timeout.write is None128    assert timeout.pool is None129130131def test_timeout_from_none():132    timeout = httpx.Timeout(timeout=None)133    assert timeout == httpx.Timeout(None)134135136def test_timeout_from_one_none_value():137    timeout = httpx.Timeout(None, read=None)138    assert timeout == httpx.Timeout(None)139140141def test_timeout_from_one_value():142    timeout = httpx.Timeout(None, read=5.0)143    assert timeout == httpx.Timeout(timeout=(None, 5.0, None, None))144145146def test_timeout_from_one_value_and_default():147    timeout = httpx.Timeout(5.0, pool=60.0)148    assert timeout == httpx.Timeout(timeout=(5.0, 5.0, 5.0, 60.0))149150151def test_timeout_missing_default():152    with pytest.raises(ValueError):153        httpx.Timeout(pool=60.0)154155156def test_timeout_from_tuple():157    timeout = httpx.Timeout(timeout=(5.0, 5.0, 5.0, 5.0))158    assert timeout == httpx.Timeout(timeout=5.0)159160161def test_timeout_from_config_instance():162    timeout = httpx.Timeout(timeout=5.0)163    assert httpx.Timeout(timeout) == httpx.Timeout(timeout=5.0)164165166def test_timeout_repr():167    timeout = httpx.Timeout(timeout=5.0)168    assert repr(timeout) == "Timeout(timeout=5.0)"169170    timeout = httpx.Timeout(None, read=5.0)171    assert repr(timeout) == "Timeout(connect=None, read=5.0, write=None, pool=None)"172173174@pytest.mark.skipif(175    not hasattr(ssl.SSLContext, "keylog_filename"),176    reason="requires OpenSSL 1.1.1 or higher",177)178def test_ssl_config_support_for_keylog_file(tmpdir, monkeypatch):  # pragma: no cover179    with monkeypatch.context() as m:180        m.delenv("SSLKEYLOGFILE", raising=False)181182        context = httpx.create_ssl_context(trust_env=True)183184        assert context.keylog_filename is None185186    filename = str(tmpdir.join("test.log"))187188    with monkeypatch.context() as m:189        m.setenv("SSLKEYLOGFILE", filename)190191        context = httpx.create_ssl_context(trust_env=True)192193        assert context.keylog_filename == filename194195        context = httpx.create_ssl_context(trust_env=False)196197        assert context.keylog_filename is None198199200def test_proxy_from_url():201    proxy = httpx.Proxy("https://example.com")202203    assert str(proxy.url) == "https://example.com"204    assert proxy.auth is None205    assert proxy.headers == {}206    assert repr(proxy) == "Proxy('https://example.com')"207208209def test_proxy_with_auth_from_url():210    proxy = httpx.Proxy("https://username:password@example.com")211212    assert str(proxy.url) == "https://example.com"213    assert proxy.auth == ("username", "password")214    assert proxy.headers == {}215    assert repr(proxy) == "Proxy('https://example.com', auth=('username', '********'))"216217218def test_invalid_proxy_scheme():219    with pytest.raises(ValueError):220        httpx.Proxy("invalid://example.com")