Skip to content
Myr

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 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 = (105        "Limits(max_connections=100, max_keepalive_connections=None,"106        " keepalive_expiry=5.0)"107    )108    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 cover182    with monkeypatch.context() as m:183        m.delenv("SSLKEYLOGFILE", raising=False)184185        context = httpx.create_ssl_context(trust_env=True)186187        assert context.keylog_filename is None188189    filename = str(tmpdir.join("test.log"))190191    with monkeypatch.context() as m:192        m.setenv("SSLKEYLOGFILE", filename)193194        context = httpx.create_ssl_context(trust_env=True)195196        assert context.keylog_filename == filename197198        context = httpx.create_ssl_context(trust_env=False)199200        assert context.keylog_filename is None201202203def 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")