U '7`O@s dZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z m Z ddl mZddlmZmZmZmZmZmZmZmZddl mZddlmZmZddlmZddlZdd lm Z m!Z!m"Z"d d l#m$Z$m%Z%d d l m&Z&d d l'm Z d dl(m)Z)d dl*m+Z+d dl,m-Z-m.Z.d dl/m0Z0d dl1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9d dl:m;Z;erxddldkoej?dkZ@eAejdddZBeAeCejdddZDeCdddZEGd d!d!e ZFGd"d#d#eFZGGd$d%d%eFZHGd&d'd'ZIGd(d)d)e jJZKeeeed*d+d,ZLegejMfZNejOejPd-feNeQeejMd.d/d0ZRejPfeNejMd1d2d3ZSdEejMeQdd4d5d6ZTejUdd7d8ZVdFee=ejWd9d:d;ZXdGe+e-d d d-de+e+e+e+dd eAeAeee-eQeeeeeee=eCee5d?d@dAZYe+e+feeedBdCdDZZdS)HzUtilities shared by tests.N)ABCabstractmethod) TracebackType) TYPE_CHECKINGAnyCallableIteratorListOptionalTypeUnion)mock) CIMultiDictCIMultiDictProxy)URL)ClientResponse_RequestContextManager_WSRequestContextManager) ClientSessionhdrs)AbstractCookieJar)r)ClientWebSocketResponse)sentinel) HttpVersionRawRequestMessage)Signal) Application AppRunner BaseRunnerRequestServer ServerRunnerSockSiteUrlMappingMatchInfo)_RequestHandler) SSLContextposixcygwin)hostreturncCs t|dS)Nr)get_port_socket)r)r,E/opt/alt/python38/lib64/python3.8/site-packages/aiohttp/test_utils.pyget_unused_port_socket7sr.)r)portr*cCs8ttjtj}tr&|tjtjd|||f|S)Nr)socketAF_INET SOCK_STREAM REUSE_ADDRESSZ setsockoptZ SOL_SOCKETZ SO_REUSEADDRbind)r)r/sr,r,r-r+;s r+r*c Cs>ttjtj$}|d|dW5QRSQRXdS)z1Return a port that is unused on the current host.) 127.0.0.1rrN)r0r1r2r4 getsockname)r5r,r,r- unused_portFs r9c @s0eZdZdZedddddeeefee j eee e e ddddZd#ee j e ddd d Zee ed d d ZeedddZee dddZee dddZeedddZddddZddddZeeeeeeeddddZdddd Z eeeeeeeddd!d"Z!dS)$BaseTestServerFNr7)schemeloopr)r/skip_url_asserts)r;r<r)r/r=kwargsr*cKs4||_d|_d|_||_||_d|_||_||_dS)NF)_looprunner_rootr)r/_closedr;r=)selfr;r<r)r/r=r>r,r,r-__init__Ps zBaseTestServer.__init__)r<r>r*cs|jr dS||_|dd|_|jf|IdH|_|jIdH|jsNd|_t|j|j}| dd\|_|_t |j||jd}| IdH|j }|dk st |j}|dk st |d d|_|jtkr|jrd}nd}||_t|jd|jd |j|_dS) Nsslr)ZsockZ ssl_contextrZhttpshttpz://:)r@r?popZ_ssl _make_runnerZsetupr/r+r)r8r#start_serverAssertionErrorsocketsr;rrrA)rCr<r>Z_sockZsiteserverrNr;r,r,r- start_servercs.   zBaseTestServer.start_serverr>r*csdSNr,rCr>r,r,r-rJszBaseTestServer._make_runnerpathr*cCsJ|jdk stt|}|js4|r(t|j|Stt|j|SdSrR)rArMrr=Z is_absolutejoinstr)rCrUZurlr,r,r-make_urls   zBaseTestServer.make_urlr6cCs |jdk SrR)r@rCr,r,r-startedszBaseTestServer.startedcCs|jSrR)rBrYr,r,r-closedszBaseTestServer.closedcCs&|j}|dk st|jdk s t|jSrR)r@rMrO)rCr@r,r,r-handlers zBaseTestServer.handlercs@|jr<|js<|jdk st|jIdHd|_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)rZr[r@rMZcleanuprAr/rBrYr,r,r-closes zBaseTestServer.closecCs tddSNzUse async with instead TypeErrorrYr,r,r- __enter__szBaseTestServer.__enter__)exc_type exc_value tracebackr*cCsdSrRr,rCrbrcrdr,r,r-__exit__szBaseTestServer.__exit__cs|j|jdIdH|SNr<)rPr?rYr,r,r- __aenter__szBaseTestServer.__aenter__cs|IdHdSrRr]rer,r,r- __aexit__szBaseTestServer.__aexit__)N)"__name__ __module__ __qualname____test__rr rWobjectr asyncioAbstractEventLoopintboolrrDrPrrrJrrXpropertyrZr[r!r\r]rar BaseExceptionrrfrirkr,r,r,r-r:MsV       r:csPeZdZedddeeeefeee e dfddZ e e ddd Z ZS) TestServerr7Nr;r)r/)appr;r)r/r>c s$||_tjf|||d|dSNrx)rysuperrD)rCryr;r)r/r> __class__r,r-rDs zTestServer.__init__rQcst|jf|SrR)rryrSr,r,r-rJszTestServer._make_runner)rlrmrnrrr rWrpr rsrrDrrJ __classcell__r,r,r|r-rws  rwcsVeZdZedddeeeefeee e ddfddZ d e e e dd d ZZS) RawTestServerr7Nrx)r\r;r)r/r>r*c s$||_tjf|||d|dSrz)_handlerr{rD)rCr\r;r)r/r>r|r,r-rDs zRawTestServer.__init__T)debugr>r*cs.t|jf|j|d|}t|fd|i|S)N)r<rr)r!rr?r")rCrr>Zsrvr,r,r-rJszRawTestServer._make_runner)T)rlrmrnrr%r rWrpr rsrrDrtr"rJr~r,r,r|r-rs  rc@seZdZdZdZdddeeeeej e ddddZ ddd d Z e edd d Ze eedd dZe edddZe edddZe edddZeedddZeee edddZeee edddZee edddZee edd d!Zee edd"d#Zee edd$d%Z ee edd&d'Z!ee edd(d)Z"ee edd*d+Z#ee e$dd,d-Z%ee e&dd.d/Z'ddd0d1Z(ddd2d3Z)ee*e+ee+ee,dd4d5d6Z-ddd7d8Z.ee*e+ee+ee,dd4d9d:Z/dS); TestClientzb A test client implementation. To write functional tests for aiohttp based servers. FN) cookie_jarr<)rOrr<r>r*cKsht|tstdt|||_||_|dkrr,r,r-rDs  zTestClient.__init__r6cs|jj|jdIdHdSrg)rLrPr?rYr,r,r-rP szTestClient.start_servercCs|jjSrR)rLr)rYr,r,r-r)szTestClient.hostcCs|jjSrR)rLr/rYr,r,r-r/szTestClient.portcCs|jSrR)rLrYr,r,r-rOszTestClient.servercCst|jddS)Nry)getattrrLrYr,r,r-ryszTestClient.appcCs|jS)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. )rrYr,r,r-sessions zTestClient.sessionrTcCs |j|SrR)rLrX)rCrUr,r,r-rX)szTestClient.make_url)methodrUr>r*cs.|jj|||f|IdH}|j||SrR)rrequestrXrappend)rCrrUr>respr,r,r-_request,s zTestClient._requestcKst|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. )rr)rCrrUr>r,r,r-r2szTestClient.request)rUr>r*cKst|jtj|f|S)zPerform an HTTP GET request.)rrrZMETH_GETrCrUr>r,r,r-get<szTestClient.getcKst|jtj|f|S)zPerform an HTTP POST request.)rrrZ METH_POSTrr,r,r-post@szTestClient.postcKst|jtj|f|S)z Perform an HTTP OPTIONS request.)rrrZ METH_OPTIONSrr,r,r-optionsDszTestClient.optionscKst|jtj|f|S)zPerform an HTTP HEAD request.)rrrZ METH_HEADrr,r,r-headHszTestClient.headcKst|jtj|f|S)zPerform an HTTP PUT request.)rrrZMETH_PUTrr,r,r-putLszTestClient.putcKst|jtj|f|SzPerform an HTTP PATCH request.)rrrZ METH_PATCHrr,r,r-patchPszTestClient.patchcKst|jtj|f|Sr)rrrZ METH_DELETErr,r,r-deleteTszTestClient.deletecKst|j|f|S)zjInitiate websocket connection. The api corresponds to aiohttp.ClientSession.ws_connect. )r _ws_connectrr,r,r- ws_connectXszTestClient.ws_connectcs,|jj||f|IdH}|j||SrR)rrrXrr)rCrUr>wsr,r,r-r`s zTestClient._ws_connectcs^|jsZ|jD] }|q |jD]}|IdHq |jIdH|jIdHd|_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)rBrr]rrrL)rCrrr,r,r-r]es    zTestClient.closecCs tddSr^r_rYr,r,r-razszTestClient.__enter__)rbexctbr*cCsdSrRr,rCrbrrr,r,r-rf}szTestClient.__exit__cs|IdH|SrR)rPrYr,r,r-riszTestClient.__aenter__cs|IdHdSrRrjrr,r,r-rkszTestClient.__aexit__)0rlrmrn__doc__ror:r rrqrrrrDrPrurWr)rsr/rOrryrrrrXrrrrrrrrrrrrrrrr]rar rvrrfrirkr,r,r,r-rs^      rc@seZdZdZedddZedddZdddd Zddd d Zddd d Z ddddZ ee dddZ e e dddZdS)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. r6cs|S)zy This method should be overridden to return the aiohttp.web.Application object to test. )get_apprYr,r,r-get_applicationszAioHTTPTestCase.get_applicationcCs tddS)zqObsolete method used to constructing web application. Use .get_application() coroutine instead z+Did you forget to define get_application()?N) RuntimeErrorrYr,r,r-rszAioHTTPTestCase.get_appNcCslt|_|j||_|j||j|_|j||j|_|j|j |j| dSrR) setup_test_loopr<run_until_completerry get_serverrO get_clientclientrP setUpAsyncrYr,r,r-setUps zAioHTTPTestCase.setUpcsdSrRr,rYr,r,r-rszAioHTTPTestCase.setUpAsynccCs0|j||j|jt|jdSrR)r<r tearDownAsyncrr]teardown_test_looprYr,r,r-tearDownszAioHTTPTestCase.tearDowncsdSrRr,rYr,r,r-rszAioHTTPTestCase.tearDownAsync)ryr*cst||jdS)zReturn a TestServer instance.rh)rwr<)rCryr,r,r-rszAioHTTPTestCase.get_server)rOr*cst||jdS)zReturn a TestClient instance.rh)rr<)rCrOr,r,r-rszAioHTTPTestCase.get_client)rlrmrnrrrrrrrrrwrrrr,r,r,r-rs  r)funcargsr>r*cs.tjf||ttttdfdd }|S)zA decorator dedicated to use with asynchronous methods of an AioHTTPTestCase. Handles executing an asynchronous function, using the self.loop of the AioHTTPTestCase. )rC inner_args inner_kwargsr*cs|j|f||SrR)r<r)rCrrrr,r-new_funcsz#unittest_run_loop..new_func) functoolswrapsr)rrr>rr,rr-unittest_run_loopsrF) loop_factoryfastr*ccst|}|Vt||ddS)zzA contextmanager that creates an event_loop, for test purposes. Handles the creation and cleanup of a test loop. )rN)rr)rrr<r,r,r- loop_contextsr)rr*c Cs|}z|jj}d|k}Wntk r2d}YnXt|tjdkr|st}t}| |t t | |W5QRX|S)zCreate and return an asyncio.BaseEventLoop instance. The caller should also call teardown_test_loop, once they are done with the loop. ZuvloopTZwin32)r}rmAttributeErrorrqset_event_loopsysplatformZget_event_loop_policyZSafeChildWatcherZ attach_loop contextlibsuppressNotImplementedErrorZset_child_watcher)rr<moduleZ skip_watcherZpolicyZwatcherr,r,r-rs      r)r<rr*cCsB|}|s(||j|||s4ttddS)zHTeardown and cleanup an event_loop created by setup_test_loop. N) Z is_closedZ call_soonstopZ run_foreverr]gcZcollectrqr)r<rr[r,r,r-r s rcCs^tttddd}tttdddd}t}i|_||_||_d|_t||_ |j |S)N)rykeyr*cSs |j|SrR __app_dict)ryrr,r,r-get_dictsz"_create_app_mock..get_dict)ryrvaluer*cSs||j|<dSrRr)ryrrr,r,r-set_dict!sz"_create_app_mock..set_dictF) rrWr MagicMockr __getitem__ __setitem__Z_debugrZon_response_prepareZfreeze)rrryr,r,r-_create_app_mocks  r) sslcontextr*cs,t}tttdfdd }||j_|S)N)rr*cs|dkr SdSdS)Nrr,)rrr,r-get_extra_info2sz)_create_transport..get_extra_info)r MockrWr r&rZ side_effect)r transportrr,rr-_create_transport/sri.) match_infoversionclosingrywriterprotocolrpayloadrclient_max_sizer<)rrUheadersrrrryrrrrrrr<r*c  Cstt}| dkr t} d| j_|tddkr2d}|rZtt|}tdd|D}ntt}d}d| t j d k}t ||||||d d |t| }|d krt}| tkrt| } |tkrt}| |_|tkrt}td |_td |_td |_td |_| |_| |_||_| tkr.t} t|| |||| | d }t|tkrTin|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. .r,rTcss&|]\}}|d|dfVqdS)zutf-8N)encode).0kvr,r,r- ^sz&make_mocked_request..chunkedFN)r)r rZ create_future return_valuerrrtupleitemsrrZTRANSFER_ENCODINGlowerrrrrrrmake_mocked_coroZ write_headerswriteZ write_eofZdrainrr r$Zadd_appZ _match_info)rrUrrrrryrrrrrrr<ZtaskZraw_hdrsrmessageZreqr,r,r-make_mocked_request<sx           r)rraise_exceptionr*cs$tttdfdd }tj|dS)zCreates a coroutine mock.)rr>r*cs(tk r tsSIdHdSrR)rinspectZ isawaitable)rr>rrr,r- mock_coros  z#make_mocked_coro..mock_coro)r)rr r)rrrr,rr-rsr)F)N)N)[rrqrrrrosr0rZunittestabcrrtypesrtypingrrrrr r r r r Z multidictrrZyarlrrZaiohttp.clientrrrrrrrZ client_reqrepZ client_wsrZhelpersrrGrrZsignalsrZwebrrrr r!r"r#r$Z web_protocolr%rEr&namerr3rWr.rsr+r9r:rwrrZTestCaserrrrZ _LOOP_FACTORYcontextmanagerZnew_event_looprtrrrrrrrrrr,r,r,r-s (       (  |(@   ]