5
5
import ssl
6
6
import sys
7
7
from re import match as match_regex
8
+ from typing import Awaitable , Callable
8
9
from unittest import mock
9
10
from uuid import uuid4
10
11
13
14
from yarl import URL
14
15
15
16
import aiohttp
16
- from aiohttp import web
17
+ from aiohttp import ClientResponse , web
17
18
from aiohttp .client_exceptions import ClientConnectionError
18
19
from aiohttp .helpers import IS_MACOS , IS_WINDOWS
19
20
@@ -498,17 +499,22 @@ async def xtest_proxy_https_connect_with_port(proxy_test_server, get_request):
498
499
499
500
500
501
@pytest .mark .xfail
501
- async def xtest_proxy_https_send_body (proxy_test_server , loop ):
502
- sess = aiohttp .ClientSession (loop = loop )
503
- proxy = await proxy_test_server ()
504
- proxy .return_value = {"status" : 200 , "body" : b"1" * (2 ** 20 )}
505
- url = "https://www.google.com.ua/search?q=aiohttp proxy"
502
+ async def xtest_proxy_https_send_body (
503
+ proxy_test_server : Callable [[], Awaitable [mock .Mock ]],
504
+ loop : asyncio .AbstractEventLoop ,
505
+ ) -> None :
506
+ sess = aiohttp .ClientSession ()
507
+ try :
508
+ proxy = await proxy_test_server ()
509
+ proxy .return_value = {"status" : 200 , "body" : b"1" * (2 ** 20 )}
510
+ url = "https://www.google.com.ua/search?q=aiohttp proxy"
506
511
507
- async with sess .get (url , proxy = proxy .url ) as resp :
508
- body = await resp .read ()
509
- await sess .close ()
512
+ async with sess .get (url , proxy = proxy .url ) as resp :
513
+ body = await resp .read ()
510
514
511
- assert body == b"1" * (2 ** 20 )
515
+ assert body == b"1" * (2 ** 20 )
516
+ finally :
517
+ await sess .close ()
512
518
513
519
514
520
@pytest .mark .xfail
@@ -592,42 +598,46 @@ async def xtest_proxy_https_auth(proxy_test_server, get_request):
592
598
async def xtest_proxy_https_acquired_cleanup (proxy_test_server , loop ):
593
599
url = "https://secure.aiohttp.io/path"
594
600
595
- conn = aiohttp .TCPConnector (loop = loop )
596
- sess = aiohttp .ClientSession (connector = conn , loop = loop )
597
- proxy = await proxy_test_server ()
598
-
599
- assert 0 == len (conn ._acquired )
601
+ conn = aiohttp .TCPConnector ()
602
+ sess = aiohttp .ClientSession (connector = conn )
603
+ try :
604
+ proxy = await proxy_test_server ()
600
605
601
- async def request ():
602
- async with sess .get (url , proxy = proxy .url ):
603
- assert 1 == len (conn ._acquired )
606
+ assert 0 == len (conn ._acquired )
604
607
605
- await request ()
608
+ async def request () -> None :
609
+ async with sess .get (url , proxy = proxy .url ):
610
+ assert 1 == len (conn ._acquired )
606
611
607
- assert 0 == len ( conn . _acquired )
612
+ await request ( )
608
613
609
- await sess .close ()
614
+ assert 0 == len (conn ._acquired )
615
+ finally :
616
+ await sess .close ()
617
+ await conn .close ()
610
618
611
619
612
620
@pytest .mark .xfail
613
621
async def xtest_proxy_https_acquired_cleanup_force (proxy_test_server , loop ):
614
622
url = "https://secure.aiohttp.io/path"
615
623
616
- conn = aiohttp .TCPConnector (force_close = True , loop = loop )
617
- sess = aiohttp .ClientSession (connector = conn , loop = loop )
618
- proxy = await proxy_test_server ()
619
-
620
- assert 0 == len (conn ._acquired )
624
+ conn = aiohttp .TCPConnector (force_close = True )
625
+ sess = aiohttp .ClientSession (connector = conn )
626
+ try :
627
+ proxy = await proxy_test_server ()
621
628
622
- async def request ():
623
- async with sess .get (url , proxy = proxy .url ):
624
- assert 1 == len (conn ._acquired )
629
+ assert 0 == len (conn ._acquired )
625
630
626
- await request ()
631
+ async def request () -> None :
632
+ async with sess .get (url , proxy = proxy .url ):
633
+ assert 1 == len (conn ._acquired )
627
634
628
- assert 0 == len ( conn . _acquired )
635
+ await request ( )
629
636
630
- await sess .close ()
637
+ assert 0 == len (conn ._acquired )
638
+ finally :
639
+ await sess .close ()
640
+ await conn .close ()
631
641
632
642
633
643
@pytest .mark .xfail
@@ -639,26 +649,30 @@ async def xtest_proxy_https_multi_conn_limit(proxy_test_server, loop):
639
649
sess = aiohttp .ClientSession (connector = conn , loop = loop )
640
650
proxy = await proxy_test_server ()
641
651
642
- current_pid = None
652
+ try :
653
+ current_pid = None
643
654
644
- async def request (pid ) :
645
- # process requests only one by one
646
- nonlocal current_pid
655
+ async def request (pid : int ) -> ClientResponse :
656
+ # process requests only one by one
657
+ nonlocal current_pid
647
658
648
- async with sess .get (url , proxy = proxy .url ) as resp :
649
- current_pid = pid
650
- await asyncio .sleep (0.2 , loop = loop )
651
- assert current_pid == pid
659
+ async with sess .get (url , proxy = proxy .url ) as resp :
660
+ current_pid = pid
661
+ await asyncio .sleep (0.2 )
662
+ assert current_pid == pid
652
663
653
- return resp
664
+ return resp
654
665
655
- requests = [request (pid ) for pid in range (multi_conn_num )]
656
- responses = await asyncio .gather (* requests , loop = loop )
666
+ requests = [request (pid ) for pid in range (multi_conn_num )]
667
+ responses = await asyncio .gather (* requests , return_exceptions = True )
657
668
658
- assert len (responses ) == multi_conn_num
659
- assert {resp .status for resp in responses } == {200 }
660
-
661
- await sess .close ()
669
+ # Filter out exceptions to count actual responses
670
+ actual_responses = [r for r in responses if isinstance (r , ClientResponse )]
671
+ assert len (actual_responses ) == multi_conn_num
672
+ assert {resp .status for resp in actual_responses } == {200 }
673
+ finally :
674
+ await sess .close ()
675
+ await conn .close ()
662
676
663
677
664
678
def _patch_ssl_transport (monkeypatch ):
@@ -809,7 +823,7 @@ async def xtest_proxy_from_env_https(proxy_test_server, get_request, mocker):
809
823
url = "https://aiohttp.io/path"
810
824
proxy = await proxy_test_server ()
811
825
mocker .patch .dict (os .environ , {"https_proxy" : str (proxy .url )})
812
- mock .patch ("pathlib.Path.is_file" , mock_is_file )
826
+ mocker .patch ("pathlib.Path.is_file" , mock_is_file )
813
827
814
828
await get_request (url = url , trust_env = True )
815
829
0 commit comments