Skip to content
Myr

test_config.py

This file

Path

tests/test_config.py

219 lines · 6.5 KiB · Python

Last changed by

Minor config cleanup (#621)Tom Christiebefore adoption3124a38

History of this file

Take it

The walk stopped after 200 commits: lines older than 2e91d8c 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 BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

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 BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

line 6why, one step back
6

Minor config cleanup (#621)Tom Christiebefore adoption3124a38

line 7why, one step back
7import certifi

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

lines 8–10why, one step back
8import pytest
910import httpx

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

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

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

lines 12–14why, one step back
12
1314def test_load_ssl_config():

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

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

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

lines 16–21why, one step back
16 context = ssl_config.load_ssl_context()
17 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED18 assert context.check_hostname is True192021def test_load_ssl_config_verify_non_existing_path():

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

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

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

lines 23–27why, one step back
23 with pytest.raises(IOError):
24 ssl_config.load_ssl_context()252627def test_load_ssl_config_verify_existing_file():

Minor config cleanup (#621)Tom Christiebefore adoption3124a38

line 28why, one step back
28 ssl_config = SSLConfig(verify=certifi.where())

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

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

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

lines 32–40why, one step back
32
3334@pytest.mark.parametrize("config", ("SSL_CERT_FILE", "SSL_CERT_DIR"))35def test_load_ssl_config_verify_env_file(https_server, ca_cert_pem_file, config):36 os.environ[config] = (37 ca_cert_pem_file38 if config.endswith("_FILE")39 else str(Path(ca_cert_pem_file).parent)40 )

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

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

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

lines 42–55why, one step back
42 context = ssl_config.load_ssl_context()
43 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED44 assert context.check_hostname is True45 assert ssl_config.verify == os.environ[config]4647 # Skipping 'SSL_CERT_DIR' functional test for now because48 # we're unable to get the certificate within the directory to49 # load into the SSLContext. :(50 if config == "SSL_CERT_FILE":51 host = https_server.url.host52 port = https_server.url.port53 conn = socket.create_connection((host, port))54 context.wrap_socket(conn, server_hostname=host)55 assert len(context.get_ca_certs()) == 1

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

lines 56–58why, one step back
56
5758def test_load_ssl_config_verify_directory():

Minor config cleanup (#621)Tom Christiebefore adoption3124a38

line 59why, one step back
59 path = Path(certifi.where()).parent

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

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

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

lines 61–66why, one step back
61 context = ssl_config.load_ssl_context()
62 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED63 assert context.check_hostname is True646566def 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 67why, one step back
67 ssl_config = SSLConfig(cert=(cert_pem_file, cert_private_key_file))

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

lines 68–76why, one step back
68 context = ssl_config.load_ssl_context()
69 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED70 assert context.check_hostname is True717273@pytest.mark.parametrize("password", [b"password", "password"])74def test_load_ssl_config_cert_and_encrypted_key(75 cert_pem_file, cert_encrypted_private_key_file, password76):

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

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

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

lines 78–87why, one step back
78 cert=(cert_pem_file, cert_encrypted_private_key_file, password)
79 )80 context = ssl_config.load_ssl_context()81 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED82 assert context.check_hostname is True838485def test_load_ssl_config_cert_and_key_invalid_password(86 cert_pem_file, cert_encrypted_private_key_file87):

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

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

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

lines 89–96why, one step back
89 cert=(cert_pem_file, cert_encrypted_private_key_file, "password1")
90 )9192 with pytest.raises(ssl.SSLError):93 ssl_config.load_ssl_context()949596def 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 97why, one step back
97 ssl_config = SSLConfig(cert=cert_pem_file)

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

lines 98–102why, one step back
98 with pytest.raises(ssl.SSLError):
99 ssl_config.load_ssl_context()100101102def test_load_ssl_config_no_verify():

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

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

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

lines 104–110why, one step back
104 context = ssl_config.load_ssl_context()
105 assert context.verify_mode == ssl.VerifyMode.CERT_NONE106 assert context.check_hostname is False107108109def test_load_ssl_context():110 ssl_context = ssl.create_default_context()

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

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

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

lines 112–118why, one step back
112
113 assert ssl_config.verify is True114 assert ssl_config.ssl_context is ssl_context115 assert repr(ssl_config) == "SSLConfig(cert=None, verify=True)"116117118def test_ssl_repr():

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

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

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

lines 120–122why, one step back
120 assert repr(ssl) == "SSLConfig(cert=None, verify=False)"
121122

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

lines 123–127why, one step back
123def test_ssl_eq():
124 ssl = SSLConfig(verify=False)125 assert ssl == SSLConfig(verify=False)126127

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

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

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

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

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

lines 131–132why, one step back
131
132

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

lines 133–137why, one step back
133def test_limits_eq():
134 limits = httpx.PoolLimits(hard_limit=100)135 assert limits == httpx.PoolLimits(hard_limit=100)136137

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

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

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

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

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

lines 141–142why, one step back
141
142

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

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

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

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

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

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

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

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

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

lines 149–150why, one step back
149
150

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

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

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

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

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

lines 154–156why, one step back
154
155156def test_timeout_from_one_none_value():

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

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

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

lines 159–160why, one step back
159
160

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

lines 161–170why, one step back
161def test_timeout_from_one_value():
162 timeout = httpx.Timeout(read_timeout=5.0)163 assert timeout == httpx.Timeout(timeout=(None, 5.0, None, None))164165166def test_timeout_from_one_value_and_default():167 timeout = httpx.Timeout(5.0, pool_timeout=60.0)168 assert timeout == httpx.Timeout(timeout=(5.0, 5.0, 5.0, 60.0))169170

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

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

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

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

Add BaseEvent interface (#260)Florimond Mancabefore adoption2e91d8c

lines 174–176why, one step back
174
175176def test_timeout_from_config_instance():

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

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

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

lines 179–181why, one step back
179
180181def test_timeout_repr():

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

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

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

line 184why, one step back
184

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

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

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

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

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

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

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

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

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

line 189why, one step back
189 )

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

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

Minor test fixes (#583)Tom Christiebefore adoption12d00b2

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

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

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

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

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

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

lines 202–210why, one step back
202 ssl_config.load_ssl_context()
203204 assert ssl_config.ssl_context.keylog_filename is None205206 filename = str(tmpdir.join("test.log"))207208 with monkeypatch.context() as m:209 m.setenv("SSLKEYLOGFILE", filename)210

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

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

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

lines 212–215why, one step back
212 ssl_config.load_ssl_context()
213214 assert ssl_config.ssl_context.keylog_filename == filename215

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

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

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

lines 217–219why, one step back
217 ssl_config.load_ssl_context()
218219 assert ssl_config.ssl_context.keylog_filename is None