Skip to content

Commit 4e074b8

Browse files
author
Can Sarigol
committed
Changed create_ssl_context with SSLConfig into the client implementation and tests
1 parent e0a2040 commit 4e074b8

File tree

3 files changed

+29
-53
lines changed

3 files changed

+29
-53
lines changed

httpx/_client.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
UNSET,
1414
PoolLimits,
1515
Proxy,
16-
SSLConfig,
1716
Timeout,
1817
UnsetType,
18+
create_ssl_context,
1919
)
2020
from ._content_streams import ContentStream
2121
from ._exceptions import HTTPError, InvalidURL, RequestBodyUnavailable, TooManyRedirects
@@ -505,9 +505,7 @@ def init_transport(
505505
if app is not None:
506506
return WSGITransport(app=app)
507507

508-
ssl_context = SSLConfig(
509-
verify=verify, cert=cert, trust_env=trust_env
510-
).ssl_context
508+
ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)
511509

512510
return httpcore.SyncConnectionPool(
513511
ssl_context=ssl_context,
@@ -525,9 +523,7 @@ def init_proxy_transport(
525523
pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
526524
trust_env: bool = True,
527525
) -> httpcore.SyncHTTPTransport:
528-
ssl_context = SSLConfig(
529-
verify=verify, cert=cert, trust_env=trust_env
530-
).ssl_context
526+
ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)
531527

532528
return httpcore.SyncHTTPProxy(
533529
proxy_url=proxy.url.raw,
@@ -1050,9 +1046,7 @@ def init_transport(
10501046
if app is not None:
10511047
return ASGITransport(app=app)
10521048

1053-
ssl_context = SSLConfig(
1054-
verify=verify, cert=cert, trust_env=trust_env
1055-
).ssl_context
1049+
ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)
10561050

10571051
return httpcore.AsyncConnectionPool(
10581052
ssl_context=ssl_context,
@@ -1070,9 +1064,7 @@ def init_proxy_transport(
10701064
pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
10711065
trust_env: bool = True,
10721066
) -> httpcore.AsyncHTTPTransport:
1073-
ssl_context = SSLConfig(
1074-
verify=verify, cert=cert, trust_env=trust_env
1075-
).ssl_context
1067+
ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)
10761068

10771069
return httpcore.AsyncHTTPProxy(
10781070
proxy_url=proxy.url.raw,

httpx/_transports/urllib3.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import httpcore
55

6-
from .._config import Proxy, SSLConfig
6+
from .._config import Proxy, create_ssl_context
77
from .._content_streams import ByteStream, IteratorStream
88
from .._types import CertTypes, VerifyTypes
99
from .._utils import as_network_error
@@ -31,12 +31,10 @@ def __init__(
3131
urllib3 is not None
3232
), "urllib3 must be installed in order to use URLLib3Transport"
3333

34-
ssl_config = SSLConfig(
35-
verify=verify, cert=cert, trust_env=trust_env, http2=False
36-
)
37-
3834
self.pool = urllib3.PoolManager(
39-
ssl_context=ssl_config.ssl_context,
35+
ssl_context=create_ssl_context(
36+
verify=verify, cert=cert, trust_env=trust_env, http2=False
37+
),
4038
num_pools=pool_connections,
4139
maxsize=pool_maxsize,
4240
block=pool_block,
@@ -137,14 +135,12 @@ def __init__(
137135
urllib3 is not None
138136
), "urllib3 must be installed in order to use URLLib3ProxyTransport"
139137

140-
ssl_config = SSLConfig(
141-
verify=verify, cert=cert, trust_env=trust_env, http2=False
142-
)
143-
144138
self.pool = urllib3.ProxyManager(
145139
proxy_url=proxy_url,
146140
proxy_headers=proxy_headers,
147-
ssl_context=ssl_config.ssl_context,
141+
ssl_context=create_ssl_context(
142+
verify=verify, cert=cert, trust_env=trust_env, http2=False
143+
),
148144
num_pools=pool_connections,
149145
maxsize=pool_maxsize,
150146
block=pool_block,

tests/test_config.py

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212

1313

1414
def test_load_ssl_config():
15-
ssl_config = SSLConfig()
16-
context = ssl_config.ssl_context
15+
context = httpx.create_ssl_context()
1716
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
1817
assert context.check_hostname is True
1918

@@ -24,8 +23,7 @@ def test_load_ssl_config_verify_non_existing_path():
2423

2524

2625
def test_load_ssl_config_verify_existing_file():
27-
ssl_config = SSLConfig(verify=certifi.where())
28-
context = ssl_config.ssl_context
26+
context = httpx.create_ssl_context(verify=certifi.where())
2927
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
3028
assert context.check_hostname is True
3129

@@ -38,10 +36,11 @@ def test_load_ssl_config_verify_env_file(https_server, ca_cert_pem_file, config)
3836
else str(Path(ca_cert_pem_file).parent)
3937
)
4038
ssl_config = SSLConfig(trust_env=True)
41-
context = ssl_config.ssl_context
39+
assert ssl_config.verify == os.environ[config]
40+
41+
context = httpx.create_ssl_context(trust_env=True)
4242
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
4343
assert context.check_hostname is True
44-
assert ssl_config.verify == os.environ[config]
4544

4645
# Skipping 'SSL_CERT_DIR' functional test for now because
4746
# we're unable to get the certificate within the directory to
@@ -56,15 +55,13 @@ def test_load_ssl_config_verify_env_file(https_server, ca_cert_pem_file, config)
5655

5756
def test_load_ssl_config_verify_directory():
5857
path = Path(certifi.where()).parent
59-
ssl_config = SSLConfig(verify=path)
60-
context = ssl_config.ssl_context
58+
context = httpx.create_ssl_context(verify=path)
6159
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
6260
assert context.check_hostname is True
6361

6462

6563
def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):
66-
ssl_config = SSLConfig(cert=(cert_pem_file, cert_private_key_file))
67-
context = ssl_config.ssl_context
64+
context = httpx.create_ssl_context(cert=(cert_pem_file, cert_private_key_file))
6865
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
6966
assert context.check_hostname is True
7067

@@ -73,10 +70,9 @@ def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):
7370
def test_load_ssl_config_cert_and_encrypted_key(
7471
cert_pem_file, cert_encrypted_private_key_file, password
7572
):
76-
ssl_config = SSLConfig(
73+
context = httpx.create_ssl_context(
7774
cert=(cert_pem_file, cert_encrypted_private_key_file, password)
7875
)
79-
context = ssl_config.ssl_context
8076
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
8177
assert context.check_hostname is True
8278

@@ -94,17 +90,16 @@ def test_load_ssl_config_cert_without_key_raises(cert_pem_file):
9490

9591

9692
def test_load_ssl_config_no_verify():
97-
ssl_config = SSLConfig(verify=False)
98-
context = ssl_config.ssl_context
93+
context = httpx.create_ssl_context(verify=False)
9994
assert context.verify_mode == ssl.VerifyMode.CERT_NONE
10095
assert context.check_hostname is False
10196

10297

10398
def test_load_ssl_context():
10499
ssl_context = ssl.create_default_context()
105-
ssl_config = SSLConfig(verify=ssl_context)
100+
context = httpx.create_ssl_context(verify=ssl_context)
106101

107-
assert ssl_config.ssl_context is ssl_context
102+
assert context is ssl_context
108103

109104

110105
def test_ssl_repr():
@@ -123,13 +118,6 @@ def test_create_ssl_context_with_get_request(server, cert_pem_file):
123118
assert response.status_code == 200
124119

125120

126-
def test_create_ssl_context():
127-
context = httpx.create_ssl_context()
128-
129-
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
130-
assert context.check_hostname is True
131-
132-
133121
def test_limits_repr():
134122
limits = httpx.PoolLimits(max_connections=100)
135123
assert repr(limits) == "PoolLimits(max_keepalive=None, max_connections=100)"
@@ -203,22 +191,22 @@ def test_ssl_config_support_for_keylog_file(tmpdir, monkeypatch): # pragma: noc
203191
with monkeypatch.context() as m:
204192
m.delenv("SSLKEYLOGFILE", raising=False)
205193

206-
ssl_config = SSLConfig(trust_env=True)
194+
context = httpx.create_ssl_context(trust_env=True)
207195

208-
assert ssl_config.ssl_context.keylog_filename is None
196+
assert context.keylog_filename is None
209197

210198
filename = str(tmpdir.join("test.log"))
211199

212200
with monkeypatch.context() as m:
213201
m.setenv("SSLKEYLOGFILE", filename)
214202

215-
ssl_config = SSLConfig(trust_env=True)
203+
context = httpx.create_ssl_context(trust_env=True)
216204

217-
assert ssl_config.ssl_context.keylog_filename == filename
205+
assert context.keylog_filename == filename
218206

219-
ssl_config = SSLConfig(trust_env=False)
207+
context = httpx.create_ssl_context(trust_env=False)
220208

221-
assert ssl_config.ssl_context.keylog_filename is None
209+
assert context.keylog_filename is None
222210

223211

224212
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)