UNSET,
PoolLimits,
Proxy,
- SSLConfig,
Timeout,
UnsetType,
+ create_ssl_context,
)
from ._content_streams import ContentStream
from ._exceptions import (
if app is not None:
return WSGITransport(app=app)
- ssl_context = SSLConfig(
- verify=verify, cert=cert, trust_env=trust_env
- ).ssl_context
+ ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)
return httpcore.SyncConnectionPool(
ssl_context=ssl_context,
pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
trust_env: bool = True,
) -> httpcore.SyncHTTPTransport:
- ssl_context = SSLConfig(
- verify=verify, cert=cert, trust_env=trust_env
- ).ssl_context
+ ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)
return httpcore.SyncHTTPProxy(
proxy_url=proxy.url.raw,
if app is not None:
return ASGITransport(app=app)
- ssl_context = SSLConfig(
- verify=verify, cert=cert, trust_env=trust_env
- ).ssl_context
+ ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)
return httpcore.AsyncConnectionPool(
ssl_context=ssl_context,
pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
trust_env: bool = True,
) -> httpcore.AsyncHTTPTransport:
- ssl_context = SSLConfig(
- verify=verify, cert=cert, trust_env=trust_env
- ).ssl_context
+ ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)
return httpcore.AsyncHTTPProxy(
proxy_url=proxy.url.raw,
import httpcore
-from .._config import SSLConfig
+from .._config import create_ssl_context
from .._content_streams import ByteStream, IteratorStream
from .._exceptions import NetworkError, map_exceptions
from .._types import CertTypes, VerifyTypes
urllib3 is not None
), "urllib3 must be installed in order to use URLLib3Transport"
- ssl_config = SSLConfig(
- verify=verify, cert=cert, trust_env=trust_env, http2=False
- )
-
self.pool = urllib3.PoolManager(
- ssl_context=ssl_config.ssl_context,
+ ssl_context=create_ssl_context(
+ verify=verify, cert=cert, trust_env=trust_env, http2=False
+ ),
num_pools=pool_connections,
maxsize=pool_maxsize,
block=pool_block,
urllib3 is not None
), "urllib3 must be installed in order to use URLLib3ProxyTransport"
- ssl_config = SSLConfig(
- verify=verify, cert=cert, trust_env=trust_env, http2=False
- )
-
self.pool = urllib3.ProxyManager(
proxy_url=proxy_url,
proxy_headers=proxy_headers,
- ssl_context=ssl_config.ssl_context,
+ ssl_context=create_ssl_context(
+ verify=verify, cert=cert, trust_env=trust_env, http2=False
+ ),
num_pools=pool_connections,
maxsize=pool_maxsize,
block=pool_block,
import os
-import socket
import ssl
import sys
from pathlib import Path
import pytest
import httpx
-from httpx._config import SSLConfig
def test_load_ssl_config():
- ssl_config = SSLConfig()
- context = ssl_config.ssl_context
+ context = httpx.create_ssl_context()
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
assert context.check_hostname is True
def test_load_ssl_config_verify_non_existing_path():
with pytest.raises(IOError):
- SSLConfig(verify="/path/to/nowhere")
+ httpx.create_ssl_context(verify="/path/to/nowhere")
def test_load_ssl_config_verify_existing_file():
- ssl_config = SSLConfig(verify=certifi.where())
- context = ssl_config.ssl_context
+ context = httpx.create_ssl_context(verify=certifi.where())
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
assert context.check_hostname is True
@pytest.mark.parametrize("config", ("SSL_CERT_FILE", "SSL_CERT_DIR"))
-def test_load_ssl_config_verify_env_file(https_server, ca_cert_pem_file, config):
+def test_load_ssl_config_verify_env_file(
+ https_server, ca_cert_pem_file, config, cert_authority
+):
os.environ[config] = (
ca_cert_pem_file
if config.endswith("_FILE")
else str(Path(ca_cert_pem_file).parent)
)
- ssl_config = SSLConfig(trust_env=True)
- context = ssl_config.ssl_context
+ context = httpx.create_ssl_context(trust_env=True)
+ cert_authority.configure_trust(context)
+
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
assert context.check_hostname is True
- assert ssl_config.verify == os.environ[config]
-
- # Skipping 'SSL_CERT_DIR' functional test for now because
- # we're unable to get the certificate within the directory to
- # load into the SSLContext. :(
- if config == "SSL_CERT_FILE":
- host = https_server.url.host
- port = https_server.url.port
- conn = socket.create_connection((host, port))
- context.wrap_socket(conn, server_hostname=host)
- assert len(context.get_ca_certs()) == 1
+ assert len(context.get_ca_certs()) == 1
def test_load_ssl_config_verify_directory():
path = Path(certifi.where()).parent
- ssl_config = SSLConfig(verify=str(path))
- context = ssl_config.ssl_context
+ context = httpx.create_ssl_context(verify=str(path))
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
assert context.check_hostname is True
def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):
- ssl_config = SSLConfig(cert=(cert_pem_file, cert_private_key_file))
- context = ssl_config.ssl_context
+ context = httpx.create_ssl_context(cert=(cert_pem_file, cert_private_key_file))
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
assert context.check_hostname is True
def test_load_ssl_config_cert_and_encrypted_key(
cert_pem_file, cert_encrypted_private_key_file, password
):
- ssl_config = SSLConfig(
+ context = httpx.create_ssl_context(
cert=(cert_pem_file, cert_encrypted_private_key_file, password)
)
- context = ssl_config.ssl_context
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
assert context.check_hostname is True
cert_pem_file, cert_encrypted_private_key_file
):
with pytest.raises(ssl.SSLError):
- SSLConfig(cert=(cert_pem_file, cert_encrypted_private_key_file, "password1"))
+ httpx.create_ssl_context(
+ cert=(cert_pem_file, cert_encrypted_private_key_file, "password1")
+ )
def test_load_ssl_config_cert_without_key_raises(cert_pem_file):
with pytest.raises(ssl.SSLError):
- SSLConfig(cert=cert_pem_file)
+ httpx.create_ssl_context(cert=cert_pem_file)
def test_load_ssl_config_no_verify():
- ssl_config = SSLConfig(verify=False)
- context = ssl_config.ssl_context
+ context = httpx.create_ssl_context(verify=False)
assert context.verify_mode == ssl.VerifyMode.CERT_NONE
assert context.check_hostname is False
def test_load_ssl_context():
ssl_context = ssl.create_default_context()
- ssl_config = SSLConfig(verify=ssl_context)
-
- assert ssl_config.ssl_context is ssl_context
-
+ context = httpx.create_ssl_context(verify=ssl_context)
-def test_ssl_repr():
- ssl = SSLConfig(verify=False)
- assert repr(ssl) == "SSLConfig(cert=None, verify=False)"
+ assert context is ssl_context
-def test_ssl_eq():
- ssl = SSLConfig(verify=False)
- assert ssl == SSLConfig(verify=False)
+def test_create_ssl_context_with_get_request(server, cert_pem_file):
+ context = httpx.create_ssl_context(verify=cert_pem_file)
+ response = httpx.get(server.url, verify=context)
+ assert response.status_code == 200
def test_limits_repr():
with monkeypatch.context() as m:
m.delenv("SSLKEYLOGFILE", raising=False)
- ssl_config = SSLConfig(trust_env=True)
+ context = httpx.create_ssl_context(trust_env=True)
- assert ssl_config.ssl_context.keylog_filename is None # type: ignore
+ assert context.keylog_filename is None # type: ignore
filename = str(tmpdir.join("test.log"))
with monkeypatch.context() as m:
m.setenv("SSLKEYLOGFILE", filename)
- ssl_config = SSLConfig(trust_env=True)
+ context = httpx.create_ssl_context(trust_env=True)
- assert ssl_config.ssl_context.keylog_filename == filename # type: ignore
+ assert context.keylog_filename == filename # type: ignore
- ssl_config = SSLConfig(trust_env=False)
+ context = httpx.create_ssl_context(trust_env=False)
- assert ssl_config.ssl_context.keylog_filename is None # type: ignore
+ assert context.keylog_filename is None # type: ignore
@pytest.mark.parametrize(