Skip to content
Myr

test_config.py

This file

Path

tests/test_config.py

241 lines · 7.1 KiB · Python

Last changed by

Rename pool limit options (#968)Tom Christiebefore adoption991915a

History of this file

Take it

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

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 1–6why, one step back
1import os
2import socket3import ssl4import sys5from pathlib import Path6

Minor config cleanup (#621)Tom Christiebefore adoption3124a38

line 7why, one step back
7import certifi

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

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

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

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

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

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()

Load SSL Context on init (#709)Tom Christiebefore adoption79a9748

line 16why, one step back
16 context = ssl_config.ssl_context

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 17–22why, one step back
17 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
18 assert context.check_hostname is True192021def test_load_ssl_config_verify_non_existing_path():22 with pytest.raises(IOError):

Load SSL Context on init (#709)Tom Christiebefore adoption79a9748

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

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 24–26why, one step back
24
2526def test_load_ssl_config_verify_existing_file():

Minor config cleanup (#621)Tom Christiebefore adoption3124a38

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

Load SSL Context on init (#709)Tom Christiebefore adoption79a9748

line 28why, one step back
28 context = ssl_config.ssl_context

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 29–39why, one step back
29 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
30 assert context.check_hostname is True313233@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)

Load SSL Context on init (#709)Tom Christiebefore adoption79a9748

line 41why, one step back
41 context = ssl_config.ssl_context

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 42–57why, one step back
42 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
43 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()) == 1555657def test_load_ssl_config_verify_directory():

Minor config cleanup (#621)Tom Christiebefore adoption3124a38

line 58why, one step back
58 path = Path(certifi.where()).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)

Load SSL Context on init (#709)Tom Christiebefore adoption79a9748

line 60why, one step back
60 context = ssl_config.ssl_context

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 61–65why, one step back
61 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
62 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))

Load SSL Context on init (#709)Tom Christiebefore adoption79a9748

line 67why, one step back
67 context = ssl_config.ssl_context

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 68–75why, one step back
68 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
69 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(

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 77–78why, one step back
77 cert=(cert_pem_file, cert_encrypted_private_key_file, password)
78 )

Load SSL Context on init (#709)Tom Christiebefore adoption79a9748

line 79why, one step back
79 context = ssl_config.ssl_context

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 80–87why, one step back
80 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
81 assert context.check_hostname is True828384def test_load_ssl_config_cert_and_key_invalid_password(85 cert_pem_file, cert_encrypted_private_key_file86):87 with pytest.raises(ssl.SSLError):

Load SSL Context on init (#709)Tom Christiebefore adoption79a9748

line 88why, one step back
88 SSLConfig(cert=(cert_pem_file, cert_encrypted_private_key_file, "password1"))

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 89–92why, one step back
89
9091def test_load_ssl_config_cert_without_key_raises(cert_pem_file):92 with pytest.raises(ssl.SSLError):

Load SSL Context on init (#709)Tom Christiebefore adoption79a9748

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

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 94–96why, one step back
94
9596def test_load_ssl_config_no_verify():

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

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

Load SSL Context on init (#709)Tom Christiebefore adoption79a9748

line 98why, one step back
98 context = ssl_config.ssl_context

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 99–104why, one step back
99 assert context.verify_mode == ssl.VerifyMode.CERT_NONE
100 assert context.check_hostname is False101102103def test_load_ssl_context():104 ssl_context = ssl.create_default_context()

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

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

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 106–110why, one step back
106
107 assert ssl_config.ssl_context is ssl_context108109110def test_ssl_repr():

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

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

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 112–114why, one step back
112 assert repr(ssl) == "SSLConfig(cert=None, verify=False)"
113114

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

lines 115–119why, one step back
115def test_ssl_eq():
116 ssl = SSLConfig(verify=False)117 assert ssl == SSLConfig(verify=False)118119

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

line 120why, one step back
120def test_limits_repr():

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 121–122why, one step back
121 limits = httpx.PoolLimits(max_connections=100)
122 assert repr(limits) == "PoolLimits(max_keepalive=None, max_connections=100)"

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 123–125why, one step back
123
124125def test_limits_eq():

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 126–127why, one step back
126 limits = httpx.PoolLimits(max_connections=100)
127 assert limits == httpx.PoolLimits(max_connections=100)

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 128–192why, one step back
128
129130def test_timeout_eq():131 timeout = httpx.Timeout(timeout=5.0)132 assert timeout == httpx.Timeout(timeout=5.0)133134135def test_timeout_from_nothing():136 timeout = httpx.Timeout()137 assert timeout.connect_timeout is None138 assert timeout.read_timeout is None139 assert timeout.write_timeout is None140 assert timeout.pool_timeout is None141142143def test_timeout_from_none():144 timeout = httpx.Timeout(timeout=None)145 assert timeout == httpx.Timeout()146147148def test_timeout_from_one_none_value():149 timeout = httpx.Timeout(read_timeout=None)150 assert timeout == httpx.Timeout()151152153def test_timeout_from_one_value():154 timeout = httpx.Timeout(read_timeout=5.0)155 assert timeout == httpx.Timeout(timeout=(None, 5.0, None, None))156157158def test_timeout_from_one_value_and_default():159 timeout = httpx.Timeout(5.0, pool_timeout=60.0)160 assert timeout == httpx.Timeout(timeout=(5.0, 5.0, 5.0, 60.0))161162163def test_timeout_from_tuple():164 timeout = httpx.Timeout(timeout=(5.0, 5.0, 5.0, 5.0))165 assert timeout == httpx.Timeout(timeout=5.0)166167168def test_timeout_from_config_instance():169 timeout = httpx.Timeout(timeout=5.0)170 assert httpx.Timeout(timeout) == httpx.Timeout(timeout=5.0)171172173def test_timeout_repr():174 timeout = httpx.Timeout(timeout=5.0)175 assert repr(timeout) == "Timeout(timeout=5.0)"176177 timeout = httpx.Timeout(read_timeout=5.0)178 assert repr(timeout) == (179 "Timeout(connect_timeout=None, read_timeout=5.0, "180 "write_timeout=None, pool_timeout=None)"181 )182183184@pytest.mark.skipif(185 not hasattr(ssl.SSLContext, "keylog_filename"),186 reason="requires OpenSSL 1.1.1 or higher",187)188@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")189def test_ssl_config_support_for_keylog_file(tmpdir, monkeypatch): # pragma: nocover190 with monkeypatch.context() as m:191 m.delenv("SSLKEYLOGFILE", raising=False)192

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

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

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 194–201why, one step back
194
195 assert ssl_config.ssl_context.keylog_filename is None196197 filename = str(tmpdir.join("test.log"))198199 with monkeypatch.context() as m:200 m.setenv("SSLKEYLOGFILE", filename)201

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

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

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 203–205why, one step back
203
204 assert ssl_config.ssl_context.keylog_filename == filename205

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

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

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 207–208why, one step back
207
208 assert ssl_config.ssl_context.keylog_filename is None

Prep for introducing SyncClient (#713)Tom Christiebefore adoptionf17ab37

lines 209–210why, one step back
209
210

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 211–231why, one step back
211@pytest.mark.parametrize(
212 "url,expected_url,expected_headers,expected_mode",213 [214 ("https://example.com", "https://example.com", {}, "DEFAULT"),215 (216 "https://user:pass@example.com",217 "https://example.com:443",218 {"proxy-authorization": "Basic dXNlcjpwYXNz"},219 "DEFAULT",220 ),221 ],222)223def test_proxy_from_url(url, expected_url, expected_headers, expected_mode):224 proxy = httpx.Proxy(url)225226 assert str(proxy.url) == expected_url227 assert dict(proxy.headers) == expected_headers228 assert proxy.mode == expected_mode229 assert repr(proxy) == "Proxy(url='{}', headers={}, mode='{}')".format(230 expected_url, str(expected_headers), expected_mode231 )

Prep for introducing SyncClient (#713)Tom Christiebefore adoptionf17ab37

lines 232–241why, one step back
232
233234def test_invalid_proxy_scheme():235 with pytest.raises(ValueError):236 httpx.Proxy("invalid://example.com")237238239def test_invalid_proxy_mode():240 with pytest.raises(ValueError):241 httpx.Proxy("https://example.com", mode="INVALID")