Skip to content
Myr

test_config.py

This file

Path

tests/test_config.py

232 lines · 6.9 KiB · Python

Last changed by

Add keepalive_expiry to Limits config (#1398)Tom Christiebefore adoptiond0835da

History of this file

Take it

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

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 1–12why, one step back
1import os
2import ssl3import sys4from pathlib import Path56import certifi7import pytest89import httpx101112def test_load_ssl_config():

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

line 13why, one step back
13 context = httpx.create_ssl_context()

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 14–19why, one step back
14 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
15 assert context.check_hostname is True161718def test_load_ssl_config_verify_non_existing_path():19 with pytest.raises(IOError):

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

line 20why, one step back
20 httpx.create_ssl_context(verify="/path/to/nowhere")

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 21–23why, one step back
21
2223def test_load_ssl_config_verify_existing_file():

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

line 24why, one step back
24 context = httpx.create_ssl_context(verify=certifi.where())

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 25–29why, one step back
25 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
26 assert context.check_hostname is True272829@pytest.mark.parametrize("config", ("SSL_CERT_FILE", "SSL_CERT_DIR"))

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

lines 30–32why, one step back
30def test_load_ssl_config_verify_env_file(
31 https_server, ca_cert_pem_file, config, cert_authority32):

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 33–37why, one step back
33 os.environ[config] = (
34 ca_cert_pem_file35 if config.endswith("_FILE")36 else str(Path(ca_cert_pem_file).parent)37 )

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

lines 38–40why, one step back
38 context = httpx.create_ssl_context(trust_env=True)
39 cert_authority.configure_trust(context)40

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

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

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

line 43why, one step back
43 assert len(context.get_ca_certs()) == 1

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 44–47why, one step back
44
4546def test_load_ssl_config_verify_directory():47 path = Path(certifi.where()).parent

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

line 48why, one step back
48 context = httpx.create_ssl_context(verify=str(path))

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 49–53why, one step back
49 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
50 assert context.check_hostname is True515253def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

line 54why, one step back
54 context = httpx.create_ssl_context(cert=(cert_pem_file, cert_private_key_file))

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 55–62why, one step back
55 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
56 assert context.check_hostname is True575859@pytest.mark.parametrize("password", [b"password", "password"])60def test_load_ssl_config_cert_and_encrypted_key(61 cert_pem_file, cert_encrypted_private_key_file, password62):

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

line 63why, one step back
63 context = httpx.create_ssl_context(

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 64–73why, one step back
64 cert=(cert_pem_file, cert_encrypted_private_key_file, password)
65 )66 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED67 assert context.check_hostname is True686970def test_load_ssl_config_cert_and_key_invalid_password(71 cert_pem_file, cert_encrypted_private_key_file72):73 with pytest.raises(ssl.SSLError):

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

lines 74–76why, one step back
74 httpx.create_ssl_context(
75 cert=(cert_pem_file, cert_encrypted_private_key_file, "password1")76 )

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 77–80why, one step back
77
7879def test_load_ssl_config_cert_without_key_raises(cert_pem_file):80 with pytest.raises(ssl.SSLError):

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

line 81why, one step back
81 httpx.create_ssl_context(cert=cert_pem_file)

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 82–84why, one step back
82
8384def test_load_ssl_config_no_verify():

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

line 85why, one step back
85 context = httpx.create_ssl_context(verify=False)

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 86–91why, one step back
86 assert context.verify_mode == ssl.VerifyMode.CERT_NONE
87 assert context.check_hostname is False888990def test_load_ssl_context():91 ssl_context = ssl.create_default_context()

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

line 92why, one step back
92 context = httpx.create_ssl_context(verify=ssl_context)

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

line 93why, one step back
93

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

line 94why, one step back
94 assert context is ssl_context

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 95–96why, one step back
95
96

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

lines 97–100why, one step back
97def test_create_ssl_context_with_get_request(server, cert_pem_file):
98 context = httpx.create_ssl_context(verify=cert_pem_file)99 response = httpx.get(server.url, verify=context)100 assert response.status_code == 200

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 101–103why, one step back
101
102103def test_limits_repr():

Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925

line 104why, one step back
104 limits = httpx.Limits(max_connections=100)

Add keepalive_expiry to Limits config (#1398)Tom Christiebefore adoptiond0835da

lines 105–106why, one step back
105 expected = "Limits(max_connections=100, max_keepalive_connections=None, keepalive_expiry=5.0)"
106 assert repr(limits) == expected

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 107–109why, one step back
107
108109def test_limits_eq():

Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925

lines 110–113why, one step back
110 limits = httpx.Limits(max_connections=100)
111 assert limits == httpx.Limits(max_connections=100)112113

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 114–118why, one step back
114def test_timeout_eq():
115 timeout = httpx.Timeout(timeout=5.0)116 assert timeout == httpx.Timeout(timeout=5.0)117118

httpx.Timeout must include a default (#1085)Tom Christiebefore adoptiondba83d4

line 119why, one step back
119def test_timeout_all_parameters_set():

Switch to more concise Timeout parameters (#1111)Florimond Mancabefore adoption26cd4f5

line 120why, one step back
120 timeout = httpx.Timeout(connect=5.0, read=5.0, write=5.0, pool=5.0)

httpx.Timeout must include a default (#1085)Tom Christiebefore adoptiondba83d4

lines 121–123why, one step back
121 assert timeout == httpx.Timeout(timeout=5.0)
122123

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

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

httpx.Timeout must include a default (#1085)Tom Christiebefore adoptiondba83d4

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

Switch to more concise Timeout parameters (#1111)Florimond Mancabefore adoption26cd4f5

lines 126–129why, one step back
126 assert timeout.connect is None
127 assert timeout.read is None128 assert timeout.write is None129 assert timeout.pool is None

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 130–133why, one step back
130
131132def test_timeout_from_none():133 timeout = httpx.Timeout(timeout=None)

httpx.Timeout must include a default (#1085)Tom Christiebefore adoptiondba83d4

line 134why, one step back
134 assert timeout == httpx.Timeout(None)

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 135–137why, one step back
135
136137def test_timeout_from_one_none_value():

Switch to more concise Timeout parameters (#1111)Florimond Mancabefore adoption26cd4f5

line 138why, one step back
138 timeout = httpx.Timeout(None, read=None)

httpx.Timeout must include a default (#1085)Tom Christiebefore adoptiondba83d4

line 139why, one step back
139 assert timeout == httpx.Timeout(None)

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 140–142why, one step back
140
141142def test_timeout_from_one_value():

Switch to more concise Timeout parameters (#1111)Florimond Mancabefore adoption26cd4f5

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

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 144–147why, one step back
144 assert timeout == httpx.Timeout(timeout=(None, 5.0, None, None))
145146147def test_timeout_from_one_value_and_default():

Switch to more concise Timeout parameters (#1111)Florimond Mancabefore adoption26cd4f5

line 148why, one step back
148 timeout = httpx.Timeout(5.0, pool=60.0)

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 149–151why, one step back
149 assert timeout == httpx.Timeout(timeout=(5.0, 5.0, 5.0, 60.0))
150151

httpx.Timeout must include a default (#1085)Tom Christiebefore adoptiondba83d4

line 152why, one step back
152def test_timeout_missing_default():

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

lines 153–154why, one step back
153 with pytest.raises(ValueError):
154 httpx.Timeout(pool=60.0)

httpx.Timeout must include a default (#1085)Tom Christiebefore adoptiondba83d4

lines 155–156why, one step back
155
156

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 157–170why, one step back
157def test_timeout_from_tuple():
158 timeout = httpx.Timeout(timeout=(5.0, 5.0, 5.0, 5.0))159 assert timeout == httpx.Timeout(timeout=5.0)160161162def test_timeout_from_config_instance():163 timeout = httpx.Timeout(timeout=5.0)164 assert httpx.Timeout(timeout) == httpx.Timeout(timeout=5.0)165166167def test_timeout_repr():168 timeout = httpx.Timeout(timeout=5.0)169 assert repr(timeout) == "Timeout(timeout=5.0)"170

Switch to more concise Timeout parameters (#1111)Florimond Mancabefore adoption26cd4f5

lines 171–172why, one step back
171 timeout = httpx.Timeout(None, read=5.0)
172 assert repr(timeout) == "Timeout(connect=None, read=5.0, write=None, pool=None)"

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 173–183why, one step back
173
174175@pytest.mark.skipif(176 not hasattr(ssl.SSLContext, "keylog_filename"),177 reason="requires OpenSSL 1.1.1 or higher",178)179@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")180def test_ssl_config_support_for_keylog_file(tmpdir, monkeypatch): # pragma: nocover181 with monkeypatch.context() as m:182 m.delenv("SSLKEYLOGFILE", raising=False)183

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

line 184why, one step back
184 context = httpx.create_ssl_context(trust_env=True)

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

line 185why, one step back
185

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

line 186why, one step back
186 assert context.keylog_filename is None # type: ignore

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 187–192why, one step back
187
188 filename = str(tmpdir.join("test.log"))189190 with monkeypatch.context() as m:191 m.setenv("SSLKEYLOGFILE", filename)192

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

line 193why, one step back
193 context = httpx.create_ssl_context(trust_env=True)

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

line 194why, one step back
194

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

line 195why, one step back
195 assert context.keylog_filename == filename # type: ignore

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

line 196why, one step back
196

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

line 197why, one step back
197 context = httpx.create_ssl_context(trust_env=False)

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

line 198why, one step back
198

Included create_ssl_context function to create the same context with SSLConfig and serve as API (#996)Can Sarıgölbefore adoption926a55a

line 199why, one step back
199 assert context.keylog_filename is None # type: ignore

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

lines 200–232why, one step back
200
201202@pytest.mark.parametrize(203 "url,expected_url,expected_headers,expected_mode",204 [205 ("https://example.com", "https://example.com", {}, "DEFAULT"),206 (207 "https://user:pass@example.com",208 "https://example.com",209 {"proxy-authorization": "Basic dXNlcjpwYXNz"},210 "DEFAULT",211 ),212 ],213)214def test_proxy_from_url(url, expected_url, expected_headers, expected_mode):215 proxy = httpx.Proxy(url)216217 assert str(proxy.url) == expected_url218 assert dict(proxy.headers) == expected_headers219 assert proxy.mode == expected_mode220 assert repr(proxy) == "Proxy(url='{}', headers={}, mode='{}')".format(221 expected_url, str(expected_headers), expected_mode222 )223224225def test_invalid_proxy_scheme():226 with pytest.raises(ValueError):227 httpx.Proxy("invalid://example.com")228229230def test_invalid_proxy_mode():231 with pytest.raises(ValueError):232 httpx.Proxy("https://example.com", mode="INVALID")