1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
from __future__ import annotations
import gc
import socket
from test import resolvesLocalhostFQDN
from unittest import mock
from unittest.mock import MagicMock, patch
import pytest
from urllib3 import connection_from_url
from urllib3.connectionpool import HTTPSConnectionPool
from urllib3.exceptions import LocationValueError
from urllib3.poolmanager import (
_DEFAULT_BLOCKSIZE,
PoolKey,
PoolManager,
key_fn_by_scheme,
)
from urllib3.util import retry, timeout
from urllib3.util.url import Url
class TestPoolManager:
@resolvesLocalhostFQDN()
def test_same_url(self) -> None:
# Convince ourselves that normally we don't get the same object
conn1 = connection_from_url("http://localhost:8081/foo")
conn2 = connection_from_url("http://localhost:8081/bar")
assert conn1 != conn2
# Now try again using the PoolManager
p = PoolManager(1)
conn1 = p.connection_from_url("http://localhost:8081/foo")
conn2 = p.connection_from_url("http://localhost:8081/bar")
assert conn1 == conn2
# Ensure that FQDNs are handled separately from relative domains
p = PoolManager(2)
conn1 = p.connection_from_url("http://localhost.:8081/foo")
conn2 = p.connection_from_url("http://localhost:8081/bar")
assert conn1 != conn2
def test_many_urls(self) -> None:
urls = [
"http://localhost:8081/foo",
"http://www.google.com/mail",
"http://localhost:8081/bar",
"https://www.google.com/",
"https://www.google.com/mail",
"http://yahoo.com",
"http://bing.com",
"http://yahoo.com/",
]
connections = set()
p = PoolManager(10)
for url in urls:
conn = p.connection_from_url(url)
connections.add(conn)
assert len(connections) == 5
def test_manager_clear(self) -> None:
p = PoolManager(5)
p.connection_from_url("http://google.com")
assert len(p.pools) == 1
p.clear()
assert len(p.pools) == 0
@pytest.mark.parametrize("url", ["http://@", None])
def test_nohost(self, url: str | None) -> None:
p = PoolManager(5)
with pytest.raises(LocationValueError):
p.connection_from_url(url=url) # type: ignore[arg-type]
def test_contextmanager(self) -> None:
with PoolManager(1) as p:
p.connection_from_url("http://google.com")
assert len(p.pools) == 1
assert len(p.pools) == 0
def test_http_pool_key_fields(self) -> None:
"""Assert the HTTPPoolKey fields are honored when selecting a pool."""
connection_pool_kw = {
"timeout": timeout.Timeout(3.14),
"retries": retry.Retry(total=6, connect=2),
"block": True,
"source_address": "127.0.0.1",
"blocksize": _DEFAULT_BLOCKSIZE + 1,
}
p = PoolManager()
conn_pools = [
p.connection_from_url("http://example.com/"),
p.connection_from_url("http://example.com:8000/"),
p.connection_from_url("http://other.example.com/"),
]
for key, value in connection_pool_kw.items():
p.connection_pool_kw[key] = value
conn_pools.append(p.connection_from_url("http://example.com/"))
assert all(
x is not y
for i, x in enumerate(conn_pools)
for j, y in enumerate(conn_pools)
if i != j
)
assert all(isinstance(key, PoolKey) for key in p.pools.keys())
def test_https_pool_key_fields(self) -> None:
"""Assert the HTTPSPoolKey fields are honored when selecting a pool."""
connection_pool_kw = {
"timeout": timeout.Timeout(3.14),
"retries": retry.Retry(total=6, connect=2),
"block": True,
"source_address": "127.0.0.1",
"key_file": "/root/totally_legit.key",
"cert_file": "/root/totally_legit.crt",
"cert_reqs": "CERT_REQUIRED",
"ca_certs": "/root/path_to_pem",
"ssl_version": "SSLv23_METHOD",
"blocksize": _DEFAULT_BLOCKSIZE + 1,
}
p = PoolManager()
conn_pools = [
p.connection_from_url("https://example.com/"),
p.connection_from_url("https://example.com:4333/"),
p.connection_from_url("https://other.example.com/"),
]
# Asking for a connection pool with the same key should give us an
# existing pool.
dup_pools = []
for key, value in connection_pool_kw.items():
p.connection_pool_kw[key] = value
conn_pools.append(p.connection_from_url("https://example.com/"))
dup_pools.append(p.connection_from_url("https://example.com/"))
assert all(
x is not y
for i, x in enumerate(conn_pools)
for j, y in enumerate(conn_pools)
if i != j
)
assert all(pool in conn_pools for pool in dup_pools)
assert all(isinstance(key, PoolKey) for key in p.pools.keys())
def test_default_pool_key_funcs_copy(self) -> None:
"""Assert each PoolManager gets a copy of ``pool_keys_by_scheme``."""
p = PoolManager()
assert p.key_fn_by_scheme == p.key_fn_by_scheme
assert p.key_fn_by_scheme is not key_fn_by_scheme
def test_pools_keyed_with_from_host(self) -> None:
"""Assert pools are still keyed correctly with connection_from_host."""
ssl_kw = {
"key_file": "/root/totally_legit.key",
"cert_file": "/root/totally_legit.crt",
"cert_reqs": "CERT_REQUIRED",
"ca_certs": "/root/path_to_pem",
"ssl_version": "SSLv23_METHOD",
}
p = PoolManager(5, **ssl_kw) # type: ignore[arg-type]
conns = [p.connection_from_host("example.com", 443, scheme="https")]
for k in ssl_kw:
p.connection_pool_kw[k] = "newval"
conns.append(p.connection_from_host("example.com", 443, scheme="https"))
assert all(
x is not y
for i, x in enumerate(conns)
for j, y in enumerate(conns)
if i != j
)
def test_https_connection_from_url_case_insensitive(self) -> None:
"""Assert scheme case is ignored when pooling HTTPS connections."""
p = PoolManager()
pool = p.connection_from_url("https://example.com/")
other_pool = p.connection_from_url("HTTPS://EXAMPLE.COM/")
assert 1 == len(p.pools)
assert pool is other_pool
assert all(isinstance(key, PoolKey) for key in p.pools.keys())
def test_https_connection_from_host_case_insensitive(self) -> None:
"""Assert scheme case is ignored when getting the https key class."""
p = PoolManager()
pool = p.connection_from_host("example.com", scheme="https")
other_pool = p.connection_from_host("EXAMPLE.COM", scheme="HTTPS")
assert 1 == len(p.pools)
assert pool is other_pool
assert all(isinstance(key, PoolKey) for key in p.pools.keys())
def test_https_connection_from_context_case_insensitive(self) -> None:
"""Assert scheme case is ignored when getting the https key class."""
p = PoolManager()
context = {"scheme": "https", "host": "example.com", "port": "443"}
other_context = {"scheme": "HTTPS", "host": "EXAMPLE.COM", "port": "443"}
pool = p.connection_from_context(context)
other_pool = p.connection_from_context(other_context)
assert 1 == len(p.pools)
assert pool is other_pool
assert all(isinstance(key, PoolKey) for key in p.pools.keys())
def test_http_connection_from_url_case_insensitive(self) -> None:
"""Assert scheme case is ignored when pooling HTTP connections."""
p = PoolManager()
pool = p.connection_from_url("http://example.com/")
other_pool = p.connection_from_url("HTTP://EXAMPLE.COM/")
assert 1 == len(p.pools)
assert pool is other_pool
assert all(isinstance(key, PoolKey) for key in p.pools.keys())
def test_http_connection_from_host_case_insensitive(self) -> None:
"""Assert scheme case is ignored when getting the https key class."""
p = PoolManager()
pool = p.connection_from_host("example.com", scheme="http")
other_pool = p.connection_from_host("EXAMPLE.COM", scheme="HTTP")
assert 1 == len(p.pools)
assert pool is other_pool
assert all(isinstance(key, PoolKey) for key in p.pools.keys())
def test_assert_hostname_and_fingerprint_flag(self) -> None:
"""Assert that pool manager can accept hostname and fingerprint flags."""
fingerprint = "92:81:FE:85:F7:0C:26:60:EC:D6:B3:BF:93:CF:F9:71:CC:07:7D:0A"
p = PoolManager(assert_hostname=True, assert_fingerprint=fingerprint)
pool = p.connection_from_url("https://example.com/")
assert 1 == len(p.pools)
assert isinstance(pool, HTTPSConnectionPool)
assert pool.assert_hostname
assert fingerprint == pool.assert_fingerprint
def test_http_connection_from_context_case_insensitive(self) -> None:
"""Assert scheme case is ignored when getting the https key class."""
p = PoolManager()
context = {"scheme": "http", "host": "example.com", "port": "8080"}
other_context = {"scheme": "HTTP", "host": "EXAMPLE.COM", "port": "8080"}
pool = p.connection_from_context(context)
other_pool = p.connection_from_context(other_context)
assert 1 == len(p.pools)
assert pool is other_pool
assert all(isinstance(key, PoolKey) for key in p.pools.keys())
@patch("urllib3.poolmanager.PoolManager.connection_from_pool_key")
def test_connection_from_context_strict_param(
self, connection_from_pool_key: mock.MagicMock
) -> None:
p = PoolManager()
context = {
"scheme": "http",
"host": "example.com",
"port": 8080,
"strict": True,
}
with pytest.warns(DeprecationWarning) as records:
p.connection_from_context(context)
msg = (
"The 'strict' parameter is no longer needed on Python 3+. "
"This will raise an error in urllib3 v2.1.0."
)
record = records[0]
assert isinstance(record.message, Warning)
assert record.message.args[0] == msg
_, kwargs = connection_from_pool_key.call_args
assert kwargs["request_context"] == {
"scheme": "http",
"host": "example.com",
"port": 8080,
}
def test_custom_pool_key(self) -> None:
"""Assert it is possible to define a custom key function."""
p = PoolManager(10)
p.key_fn_by_scheme["http"] = lambda x: tuple(x["key"]) # type: ignore[assignment]
pool1 = p.connection_from_url(
"http://example.com", pool_kwargs={"key": "value"}
)
pool2 = p.connection_from_url(
"http://example.com", pool_kwargs={"key": "other"}
)
pool3 = p.connection_from_url(
"http://example.com", pool_kwargs={"key": "value", "x": "y"}
)
assert 2 == len(p.pools)
assert pool1 is pool3
assert pool1 is not pool2
def test_override_pool_kwargs_url(self) -> None:
"""Assert overriding pool kwargs works with connection_from_url."""
p = PoolManager()
pool_kwargs = {"retries": 100, "block": True}
default_pool = p.connection_from_url("http://example.com/")
override_pool = p.connection_from_url(
"http://example.com/", pool_kwargs=pool_kwargs
)
assert retry.Retry.DEFAULT == default_pool.retries
assert not default_pool.block
assert 100 == override_pool.retries
assert override_pool.block
def test_override_pool_kwargs_host(self) -> None:
"""Assert overriding pool kwargs works with connection_from_host"""
p = PoolManager()
pool_kwargs = {"retries": 100, "block": True}
default_pool = p.connection_from_host("example.com", scheme="http")
override_pool = p.connection_from_host(
"example.com", scheme="http", pool_kwargs=pool_kwargs
)
assert retry.Retry.DEFAULT == default_pool.retries
assert not default_pool.block
assert 100 == override_pool.retries
assert override_pool.block
def test_pool_kwargs_socket_options(self) -> None:
"""Assert passing socket options works with connection_from_host"""
p = PoolManager(socket_options=[])
override_opts = [
(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1),
(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1),
]
pool_kwargs = {"socket_options": override_opts}
default_pool = p.connection_from_host("example.com", scheme="http")
override_pool = p.connection_from_host(
"example.com", scheme="http", pool_kwargs=pool_kwargs
)
assert default_pool.conn_kw["socket_options"] == []
assert override_pool.conn_kw["socket_options"] == override_opts
def test_merge_pool_kwargs(self) -> None:
"""Assert _merge_pool_kwargs works in the happy case"""
p = PoolManager(retries=100)
merged = p._merge_pool_kwargs({"new_key": "value"})
assert {"retries": 100, "new_key": "value"} == merged
def test_merge_pool_kwargs_none(self) -> None:
"""Assert false-y values to _merge_pool_kwargs result in defaults"""
p = PoolManager(retries=100)
merged = p._merge_pool_kwargs({})
assert p.connection_pool_kw == merged
merged = p._merge_pool_kwargs(None)
assert p.connection_pool_kw == merged
def test_merge_pool_kwargs_remove_key(self) -> None:
"""Assert keys can be removed with _merge_pool_kwargs"""
p = PoolManager(retries=100)
merged = p._merge_pool_kwargs({"retries": None})
assert "retries" not in merged
def test_merge_pool_kwargs_invalid_key(self) -> None:
"""Assert removing invalid keys with _merge_pool_kwargs doesn't break"""
p = PoolManager(retries=100)
merged = p._merge_pool_kwargs({"invalid_key": None})
assert p.connection_pool_kw == merged
def test_pool_manager_no_url_absolute_form(self) -> None:
"""Valides we won't send a request with absolute form without a proxy"""
p = PoolManager()
assert p._proxy_requires_url_absolute_form(Url("http://example.com")) is False
assert p._proxy_requires_url_absolute_form(Url("https://example.com")) is False
@pytest.mark.parametrize(
"input_blocksize,expected_blocksize",
[
(_DEFAULT_BLOCKSIZE, _DEFAULT_BLOCKSIZE),
(None, _DEFAULT_BLOCKSIZE),
(8192, 8192),
],
)
def test_poolmanager_blocksize(
self, input_blocksize: int, expected_blocksize: int
) -> None:
"""Assert PoolManager sets blocksize properly"""
p = PoolManager()
pool_blocksize = p.connection_from_url(
"http://example.com", {"blocksize": input_blocksize}
)
assert pool_blocksize.conn_kw["blocksize"] == expected_blocksize
assert pool_blocksize._get_conn().blocksize == expected_blocksize
@pytest.mark.parametrize(
"url",
[
"[a::b%zone]",
"[a::b%25zone]",
"http://[a::b%zone]",
"http://[a::b%25zone]",
],
)
@patch("urllib3.util.connection.create_connection")
def test_e2e_connect_to_ipv6_scoped(
self, create_connection: MagicMock, url: str
) -> None:
"""Checks that IPv6 scoped addresses are properly handled end-to-end.
This is not strictly speaking a pool manager unit test - this test
lives here in absence of a better code location for e2e/integration
tests.
"""
p = PoolManager()
conn_pool = p.connection_from_url(url)
conn = conn_pool._get_conn()
conn.connect()
assert create_connection.call_args[0][0] == ("a::b%zone", 80)
@patch("urllib3.connection.ssl_wrap_socket")
@patch("urllib3.util.connection.create_connection")
def test_e2e_connect_to_ipv6_scoped_tls(
self, create_connection: MagicMock, ssl_wrap_socket: MagicMock
) -> None:
p = PoolManager()
conn_pool = p.connection_from_url(
"https://[a::b%zone]", pool_kwargs={"assert_hostname": False}
)
conn = conn_pool._get_conn()
conn.connect()
assert ssl_wrap_socket.call_args[1]["server_hostname"] == "a::b"
def test_thread_safty(self) -> None:
pool_manager = PoolManager(num_pools=2)
# thread 1 gets a pool for host x
pool_1 = pool_manager.connection_from_url("http://host_x:80/")
# thread 2 gets a pool for host y
pool_2 = pool_manager.connection_from_url("http://host_y:80/")
# thread 3 gets a pool for host z
pool_3 = pool_manager.connection_from_url("http://host_z:80")
# None of the pools should be closed, since all of them are referenced.
assert pool_1.pool is not None
assert pool_2.pool is not None
assert pool_3.pool is not None
conn_queue = pool_1.pool
assert conn_queue.qsize() > 0
# thread 1 stops.
del pool_1
gc.collect()
# Connection should be closed, because reference to pool_1 is gone.
assert conn_queue.qsize() == 0
|