Skip to content
Myr

test_config.py

This file

Path

tests/test_config.py

218 lines · 6.6 KiB · Python

Last changed by

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

History of this file

Take it

The walk stopped after 200 commits: lines older than 7156c36 point at it.

Add support for SSL_CERT_FILE and SSL_CERT_DIR (#307)Can Sarıgölbefore adoptionc9810a7

lines 1–2why, one step back
1import os
2import socket

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

line 3why, one step back
3import ssl

Add support for SSLKEYLOGFILE (#301)Tomás Faríasbefore adoptiondb7f2d0

line 4why, one step back
4import sys

Add support for SSL_CERT_FILE and SSL_CERT_DIR (#307)Can Sarıgölbefore adoptionc9810a7

line 5why, one step back
5from pathlib import Path

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

lines 6–9why, one step back
6
7import pytest89import httpx

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 10why, one step back
10from httpx.config import SSLConfig

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

lines 11–13why, one step back
11
1213def test_load_ssl_config():

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 14why, one step back
14 ssl_config = SSLConfig()

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

lines 15–20why, one step back
15 context = ssl_config.load_ssl_context()
16 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED17 assert context.check_hostname is True181920def test_load_ssl_config_verify_non_existing_path():

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 21why, one step back
21 ssl_config = SSLConfig(verify="/path/to/nowhere")

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

lines 22–26why, one step back
22 with pytest.raises(IOError):
23 ssl_config.load_ssl_context()242526def test_load_ssl_config_verify_existing_file():

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 27why, one step back
27 ssl_config = SSLConfig(verify=httpx.config.DEFAULT_CA_BUNDLE_PATH)

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

lines 28–30why, one step back
28 context = ssl_config.load_ssl_context()
29 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED30 assert context.check_hostname is True

Add support for SSL_CERT_FILE and SSL_CERT_DIR (#307)Can Sarıgölbefore adoptionc9810a7

lines 31–39why, one step back
31
3233@pytest.mark.parametrize("config", ("SSL_CERT_FILE", "SSL_CERT_DIR"))34def test_load_ssl_config_verify_env_file(https_server, ca_cert_pem_file, config):35 os.environ[config] = (36 ca_cert_pem_file37 if config.endswith("_FILE")38 else str(Path(ca_cert_pem_file).parent)39 )

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 40why, one step back
40 ssl_config = SSLConfig(trust_env=True)

Add support for SSL_CERT_FILE and SSL_CERT_DIR (#307)Can Sarıgölbefore adoptionc9810a7

lines 41–54why, one step back
41 context = ssl_config.load_ssl_context()
42 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED43 assert context.check_hostname is True44 assert ssl_config.verify == os.environ[config]4546 # Skipping 'SSL_CERT_DIR' functional test for now because47 # we're unable to get the certificate within the directory to48 # load into the SSLContext. :(49 if config == "SSL_CERT_FILE":50 host = https_server.url.host51 port = https_server.url.port52 conn = socket.create_connection((host, port))53 context.wrap_socket(conn, server_hostname=host)54 assert len(context.get_ca_certs()) == 1

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

lines 55–58why, one step back
55
5657def test_load_ssl_config_verify_directory():58 path = httpx.config.DEFAULT_CA_BUNDLE_PATH.parent

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 59why, one step back
59 ssl_config = SSLConfig(verify=path)

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

lines 60–65why, one step back
60 context = ssl_config.load_ssl_context()
61 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED62 assert context.check_hostname is True636465def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 66why, one step back
66 ssl_config = SSLConfig(cert=(cert_pem_file, cert_private_key_file))

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

lines 67–75why, one step back
67 context = ssl_config.load_ssl_context()
68 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED69 assert context.check_hostname is True707172@pytest.mark.parametrize("password", [b"password", "password"])73def test_load_ssl_config_cert_and_encrypted_key(74 cert_pem_file, cert_encrypted_private_key_file, password75):

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 76why, one step back
76 ssl_config = SSLConfig(

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

lines 77–86why, one step back
77 cert=(cert_pem_file, cert_encrypted_private_key_file, password)
78 )79 context = ssl_config.load_ssl_context()80 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED81 assert context.check_hostname is True828384def test_load_ssl_config_cert_and_key_invalid_password(85 cert_pem_file, cert_encrypted_private_key_file86):

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 87why, one step back
87 ssl_config = SSLConfig(

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

lines 88–95why, one step back
88 cert=(cert_pem_file, cert_encrypted_private_key_file, "password1")
89 )9091 with pytest.raises(ssl.SSLError):92 ssl_config.load_ssl_context()939495def test_load_ssl_config_cert_without_key_raises(cert_pem_file):

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 96why, one step back
96 ssl_config = SSLConfig(cert=cert_pem_file)

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

lines 97–101why, one step back
97 with pytest.raises(ssl.SSLError):
98 ssl_config.load_ssl_context()99100101def test_load_ssl_config_no_verify():

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 102why, one step back
102 ssl_config = SSLConfig(verify=False)

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

lines 103–107why, one step back
103 context = ssl_config.load_ssl_context()
104 assert context.verify_mode == ssl.VerifyMode.CERT_NONE105 assert context.check_hostname is False106107

Accept SSLContext into SSLConfig(verify=...) (#215)Seth Michael Larsonbefore adoptiondf8874b

lines 108–109why, one step back
108def test_load_ssl_context():
109 ssl_context = ssl.create_default_context()

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 110why, one step back
110 ssl_config = SSLConfig(verify=ssl_context)

Accept SSLContext into SSLConfig(verify=...) (#215)Seth Michael Larsonbefore adoptiondf8874b

lines 111–116why, one step back
111
112 assert ssl_config.verify is True113 assert ssl_config.ssl_context is ssl_context114 assert repr(ssl_config) == "SSLConfig(cert=None, verify=True)"115116

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

line 117why, one step back
117def test_ssl_repr():

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 118why, one step back
118 ssl = SSLConfig(verify=False)

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

lines 119–121why, one step back
119 assert repr(ssl) == "SSLConfig(cert=None, verify=False)"
120121

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

lines 122–126why, one step back
122def test_ssl_eq():
123 ssl = SSLConfig(verify=False)124 assert ssl == SSLConfig(verify=False)125126

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

lines 127–128why, one step back
127def test_limits_repr():
128 limits = httpx.PoolLimits(hard_limit=100)

Pool timeouts should be on the TimeoutConfig, not PoolLimits (#563)Tom Christiebefore adoption364378a

line 129why, one step back
129 assert repr(limits) == "PoolLimits(soft_limit=None, hard_limit=100)"

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

lines 130–131why, one step back
130
131

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

lines 132–136why, one step back
132def test_limits_eq():
133 limits = httpx.PoolLimits(hard_limit=100)134 assert limits == httpx.PoolLimits(hard_limit=100)135136

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

line 137why, one step back
137def test_timeout_eq():

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

lines 138–139why, one step back
138 timeout = httpx.Timeout(timeout=5.0)
139 assert timeout == httpx.Timeout(timeout=5.0)

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

lines 140–141why, one step back
140
141

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

line 142why, one step back
142def test_timeout_from_nothing():

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

line 143why, one step back
143 timeout = httpx.Timeout()

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

lines 144–146why, one step back
144 assert timeout.connect_timeout is None
145 assert timeout.read_timeout is None146 assert timeout.write_timeout is None

Pool timeouts should be on the TimeoutConfig, not PoolLimits (#563)Tom Christiebefore adoption364378a

line 147why, one step back
147 assert timeout.pool_timeout is None

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

lines 148–149why, one step back
148
149

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

line 150why, one step back
150def test_timeout_from_none():

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

lines 151–152why, one step back
151 timeout = httpx.Timeout(timeout=None)
152 assert timeout == httpx.Timeout()

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

lines 153–155why, one step back
153
154155def test_timeout_from_one_none_value():

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

lines 156–157why, one step back
156 timeout = httpx.Timeout(read_timeout=None)
157 assert timeout == httpx.Timeout()

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

lines 158–159why, one step back
158
159

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

lines 160–169why, one step back
160def test_timeout_from_one_value():
161 timeout = httpx.Timeout(read_timeout=5.0)162 assert timeout == httpx.Timeout(timeout=(None, 5.0, None, None))163164165def test_timeout_from_one_value_and_default():166 timeout = httpx.Timeout(5.0, pool_timeout=60.0)167 assert timeout == httpx.Timeout(timeout=(5.0, 5.0, 5.0, 60.0))168169

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

line 170why, one step back
170def test_timeout_from_tuple():

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

lines 171–172why, one step back
171 timeout = httpx.Timeout(timeout=(5.0, 5.0, 5.0, 5.0))
172 assert timeout == httpx.Timeout(timeout=5.0)

Add additional info to Contributing Guide (#213)Humberto Rochabefore adoption7156c36

lines 173–175why, one step back
173
174175def test_timeout_from_config_instance():

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

lines 176–177why, one step back
176 timeout = httpx.Timeout(timeout=5.0)
177 assert httpx.Timeout(timeout) == httpx.Timeout(timeout=5.0)

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

lines 178–180why, one step back
178
179180def test_timeout_repr():

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

lines 181–182why, one step back
181 timeout = httpx.Timeout(timeout=5.0)
182 assert repr(timeout) == "Timeout(timeout=5.0)"

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

line 183why, one step back
183

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

line 184why, one step back
184 timeout = httpx.Timeout(read_timeout=5.0)

Pool timeouts should be on the TimeoutConfig, not PoolLimits (#563)Tom Christiebefore adoption364378a

line 185why, one step back
185 assert repr(timeout) == (

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

line 186why, one step back
186 "Timeout(connect_timeout=None, read_timeout=5.0, "

Pool timeouts should be on the TimeoutConfig, not PoolLimits (#563)Tom Christiebefore adoption364378a

line 187why, one step back
187 "write_timeout=None, pool_timeout=None)"

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

line 188why, one step back
188 )

Add support for SSLKEYLOGFILE (#301)Tomás Faríasbefore adoptiondb7f2d0

lines 189–195why, one step back
189
190191@pytest.mark.skipif(192 not hasattr(ssl.SSLContext, "keylog_filename"),193 reason="requires OpenSSL 1.1.1 or higher",194)195@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")

Minor test fixes (#583)Tom Christiebefore adoption12d00b2

line 196why, one step back
196def test_ssl_config_support_for_keylog_file(tmpdir, monkeypatch): # pragma: nocover

Add support for SSLKEYLOGFILE (#301)Tomás Faríasbefore adoptiondb7f2d0

lines 197–199why, one step back
197 with monkeypatch.context() as m:
198 m.delenv("SSLKEYLOGFILE", raising=False)199

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 200why, one step back
200 ssl_config = SSLConfig(trust_env=True)

Add support for SSLKEYLOGFILE (#301)Tomás Faríasbefore adoptiondb7f2d0

lines 201–209why, one step back
201 ssl_config.load_ssl_context()
202203 assert ssl_config.ssl_context.keylog_filename is None204205 filename = str(tmpdir.join("test.log"))206207 with monkeypatch.context() as m:208 m.setenv("SSLKEYLOGFILE", filename)209

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 210why, one step back
210 ssl_config = SSLConfig(trust_env=True)

Add support for SSLKEYLOGFILE (#301)Tomás Faríasbefore adoptiondb7f2d0

lines 211–214why, one step back
211 ssl_config.load_ssl_context()
212213 assert ssl_config.ssl_context.keylog_filename == filename214

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 215why, one step back
215 ssl_config = SSLConfig(trust_env=False)

Add support for SSLKEYLOGFILE (#301)Tomás Faríasbefore adoptiondb7f2d0

lines 216–218why, one step back
216 ssl_config.load_ssl_context()
217218 assert ssl_config.ssl_context.keylog_filename is None