Skip to content
Myr

test_config.py

This file

Path

tests/test_config.py

247 lines · 7.5 KiB · Python

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than fb17459 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

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

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

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

lines 6–11why, one step back
6
7import pytest89import httpx1011

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

line 12why, one step back
12def test_load_ssl_config():

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

line 13why, one step back
13 ssl_config = httpx.SSLConfig()

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

line 14why, one step back
14 context = ssl_config.load_ssl_context()

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

lines 15–18why, one step back
15 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
16 assert context.check_hostname is True1718

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

line 19why, one step back
19def test_load_ssl_config_verify_non_existing_path():

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

lines 20–21why, one step back
20 ssl_config = httpx.SSLConfig(verify="/path/to/nowhere")
21 with pytest.raises(IOError):

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

line 22why, one step back
22 ssl_config.load_ssl_context()

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

lines 23–24why, one step back
23
24

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

line 25why, one step back
25def test_load_ssl_config_verify_existing_file():

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

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

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

line 27why, one step back
27 context = ssl_config.load_ssl_context()

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

lines 28–29why, one step back
28 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
29 assert context.check_hostname is True

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

lines 30–53why, one step back
30
3132@pytest.mark.parametrize("config", ("SSL_CERT_FILE", "SSL_CERT_DIR"))33def test_load_ssl_config_verify_env_file(https_server, ca_cert_pem_file, config):34 os.environ[config] = (35 ca_cert_pem_file36 if config.endswith("_FILE")37 else str(Path(ca_cert_pem_file).parent)38 )39 ssl_config = httpx.SSLConfig(trust_env=True)40 context = ssl_config.load_ssl_context()41 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED42 assert context.check_hostname is True43 assert ssl_config.verify == os.environ[config]4445 # Skipping 'SSL_CERT_DIR' functional test for now because46 # we're unable to get the certificate within the directory to47 # load into the SSLContext. :(48 if config == "SSL_CERT_FILE":49 host = https_server.url.host50 port = https_server.url.port51 conn = socket.create_connection((host, port))52 context.wrap_socket(conn, server_hostname=host)53 assert len(context.get_ca_certs()) == 1

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

lines 54–55why, one step back
54
55

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

line 56why, one step back
56def test_load_ssl_config_verify_directory():

Use pathlib.Path instead of os.path functions (#188)Stephen Brown IIbefore adoptionfde30b8

line 57why, one step back
57 path = httpx.config.DEFAULT_CA_BUNDLE_PATH.parent

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

line 58why, one step back
58 ssl_config = httpx.SSLConfig(verify=path)

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

line 59why, one step back
59 context = ssl_config.load_ssl_context()

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

lines 60–63why, one step back
60 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
61 assert context.check_hostname is True6263

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

line 64why, one step back
64def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

line 65why, one step back
65 ssl_config = httpx.SSLConfig(cert=(cert_pem_file, cert_private_key_file))

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

line 66why, one step back
66 context = ssl_config.load_ssl_context()

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

lines 67–71why, one step back
67 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
68 assert context.check_hostname is True697071@pytest.mark.parametrize("password", [b"password", "password"])

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

line 72why, one step back
72def test_load_ssl_config_cert_and_encrypted_key(

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

lines 73–77why, one step back
73 cert_pem_file, cert_encrypted_private_key_file, password
74):75 ssl_config = httpx.SSLConfig(76 cert=(cert_pem_file, cert_encrypted_private_key_file, password)77 )

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

line 78why, one step back
78 context = ssl_config.load_ssl_context()

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

lines 79–82why, one step back
79 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
80 assert context.check_hostname is True8182

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

line 83why, one step back
83def test_load_ssl_config_cert_and_key_invalid_password(

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

lines 84–90why, one step back
84 cert_pem_file, cert_encrypted_private_key_file
85):86 ssl_config = httpx.SSLConfig(87 cert=(cert_pem_file, cert_encrypted_private_key_file, "password1")88 )8990 with pytest.raises(ssl.SSLError):

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

line 91why, one step back
91 ssl_config.load_ssl_context()

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

lines 92–93why, one step back
92
93

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

line 94why, one step back
94def test_load_ssl_config_cert_without_key_raises(cert_pem_file):

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

lines 95–96why, one step back
95 ssl_config = httpx.SSLConfig(cert=cert_pem_file)
96 with pytest.raises(ssl.SSLError):

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

line 97why, one step back
97 ssl_config.load_ssl_context()

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

lines 98–99why, one step back
98
99

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

line 100why, one step back
100def test_load_ssl_config_no_verify():

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

line 101why, one step back
101 ssl_config = httpx.SSLConfig(verify=False)

Run SSLConfig cert loading in threadpool (#209)Can Sarıgölbefore adoptionc5248e6

line 102why, one step back
102 context = ssl_config.load_ssl_context()

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

lines 103–106why, one step back
103 assert context.verify_mode == ssl.VerifyMode.CERT_NONE
104 assert context.check_hostname is False105106

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

lines 107–115why, one step back
107def test_load_ssl_context():
108 ssl_context = ssl.create_default_context()109 ssl_config = httpx.SSLConfig(verify=ssl_context)110111 assert ssl_config.verify is True112 assert ssl_config.ssl_context is ssl_context113 assert repr(ssl_config) == "SSLConfig(cert=None, verify=True)"114115

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

lines 116–120why, one step back
116def test_ssl_repr():
117 ssl = httpx.SSLConfig(verify=False)118 assert repr(ssl) == "SSLConfig(cert=None, verify=False)"119120

Merge pull request #241 from encode/protocol-supportTom Christiebefore adoptione0b523d

lines 121–145why, one step back
121def test_http_versions_repr():
122 http_versions = httpx.HTTPVersionConfig()123 assert repr(http_versions) == "HTTPVersionConfig(['HTTP/1.1', 'HTTP/2'])"124125126def test_http_versions_from_string():127 http_versions = httpx.HTTPVersionConfig("HTTP/1.1")128 assert repr(http_versions) == "HTTPVersionConfig(['HTTP/1.1'])"129130131def test_http_versions_from_list():132 http_versions = httpx.HTTPVersionConfig(["http/1.1"])133 assert repr(http_versions) == "HTTPVersionConfig(['HTTP/1.1'])"134135136def test_http_versions_from_config():137 http_versions = httpx.HTTPVersionConfig(httpx.HTTPVersionConfig("HTTP/1.1"))138 assert repr(http_versions) == "HTTPVersionConfig(['HTTP/1.1'])"139140141def test_invalid_http_version():142 with pytest.raises(ValueError):143 httpx.HTTPVersionConfig("HTTP/9")144145

Raise TypeError if invalid parameter for HTTPVersionConfig (#253)Can Sarıgölbefore adoption4ea4af6

lines 146–155why, one step back
146def test_invalid_http_version_type():
147 with pytest.raises(TypeError):148 httpx.HTTPVersionConfig(123)149150151def test_invalid_http_version_list_type():152 with pytest.raises(ValueError):153 httpx.HTTPVersionConfig([123])154155

Merge pull request #241 from encode/protocol-supportTom Christiebefore adoptione0b523d

lines 156–160why, one step back
156def test_empty_http_version():
157 with pytest.raises(ValueError):158 httpx.HTTPVersionConfig([])159160

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

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

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

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

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

lines 164–170why, one step back
164
165166def test_ssl_eq():167 ssl = httpx.SSLConfig(verify=False)168 assert ssl == httpx.SSLConfig(verify=False)169170

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

lines 171–175why, one step back
171def test_limits_eq():
172 limits = httpx.PoolLimits(hard_limit=100)173 assert limits == httpx.PoolLimits(hard_limit=100)174175

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

lines 176–180why, one step back
176def test_timeout_eq():
177 timeout = httpx.TimeoutConfig(timeout=5.0)178 assert timeout == httpx.TimeoutConfig(timeout=5.0)179180

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

lines 181–185why, one step back
181def test_timeout_from_nothing():
182 timeout = httpx.TimeoutConfig()183 assert timeout.connect_timeout is None184 assert timeout.read_timeout is None185 assert timeout.write_timeout is None

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

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

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

lines 187–188why, one step back
187
188

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

lines 189–198why, one step back
189def test_timeout_from_none():
190 timeout = httpx.TimeoutConfig(timeout=None)191 assert timeout == httpx.TimeoutConfig()192193194def test_timeout_from_one_none_value():195 timeout = httpx.TimeoutConfig(read_timeout=None)196 assert timeout == httpx.TimeoutConfig()197198

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

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

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

line 200why, one step back
200 timeout = httpx.TimeoutConfig(timeout=(5.0, 5.0, 5.0, 5.0))

Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459

lines 201–206why, one step back
201 assert timeout == httpx.TimeoutConfig(timeout=5.0)
202203204def test_timeout_from_config_instance():205 timeout = httpx.TimeoutConfig(timeout=5.0)206 assert httpx.TimeoutConfig(timeout) == httpx.TimeoutConfig(timeout=5.0)

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

lines 207–213why, one step back
207
208209def test_timeout_repr():210 timeout = httpx.TimeoutConfig(timeout=5.0)211 assert repr(timeout) == "TimeoutConfig(timeout=5.0)"212213 timeout = httpx.TimeoutConfig(read_timeout=5.0)

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

lines 214–216why, one step back
214 assert repr(timeout) == (
215 "TimeoutConfig(connect_timeout=None, read_timeout=5.0, "216 "write_timeout=None, pool_timeout=None)"

Reorganize timeout config tests (#491)Florimond Mancabefore adoptione3140a0

line 217why, one step back
217 )

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

lines 218–247why, one step back
218
219220@pytest.mark.skipif(221 not hasattr(ssl.SSLContext, "keylog_filename"),222 reason="requires OpenSSL 1.1.1 or higher",223)224@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")225def test_ssl_config_support_for_keylog_file(tmpdir, monkeypatch):226 with monkeypatch.context() as m:227 m.delenv("SSLKEYLOGFILE", raising=False)228229 ssl_config = httpx.SSLConfig(trust_env=True)230 ssl_config.load_ssl_context()231232 assert ssl_config.ssl_context.keylog_filename is None233234 filename = str(tmpdir.join("test.log"))235236 with monkeypatch.context() as m:237 m.setenv("SSLKEYLOGFILE", filename)238239 ssl_config = httpx.SSLConfig(trust_env=True)240 ssl_config.load_ssl_context()241242 assert ssl_config.ssl_context.keylog_filename == filename243244 ssl_config = httpx.SSLConfig(trust_env=False)245 ssl_config.load_ssl_context()246247 assert ssl_config.ssl_context.keylog_filename is None