• File: test_utils.cpython-37.pyc
  • Full Path: /home3/farmfre2/public_html/ALFA_DATA/alfasymlink/root/opt/alt/python37/lib64/python3.7/site-packages/aiohttp/__pycache__/test_utils.cpython-37.pyc
  • Date Modified: 05/11/2022 10:20 AM
  • File size: 20.81 KB
  • MIME-type: text/x-bytecode.python
  • Charset: 8 bit
B

kaR@sPdZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZmZddl
mZddlmZmZmZmZmZmZmZmZmZddlmZddlmZddlmZmZdd	l m!Z!ddl"Z"dd
l#m$Z$m%Z%ddl&m'Z'm(Z(dd
l
m)Z)ddl*m+Z+ddl,m-Z-ddl.m/Z/m0Z0ddl1m2Z2m3Z3ddl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<ddl=m>Z>er|ddl?m@Z@ndZ@e/rddlmAZBnddlCmBZBejDdkoejEdkZFejGfeHejIejdddZJeHeKejIejdddZLeKdd d!ZMGd"d#d#eZNGd$d%d%eNZOGd&d'd'eNZPGd(d)d)ZQGd*d+d+eBZReeeed,d-d.ZSegejTfZUejVejWd/feUeXeejTd0d1d2ZYejWfeUejTd3d4d5ZZdGejTeXdd6d7d8Z[ej\dd9d:Z]dHee@ej^d;d<d=Z_dIe0e2ddd/de0e0e0e0dd>d?d@eHeHeee2eXeeeeeee@eKee8dAdBdCZ`e0e0feeedDdEdFZadS)JzUtilities shared by tests.N)ABCabstractmethod)
TracebackType)	
TYPE_CHECKINGAnyCallableIteratorListOptionalTypeUnioncast)mock)Signal)CIMultiDictCIMultiDictProxy)URL)_RequestContextManager_WSRequestContextManager)
ClientSessionhdrs)AbstractCookieJar)ClientResponse)ClientWebSocketResponse)PY_38sentinel)HttpVersionRawRequestMessage)Application	AppRunner
BaseRunnerRequestServerServerRunnerSockSiteUrlMappingMatchInfo)_RequestHandler)
SSLContext)IsolatedAsyncioTestCase)TestCaseposixcygwin)hostfamilyreturncCst|d|S)Nr)get_port_socket)r-r.r1E/opt/alt/python37/lib64/python3.7/site-packages/aiohttp/test_utils.pyget_unused_port_socketAsr3)r-portr.r/cCs6t|tj}tr$|tjtjd|||f|S)Nr)socketSOCK_STREAM
REUSE_ADDRESSZ
setsockoptZ
SOL_SOCKETZSO_REUSEADDRbind)r-r4r.sr1r1r2r0Gs
r0)r/c	Cs8ttjtj}|dtt|dSQRXdS)z1Return a port that is unused on the current host.)z	127.0.0.1rrN)r5AF_INETr6r8r
intgetsockname)r9r1r1r2unused_portTs
r=c
@sFeZdZdZeddddedeeefe	e
jee	ee
eeeejgejfeddddZd#e	e
jeddd	d
Zeeeddd
ZeedddZee
dddZee
dddZeedddZddddZddddZe	e e!e	e!e	e"ddddZ#dddd Z$e	e e!e	e!e	e"ddd!d"Z%dS)$BaseTestServerFNz	127.0.0.1)schemeloopr-r4skip_url_assertssocket_factory)r?r@r-r4rArBkwargsr/cKs:||_d|_d|_||_||_d|_||_||_||_dS)NF)	_looprunner_rootr-r4_closedr?rArB)selfr?r@r-r4rArBrCr1r1r2__init__^s
zBaseTestServer.__init__)r@rCr/c
sV|jr
dS||_|dd|_|jf|IdH|_|jIdH|jsNd|_yt|j	j
}Wntk
rxd}YnX|dkrtj
ntj}||j	|j|}|dd\|_	|_t|j||jd}|IdH|j}|dk	st|j}|dk	st|dd|_|jtkr2|jr(d}	nd	}	|	|_t|jd
|j	d|j|_dS)Nsslr)ZsockZssl_contextrZhttpshttpz://:)rErDpopZ_ssl_make_runnerZsetupr4	ipaddressZ
ip_addressr-version
ValueErrorr5ZAF_INET6r:rBr<r%start_serverAssertionErrorsocketsr?rrrF)
rHr@rCrSr.Z_sockZsiteserverrXr?r1r1r2start_serverus8
zBaseTestServer.start_server)rCr/csdS)Nr1)rHrCr1r1r2rQszBaseTestServer._make_runner)pathr/cCsJ|jdk	stt|}|js4|r(t|j|Stt|j|SdS)N)rFrWrrAZis_absolutejoinstr)rHr[Zurlr1r1r2make_urlszBaseTestServer.make_url)r/cCs
|jdk	S)N)rE)rHr1r1r2startedszBaseTestServer.startedcCs|jS)N)rG)rHr1r1r2closedszBaseTestServer.closedcCs&|j}|dk	st|jdk	s t|jS)N)rErWrY)rHrEr1r1r2handlers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)r_r`rErWZcleanuprFr4rG)rHr1r1r2closeszBaseTestServer.closecCstddS)NzUse async with instead)	TypeError)rHr1r1r2	__enter__szBaseTestServer.__enter__)exc_type	exc_value	tracebackr/cCsdS)Nr1)rHrerfrgr1r1r2__exit__szBaseTestServer.__exit__cs|j|jdIdH|S)N)r@)rZrD)rHr1r1r2
__aenter__szBaseTestServer.__aenter__cs|IdHdS)N)rb)rHrerfrgr1r1r2	__aexit__szBaseTestServer.__aexit__)N)&__name__
__module____qualname____test__rr0rr]objectr
asyncioAbstractEventLoopr;boolrr5
AddressFamilyrrIrZrr!rQrr^propertyr_r`r#rarbrdr
BaseExceptionrrhrirjr1r1r1r2r>[s>6	

r>csPeZdZedddeeeefeee	e
dfddZe
eddd	Z
ZS)

TestServerz	127.0.0.1N)r?r-r4)appr?r-r4rCcs$||_tjf|||d|dS)N)r?r-r4)rwsuperrI)rHrwr?r-r4rC)	__class__r1r2rIs	zTestServer.__init__)rCr/cst|jf|S)N)r rw)rHrCr1r1r2rQszTestServer._make_runner)rkrlrmrrrr]ror
r;rrIr!rQ
__classcell__r1r1)ryr2rvs
rvcsVeZdZedddeeeefeee	e
ddfddZdee
e
dd	d
ZZS)
RawTestServerz	127.0.0.1N)r?r-r4)rar?r-r4rCr/cs$||_tjf|||d|dS)N)r?r-r4)_handlerrxrI)rHrar?r-r4rC)ryr1r2rIs	zRawTestServer.__init__T)debugrCr/cs.t|jf|j|d|}t|fd|i|S)N)r@r}r})r#r|rDr$)rHr}rCZsrvr1r1r2rQszRawTestServer._make_runner)T)rkrlrmrr'rr]ror
r;rrIrrr$rQrzr1r1)ryr2r{sr{c@seZdZdZdZdddeeeeej	e
ddddZddd	d
Ze
edddZe
eedd
dZe
edddZe
eedddZe
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@)rYrr@rCr/cKsht|tstdt|||_||_|dkr<tjd|d}tf||d||_	d|_
g|_g|_dS)Nz2server must be TestServer instance, found type: %rT)Zunsafer@)r@rF)
isinstancer>rctyperVrDaiohttpZ	CookieJarr_sessionrG
_responses_websockets)rHrYrr@rCr1r1r2rI
s
zTestClient.__init__)r/cs|jj|jdIdHdS)N)r@)rVrZrD)rHr1r1r2rZ"szTestClient.start_servercCs|jjS)N)rVr-)rHr1r1r2r-%szTestClient.hostcCs|jjS)N)rVr4)rHr1r1r2r4)szTestClient.portcCs|jS)N)rV)rHr1r1r2rY-szTestClient.servercCstttt|jddS)Nrw)r
r
rgetattrrV)rHr1r1r2rw1szTestClient.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.

        )r)rHr1r1r2session5s	zTestClient.session)r[r/cCs|j|S)N)rVr^)rHr[r1r1r2r^@szTestClient.make_url)methodr[rCr/cs.|jj|||f|IdH}|j||S)N)rrequestr^rappend)rHrr[rCrespr1r1r2_requestCs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)rHrr[rCr1r1r2rIszTestClient.request)r[rCr/cKst|jtj|f|S)zPerform an HTTP GET request.)rrrZMETH_GET)rHr[rCr1r1r2getSszTestClient.getcKst|jtj|f|S)zPerform an HTTP POST request.)rrrZ	METH_POST)rHr[rCr1r1r2postWszTestClient.postcKst|jtj|f|S)z Perform an HTTP OPTIONS request.)rrrZMETH_OPTIONS)rHr[rCr1r1r2options[szTestClient.optionscKst|jtj|f|S)zPerform an HTTP HEAD request.)rrrZ	METH_HEAD)rHr[rCr1r1r2head_szTestClient.headcKst|jtj|f|S)zPerform an HTTP PUT request.)rrrZMETH_PUT)rHr[rCr1r1r2putcszTestClient.putcKst|jtj|f|S)zPerform an HTTP PATCH request.)rrrZ
METH_PATCH)rHr[rCr1r1r2patchgszTestClient.patchcKst|jtj|f|S)zPerform an HTTP PATCH request.)rrrZMETH_DELETE)rHr[rCr1r1r2deletekszTestClient.deletecKst|j|f|S)zjInitiate websocket connection.

        The api corresponds to aiohttp.ClientSession.ws_connect.

        )r_ws_connect)rHr[rCr1r1r2
ws_connectoszTestClient.ws_connectcs,|jj||f|IdH}|j||S)N)rrr^rr)rHr[rCwsr1r1r2rwszTestClient._ws_connectcsf|jsbx|jD]}|qWx|jD]}|IdHq&W|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)rGrrbrrrV)rHrrr1r1r2rb|szTestClient.closecCstddS)NzUse async with instead)rc)rHr1r1r2rdszTestClient.__enter__)reexctbr/cCsdS)Nr1)rHrerrr1r1r2rhszTestClient.__exit__cs|IdH|S)N)rZ)rHr1r1r2riszTestClient.__aenter__cs|IdHdS)N)rb)rHrerrr1r1r2rjszTestClient.__aexit__)0rkrlrm__doc__rnr>r
rrprqrrIrZrtr]r-r;r4rYrrwrrrr^rrrrrrrrrrrrrrrrbrdrrurrhrirjr1r1r1r2r~sP



r~c@seZdZdZedddZedddZdddd	Zddd
dZdddd
Z	ddddZ
eedddZee
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.
    )r/cs|S)zGet application.

        This method should be overridden
        to return the aiohttp.web.Application
        object to test.
        )get_app)rHr1r1r2get_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)RuntimeError)rHr1r1r2rszAioHTTPTestCase.get_appNc	CsJyt|_Wn&ttfk
r4t|_YnX|j|dS)N)	rpZget_running_loopr@AttributeErrorrget_event_loop_policyZget_event_looprun_until_complete
setUpAsync)rHr1r1r2setUps
zAioHTTPTestCase.setUpcsL|IdH|_||jIdH|_||jIdH|_|jIdHdS)N)rrw
get_serverrY
get_clientclientrZ)rHr1r1r2rszAioHTTPTestCase.setUpAsynccCs|j|dS)N)r@r
tearDownAsync)rHr1r1r2tearDownszAioHTTPTestCase.tearDowncs|jIdHdS)N)rrb)rHr1r1r2rszAioHTTPTestCase.tearDownAsync)rwr/cst||jdS)zReturn a TestServer instance.)r@)rvr@)rHrwr1r1r2rszAioHTTPTestCase.get_server)rYr/cst||jdS)zReturn a TestClient instance.)r@)r~r@)rHrYr1r1r2rszAioHTTPTestCase.get_client)rkrlrmrrrrrrrrrvrr~rr1r1r1r2rs
	r)funcargsrCr/cOstjdtdd|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+rM)
stacklevel)warningswarnDeprecationWarning)rrrCr1r1r2unittest_run_loops
rF)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)setup_test_loopteardown_test_loop)rrr@r1r1r2loop_contextsr)rr/c	Cs|}y|jj}d|k}Wntk
r2d}YnXt|tjdkr|st}yt}Wntk
r|t	}YnX|
|tt
||WdQRX|S)zCreate and return an asyncio.BaseEventLoop instance.

    The caller should also call teardown_test_loop,
    once they are done with the loop.
    ZuvloopTZwin32N)ryrlrrpset_event_loopsysplatformrZThreadedChildWatcherZSafeChildWatcherZattach_loop
contextlibsuppressNotImplementedErrorZset_child_watcher)rr@moduleZskip_watcherZpolicyZwatcherr1r1r2rs"


r)r@rr/cCsB|}|s(||j|||s4ttddS)z>Teardown and cleanup an event_loop created by setup_test_loop.N)	Z	is_closedZ	call_soonstopZrun_foreverrbgcZcollectrpr)r@rr`r1r1r2r'srcCs^tttddd}tttdddd}t}i|_||_||_d|_t||_	|j	
|S)N)rwkeyr/cSs
|j|S)N)
__app_dict)rwrr1r1r2get_dict6sz"_create_app_mock.<locals>.get_dict)rwrvaluer/cSs||j|<dS)N)r)rwrrr1r1r2set_dict9sz"_create_app_mock.<locals>.set_dictF)rr]r	MagicMockr__getitem____setitem__Z_debugrZon_response_prepareZfreeze)rrrwr1r1r2_create_app_mock5s

r)
sslcontextr/cs,t}tttdfdd}||j_|S)N)rr/cs|dkrSdSdS)Nrr1)r)rr1r2get_extra_infoJsz)_create_transport.<locals>.get_extra_info)rMockr]r
r(rZside_effect)r	transportrr1)rr2_create_transportGsri.)
match_inforSclosingrwwriterprotocolrpayloadrclient_max_sizer@)rr[headersrrSrrwrrrrrrr@r/cCstt}|
dkr t}
d|
j_|tddkr2d}|rZtt|}tdd|D}ntt}d}d|	t
jd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.
    .r1rTcss&|]\}}|d|dfVqdS)zutf-8N)encode).0kvr1r1r2	<genexpr>usz&make_mocked_request.<locals>.<genexpr>chunkedNF)r)rrZ
create_futurereturn_valuerrrtupleitemsrrZTRANSFER_ENCODINGlowerrrrrrrmake_mocked_coroZ
write_headerswriteZ	write_eofZdrainrr"r&Zadd_appZ_match_info)rr[rrrSrrwrrrrrrr@ZtaskZraw_hdrsrmessageZreqr1r1r2make_mocked_requestTsb








r)rraise_exceptionr/cs$tttdfdd}tj|dS)zCreates a coroutine mock.)rrCr/cs(tk	rtsSIdHdS)N)rinspectZisawaitable)rrC)rrr1r2	mock_coros

z#make_mocked_coro.<locals>.mock_coro)wraps)rrr)rrrr1)rrr2rsr)F)N)N)brrprrrrRosr5rrabcrrtypesrtypingrrrrr	r
rrr
ZunittestrZ	aiosignalrZ	multidictrrZyarlrrZaiohttp.clientrrrrrrZ
client_reqreprZ	client_wsrZhelpersrrrNrrZwebrr r!r"r#r$r%r&Zweb_protocolr'rJr(r)r*Z	asynctestnamerr7r:r]rsr3r;r0r=r>rvr{r~rrrqZ
_LOOP_FACTORYcontextmanagerZnew_event_looprrrrrrrrrrrr1r1r1r2<module>s,(
(= &K