Viewing File: /opt/alt/python35/lib64/python3.5/site-packages/aiohttp/__pycache__/test_utils.cpython-35.opt-1.pyc



Z>@sdZddlZddlZddlZddlZddlZddlZddlZddlm	Z	m
Z
ddlmZddlm
Z
ddlmZddlZddlmZmZdd	lmZmZdd
lmZddlmZmZddlmZdd
lmZm Z m!Z!m"Z"m#Z#m$Z$ddZ%Gddde	Z&Gddde&Z'Gddde&Z(GdddZ)Gdddej*Z+ddZ,ej-ej.dddZ/ej.dd Z0dd!d"Z1d#d$Z2dd%d&Z3dd'ed(eddd)dd*dd+ed,ed-ed.ed/ed0dd1d:d4d5d6d7Z4eed8d9Z5dS);zUtilities shared by tests.N)ABCabstractmethod)mock)CIMultiDict)URL)_RequestContextManager_WSRequestContextManager)
ClientSessionhdrs)sentinel)HttpVersionRawRequestMessage)Signal)	AppRunnerRequestServerServerRunnerTCPSiteUrlMappingMatchInfoc
CsAtjtjtj"}|jd|jdSWdQRXdS)z1Return a port that is unused on the current host.	127.0.0.1rr	N)rr)socketAF_INETSOCK_STREAMbindgetsockname)sr/test_utils.pyunused_ports
rc@seZdZdeddddddddd	d
ZdddZed
dZddZe	ddZ
e	ddZe	ddZddZ
ddZddZddZdd ZdS)!BaseTestServerschemeloopNhostz	127.0.0.1portskip_url_assertsFcKsL||_d|_d|_||_||_d|_||_||_dS)NF)_looprunner_rootr#r$_closedr!r%)selfr!r"r#r$r%kwargsrrr__init__#s							zBaseTestServer.__init__cs|jr
dS||_|jdd|_|j|IdH|_|jjIdH|jsit|_t|jd|j	d|jd|j}|j
IdH|jtkr|jrd}nd}||_t
dj|j|j	|j|_dS)NZsslr#r$Zssl_contextZhttpshttpz
{}://{}:{})r'r&popZ_ssl_make_runnerZsetupr$rrr#startr!rrformatr()r*r"r+Zsiter!rrrstart_server/s&						zBaseTestServer.start_servercsdS)Nr)r*r+rrrr/EszBaseTestServer._make_runnercCs@t|}|js%|jj|Stt|j|SdS)N)rr%r(joinstr)r*pathZurlrrrmake_urlIs	zBaseTestServer.make_urlcCs
|jdk	S)N)r')r*rrrstartedQszBaseTestServer.startedcCs|jS)N)r))r*rrrclosedUszBaseTestServer.closedcCs
|jjS)N)r'server)r*rrrhandlerYszBaseTestServer.handlercsD|jr@|jr@|jjIdHd|_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)r7r8r'cleanupr(r$r))r*rrrclose_s
		zBaseTestServer.closecCstddS)NzUse async with instead)	TypeError)r*rrr	__enter__qszBaseTestServer.__enter__cCsdS)Nr)r*exc_type	exc_value	tracebackrrr__exit__tszBaseTestServer.__exit__cs|jd|jIdH|S)Nr")r2r&)r*rrr
__aenter__xszBaseTestServer.__aenter__cs|jIdHdS)N)r<)r*r?r@rArrr	__aexit__|szBaseTestServer.__aexit__)__name__
__module____qualname__rr,r2rr/r6propertyr7r8r:r<r>rBrCrDrrrrr "sr c	s@eZdZdeddddfddZdd	ZS)

TestServerr!r#z	127.0.0.1r$Ncs/||_tjd|d|d||dS)Nr!r#r$)appsuperr,)r*rJr!r#r$r+)	__class__rrr,s	zTestServer.__init__cst|j|S)N)rrJ)r*r+rrrr/szTestServer._make_runner)rErFrGrr,r/rr)rLrrIs!rIc	sCeZdZdeddddfddZdd	d
ZS)
RawTestServerr!r#z	127.0.0.1r$Ncs/||_tjd|d|d||dS)Nr!r#r$)_handlerrKr,)r*r:r!r#r$r+)rLrrr,s	zRawTestServer.__init__Tcs4t|jd|jdd|}t|d||S)Nr"debugT)rrNr&r)r*rOr+Zsrvrrrr/szRawTestServer._make_runner)rErFrGrr,r/rr)rLrrMs!rMc@sBeZdZdZddddddZddZed	d
ZeddZed
dZ	eddZ
ddZddZddZ
ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0ZdS)1
TestClientzb
    A test client implementation.

    To write functional tests for aiohttp based servers.

    
cookie_jarNr"cKst|ts%tdt|||_||_|dkr[tjddd|}td|d|||_	d|_
g|_g|_dS)NzBserver must be web.Application TestServer instance, found type: %rZunsafeTr"rQF)
isinstancer r=type_serverr&aiohttpZ	CookieJarr
_sessionr)
_responses_websockets)r*r9rQr"r+rrrr,s				zTestClient.__init__cs|jjd|jIdHdS)Nr")rTr2r&)r*rrrr2szTestClient.start_servercCs
|jjS)N)rTr#)r*rrrr#szTestClient.hostcCs
|jjS)N)rTr$)r*rrrr$szTestClient.portcCs|jS)N)rT)r*rrrr9szTestClient.servercCs|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.

        )rV)r*rrrsessions	zTestClient.sessioncCs|jj|S)N)rTr6)r*r5rrrr6szTestClient.make_urlcs=|jj||j|||IdH}|jj||S)zRoutes 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)rVrequestr6rWappend)r*methodr5argsr+resprrrrZs	 zTestClient.requestcOst|jtj|||S)zPerform an HTTP GET request.)rrZrZMETH_GET)r*r5r]r+rrrgetszTestClient.getcOst|jtj|||S)zPerform an HTTP POST request.)rrZrZ	METH_POST)r*r5r]r+rrrpostszTestClient.postcOst|jtj|||S)z Perform an HTTP OPTIONS request.)rrZrZMETH_OPTIONS)r*r5r]r+rrroptionsszTestClient.optionscOst|jtj|||S)zPerform an HTTP HEAD request.)rrZrZ	METH_HEAD)r*r5r]r+rrrheadszTestClient.headcOst|jtj|||S)zPerform an HTTP PUT request.)rrZrZMETH_PUT)r*r5r]r+rrrputszTestClient.putcOst|jtj|||S)zPerform an HTTP PATCH request.)rrZrZ
METH_PATCH)r*r5r]r+rrrpatchszTestClient.patchcOst|jtj|||S)zPerform an HTTP PATCH request.)rrZrZMETH_DELETE)r*r5r]r+rrrdeleteszTestClient.deletecOst|j|||S)zjInitiate websocket connection.

        The api corresponds to aiohttp.ClientSession.ws_connect.

        )r_ws_connect)r*r5r]r+rrr
ws_connectszTestClient.ws_connectcs:|jj|j|||IdH}|jj||S)N)rVrgr6rXr[)r*r5r]r+wsrrrrfs	zTestClient._ws_connectcs{|jswx|jD]}|jqWx |jD]}|jIdHq1W|jjIdH|jjIdHd|_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)rWr<rXrVrT)r*r^rhrrrr<s	zTestClient.closecCstddS)NzUse async with instead)r=)r*rrrr>*szTestClient.__enter__cCsdS)Nr)r*r?r@rArrrrB-szTestClient.__exit__cs|jIdH|S)N)r2)r*rrrrC1szTestClient.__aenter__cs|jIdHdS)N)r<)r*r?r@rArrrrD5szTestClient.__aexit__)rErFrG__doc__r,r2rHr#r$r9rYr6rZr_r`rarbrcrdrergrfr<r>rBrCrDrrrrrPs.
rPc@sveZdZdZddZddZddZdd	Zd
dZdd
Z	ddZ
ddZdS)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.
    cs
|jS)zy
        This method should be overridden
        to return the aiohttp.web.Application
        object to test.

        )get_app)r*rrrget_applicationIszAioHTTPTestCase.get_applicationcCstddS)zqObsolete method used to constructing web application.

        Use .get_application() coroutine instead

        z+Did you forget to define get_application()?N)RuntimeError)r*rrrrkRszAioHTTPTestCase.get_appcCst|_|jj|j|_|jj|j|j|_|jj|j|j|_|jj|jj	|jj|j
dS)N)setup_test_loopr"run_until_completerlrJ
get_serverr9
get_clientclientr2
setUpAsync)r*rrrsetUpZs!	zAioHTTPTestCase.setUpcsdS)Nr)r*rrrrsfszAioHTTPTestCase.setUpAsynccCs@|jj|j|jj|jjt|jdS)N)r"ro
tearDownAsyncrrr<teardown_test_loop)r*rrrtearDowniszAioHTTPTestCase.tearDowncsdS)Nr)r*rrrrunszAioHTTPTestCase.tearDownAsynccst|d|jS)zReturn a TestServer instance.r")rIr")r*rJrrrrpqszAioHTTPTestCase.get_servercst|d|jS)zReturn a TestClient instance.r")rPr")r*r9rrrrquszAioHTTPTestCase.get_clientN)rErFrGrirlrkrtrsrwrurprqrrrrrj9s	rjcs+tj||fdd}|S)zA 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"ro)r*Z
inner_argsZinner_kwargs)funcrrnew_funcs	z#unittest_run_loop.<locals>.new_func)	functoolswraps)rxr]r+ryr)rxrunittest_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)rnrv)loop_factoryr}r"rrrloop_contextsrc	Csr|}tjdtjdkrntj}tj}|j|tjt	|j
|WdQRX|S)zCreate and return an asyncio.BaseEventLoop
    instance.

    The caller should also call teardown_test_loop,
    once they are done with the loop.
    Nwin32)asyncioset_event_loopsysplatformZget_event_loop_policyZSafeChildWatcherZattach_loop
contextlibsuppressNotImplementedErrorZset_child_watcher)r~r"ZpolicyZwatcherrrrrns	

rncCsW|j}|s6|j|j|j|j|sFtjtjddS)zHTeardown and cleanup an event_loop created
    by setup_test_loop.

    N)	Z	is_closedZ	call_soonstopZrun_foreverr<gcZcollectrr)r"r}r8rrrrvs


rvcCs5tj}d|_t||_|jj|S)NF)rMockZ_debugrZon_response_prepareZfreeze)rJrrr_create_app_mocks
	
rcs.tj}fdd}||j_|S)Ncs|dkrSdSdS)N
sslcontextr)key)rrrget_extra_infosz)_create_transport.<locals>.get_extra_info)rrrZside_effect)r	transportrr)rr_create_transportsr
match_infoversionclosingrJwriterpayload_writerprotocolrpayloadrclient_max_sizeir".cCstj}|dkr0tj}f|j_|tddkrKd}|rt|}tdd|jD}nt}f}d|jt	j
djk}t||||||dd|t
|
}|d	krt}|	tkr	tj}	|
tkr!t|}
|tkrBtj}|
|_|tkrtj}td	|_td	|_td	|_|
|	_||	_|tkrtj}t|||	|||d
|
}t|tkrin|tj}|j|||_|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	Tcss3|])\}}|jd|jdfVqdS)zutf-8N)encode).0kvrrr	<genexpr>sz&make_mocked_request.<locals>.<genexpr>chunkedFNr)rrZ
create_futurereturn_valuer
rtupleitemsr_rZTRANSFER_ENCODINGlowerrrrrrrmake_mocked_corowriteZ	write_eofZdrainrrrZadd_appZ_match_info)r\r5ZheadersrrrrJrrrrrrrr"ZtaskZraw_hdrsrmessageZreqrrrmake_mocked_requestsT	!						$
	rcs.tjfdd}tjd|S)zCreates a coroutine mock.cstk	rS)N)r)r]r+)raise_exceptionrrr	mock_corosz#make_mocked_coro.<locals>.mock_coror{)r	coroutinerr)rrrr)rrrrsri)6rirrrzrrrZunittestabcrrrZ	multidictrZyarlrrUZaiohttp.clientrrrr
rZhelpersrr-r
rZsignalsrZwebrrrrrrrr rIrMrPZTestCaserjr|contextmanagerZnew_event_looprrnrvrrrrrrrr<module>sX.^
A

B
Back to Directory File Manager