MOON
Server: Apache
System: Linux server30c.hostingraja.org 3.10.0-962.3.2.lve1.5.63.el7.x86_64 #1 SMP Fri Oct 8 12:03:35 UTC 2021 x86_64
User: jibhires (1887)
PHP: 8.1.30
Disabled: show_source, system, shell_exec, passthru, exec, popen, proc_open, allow_url_fopen, symlink, escapeshellcmd, pcntl_exec
Upload Files
File: //opt/alt/python35/lib64/python3.5/site-packages/aiohttp/__pycache__/test_utils.cpython-35.pyc


���Z�>�@s�dZddlZddlZddlZddlZddlZddlZddlZddlm	Z	m
Z
ddlmZddlm
Z
ddlmZddlZddlmZmZdd	lmZmZdd
lmZddlmZmZddlmZdd
lmZm Z m!Z!m"Z"m#Z#m$Z$dd�Z%Gdd�de	�Z&Gdd�de&�Z'Gdd�de&�Z(Gdd�d�Z)Gdd�dej*�Z+dd�Z,ej-ej.ddd��Z/ej.dd �Z0dd!d"�Z1d#d$�Z2dd%d&�Z3dd'ed(edd�d)dd*dd+ed,ed-ed.ed/ed0dd1d:d4d5d6d7�Z4eed8d9�Z5dS);zUtilities shared by tests.�N)�ABC�abstractmethod)�mock)�CIMultiDict)�URL)�_RequestContextManager�_WSRequestContextManager�)�
ClientSession�hdrs)�sentinel)�HttpVersion�RawRequestMessage)�Signal)�	AppRunner�Request�Server�ServerRunner�TCPSite�UrlMappingMatchInfoc
CsAtjtjtj��"}|jd�|j�dSWdQRXdS)z1Return a port that is unused on the current host.�	127.0.0.1rr	N)rr)�socket�AF_INET�SOCK_STREAM�bind�getsockname)�s�r�/test_utils.py�unused_ports
rc@s�eZdZdeddddddddd	d
�Zddd�Zed
d��Zdd�Ze	dd��Z
e	dd��Ze	dd��Zdd�Z
dd�Zdd�Zdd�Zdd �ZdS)!�BaseTestServer�scheme�loopN�hostz	127.0.0.1�port�skip_url_assertsFcKsL||_d|_d|_||_||_d|_||_||_dS)NF)�_loop�runner�_rootr#r$�_closedr!r%)�selfr!r"r#r$r%�kwargsrrr�__init__#s							zBaseTestServer.__init__c�s�|jr
dS||_|jdd�|_|j|�IdH|_|jj�IdH|jsit�|_t|jd|j	d|jd|j�}|j
�IdH|jtkr�|jr�d}nd}||_t
dj|j|j	|j��|_dS)NZsslr#r$Zssl_contextZhttps�httpz
{}://{}:{})r'r&�popZ_ssl�_make_runnerZsetupr$rrr#�startr!rr�formatr()r*r"r+Zsiter!rrr�start_server/s&						zBaseTestServer.start_serverc�sdS)Nr)r*r+rrrr/EszBaseTestServer._make_runnercCsSt|�}|js8|j�s(t�|jj|�Stt|j�|�SdS)N)rr%Zis_absolute�AssertionErrorr(�join�str)r*�pathZurlrrr�make_urlIs
	zBaseTestServer.make_urlcCs
|jdk	S)N)r')r*rrr�startedQszBaseTestServer.startedcCs|jS)N)r))r*rrr�closedUszBaseTestServer.closedcCs
|jjS)N)r'�server)r*rrr�handlerYszBaseTestServer.handlerc�sD|jr@|jr@|jj�IdHd|_d|_d|_dS)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)r8r9r'�cleanupr(r$r))r*rrr�close_s
		zBaseTestServer.closecCstd��dS)NzUse async with instead)�	TypeError)r*rrr�	__enter__qszBaseTestServer.__enter__cCsdS)Nr)r*�exc_type�	exc_value�	tracebackrrr�__exit__tszBaseTestServer.__exit__c�s|jd|j�IdH|S)Nr")r2r&)r*rrr�
__aenter__xszBaseTestServer.__aenter__c�s|j�IdHdS)N)r=)r*r@rArBrrr�	__aexit__|szBaseTestServer.__aexit__)�__name__�
__module__�__qualname__rr,r2rr/r7�propertyr8r9r;r=r?rCrDrErrrrr "sr c	s@eZdZdedddd�fdd�Zdd	�Z�S)
�
TestServerr!r#z	127.0.0.1r$Ncs/||_t�jd|d|d||�dS)Nr!r#r$)�app�superr,)r*rKr!r#r$r+)�	__class__rrr,�s	zTestServer.__init__c�st|j|�S)N)rrK)r*r+rrrr/�szTestServer._make_runner)rFrGrHrr,r/rr)rMrrJ�s!rJc	sCeZdZdedddd�fdd�Zdd	d
�Z�S)�
RawTestServerr!r#z	127.0.0.1r$Ncs/||_t�jd|d|d||�dS)Nr!r#r$)�_handlerrLr,)r*r;r!r#r$r+)rMrrr,�s	zRawTestServer.__init__Tc�s4t|jd|jdd|�}t|d||�S)Nr"�debugT)rrOr&r)r*rPr+Zsrvrrrr/�szRawTestServer._make_runner)rFrGrHrr,r/rr)rMrrN�s!rNc@sBeZdZdZdddddd�Zdd�Zed	d
��Zedd��Zed
d��Z	edd��Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�ZdS)1�
TestClientzb
    A test client implementation.

    To write functional tests for aiohttp based servers.

    �
cookie_jarNr"cKs�t|t�s%tdt|���||_||_|dkr[tjddd|�}td|d||�|_	d|_
g|_g|_dS)NzBserver must be web.Application TestServer instance, found type: %rZunsafeTr"rRF)
�
isinstancer r>�type�_serverr&�aiohttpZ	CookieJarr
�_sessionr)�
_responses�_websockets)r*r:rRr"r+rrrr,�s				zTestClient.__init__c�s|jjd|j�IdHdS)Nr")rUr2r&)r*rrrr2�szTestClient.start_servercCs
|jjS)N)rUr#)r*rrrr#�szTestClient.hostcCs
|jjS)N)rUr$)r*rrrr$�szTestClient.portcCs|jS)N)rU)r*rrrr:�szTestClient.servercCs|jS)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.

        )rW)r*rrr�session�s	zTestClient.sessioncCs|jj|�S)N)rUr7)r*r6rrrr7�szTestClient.make_urlc�s=|jj||j|�||�IdH}|jj|�|S)z�Routes a request to tested http server.

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

        N)rW�requestr7rX�append)r*�methodr6�argsr+�resprrrr[�s	 zTestClient.requestcOst|jtj|||��S)zPerform an HTTP GET request.)rr[rZMETH_GET)r*r6r^r+rrr�get�szTestClient.getcOst|jtj|||��S)zPerform an HTTP POST request.)rr[rZ	METH_POST)r*r6r^r+rrr�post�szTestClient.postcOst|jtj|||��S)z Perform an HTTP OPTIONS request.)rr[rZMETH_OPTIONS)r*r6r^r+rrr�options�szTestClient.optionscOst|jtj|||��S)zPerform an HTTP HEAD request.)rr[rZ	METH_HEAD)r*r6r^r+rrr�head�szTestClient.headcOst|jtj|||��S)zPerform an HTTP PUT request.)rr[rZMETH_PUT)r*r6r^r+rrr�put�szTestClient.putcOst|jtj|||��S)zPerform an HTTP PATCH request.)rr[rZ
METH_PATCH)r*r6r^r+rrr�patch�szTestClient.patchcOst|jtj|||��S)zPerform an HTTP PATCH request.)rr[rZMETH_DELETE)r*r6r^r+rrr�delete�szTestClient.deletecOst|j|||��S)zjInitiate websocket connection.

        The api corresponds to aiohttp.ClientSession.ws_connect.

        )r�_ws_connect)r*r6r^r+rrr�
ws_connectszTestClient.ws_connectc�s:|jj|j|�||�IdH}|jj|�|S)N)rWrhr7rYr\)r*r6r^r+�wsrrrrgs	zTestClient._ws_connectc�s{|jswx|jD]}|j�qWx |jD]}|j�IdHq1W|jj�IdH|jj�IdHd|_dS)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)r)rXr=rYrWrU)r*r_rirrrr=s	zTestClient.closecCstd��dS)NzUse async with instead)r>)r*rrrr?*szTestClient.__enter__cCsdS)Nr)r*r@rArBrrrrC-szTestClient.__exit__c�s|j�IdH|S)N)r2)r*rrrrD1szTestClient.__aenter__c�s|j�IdHdS)N)r=)r*r@rArBrrrrE5szTestClient.__aexit__)rFrGrH�__doc__r,r2rIr#r$r:rZr7r[r`rarbrcrdrerfrhrgr=r?rCrDrErrrrrQ�s.
rQc@sveZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�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.
    c�s
|j�S)zy
        This method should be overridden
        to return the aiohttp.web.Application
        object to test.

        )�get_app)r*rrr�get_applicationIszAioHTTPTestCase.get_applicationcCstd��dS)zqObsolete method used to constructing web application.

        Use .get_application() coroutine instead

        z+Did you forget to define get_application()?N)�RuntimeError)r*rrrrlRszAioHTTPTestCase.get_appcCs�t�|_|jj|j��|_|jj|j|j��|_|jj|j|j��|_|jj|jj	��|jj|j
��dS)N)�setup_test_loopr"�run_until_completermrK�
get_serverr:�
get_client�clientr2�
setUpAsync)r*rrr�setUpZs!	zAioHTTPTestCase.setUpc�sdS)Nr)r*rrrrtfszAioHTTPTestCase.setUpAsynccCs@|jj|j��|jj|jj��t|j�dS)N)r"rp�
tearDownAsyncrsr=�teardown_test_loop)r*rrr�tearDowniszAioHTTPTestCase.tearDownc�sdS)Nr)r*rrrrvnszAioHTTPTestCase.tearDownAsyncc�st|d|j�S)zReturn a TestServer instance.r")rJr")r*rKrrrrqqszAioHTTPTestCase.get_serverc�st|d|j�S)zReturn a TestClient instance.r")rQr")r*r:rrrrruszAioHTTPTestCase.get_clientN)rFrGrHrjrmrlrurtrxrvrqrrrrrrrk9s	rkcs+tj�||��fdd��}|S)z�A decorator dedicated to use with asynchronous methods of an
    AioHTTPTestCase.

    Handles executing an asynchronous function, using
    the self.loop of the AioHTTPTestCase.
    cs|jj�|||��S)N)r"rp)r*Z
inner_argsZinner_kwargs)�funcrr�new_func�s	z#unittest_run_loop.<locals>.new_func)�	functools�wraps)ryr^r+rzr)ryr�unittest_run_loopzs'r}Fccs%t|�}|Vt|d|�dS)zzA contextmanager that creates an event_loop, for test purposes.

    Handles the creation and cleanup of a test loop.
    �fastN)rorw)�loop_factoryr~r"rrr�loop_context�sr�c	Csr|�}tjd�tjdkrntj�}tj�}|j|�tjt	��|j
|�WdQRX|S)z�Create and return an asyncio.BaseEventLoop
    instance.

    The caller should also call teardown_test_loop,
    once they are done with the loop.
    N�win32)�asyncio�set_event_loop�sys�platformZget_event_loop_policyZSafeChildWatcherZattach_loop�
contextlib�suppress�NotImplementedErrorZset_child_watcher)rr"ZpolicyZwatcherrrrro�s	

rocCsW|j�}|s6|j|j�|j�|j�|sFtj�tjd�dS)zHTeardown and cleanup an event_loop created
    by setup_test_loop.

    N)	Z	is_closedZ	call_soon�stopZrun_foreverr=�gcZcollectr�r�)r"r~r9rrrrw�s


rwcCs5tj�}d|_t|�|_|jj�|S)NF)r�MockZ_debugrZon_response_prepareZfreeze)rKrrr�_create_app_mock�s
	
r�cs.tj�}�fdd�}||j_|S)Ncs|dkr�SdSdS)N�
sslcontextr)�key)r�rr�get_extra_info�sz)_create_transport.<locals>.get_extra_info)rr�r�Zside_effect)r��	transportr�r)r�r�_create_transport�sr��
match_info�version�closingrK�writer�payload_writer�protocolr��payloadr��client_max_sizei�r".cCstj�}|dkr0tj�}f|j_|tdd�krKd}|rt|�}tdd�|j�D��}nt�}f}d|jt	j
d�j�k}t||||||dd|t
|��
}|d	kr�t�}|	tkr	tj�}	|
tkr!t|�}
|tkrBtj�}|
|_|tkr�tj�}td	�|_td	�|_td	�|_|
|	_||	_|tkr�tj�}t|||	|||d
|
�}t|tkr�in|tj��}|j|�||_|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	Tcss3|])\}}|jd�|jd�fVqdS)zutf-8N)�encode)�.0�k�vrrr�	<genexpr>�sz&make_mocked_request.<locals>.<genexpr>�chunked�FNr�)rr�Z
create_future�return_valuer
r�tuple�itemsr`rZTRANSFER_ENCODING�lowerrrr�rr�r��make_mocked_coro�writeZ	write_eofZdrainr�rrZadd_appZ_match_info)r]r6Zheadersr�r�r�rKr�r�r�r�r�r�r�r"ZtaskZraw_hdrsr��messageZreqrrr�make_mocked_request�sT	!						$
	r�cs.tj��fdd��}tjd|�S)zCreates a coroutine mock.cs�tk	r���S)N)r)r^r+)�raise_exceptionr�rr�	mock_corosz#make_mocked_coro.<locals>.mock_coror|)r��	coroutinerr�)r�r�r�r)r�r�rr�sr�i)6rjr�r�r{r�rr�Zunittest�abcrrrZ	multidictrZyarlrrVZaiohttp.clientrrr�r
rZhelpersrr-r
rZsignalsrZwebrrrrrrrr rJrNrQZTestCaserkr}�contextmanagerZnew_event_loopr�rorwr�r�r�r�rrrr�<module>sX.^
�A

B