HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/local/lib/python3.10/dist-packages/aiohttp/__pycache__/test_utils.cpython-310.pyc
o

���gY� @s�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZmZddl
mZddlmZmZmZmZmZmZmZmZmZmZmZddlmZmZddlmZddl m!Z!m"Z"dd	l#m$Z$ddl%Z%dd
l&m'Z'm(Z(m)Z)ddl*m+Z+m,Z,dd
l
m-Z-ddl.m/Z/ddl0m1Z1ddl2m3Z3ddl4m5Z5m6Z6ddl7m8Z8m9Z9ddl:m;Z;ddl<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEddlFmGZGer�ddlHmIZIndZIejJdkr�er�ddlmKZKejJdkr�ddlmLZLneZLede=d�ZMede?d�ZNejOdk�oejPdkZQejRfdeSd ejTd!ejfd"d#�ZUdeSd$eVd ejTd!ejfd%d&�ZWd!eVfd'd(�ZXGd)d*�d*e�ZYGd+d,�d,eY�ZZGd-d.�d.eY�Z[Gd/d0�d0eeNeMf�Z\Gd1d2�d2e�Z]d3ed4ed5ed!efd6d7�Z^egej_fZ`ejaejbd8fd9e`d:ecd!eej_fd;d<��Zdejbfd9e`d!ej_fd=d>�Zed\d?ej_d:ecd!dfd@dA�Zfd!ejgfdBdC�Zhd]dDeeId!ejifdEdF�Zj	d]e3e5dd�d8de3e3e3e8ddGdHdI�dJeSdKeSdLedMedNe5dOecdPedQedRedSedTe9dDeeIdUeVd?ed!eAfdVdW�Zke3e3fdXedYed!efdZd[�ZldS)^zUtilities shared by tests.�N)�ABC�abstractmethod)�
TracebackType)�
TYPE_CHECKING�Any�Callable�Generic�Iterator�List�Optional�Type�TypeVar�cast�overload)�IsolatedAsyncioTestCase�mock)�Signal)�CIMultiDict�CIMultiDictProxy)�URL)�_RequestContextManager�_RequestOptions�_WSRequestContextManager�)�
ClientSession�hdrs)�AbstractCookieJar)�ClientResponse)�ClientWebSocketResponse)�sentinel)�HttpVersion�RawRequestMessage)�
EMPTY_PAYLOAD�StreamReader)�StrOrURL)	�Application�	AppRunner�BaseRequest�
BaseRunner�Request�Server�ServerRunner�SockSite�UrlMappingMatchInfo)�_RequestHandler)�
SSLContext���)�Unpack)�Self�_ApplicationNone�_Request)�bound�posix�cygwin�host�family�returncCst|d|�S)Nr)�get_port_socket)r:r;�r>�=/usr/local/lib/python3.10/dist-packages/aiohttp/test_utils.py�get_unused_port_socketPsr@�portcCs6t�|tj�}tr|�tjtjd�|�||f�|S)Nr)�socket�SOCK_STREAM�
REUSE_ADDRESS�
setsockopt�
SOL_SOCKET�SO_REUSEADDR�bind)r:rAr;�sr>r>r?r=Vs
r=cCsPt�tjtj��}|�d�tt|��d�Wd�S1s!wYdS)z1Return a port that is unused on the current host.)�	127.0.0.1rrN)rB�AF_INETrCrHr�int�getsockname)rIr>r>r?�unused_portcs
$�rNc@sVeZdZdZddddded�dedeejded	ee	d
e
deee	ej
gejfded
dfdd�Z	d*deejded
dfdd�Zeded
efdd��Zded
efdd�Zed
e
fdd��Zed
e
fdd��Zed
efdd��Zd+dd�Zd+dd �Zd!eeed"eed#ee d
dfd$d%�Z!d,d&d'�Z"d!eeed"eed#ee d
dfd(d)�Z#dS)-�BaseTestServerF�NrJ)�scheme�loopr:rA�skip_url_asserts�socket_factoryrQrRr:rArSrT�kwargsr<cKs:||_d|_d|_||_||_d|_||_||_||_dS)NF)	�_loop�runner�_rootr:rA�_closedrQrSrT)�selfrQrRr:rArSrTrUr>r>r?�__init__ms

zBaseTestServer.__init__c
�sj�|jrdS||_|�dd�|_|jdddi|��IdH|_|j��IdH|js,d|_|j}z	t�	|j�j
}WntyCd}Ynw|dkrOd|j�d�}|dkrVtj
ntj}|�|j|j|�}|��dd	�\|_|_t|j||jd
�}|��IdH|j}|dus�J�|j}	|	dus�J�|	d��d|_|js�|jr�dnd
|_t|j�d|�d|j���|_dS)N�ssl�handler_cancellationTr���[�]�)�sock�ssl_contextr�https�httpz://�:r>)rWrV�pop�_ssl�_make_runner�setuprAr:�	ipaddress�
ip_address�version�
ValueErrorrB�AF_INET6rKrTrMr,�start�_server�socketsrQrrX)
rZrRrU�
absolute_hostrnr;�_sock�site�serverrsr>r>r?�start_server�s<��"zBaseTestServer.start_serverc�s�dS�Nr>�rZrUr>r>r?rj�s�zBaseTestServer._make_runner�pathcCsH|jdusJ�t|�}|js|jrJ�|j�|�Stt|j�t|��Sry)rXrrS�absolute�join�str)rZr{�urlr>r>r?�make_url�s
zBaseTestServer.make_urlcCs
|jduSry)rW�rZr>r>r?�started�s
zBaseTestServer.startedcC�|jSry)rYr�r>r>r?�closed��zBaseTestServer.closedcCs&|j}|dus	J�|jdusJ�|jSry)rWrw)rZrWr>r>r?�handler�szBaseTestServer.handlerc�sJ�|jr!|js#|jdusJ�|j��IdHd|_d|_d|_dSdSdS)a`Close all fixtures created by the test client.

        After that point, the TestClient is no longer usable.

        This is an idempotent function: running close multiple times
        will not have any additional effects.

        close is also run when the object is garbage collected, and on
        exit when used as a context manager.

        NT)r�r�rW�cleanuprXrArYr�r>r>r?�close�s�
�zBaseTestServer.closecC�td���NzUse async with instead��	TypeErrorr�r>r>r?�	__enter__��zBaseTestServer.__enter__�exc_type�	exc_value�	tracebackcC�dSryr>�rZr�r�r�r>r>r?�__exit__��zBaseTestServer.__exit__c�s�|j|jd�IdH|S�N�rR)rxrVr�r>r>r?�
__aenter__�s�zBaseTestServer.__aenter__c���|��IdHdSry�r�r�r>r>r?�	__aexit__���zBaseTestServer.__aexit__ry�r<N)r<rO)$�__name__�
__module__�__qualname__�__test__r=r~r�asyncio�AbstractEventLooprL�boolrrB�
AddressFamilyrr[rxrr(rjr$rr��propertyr�r�r*r�r�r�r�
BaseExceptionrr�r�r�r>r>r>r?rOjs����������
����
� 	


���
�
	
����rOcsReZdZdddd�dedededeed	ef
�fd
d�Zd	ede	fd
d�Z
�ZS)�
TestServerrPrJN�rQr:rA�apprQr:rArUc�$||_t�jd|||d�|��dS�Nr�r>)r��superr[)rZr�rQr:rArU��	__class__r>r?r[��	zTestServer.__init__r<c�s�t|jfi|��Sry)r&r�rzr>r>r?rj�s�zTestServer._make_runner)r�r�r�r%r~rrLrr[r(rj�
__classcell__r>r>r�r?r��s ������r�c
s\eZdZdddd�dedededeed	ed
df�fdd�Zdde	d	ed
e
fdd�Z�ZS)�
RawTestServerrPrJNr�r�rQr:rArUr<cr�r�)�_handlerr�r[)rZr�rQr:rArUr�r>r?r[r�zRawTestServer.__init__T�debugc�s0�t|jf|j|d�|��}t|fd|i|��S)N)rRr�r�)r*r�rVr+)rZr�rU�srvr>r>r?rjs�zRawTestServer._make_runner)T)
r�r�r�r.r~rrLrr[r�r+rjr�r>r>r�r?r��s$������� r�c@sneZdZdZdZedd�dddedeed	e	d
df
dd��Z
edd�dd
dedeed	e	d
df
dd��Z
ddd�dedeedeej
d	e	d
df
dd�Z
dNdd�Zed
efdd��Zed
eefdd��Zed
efdd��Zed
efdd��Zed
efdd��Zded
efdd �Zd!eded	e	d
efd"d#�Zejd$k�r
e �r
d!eded	e!e"d
e#fd%d&�Z$ded	e!e"d
e#fd'd(�Z%ded	e!e"d
e#fd)d*�Z&ded	e!e"d
e#fd+d,�Z'ded	e!e"d
e#fd-d.�Z(ded	e!e"d
e#fd/d0�Z)ded	e!e"d
e#fd1d2�Z*ded	e!e"d
e#fd3d4�Z+nZd!eded	e	d
e#fd5d&�Z$ded	e	d
e#fd6d(�Z%ded	e	d
e#fd7d.�Z(ded	e	d
e#fd8d*�Z&ded	e	d
e#fd9d,�Z'ded	e	d
e#fd:d0�Z)ded	e	d
e#fd;d2�Z*ded	e	d
e#fd<d4�Z+ded	e	d
e,fd=d>�Z-ded	e	d
e.fd?d@�Z/dNdAdB�Z0dNdCdD�Z1dEee2e3dFee3dGee4d
dfdHdI�Z5d
e6fdJdK�Z7dEee2e3dFee3dGee4d
dfdLdM�Z8dS)O�
TestClientzb
    A test client implementation.

    To write functional tests for aiohttp based servers.

    FN)�
cookie_jarrZz TestClient[Request, Application]rwr�rUr<cKr�ryr>�rZrwr�rUr>r>r?r[r�zTestClient.__init__zTestClient[_Request, None]cKr�ryr>r�r>r>r?r[#r�)r�rRrRcKspt|t�s
tdt|���||_||_|durtjd|d�}td||d�|��|_	d|j	_
d|_g|_g|_
dS)Nz2server must be TestServer instance, found type: %rT)�unsaferR)rRr�Fr>)�
isinstancerOr��typerrrV�aiohttp�	CookieJarr�_session�_retry_connectionrY�
_responses�_websockets)rZrwr�rRrUr>r>r?r[+s

�
c�s�|jj|jd�IdHdSr�)rrrxrVr�r>r>r?rxAs�zTestClient.start_servercC�|jjSry)rrr:r�r>r>r?r:D�zTestClient.hostcCr�ry)rrrAr�r>r>r?rAHr�zTestClient.portcCr�ry)rrr�r>r>r?rwLr�zTestClient.servercCst|jdd�S)Nr�)�getattrrrr�r>r>r?r�PszTestClient.appcCr�)z�An internal aiohttp.ClientSession.

        Unlike the methods on the TestClient, client session requests
        do not automatically include the host in the url queried, and
        will require an absolute path to the resource.

        )r�r�r>r>r?�sessionTs	zTestClient.sessionr{cCs|j�|�Sry)rrr�)rZr{r>r>r?r�_szTestClient.make_url�methodc�s4�|jj||�|�fi|��IdH}|j�|�|Sry)r��requestr�r��append)rZr�r{rU�respr>r>r?�_requestbs�"zTestClient._requestr0cKr�ryr>�rZr�r{rUr>r>r?r�lszTestClient.requestcKr�ryr>�rZr{rUr>r>r?�getp�zTestClient.getcKr�ryr>r�r>r>r?�optionsvr�zTestClient.optionscKr�ryr>r�r>r>r?�head|r�zTestClient.headcKr�ryr>r�r>r>r?�post�r�zTestClient.postcKr�ryr>r�r>r>r?�put�r�zTestClient.putcKr�ryr>r�r>r>r?�patch�r�zTestClient.patchcKr�ryr>r�r>r>r?�delete�r�zTestClient.deletecKst|j||fi|���S)z�Routes a request to tested http server.

            The interface is identical to aiohttp.ClientSession.request,
            except the loop kwarg is overridden by the instance used by the
            test server.

            )rr�r�r>r>r?r��s
cK�t|jtj|fi|���S)zPerform an HTTP GET request.)rr�r�METH_GETr�r>r>r?r���cKr�)zPerform an HTTP POST request.)rr�r�	METH_POSTr�r>r>r?r��r�cKr�)z Perform an HTTP OPTIONS request.)rr�r�METH_OPTIONSr�r>r>r?r����cKr�)zPerform an HTTP HEAD request.)rr�r�	METH_HEADr�r>r>r?r��r�cKr�)zPerform an HTTP PUT request.)rr�r�METH_PUTr�r>r>r?r��r�cKr��zPerform an HTTP PATCH request.)rr�r�
METH_PATCHr�r>r>r?r��r�cKr�r�)rr�r�METH_DELETEr�r>r>r?r��r�cKst|j|fi|���S)zjInitiate websocket connection.

        The api corresponds to aiohttp.ClientSession.ws_connect.

        )r�_ws_connectr�r>r>r?�
ws_connect�szTestClient.ws_connectc�s2�|jj|�|�fi|��IdH}|j�|�|Sry)r�r�r�r�r�)rZr{rU�wsr>r>r?r��s� zTestClient._ws_connectc�sd�|js0|jD]}|��q|jD]	}|��IdHq|j��IdH|j��IdHd|_dSdS)aHClose all fixtures created by the test client.

        After that point, the TestClient is no longer usable.

        This is an idempotent function: running close multiple times
        will not have any additional effects.

        close is also run on exit when used as a(n) (asynchronous)
        context manager.

        NT)rYr�r�r�r�rr)rZr�r�r>r>r?r��s�



�zTestClient.closecCr�r�r�r�r>r>r?r��r�zTestClient.__enter__r��exc�tbcCr�ryr>�rZr�r�r�r>r>r?r��r�zTestClient.__exit__c�s�|��IdH|Sry)rxr�r>r>r?r��s�zTestClient.__aenter__c�r�ryr�r�r>r>r?r��r�zTestClient.__aexit__r�)9r�r�r��__doc__r�rr�rrrr[rOr�r�rxr�r~r:rLrArwr5r�rr�r$rr�rr��sys�version_inforr3rrr�r�r�r�r�r�r�r�rr�rr�r�r�rr�rr�r4r�r�r>r>r>r?r�sN�����������������
�

���
����
���
���
���
���
���
���
�������
���
�


���
�	
����r�c@s�eZdZdZdefdd�Zdefdd�Zddd	�Zdd
d�Zddd
�Z	ddd�Z
dedefdd�Zdede
eeffdd�ZdS)�AioHTTPTestCaseaA base class to allow for unittest web applications using aiohttp.

    Provides the following:

    * self.client (aiohttp.test_utils.TestClient): an aiohttp test client.
    * self.loop (asyncio.BaseEventLoop): the event loop in which the
        application and server are running.
    * self.app (aiohttp.web.Application): the application returned by
        self.get_application()

    Note that the TestClient's methods are asynchronous: you have to
    execute function on the test client using asynchronous methods.
    r<c�s
�|��S)z�Get application.

        This method should be overridden
        to return the aiohttp.web.Application
        object to test.
        )�get_appr�r>r>r?�get_applications�zAioHTTPTestCase.get_applicationcCr�)zqObsolete method used to constructing web application.

        Use .get_application() coroutine instead.
        z+Did you forget to define get_application()?)�RuntimeErrorr�r>r>r?r�szAioHTTPTestCase.get_appNc�s�t��|_|��IdHSry)r��get_running_looprR�
setUpAsyncr�r>r>r?�
asyncSetUp&s�
zAioHTTPTestCase.asyncSetUpc�sN�|��IdH|_|�|j�IdH|_|�|j�IdH|_|j��IdHdSry)r�r��
get_serverrw�
get_client�clientrxr�r>r>r?r�*s
�zAioHTTPTestCase.setUpAsyncc�s�|��IdHSry)�
tearDownAsyncr�r>r>r?�
asyncTearDown1s�zAioHTTPTestCase.asyncTearDownc�s�|j��IdHdSry)r�r�r�r>r>r?r�4s�zAioHTTPTestCase.tearDownAsyncr�c���t||jd�S)zReturn a TestServer instance.r�)r�rR)rZr�r>r>r?r�7��zAioHTTPTestCase.get_serverrwc�r�)zReturn a TestClient instance.r�)r�rR)rZrwr>r>r?r�;r�zAioHTTPTestCase.get_clientr�)r�r�r�r�r%r�r�r�r�r�r�r�r�r�r)r�r>r>r>r?r�s	



r��func�argsrUcOstjdtdd�|S)zw
    A decorator dedicated to use with asynchronous AioHTTPTestCase test methods.

    In 3.8+, this does nothing.
    zBDecorator `@unittest_run_loop` is no longer needed in aiohttp 3.8+rb)�
stacklevel)�warnings�warn�DeprecationWarning)r�r�rUr>r>r?�unittest_run_loop@s�r�F�loop_factory�fastccs �t|�}|Vt||d�dS)zzA contextmanager that creates an event_loop, for test purposes.

    Handles the creation and cleanup of a test loop.
    )r�N)�setup_test_loop�teardown_test_loop)r�r�rRr>r>r?�loop_contextQs�rcCs|�}t�|�|S)z�Create and return an asyncio.BaseEventLoop instance.

    The caller should also call teardown_test_loop,
    once they are done with the loop.
    )r��set_event_loop)r�rRr>r>r?r^s
rrRcCsB|��}|s|�|j�|��|��|st��t�d�dS)z>Teardown and cleanup an event_loop created by setup_test_loop.N)	�	is_closed�	call_soon�stop�run_foreverr��gc�collectr�r)rRr�r�r>r>r?rksrcCsldtdtdtfdd�}dtdtdtddfdd�}tjtd	�}i|_||_||_d
|_t	|�|_
|j
��|S)Nr��keyr<cSs
|j|Sry��
__app_dict)r�r
r>r>r?�get_dictzs
z"_create_app_mock.<locals>.get_dict�valuecSs||j|<dSryr)r�r
rr>r>r?�set_dict}sz"_create_app_mock.<locals>.set_dict)�specF)rr~r�	MagicMockr%r�__getitem__�__setitem__�_debugr�on_response_prepare�freeze)r
rr�r>r>r?�_create_app_mockys

r�
sslcontextcs.t��}dtdttf�fdd�}||j_|S)Nr
r<cs|dkr�SdS)Nrr>)r
�rr>r?�get_extra_info�sz)_create_transport.<locals>.get_extra_info)r�Mockr~rr/r�side_effect)r�	transportrr>rr?�_create_transport�sri.)�
match_inforn�closingr��writer�protocolr�payloadr�client_max_sizerRr�r{�headersrrnr r�r!r"rr#r$cCs~t��}|
dur zt��}
Wntyt��}
d|
j_Ynw|tdd�kr)d}|r=tt	|��}t
dd�|��D��}ntt	��}d}d|�t
jd���v}t||||||d	d
|t|��
}|d	uret�}|	turmt|�}	|turxt��}|	|_|tur�t��}td	�|_td	�|_td	�|_td	�|_|	|_|	|_||_t||
||||
|d�}t|tur�in|t���}|�|�||_|S)z�Creates mocked web.Request testing purposes.

    Useful in unit tests, when spinning full web server is overkill or
    specific conditions and errors are hard to trigger.
    .r>rTcss(�|]\}}|�d�|�d�fVqdS)zutf-8N)�encode)�.0�k�vr>r>r?�	<genexpr>�s�
�z&make_mocked_request.<locals>.<genexpr>�chunkedrPNF)r$) rrr�r�r��
create_future�return_valuer rr�tuple�itemsr�r�TRANSFER_ENCODING�lowerr!rrrrr�make_mocked_coro�
write_headers�write�	write_eof�drainr!r)r-�add_app�_match_info)r�r{r%rrnr r�r!r"rr#rr$rR�task�raw_hdrsr+�message�reqr>r>r?�make_mocked_request�sn�
�
�



��
r=r-�raise_exceptioncs(dtdtdtf��fdd�}tj|d�S)zCreates a coroutine mock.r�rUr<c�s*��tur��t���s�S�IdHdSry)r�inspect�isawaitable)r�rU�r>r-r>r?�	mock_coro�s�
z#make_mocked_coro.<locals>.mock_coro)�wraps)rrr)r-r>rBr>rAr?r2�sr2)Fry)mr�r��
contextlibrr?rl�osrBr�r��abcrr�typesr�typingrrrrr	r
rrr
rr�unittestrr�	aiosignalr�	multidictrr�yarlrr��aiohttp.clientrrrrPrrr�
client_reqrepr�	client_wsr�helpersrrfr r!�streamsr"r#�typedefsr$�webr%r&r'r(r)r*r+r,r-�web_protocolr.r\r/r�r3r4r5r6�name�platformrDrKr~r�r@rLr=rNrOr�r�r�r�r�r��
_LOOP_FACTORY�contextmanager�new_event_loopr�rrrrrrrr=r2r>r>r>r?�<module>s4
,
���
����
�
w9����
��
�
���������	�
���
���
�_����