Skip to content
Myr

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 file

Take it

1import os2import socket3import ssl4import sys5from pathlib import Path67import pytest89import httpx101112def test_load_ssl_config():13    ssl_config = httpx.SSLConfig()14    context = ssl_config.load_ssl_context()15    assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED16    assert context.check_hostname is True171819def test_load_ssl_config_verify_non_existing_path():20    ssl_config = httpx.SSLConfig(verify="/path/to/nowhere")21    with pytest.raises(IOError):22        ssl_config.load_ssl_context()232425def test_load_ssl_config_verify_existing_file():26    ssl_config = httpx.SSLConfig(verify=httpx.config.DEFAULT_CA_BUNDLE_PATH)27    context = ssl_config.load_ssl_context()28    assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED29    assert context.check_hostname is True303132@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()) == 1545556def test_load_ssl_config_verify_directory():57    path = httpx.config.DEFAULT_CA_BUNDLE_PATH.parent58    ssl_config = httpx.SSLConfig(verify=path)59    context = ssl_config.load_ssl_context()60    assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED61    assert context.check_hostname is True626364def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):65    ssl_config = httpx.SSLConfig(cert=(cert_pem_file, cert_private_key_file))66    context = ssl_config.load_ssl_context()67    assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED68    assert context.check_hostname is True697071@pytest.mark.parametrize("password", [b"password", "password"])72def test_load_ssl_config_cert_and_encrypted_key(73    cert_pem_file, cert_encrypted_private_key_file, password74):75    ssl_config = httpx.SSLConfig(76        cert=(cert_pem_file, cert_encrypted_private_key_file, password)77    )78    context = ssl_config.load_ssl_context()79    assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED80    assert context.check_hostname is True818283def test_load_ssl_config_cert_and_key_invalid_password(84    cert_pem_file, cert_encrypted_private_key_file85):86    ssl_config = httpx.SSLConfig(87        cert=(cert_pem_file, cert_encrypted_private_key_file, "password1")88    )8990    with pytest.raises(ssl.SSLError):91        ssl_config.load_ssl_context()929394def test_load_ssl_config_cert_without_key_raises(cert_pem_file):95    ssl_config = httpx.SSLConfig(cert=cert_pem_file)96    with pytest.raises(ssl.SSLError):97        ssl_config.load_ssl_context()9899100def test_load_ssl_config_no_verify():101    ssl_config = httpx.SSLConfig(verify=False)102    context = ssl_config.load_ssl_context()103    assert context.verify_mode == ssl.VerifyMode.CERT_NONE104    assert context.check_hostname is False105106107def 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)"114115116def test_ssl_repr():117    ssl = httpx.SSLConfig(verify=False)118    assert repr(ssl) == "SSLConfig(cert=None, verify=False)"119120121def 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")144145146def 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])154155156def test_empty_http_version():157    with pytest.raises(ValueError):158        httpx.HTTPVersionConfig([])159160161def test_timeout_repr():162    timeout = httpx.TimeoutConfig(timeout=5.0)163    assert repr(timeout) == "TimeoutConfig(timeout=5.0)"164165    timeout = httpx.TimeoutConfig(read_timeout=5.0)166    assert (167        repr(timeout)168        == "TimeoutConfig(connect_timeout=None, read_timeout=5.0, write_timeout=None)"169    )170171172def test_limits_repr():173    limits = httpx.PoolLimits(hard_limit=100)174    assert (175        repr(limits) == "PoolLimits(soft_limit=None, hard_limit=100, pool_timeout=None)"176    )177178179def test_ssl_eq():180    ssl = httpx.SSLConfig(verify=False)181    assert ssl == httpx.SSLConfig(verify=False)182183184def test_timeout_eq():185    timeout = httpx.TimeoutConfig(timeout=5.0)186    assert timeout == httpx.TimeoutConfig(timeout=5.0)187188189def test_limits_eq():190    limits = httpx.PoolLimits(hard_limit=100)191    assert limits == httpx.PoolLimits(hard_limit=100)192193194def test_timeout_from_tuple():195    timeout = httpx.TimeoutConfig(timeout=(5.0, 5.0, 5.0))196    assert timeout == httpx.TimeoutConfig(timeout=5.0)197198199def test_timeout_from_config_instance():200    timeout = httpx.TimeoutConfig(timeout=5.0)201    assert httpx.TimeoutConfig(timeout) == httpx.TimeoutConfig(timeout=5.0)202203204@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