U
    ;gU                     @   sj  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZmZ ddlmZ ddlmZmZmZmZmZmZmZmZ ddlmZmZ ddlmZ ddlmZmZ dd	l m!Z! ddl"Z"dd
l#m$Z$m%Z%m&Z& ddl'm(Z(m)Z) ddl
m*Z* ddl+m,Z, ddl-m.Z. ddl/m0Z0 ddl1m2Z2m3Z3 ddl4m5Z5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZA ddlBmCZC erddlDmEZE ndZEejFdkrerddlmGZG ejHdkoejIdkZJejKfeLejMejdddZNeLeOejMejddd ZPeOd!d"d#ZQG d$d% d%eZRG d&d' d'eRZSG d(d) d)eRZTG d*d+ d+ZUG d,d- d-eZVeeeed.d/d0ZWeg ejXf ZYejZej[d1feYe\eejX d2d3d4Z]ej[feYejXd5d6d7Z^dIejXe\dd8d9d:Z_ej`d!d;d<ZadJeeE ejbd=d>d?ZcdKe0e2ddd1de0e0e0e5dd@dAdBeLeLeee2e\eeeee6eeE eOee=dCdDdEZde0e0feeedFdGdHZedS )LzUtilities shared by tests.    N)ABCabstractmethod)TracebackType)TYPE_CHECKINGAnyCallableIteratorListOptionalTypecast)IsolatedAsyncioTestCasemock)Signal)CIMultiDictCIMultiDictProxy)URL)_RequestContextManager_RequestOptions_WSRequestContextManager   )ClientSessionhdrs)AbstractCookieJar)ClientResponse)ClientWebSocketResponse)sentinel)HttpVersionRawRequestMessage)EMPTY_PAYLOADStreamReader)StrOrURL)Application	AppRunner
BaseRunnerRequestServerServerRunnerSockSiteUrlMappingMatchInfo)_RequestHandler)
SSLContext      )Unpackposixcygwin)hostfamilyreturnc                 C   s   t | d|S )Nr   )get_port_socket)r2   r3    r6   R/opt/bot/vods_filmes/script/venv/lib/python3.8/site-packages/aiohttp/test_utils.pyget_unused_port_socket;   s    r8   )r2   portr3   r4   c                 C   s6   t  |t j}tr$|t jt jd || |f |S )Nr   )socketSOCK_STREAMREUSE_ADDRESS
setsockopt
SOL_SOCKETSO_REUSEADDRbind)r2   r9   r3   sr6   r6   r7   r5   A   s
    r5   r4   c               
   C   sD   t  t jt j*} | d tt|  d W  5 Q R  S Q R X dS )z1Return a port that is unused on the current host.)	127.0.0.1r   r   N)r:   AF_INETr;   r@   r   intgetsockname)rA   r6   r6   r7   unused_portN   s    
rG   c                
   @   s>  e Zd ZdZdddddedeeej eee	 e
eee	ejgejf eddddZd$eej edd	d
dZeeedddZeedddZee
dddZee
dddZeedddZddddZddddZeee  ee ee  ddddZ!d dd d!Z"eee  ee ee  ddd"d#Z#dS )%BaseTestServerF NrC   )schemeloopr2   r9   skip_url_assertssocket_factory)rJ   rK   r2   r9   rL   rM   kwargsr4   c                K   s:   || _ d | _d | _|| _|| _d| _|| _|| _|| _d S )NF)	_looprunner_rootr2   r9   _closedrJ   rL   rM   )selfrJ   rK   r2   r9   rL   rM   rN   r6   r6   r7   __init__X   s    zBaseTestServer.__init__)rK   rN   r4   c           
         sr  | j r
d S || _|dd | _| jf ddi|I d H | _ | j  I d H  | jsVd| _| j}zt	| jj
}W n tk
r   d}Y nX |dkrd| j d}|dkrtjntj}| | j| j|}| d d	 \| _| _t| j || jd
}| I d H  |j}|d k	st|j}	|	d k	s$t|	d  d | _| jsP| jrJdnd| _t| j d| d| j | _d S )NsslZhandler_cancellationTr         []   )sockssl_contextr   httpshttpz://:)rP   rO   pop_ssl_make_runnersetupr9   r2   	ipaddress
ip_addressversion
ValueErrorr:   AF_INET6rD   rM   rF   r(   start_serverAssertionErrorsocketsrJ   r   rQ   )
rS   rK   rN   Zabsolute_hostrf   r3   _socksiteserverrl   r6   r6   r7   start_servero   s8    
zBaseTestServer.start_serverrN   r4   c                    s   d S Nr6   rS   rN   r6   r6   r7   rb      s    zBaseTestServer._make_runnerpathr4   c                 C   sL   | j d k	stt|}| js2|jr&t| j |S tt| j t| S d S rr   )rQ   rk   r   rL   absolutejoinstr)rS   ru   urlr6   r6   r7   make_url   s    
zBaseTestServer.make_urlrB   c                 C   s
   | j d k	S rr   )rP   rS   r6   r6   r7   started   s    zBaseTestServer.startedc                 C   s   | j S rr   )rR   r{   r6   r6   r7   closed   s    zBaseTestServer.closedc                 C   s&   | j }|d k	st|jd k	s t|jS rr   )rP   rk   ro   )rS   rP   r6   r6   r7   handler   s    zBaseTestServer.handlerc                    s@   | j r<| js<| jdk	st| j I dH  d| _d| _d| _dS )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}   rP   rk   cleanuprQ   r9   rR   r{   r6   r6   r7   close   s    zBaseTestServer.closec                 C   s   t dd S NzUse async with instead	TypeErrorr{   r6   r6   r7   	__enter__   s    zBaseTestServer.__enter__)exc_type	exc_value	tracebackr4   c                 C   s   d S rr   r6   rS   r   r   r   r6   r6   r7   __exit__   s    zBaseTestServer.__exit__c                    s   | j | jdI d H  | S NrK   )rp   rO   r{   r6   r6   r7   
__aenter__   s    zBaseTestServer.__aenter__c                    s   |   I d H  d S rr   r   r   r6   r6   r7   	__aexit__   s    zBaseTestServer.__aexit__)N)$__name__
__module____qualname____test__r5   rx   r
   asyncioAbstractEventLooprE   boolr   r:   AddressFamilyr   rT   rp   r   r$   rb   r!   r   rz   propertyr|   r}   r&   r~   r   r   r   BaseExceptionr   r   r   r   r6   r6   r6   r7   rH   U   s^      	
	
rH   c                       sH   e Zd Zddddeeeee ed fddZee	dd	d
Z
  ZS )
TestServerrI   rC   NrJ   r2   r9   )apprJ   r2   r9   rN   c                   s$   || _ t jf |||d| d S Nr   )r   superrT   )rS   r   rJ   r2   r9   rN   	__class__r6   r7   rT      s    	zTestServer.__init__rq   c                    s   t | jf|S rr   )r#   r   rs   r6   r6   r7   rb      s    zTestServer._make_runner)r   r   r   r"   rx   r
   rE   r   rT   r$   rb   __classcell__r6   r6   r   r7   r      s   r   c                       sN   e Zd Zddddeeeee edd fddZde	ee
d	d
dZ  ZS )RawTestServerrI   rC   Nr   )r~   rJ   r2   r9   rN   r4   c                   s$   || _ t jf |||d| d S r   )_handlerr   rT   )rS   r~   rJ   r2   r9   rN   r   r6   r7   rT      s    	zRawTestServer.__init__T)debugrN   r4   c                    s.   t | jf| j|d|}t|fd|i|S )N)rK   r   r   )r&   r   rO   r'   )rS   r   rN   Zsrvr6   r6   r7   rb      s    zRawTestServer._make_runner)T)r   r   r   r*   rx   r
   rE   r   rT   r   r'   rb   r   r6   r6   r   r7   r      s   r   c                   @   s  e Zd ZdZdZdddeee eej	 e
ddddZddd	d
ZeedddZeee dddZeedddZeee dddZeedddZeedddZeee
edddZejdkrereeee  e!dddZ"eee  e!ddd Z#eee  e!dd!d"Z$eee  e!dd#d$Z%eee  e!dd%d&Z&eee  e!dd'd(Z'eee  e!dd)d*Z(eee  e!dd+d,Z)neee
e!dd-dZ"ee
e!dd.d Z#ee
e!dd/d&Z&ee
e!dd0d"Z$ee
e!dd1d$Z%ee
e!dd2d(Z'ee
e!dd3d*Z(ee
e!dd4d,Z)ee
e*dd5d6Z+ee
e,dd7d8Z-ddd9d:Z.ddd;d<Z/ee0e1  ee1 ee2 dd=d>d?Z3d dd@dAZ4ee0e1  ee1 ee2 dd=dBdCZ5dS )D
TestClientzb
    A test client implementation.

    To write functional tests for aiohttp based servers.

    FN)
cookie_jarrK   )ro   r   rK   rN   r4   c                K   sh   t |tstdt| || _|| _|d kr<tjd|d}tf ||d|| _	d| _
g | _g | _d S )Nz2server must be TestServer instance, found type: %rT)unsaferK   )rK   r   F)
isinstancerH   r   typerj   rO   aiohttp	CookieJarr   _sessionrR   
_responses_websockets)rS   ro   r   rK   rN   r6   r6   r7   rT     s    

zTestClient.__init__rB   c                    s   | j j| jdI d H  d S r   )rj   rp   rO   r{   r6   r6   r7   rp     s    zTestClient.start_serverc                 C   s   | j jS rr   )rj   r2   r{   r6   r6   r7   r2     s    zTestClient.hostc                 C   s   | j jS rr   )rj   r9   r{   r6   r6   r7   r9   "  s    zTestClient.portc                 C   s   | j S rr   )rj   r{   r6   r6   r7   ro   &  s    zTestClient.serverc                 C   s   t tt t| jdd S )Nr   )r   r
   r"   getattrrj   r{   r6   r6   r7   r   *  s    zTestClient.appc                 C   s   | j S )zAn 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{   r6   r6   r7   session.  s    	zTestClient.sessionrt   c                 C   s   | j |S rr   )rj   rz   )rS   ru   r6   r6   r7   rz   9  s    zTestClient.make_url)methodru   rN   r4   c                    s.   | j j|| |f|I d H }| j| |S rr   )r   requestrz   r   append)rS   r   ru   rN   respr6   r6   r7   _request<  s    zTestClient._requestr,   c                 K   s   d S rr   r6   rS   r   ru   rN   r6   r6   r7   r   F  s    zTestClient.request)ru   rN   r4   c                 K   s   d S rr   r6   rS   ru   rN   r6   r6   r7   getJ  s    zTestClient.getc                 K   s   d S rr   r6   r   r6   r6   r7   optionsP  s    zTestClient.optionsc                 K   s   d S rr   r6   r   r6   r6   r7   headV  s    zTestClient.headc                 K   s   d S rr   r6   r   r6   r6   r7   post\  s    zTestClient.postc                 K   s   d S rr   r6   r   r6   r6   r7   putb  s    zTestClient.putc                 K   s   d S rr   r6   r   r6   r6   r7   patchh  s    zTestClient.patchc                 K   s   d S rr   r6   r   r6   r6   r7   deleten  s    zTestClient.deletec                 K   s   t | j||f|S )zRoutes 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.

            )r   r   r   r6   r6   r7   r   v  s    
c                 K   s   t | jtj|f|S )zPerform an HTTP GET request.)r   r   r   ZMETH_GETr   r6   r6   r7   r     s    c                 K   s   t | jtj|f|S )zPerform an HTTP POST request.)r   r   r   Z	METH_POSTr   r6   r6   r7   r     s    c                 K   s   t | jtj|f|S )z Perform an HTTP OPTIONS request.)r   r   r   ZMETH_OPTIONSr   r6   r6   r7   r     s    c                 K   s   t | jtj|f|S )zPerform an HTTP HEAD request.)r   r   r   Z	METH_HEADr   r6   r6   r7   r     s    c                 K   s   t | jtj|f|S )zPerform an HTTP PUT request.)r   r   r   ZMETH_PUTr   r6   r6   r7   r     s    c                 K   s   t | jtj|f|S zPerform an HTTP PATCH request.)r   r   r   Z
METH_PATCHr   r6   r6   r7   r     s    c                 K   s   t | jtj|f|S r   )r   r   r   ZMETH_DELETEr   r6   r6   r7   r     s    c                 K   s   t | j|f|S )zjInitiate websocket connection.

        The api corresponds to aiohttp.ClientSession.ws_connect.

        )r   _ws_connectr   r6   r6   r7   
ws_connect  s    zTestClient.ws_connectc                    s,   | j j| |f|I d H }| j| |S rr   )r   r   rz   r   r   )rS   ru   rN   wsr6   r6   r7   r     s    zTestClient._ws_connectc                    s^   | j sZ| jD ]}|  q| jD ]}| I dH  q | j I dH  | j I dH  d| _ dS )aH  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 on exit when used as a(n) (asynchronous)
        context manager.

        NT)rR   r   r   r   r   rj   )rS   r   r   r6   r6   r7   r     s    


zTestClient.closec                 C   s   t dd S r   r   r{   r6   r6   r7   r     s    zTestClient.__enter__)r   exctbr4   c                 C   s   d S rr   r6   rS   r   r   r   r6   r6   r7   r     s    zTestClient.__exit__c                    s   |   I d H  | S rr   )rp   r{   r6   r6   r7   r     s    zTestClient.__aenter__c                    s   |   I d H  d S rr   r   r   r6   r6   r7   r     s    zTestClient.__aexit__)6r   r   r   __doc__r   rH   r
   r   r   r   r   rT   rp   r   rx   r2   rE   r9   ro   r"   r   r   r   r!   r   rz   r   r   sysversion_infor   r/   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r6   r6   r6   r7   r      s   
    	  	 
	
r   c                   @   s   e Zd ZdZedddZedddZdddd	Zddd
dZddddZ	ddddZ
eedddZeedddZdS )AioHTTPTestCasea  A 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.
    rB   c                    s   |   S )zGet application.

        This method should be overridden
        to return the aiohttp.web.Application
        object to test.
        )get_appr{   r6   r6   r7   get_application  s    zAioHTTPTestCase.get_applicationc                 C   s   t ddS )zqObsolete method used to constructing web application.

        Use .get_application() coroutine instead.
        z+Did you forget to define get_application()?N)RuntimeErrorr{   r6   r6   r7   r     s    zAioHTTPTestCase.get_appNc                    s   t  | _|  I d H S rr   )r   get_running_looprK   
setUpAsyncr{   r6   r6   r7   
asyncSetUp   s    
zAioHTTPTestCase.asyncSetUpc                    sL   |   I d H | _| | jI d H | _| | jI d H | _| j I d H  d S rr   )r   r   
get_serverro   
get_clientclientrp   r{   r6   r6   r7   r     s    zAioHTTPTestCase.setUpAsyncc                    s   |   I d H S rr   )tearDownAsyncr{   r6   r6   r7   asyncTearDown  s    zAioHTTPTestCase.asyncTearDownc                    s   | j  I d H  d S rr   )r   r   r{   r6   r6   r7   r     s    zAioHTTPTestCase.tearDownAsync)r   r4   c                    s   t || jdS )zReturn a TestServer instance.r   )r   rK   )rS   r   r6   r6   r7   r     s    zAioHTTPTestCase.get_server)ro   r4   c                    s   t || jdS )zReturn a TestClient instance.r   )r   rK   )rS   ro   r6   r6   r7   r     s    zAioHTTPTestCase.get_client)r   r   r   r   r"   r   r   r   r   r   r   r   r   r   r   r6   r6   r6   r7   r     s   	r   )funcargsrN   r4   c                 O   s   t jdtdd | 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+rZ   )
stacklevel)warningswarnDeprecationWarning)r   r   rN   r6   r6   r7   unittest_run_loop  s    r   F)loop_factoryfastr4   c                 c   s   t | }|V  t||d dS )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   rK   r6   r6   r7   loop_context+  s    r   )r   r4   c                 C   s   |  }t | |S )zCreate 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   rK   r6   r6   r7   r   8  s    
r   )rK   r   r4   c                 C   sB   |   }|s(| | j |   |   |s4t  td dS )z>Teardown and cleanup an event_loop created by setup_test_loop.N)	Z	is_closedZ	call_soonstopZrun_foreverr   gcZcollectr   r   )rK   r   r}   r6   r6   r7   r   E  s    r   c                  C   sb   t tt ddd} t tt d ddd}tjtd}i |_| |_||_d|_t	||_
|j
  |S )	N)r   keyr4   c                 S   s
   | j | S rr   
__app_dict)r   r   r6   r6   r7   get_dictT  s    z"_create_app_mock.<locals>.get_dict)r   r   valuer4   c                 S   s   || j |< d S rr   r   )r   r   r   r6   r6   r7   set_dictW  s    z"_create_app_mock.<locals>.set_dict)specF)r   rx   r   	MagicMockr"   r   __getitem____setitem___debugr   Zon_response_preparefreeze)r   r   r   r6   r6   r7   _create_app_mockS  s    

r   )
sslcontextr4   c                    s,   t  }ttt d fdd}||j_|S )N)r   r4   c                    s   | dkr S d S d S )Nr   r6   )r   r   r6   r7   get_extra_infoh  s    z)_create_transport.<locals>.get_extra_info)r   Mockrx   r
   r+   r   Zside_effect)r   	transportr   r6   r   r7   _create_transporte  s    r   i   .)
match_inforf   closingr   writerprotocolr   payloadr   client_max_sizerK   )r   ru   headersr   rf   r   r   r   r   r   r   r   r   rK   r4   c                C   s  t  }|dkrBzt }W n$ tk
r@   t  }d|j_Y nX |tddk rTd}|r|tt	|}t
dd | D }ntt	 }d}d|tjd k}t| |||||d	d
|t|
}|d	krt }|	tkrt|}	|tkrt  }|	|_|tkr2t  }td	|_td	|_td	|_td	|_|	|_|	|_||_t||
|||||d}t|tkrdi n|t  }|| ||_|S )zCreates 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.
    .r6   r   Tc                 s   s&   | ]\}}| d | d fV  qdS )zutf-8N)encode).0kvr6   r6   r7   	<genexpr>  s    z&make_mocked_request.<locals>.<genexpr>chunkedrI   NF)r   ) r   r   r   r   r   Zcreate_futurereturn_valuer   r   r   tupleitemsr   r   ZTRANSFER_ENCODINGlowerr   r   r   r   r   r   make_mocked_coroZwrite_headerswrite	write_eofZdrainr   r%   r)   Zadd_appZ_match_info)r   ru   r   r   rf   r   r   r   r   r   r   r   r   rK   taskZraw_hdrsr  messagereqr6   r6   r7   make_mocked_requestr  sz    






       
r  )r  raise_exceptionr4   c                    s$   t t t d fdd}tj|dS )zCreates a coroutine mock.)r   rN   r4   c                     s(    t k	r tsS I d H  d S rr   )r   inspectisawaitable)r   rN   r  r  r6   r7   	mock_coro  s
    
z#make_mocked_coro.<locals>.mock_coro)wraps)r   r   r   )r  r  r  r6   r  r7   r    s    r  )F)N)N)fr   r   
contextlibr   r  rd   osr:   r   r   abcr   r   typesr   typingr   r   r   r   r	   r
   r   r   Zunittestr   r   Z	aiosignalr   Z	multidictr   r   Zyarlr   r   Zaiohttp.clientr   r   r   rI   r   r   r   Zclient_reqrepr   Z	client_wsr   Zhelpersr   r^   r   r   streamsr   r    Ztypedefsr!   Zwebr"   r#   r$   r%   r&   r'   r(   r)   Zweb_protocolr*   rU   r+   r   r/   nameplatformr<   rD   rx   r   r8   rE   r5   rG   rH   r   r   r   r   r   r   Z_LOOP_FACTORYcontextmanagerZnew_event_loopr   r   r   r   r   r   r   r   r  r  r6   r6   r6   r7   <module>   s   ((
     f9   _  