a cp@sddlZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z ddl mZmZddlZddlZddlZddlmZddlmZddlmZmZmZmZmZddlmZejd krdd lm Z n dd l!m Z ej"gej#ej$j%fZ&ej"eegej'dfZ(ej"ege(fZ)ej"eeegej'dfZ*ej+e,ej-e,ej.e,ffZ/ej-e)e*fe0d d d Z1GdddZ2Gddde Z3Gddde4Z5GdddZ6Gdddej7Z8Gdddej9Z:dS)N)Future) GeneratorType)unquoteurljoin)StapledObjectStream)is_async_callable)ASGIAppMessageReceiveScopeSend)WebSocketDisconnect)) TypedDictappreturncCst|rt|dSt|S)N __await__)inspectisclasshasattrrrra/workspaces/shunt/resources/test-fastapi/venv/lib/python3.9/site-packages/starlette/testclient.py _is_asgi3(s  rc@s4eZdZdZeddddZeeeddddZ dS) _WrapASGI2z7 Provide an ASGI3 interface onto an ASGI2 app. NrcCs ||_dSNr)selfrrrr__init__3sz_WrapASGI2.__init__)scopereceivesendrcs||}|||IdHdSrr)rr r!r"instancerrr__call__6s z_WrapASGI2.__call__) __name__ __module__ __qualname____doc__ASGI2Apprr r r r$rrrrr.src@s*eZdZUeed<ejeejfed<dS) _AsyncBackendbackendbackend_optionsN)r%r&r'str__annotations__typingDictAnyrrrrr*;s r*c@seZdZdddddZdS)_UpgradeWebSocketTestSessionN)sessionrcCs ||_dSr)r4)rr4rrrrAsz_Upgrade.__init__)r%r&r'rrrrrr2@sr2c@seZdZeeeddddZddddZej ddd d Z ddd d Z e dd dZ e ddddZe ddddZe ddddZeddddZeddddZd,ej eddddZd-edd d!d"Ze dd#d$Zedd%d&Zedd'd(Zd.eej d)d*d+ZdS)/r3N)rr portal_factoryrcCs6||_||_d|_||_t|_t|_d|_dSr) rr accepted_subprotocolr5queueQueue_receive_queue _send_queue extra_headers)rrr r5rrrrFs  zWebSocketTestSession.__init__rcCst|_|j||_z2|j|j}|ddi| }| |Wnt yl|j Yn0| dd|_| dd|_|S)Ntypezwebsocket.connect subprotocolheaders) contextlib ExitStack exit_stack enter_contextr5portalstart_task_soon_runr"r!_raise_on_close Exceptionclosegetr6r;)r_messagerrr __enter__Ts   zWebSocketTestSession.__enter__argsrcGsNz|dW|jn |j0|jsJ|j}t|tr&|q&dS)N)rIrBr:emptyrJ isinstance BaseException)rrOrLrrr__exit__ds    zWebSocketTestSession.__exit__c sb|j}|j}|j}z||||IdHWn2ty\}z|j|WYd}~n d}~00dS)zE The sub-thread in which the websocket session runs. N)r _asgi_receive _asgi_sendrrSr:put)rr r!r"excrrrrFns zWebSocketTestSession._runcs&|jrtdIdHq|jS)Nr)r9rQanyiosleeprJrrrrrU{s z"WebSocketTestSession._asgi_receiverLrcs|j|dSr)r:rWrrLrrrrVszWebSocketTestSession._asgi_sendcCs*|ddkr&t|dd|dddS)Nr=zwebsocket.closecoderPreason)r rJr]rrrrGs z$WebSocketTestSession._raise_on_closecCs|j|dSr)r9rWr]rrrr"szWebSocketTestSession.send)datarcCs|d|ddS)Nwebsocket.receiver=textr"rrarrr send_textszWebSocketTestSession.send_textcCs|d|ddS)Nrbr=bytesrerfrrr send_bytesszWebSocketTestSession.send_bytesrd)ramodercCsJ|dvs Jt|}|dkr0|d|dn|d|dddS)Nrdbinaryrdrbrcutf-8rh)jsondumpsr"encode)rrarkrdrrr send_jsons   zWebSocketTestSession.send_jsonrP)r^rcCs|d|ddS)Nzwebsocket.disconnect)r=r^re)rr^rrrrIszWebSocketTestSession.closecCs|j}t|tr||Sr)r:rJrRrSr]rrrr!s  zWebSocketTestSession.receivecCs|}|||dS)Nrdr!rGr]rrr receive_texts z!WebSocketTestSession.receive_textcCs|}|||dS)Nrirsr]rrr receive_bytess z"WebSocketTestSession.receive_bytes)rkrcCsH|dvs J|}|||dkr0|d}n|dd}t|S)Nrlrdrirn)r!rGdecoderoloads)rrkrLrdrrr receive_jsons   z!WebSocketTestSession.receive_json)rd)rP)rd)r%r&r'ASGI3Appr _PortalFactoryTyperrMr/r1rTrFr rUrVrGr"r-rgrirjrrintrIr!rtrurxrrrrr3Es(   r3c@s8eZdZd eeeeddddZej ej ddd Z dS) _TestClientTransportTr`N)rr5raise_server_exceptions root_pathrcCs||_||_||_||_dSr)rr}r~r5)rrr5r}r~rrrrsz_TestClientTransport.__init__)requestrc sjj}jjjdd}jj}jj}jjjdd}ddddd|}d|vrl|dd\}} t| } n|}|} dj vrg} n0| |krd | fg} nd |d|  fg} | d d j D7} |d vrJj d d} | durg} ndd | dD} dt |||j|| | ddg|| g| d }t|j||j}t|ddjt |||j|| | ddg|| gdiid }dddtiddtdfdd }tddfdd }zL|.}|tj||j|||Wdn1s0YWn2tyN}z|jr:|WYd}~n d}~00|jrhs~Jd ns~d!gtd"tdd<tjfid#i}durˆ|_ |_!|S)$Nascii)encodingPi)httpwshttpswss:hostshostcSs$g|]\}}||fqSr)lowerrq.0keyvaluerrr sz7_TestClientTransport.handle_request..>rrsec-websocket-protocolcSsg|] }|qSr)strip)rrrrrr,Z websocket testclientiP) r=pathraw_pathr~scheme query_stringr?clientserver subprotocolsrz1.1http.response.debug) r= http_versionmethodrrr~rrr?rr extensionsFstreamr<csr"sIdHddiS}t|tr@|d}nl|durNd}n^t|trz,|d}t|trx|d}d|ddWStyddddYS0n|}dd|dS) Nr=zhttp.disconnectrnrz http.requestT)r=body more_body)r=r) is_setwaitreadrRr-rqrr" StopIteration)rZ body_byteschunk)rrequest_completeresponse_completerrr!s*       z4_TestClientTransport.handle_request..receiver\cs|ddkrDrJd|dd<dd|dgDd<d n|dd krs\Jd rlJd |d d}|dd}jdkrd||s܈ddn$|ddkr|dd|dddS)Nr=zhttp.response.startz1Received multiple "http.response.start" messages.status status_codecSs g|]\}}||fqSr)rvrrrrr4szE_TestClientTransport.handle_request..send..r?Tzhttp.response.bodyz.sendz(TestClient did not receive any response.i)rr?rr)"urlrnetlocrvrrquerysplitr{r?rqitemsrJrr~r3rr5r2rioBytesIOr callrYEventrSr}httpxZ ByteStreamrResponserr)rrrrrrr default_portrZ port_stringportr?r>rr r4r!r"rDrXresponser)rrrrrrrrhandle_requests       6 z#_TestClientTransport.handle_request)Tr`) r%r&r'ryrzboolr-rrRequestrrrrrrr|s r|cseZdZUdZded<dZejej j ed<d6e e e e e ejeje ejfejjeje e fdd fd d Zejejej j ddfd ddZeje eje eje ejjfdddZdddddddejjddejjdd e ejjejejjejeejejj ejejejj!ejejj"ejejjejejj#ejjfeje eje ejejj$ejjfejeje ejfej%dfddZ&dddejjddejjddejjejejj!ejejj"ejejjejejj#ejjfeje eje ejejj$ejjfejeje ejfej%d fddZ'dddejjddejjddejjejejj!ejejj"ejejjejejj#ejjfeje eje ejejj$ejjfejeje ejfej%d fddZ(dddejjddejjddejjejejj!ejejj"ejejjejejj#ejjfeje eje ejejj$ejjfejeje ejfej%d fddZ)dddddddejjddejjdd ejjejejjejeejejj ejejejj!ejejj"ejejjejejj#ejjfeje eje ejejj$ejjfejeje ejfej%dfd d!Z*dddddddejjddejjdd ejjejejjejeejejj ejejejj!ejejj"ejejjejejj#ejjfeje eje ejejj$ejjfejeje ejfej%dfd"d#Z+dddddddejjddejjdd ejjejejjejeejejj ejejejj!ejejj"ejejjejejj#ejjfeje eje ejejj$ejjfejeje ejfej%dfd$d%Z,dddejjddejjddejjejejj!ejejj"ejejjejejj#ejjfeje eje ejejj$ejjfejeje ejfej%d fd&d'Z-d7e ej.e ejejd(fd)d* Z/dd d+d,Z0ejdd-d.d/Z1dd d0d1Z2dd d2d3Z3dd d4d5Z4Z5S)8 TestClientFz Future[None]taskNrDhttp://testserverTr`asyncio) rbase_urlr}r~r+r,cookiesr?rc st||p id|_t|r,tt|}|} ntt|}t|} | |_t |j|j ||d} |durfi}| ddt j |j||| d|ddS)N)r+r,)r5r}r~z user-agentrT)rrr? transportfollow_redirectsr)r* async_backendrr/castryr)rrr|_portal_factory setdefaultsuperr) rrrr}r~r+r,rr?Zasgi_appr __class__rrrjs4    zTestClient.__init__r<ccsR|jdur|jVn:tjjfi|j}|VWdn1sD0YdSr)rDrY from_threadstart_blocking_portalr)rrDrrrrs   zTestClient._portal_factory)rallow_redirectsrcCsNtjj}|dur$d}t|t|}|dur2|}n|durJ|durJtd|S)NzMThe `allow_redirects` argument is deprecated. Use `follow_redirects` instead.z9Cannot use both `allow_redirects` and `follow_redirects`.)r_clientUSE_CLIENT_DEFAULTwarningswarnDeprecationWarning RuntimeError)rrrredirectrLrrr_choose_redirect_args zTestClient._choose_redirect_arg) contentrafilesroparamsr?rauthrrtimeoutr)rrrrarrorr?rrrrrrrc s>|j|}|| | }tj||||||||| | || |d SN) rrarrorr?rrrrr)rjoinrrr)rrrrrarrorr?rrrrrrrrrrrs"  zTestClient.request)rr?rrrrrr) rrr?rrrrrrrc s(|||} tj|||||| || dSN)rr?rrrrr)rrrJ rrrr?rrrrrrrrrrrJs zTestClient.getc s(|||} tj|||||| || dSr)rroptionsrrrrrs zTestClient.optionsc s(|||} tj|||||| || dSr)rrheadrrrrrs zTestClient.head)rrrarrorr?rrrrrrrc s0|| | }tj||||||||| || | d Sr)rrpostrrrrarrorr?rrrrrrrrrrr1s zTestClient.postc s0|| | }tj||||||||| || | d Sr)rrrWrrrrrWVs zTestClient.putc s0|| | }tj||||||||| || | d Sr)rrpatchrrrrr{s zTestClient.patchc s(|||} tj|||||| || dSr)rrdeleterrrrrs zTestClient.delete)rrkwargsrc std|}|di}|dd|dd|dd|durT|d d |||d<ztjd |fi|Wn*ty}z|j}WYd}~nd}~00td |S) Nzws://testserverr? connectionupgradezsec-websocket-keyz testserver==zsec-websocket-versionZ13rz, GETzExpected WebSocket upgrade) rrJrrrrr2r4r)rrrrr?rXr4rrrwebsocket_connects     zTestClient.websocket_connectcst}|tjjfij_|jddfdd }t t t j _ t t t j _j_j|jddfdd }|_Wdn1s0YS)Nr<cs d_dSr)rDrr[rr reset_portalsz*TestClient.__enter__..reset_portalcsjdSr)r wait_shutdownrrDrrrrsz+TestClient.__enter__..wait_shutdown)r@rArCrYrrrrDcallbackrZcreate_memory_object_streammathinf stream_sendstream_receiverElifespanrr wait_startuppop_allrB)rstackrrrrrrMs$     (zTestClient.__enter__rNcGs|jdSr)rBrI)rrOrrrrTszTestClient.__exit__c sTddi}z2|||jj|jjIdHW|jdIdHn|jdIdH0dS)Nr=r)rrr!rr")rr rrrrszTestClient.lifespancsbjddiIdHtjdfdd }|IdH}|ddvsFJ|ddkr^|IdHdS)Nr=zlifespan.startupr<cs&jIdH}|dur"j|Srrr!rresultrLr[rrr!s z(TestClient.wait_startup..receive)zlifespan.startup.completelifespan.startup.failedr)rr"r/r1rr!rLrr[rrs   zTestClient.wait_startupc stjdfdd }j4IdH`jddiIdH|IdH}|ddvsVJ|ddkrn|IdHWdIdHq1IdHs0YdS)Nr<cs&jIdH}|dur"j|Srrrr[rrr!s z)TestClient.wait_shutdown..receiver=zlifespan.shutdown)zlifespan.shutdown.completelifespan.shutdown.failedr)r/r1rrr"rrr[rrs  zTestClient.wait_shutdown)rTr`rNNN)N)6r%r&r'Z__test__r.rDr/OptionalrYabcBlockingPortalrr-rr0r1rrZ CookieTypesrr@contextmanager GeneratorrUnionZUseClientDefaultrr_typesZURLTypesZRequestContent _RequestDataZ RequestFilesZQueryParamTypesZ HeaderTypesZ AuthTypesZ TimeoutTypesrrrJrrrrWrrSequencerrMrTrrr __classcell__rrrrres   '       ,   !   !   !     )     )     )   r);r@rrrorr7sysr/rconcurrent.futuresrtypesr urllib.parserrrYZanyio.from_threadrZanyio.streams.stapledrZstarlette._utilsrZstarlette.typesrr r r r Zstarlette.websocketsr version_inforZtyping_extensionsCallableContextManagerrrrz AwaitableZ ASGIInstancer)ryMappingr-r Iterabler rrrr*rHr2r3Z BaseTransportr|ZClientrrrrrsH        s.