test_config.py
This file
Path
tests/test_config.py
134 lines · 4.0 KiB · Python
Last changed by
Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
History of this fileTake it
1import os2import ssl34import pytest56import httpx789@pytest.mark.asyncio10async def test_load_ssl_config():11 ssl_config = httpx.SSLConfig()12 context = await ssl_config.load_ssl_context()13 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED14 assert context.check_hostname is True151617@pytest.mark.asyncio18async def test_load_ssl_config_verify_non_existing_path():19 ssl_config = httpx.SSLConfig(verify="/path/to/nowhere")20 with pytest.raises(IOError):21 await ssl_config.load_ssl_context()222324@pytest.mark.asyncio25async def test_load_ssl_config_verify_existing_file():26 ssl_config = httpx.SSLConfig(verify=httpx.config.DEFAULT_CA_BUNDLE_PATH)27 context = await ssl_config.load_ssl_context()28 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED29 assert context.check_hostname is True303132@pytest.mark.asyncio33async def test_load_ssl_config_verify_directory():34 path = os.path.dirname(httpx.config.DEFAULT_CA_BUNDLE_PATH)35 ssl_config = httpx.SSLConfig(verify=path)36 context = await ssl_config.load_ssl_context()37 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED38 assert context.check_hostname is True394041@pytest.mark.asyncio42async def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):43 ssl_config = httpx.SSLConfig(cert=(cert_pem_file, cert_private_key_file))44 context = await ssl_config.load_ssl_context()45 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED46 assert context.check_hostname is True474849@pytest.mark.asyncio50@pytest.mark.parametrize("password", [b"password", "password"])51async def test_load_ssl_config_cert_and_encrypted_key(52 cert_pem_file, cert_encrypted_private_key_file, password53):54 ssl_config = httpx.SSLConfig(55 cert=(cert_pem_file, cert_encrypted_private_key_file, password)56 )57 context = await ssl_config.load_ssl_context()58 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED59 assert context.check_hostname is True606162@pytest.mark.asyncio63async def test_load_ssl_config_cert_and_key_invalid_password(64 cert_pem_file, cert_encrypted_private_key_file65):66 ssl_config = httpx.SSLConfig(67 cert=(cert_pem_file, cert_encrypted_private_key_file, "password1")68 )6970 with pytest.raises(ssl.SSLError):71 await ssl_config.load_ssl_context()727374@pytest.mark.asyncio75async def test_load_ssl_config_cert_without_key_raises(cert_pem_file):76 ssl_config = httpx.SSLConfig(cert=cert_pem_file)77 with pytest.raises(ssl.SSLError):78 await ssl_config.load_ssl_context()798081@pytest.mark.asyncio82async def test_load_ssl_config_no_verify():83 ssl_config = httpx.SSLConfig(verify=False)84 context = await ssl_config.load_ssl_context()85 assert context.verify_mode == ssl.VerifyMode.CERT_NONE86 assert context.check_hostname is False878889def test_ssl_repr():90 ssl = httpx.SSLConfig(verify=False)91 assert repr(ssl) == "SSLConfig(cert=None, verify=False)"929394def test_timeout_repr():95 timeout = httpx.TimeoutConfig(timeout=5.0)96 assert repr(timeout) == "TimeoutConfig(timeout=5.0)"9798 timeout = httpx.TimeoutConfig(read_timeout=5.0)99 assert (100 repr(timeout)101 == "TimeoutConfig(connect_timeout=None, read_timeout=5.0, write_timeout=None)"102 )103104105def test_limits_repr():106 limits = httpx.PoolLimits(hard_limit=100)107 assert (108 repr(limits) == "PoolLimits(soft_limit=None, hard_limit=100, pool_timeout=None)"109 )110111112def test_ssl_eq():113 ssl = httpx.SSLConfig(verify=False)114 assert ssl == httpx.SSLConfig(verify=False)115116117def test_timeout_eq():118 timeout = httpx.TimeoutConfig(timeout=5.0)119 assert timeout == httpx.TimeoutConfig(timeout=5.0)120121122def test_limits_eq():123 limits = httpx.PoolLimits(hard_limit=100)124 assert limits == httpx.PoolLimits(hard_limit=100)125126127def test_timeout_from_tuple():128 timeout = httpx.TimeoutConfig(timeout=(5.0, 5.0, 5.0))129 assert timeout == httpx.TimeoutConfig(timeout=5.0)130131132def test_timeout_from_config_instance():133 timeout = httpx.TimeoutConfig(timeout=5.0)134 assert httpx.TimeoutConfig(timeout) == httpx.TimeoutConfig(timeout=5.0)