17
17
from aiohttp import web
18
18
from aiohttp .client_exceptions import ClientConnectionError
19
19
20
- pytestmark = [
21
- pytest .mark .filterwarnings (
22
- "ignore:unclosed <socket.socket fd=.*:ResourceWarning" ,
23
- ),
24
- pytest .mark .filterwarnings (
25
- "ignore:"
26
- "unclosed transport <_SelectorSocketTransport closing fd=.*"
27
- ":ResourceWarning" ,
28
- ),
29
- ]
30
-
31
-
32
20
ASYNCIO_SUPPORTS_TLS_IN_TLS = sys .version_info >= (3 , 11 )
33
21
34
22
@@ -120,16 +108,14 @@ async def test_secure_https_proxy_absolute_path(
120
108
conn = aiohttp .TCPConnector ()
121
109
sess = aiohttp .ClientSession (connector = conn )
122
110
123
- response = await sess .get (
111
+ async with sess .get (
124
112
web_server_endpoint_url ,
125
113
proxy = secure_proxy_url ,
126
114
ssl = client_ssl_ctx , # used for both proxy and endpoint connections
127
- )
128
-
129
- assert response .status == 200
130
- assert await response .text () == web_server_endpoint_payload
115
+ ) as response :
116
+ assert response .status == 200
117
+ assert await response .text () == web_server_endpoint_payload
131
118
132
- response .close ()
133
119
await sess .close ()
134
120
await conn .close ()
135
121
@@ -195,7 +181,8 @@ async def test_https_proxy_unsupported_tls_in_tls(
195
181
ClientConnectionError ,
196
182
match = expected_exception_reason ,
197
183
) as conn_err :
198
- await sess .get (url , proxy = secure_proxy_url , ssl = client_ssl_ctx )
184
+ async with sess .get (url , proxy = secure_proxy_url , ssl = client_ssl_ctx ):
185
+ pass
199
186
200
187
assert isinstance (conn_err .value .__cause__ , TypeError )
201
188
assert match_regex (f"^{ type_err !s} $" , str (conn_err .value .__cause__ ))
@@ -255,13 +242,11 @@ async def proxy_server():
255
242
def get_request (loop : Any ):
256
243
async def _request (method = "GET" , * , url , trust_env = False , ** kwargs ):
257
244
connector = aiohttp .TCPConnector (ssl = False )
258
- client = aiohttp .ClientSession (connector = connector , trust_env = trust_env )
259
- try :
260
- resp = await client .request (method , url , ** kwargs )
261
- await resp .release ()
262
- return resp
263
- finally :
264
- await client .close ()
245
+ async with aiohttp .ClientSession (
246
+ connector = connector , trust_env = trust_env
247
+ ) as client :
248
+ async with client .request (method , url , ** kwargs ) as resp :
249
+ return resp
265
250
266
251
return _request
267
252
@@ -411,11 +396,8 @@ async def test_proxy_http_acquired_cleanup_force(
411
396
assert 0 == len (conn ._acquired )
412
397
413
398
async def request ():
414
- resp = await sess .get (url , proxy = proxy .url )
415
-
416
- assert 1 == len (conn ._acquired )
417
-
418
- await resp .release ()
399
+ async with sess .get (url , proxy = proxy .url ):
400
+ assert 1 == len (conn ._acquired )
419
401
420
402
await request ()
421
403
@@ -439,13 +421,11 @@ async def request(pid):
439
421
# process requests only one by one
440
422
nonlocal current_pid
441
423
442
- resp = await sess .get (url , proxy = proxy .url )
443
-
444
- current_pid = pid
445
- await asyncio .sleep (0.2 )
446
- assert current_pid == pid
424
+ async with sess .get (url , proxy = proxy .url ) as resp :
425
+ current_pid = pid
426
+ await asyncio .sleep (0.2 )
427
+ assert current_pid == pid
447
428
448
- await resp .release ()
449
429
return resp
450
430
451
431
requests = [request (pid ) for pid in range (multi_conn_num )]
@@ -498,9 +478,8 @@ async def xtest_proxy_https_send_body(proxy_test_server: Any, loop: Any) -> None
498
478
proxy .return_value = {"status" : 200 , "body" : b"1" * (2 ** 20 )}
499
479
url = "https://www.google.com.ua/search?q=aiohttp proxy"
500
480
501
- resp = await sess .get (url , proxy = proxy .url )
502
- body = await resp .read ()
503
- await resp .release ()
481
+ async with sess .get (url , proxy = proxy .url ) as resp :
482
+ body = await resp .read ()
504
483
await sess .close ()
505
484
506
485
assert body == b"1" * (2 ** 20 )
@@ -598,11 +577,8 @@ async def xtest_proxy_https_acquired_cleanup(proxy_test_server: Any, loop: Any)
598
577
assert 0 == len (conn ._acquired )
599
578
600
579
async def request ():
601
- resp = await sess .get (url , proxy = proxy .url )
602
-
603
- assert 1 == len (conn ._acquired )
604
-
605
- await resp .release ()
580
+ async with sess .get (url , proxy = proxy .url ):
581
+ assert 1 == len (conn ._acquired )
606
582
607
583
await request ()
608
584
@@ -624,11 +600,8 @@ async def xtest_proxy_https_acquired_cleanup_force(
624
600
assert 0 == len (conn ._acquired )
625
601
626
602
async def request ():
627
- resp = await sess .get (url , proxy = proxy .url )
628
-
629
- assert 1 == len (conn ._acquired )
630
-
631
- await resp .release ()
603
+ async with sess .get (url , proxy = proxy .url ):
604
+ assert 1 == len (conn ._acquired )
632
605
633
606
await request ()
634
607
@@ -652,13 +625,11 @@ async def request(pid):
652
625
# process requests only one by one
653
626
nonlocal current_pid
654
627
655
- resp = await sess .get (url , proxy = proxy .url )
656
-
657
- current_pid = pid
658
- await asyncio .sleep (0.2 )
659
- assert current_pid == pid
628
+ async with sess .get (url , proxy = proxy .url ) as resp :
629
+ current_pid = pid
630
+ await asyncio .sleep (0.2 )
631
+ assert current_pid == pid
660
632
661
- await resp .release ()
662
633
return resp
663
634
664
635
requests = [request (pid ) for pid in range (multi_conn_num )]
@@ -872,8 +843,9 @@ async def test_proxy_auth() -> None:
872
843
with pytest .raises (
873
844
ValueError , match = r"proxy_auth must be None or BasicAuth\(\) tuple"
874
845
):
875
- await session .get (
846
+ async with session .get (
876
847
"http://python.org" ,
877
848
proxy = "http://proxy.example.com" ,
878
849
proxy_auth = ("user" , "pass" ),
879
- )
850
+ ):
851
+ pass
0 commit comments