+
    Dfj5                    ^   R t ^ RIHt ^ RIHtHt ^ RIHtHtH	t	 ^ RI
Ht ^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ R	IHtHt ^ R
IHt ^ RIHtHt ^ RIHt ^ RIHt ^ RI H!t!H"t" ^ RI#H$t$ ^ RI%H&t& ^ RI'H(t(H)t)H*t* ^ RI+H,t, ]	! RRR7      t-Rt. ! R R]&4      t/ ! R R]&4      t0 ]1! R4       ^ RI2H3t3H4t4H5t5H6t6H7t7H8t8H9t9 ] ! R R]64      4       t: ! R R ]8]:,          4      t; ! R! R"]5]:,          4      t<] ! R$ R%]]-,          4      4       t>]! ].R&4       ! R' R(]4      4       t?R)#   ]= d    R#t. L;i ; i)*z%
Tests for L{twisted.web.websocket}.
)annotations)	dataclassfield)AnyGenericTypeVar)skipIf)implementer)Deferred)ConnectionDone)IPushProducer)AccumulatingProtocolMemoryReactorClock)Failure)ConnectionCompleterIOPump)SynchronousTestCase)BAD_REQUEST)AgentreadBody)IRequest)Resource)NOT_DONE_YETRequestSite)DataWSPWebSocketProtocol)boundFc                  "    ] tR t^"tR R ltRtR# )WeirdResourcec                    V ^8  d   QhRRRR/# )   requestr   returnbytes )formats   "A/usr/lib/python3/dist-packages/twisted/web/test/test_websocket.py__annotate__WeirdResource.__annotate__#   s      ' e     c                (    VP                  ^f4       R# )aw  
Per U{the wsproto documentation
<https://python-hyper.org/projects/wsproto/en/latest/api.html#wsproto.events.RejectConnection.has_body>}:

    - The only scenario in which the caller receives a RejectConnection
      with C{has_body == False} is if the peer violates sends an
      informational status code (1xx) other than 101

This is a weird edge case so we provoke it.
r+   )setResponseCodeselfr#   s   &&r(   
render_GETWeirdResource.render_GET#   s     	$r+   r&   N)__name__
__module____qualname____firstlineno__r0   __static_attributes__r&   r+   r(   r    r    "   s     r+   r    c                  &    ] tR t^2tRtR R ltRtR# )DelayedResponsez
A resource that will not respond to a C{GET} request right away.  You will
need to use the C{request.write(b'RESPONSE BODY')} and C{request.finish()}
to trigger a response.
c                    V ^8  d   QhRRRR/# )r"   r#   r   r$   intr&   )r'   s   "r(   r)   DelayedResponse.__annotate__9   s      ' c r+   c                	    Wn         \        # N)r#   r   r.   s   &&r(   r0   DelayedResponse.render_GET9   s    r+   )r#   N)r2   r3   r4   r5   __doc__r0   r6   r&   r+   r(   r8   r8   2   s     r+   r8   wsproto)ConnectionRejectedWebSocketClientEndpointWebSocketClientFactoryr   WebSocketResourceWebSocketServerFactoryWebSocketTransportc                      ] tR t^Mt$ Rt]! ]R7      tR]R&   Rt	R]R&   R R	 lt
R
 R ltR R ltR R ltR R ltR R ltR R ltR R ltRtR# )MyWSPzg
Used to implement both client-side and server-side WebSocket
application that will help with testing.
default_factoryzlist[bytes]pongsNzFailure | NonewasLostc                    V ^8  d   QhRRRR/# )r"   	transportrF   r$   Noner&   )r'   s   "r(   r)   MyWSP.__annotate__W   s     	' 	'0B 	't 	'r+   c                	    Wn         R # r=   )rN   )r/   rN   s   &&r(   negotiationStartedMyWSP.negotiationStartedW   s    &Nr+   c                   V ^8  d   QhRR/# r"   r$   rO   r&   )r'   s   "r(   r)   rP   Z   s     	 	 	r+   c                	    R # r=   r&   r/   s   &r(   negotiationFinishedMyWSP.negotiationFinishedZ   s    r+   c                    V ^8  d   QhRRRR/# )r"   reasonr   r$   rO   r&   )r'   s   "r(   r)   rP   ]   s     	" 	" 	"T 	"r+   c                	    Wn         R # r=   )rL   )r/   r[   s   &&r(   connectionLostMyWSP.connectionLost]   s    !Lr+   c                    V ^8  d   QhRRRR/# )r"   datar%   r$   rO   r&   )r'   s   "r(   r)   rP   `   s     	. 	.U 	.t 	.r+   c                	    VR 8X  d   V P                   P                  R4       R# V P                  P                  V4       R# )   request    responseN)rN   sendBytesMessage	bDeferredcallbackr/   r`   s   &&r(   bytesMessageReceivedMyWSP.bytesMessageReceived`   s/    z!//0GH''-r+   c                    V ^8  d   QhRRRR/# )r"   r`   strr$   rO   r&   )r'   s   "r(   r)   rP   f   s     	- 	-C 	-D 	-r+   c                	    VR 8X  d   V P                   P                  R4       R# V P                  P                  V4       R# )r#   responseN)rN   sendTextMessagedeferredrf   rg   s   &&r(   textMessageReceivedMyWSP.textMessageReceivedf   s.    y ..z:&&t,r+   c                    V ^8  d   QhRRRR/# )r"   payloadr%   r$   rO   r&   )r'   s   "r(   r)   rP   l   s     	' 	' 	'$ 	'r+   c                	<    V P                   P                  V4       R # r=   )rK   append)r/   rs   s   &&r(   pongReceivedMyWSP.pongReceivedl   s    JJg&r+   c                   V ^8  d   QhRR/# )r"   r$   zDeferred[str]r&   )r'   s   "r(   r)   rP   o   s     	! 	! 	!r+   c                n    \        4       V n        V P                  P                  R4       V P                  # )z:
Send a text message to the server and expect a response.
r#   )r
   ro   rN   rn   rW   s   &r(   sendRequestMyWSP.sendRequesto   s)     ,4:DMNN**95== r+   c                   V ^8  d   QhRR/# )r"   r$   zDeferred[bytes]r&   )r'   s   "r(   r)   rP   w   s     	" 	"/ 	"r+   c                n    \        4       V n        V P                  P                  R4       V P                  # )z;
Send a bytes message to the server and expect a response.
rb   )r
   re   rN   rd   rW   s   &r(   bytesRequestMyWSP.bytesRequestw   s)     /7jDNNN++J7>>!r+   )re   ro   rN   rL   )r2   r3   r4   r5   r?   r   listrK   __annotations__rL   rR   rX   r]   rh   rp   rv   rz   r~   r6   r&   r+   r(   rH   rH   M   sO    	
 #48{8"&&	'		"	.	-	'	!	" 	"r+   rH   c                  .    ] tR t^t$ R]R&   R R ltRtR# )	MyFactoryzWebSocketFixture[Any]fixturec                    V ^8  d   QhRRRR/# )r"   r#   r   r$   rH   r&   )r'   s   "r(   r)   MyFactory.__annotate__   s     	 	 	e 	r+   c                	d    \        4       pV P                  P                  P                  V4       V# r=   )rH   r   serversru   )r/   r#   news   && r(   buildProtocolMyFactory.buildProtocol   s&    'CLL  '',Jr+   r&   N)r2   r3   r4   r5   r   r   r6   r&   r+   r(   r   r      s    &&	 	r+   r   c                  "    ] tR t^tR R ltRtR# )MyClientFactoryc                    V ^8  d   QhRRRR/# )r"   urirk   r$   rH   r&   )r'   s   "r(   r)   MyClientFactory.__annotate__   s     	 	S 	U 	r+   c                	    \        4       # r=   )rH   )r/   r   s   &&r(   r   MyClientFactory.buildProtocol   s	    7Nr+   r&   N)r2   r3   r4   r5   r   r6   r&   r+   r(   r   r      s    	 	r+   r   Tc                      ] tR t^t$ ]! 4       tR]R&   ]! ]R7      tR]R&   ]! ]	R7      t
R]R&   ^PtR]R	&   ]! ]R7      tR
]R&   ]! ]R7      tR]R&   ]R R l4       tRR R lltRR R lltRtR# )WebSocketFixtureWebSocketClientFactory[WSP]clientFactoryrI   r   reactorr   resourcer:   
portNumberz	list[WSP]r   r8   slowResourcec                    V ^8  d   QhRRRR/# )r"   r   r   r$   zWebSocketFixture[WSP]r&   )r'   s   "r(   r)   WebSocketFixture.__annotate__   s       ; @U r+   c                	   V ! V4      p\        4       pW#n        VP                  P                  R \	        V4      4       VP                  P                  R\        RR4      4       VP                  P                  R\        4       4       VP                  P                  RVP                  4       VP                  P                  VP                  \        VP                  VP                  R7      4       V# )s   connects   resources	   some-datazapplication/octet-streams
   processings   slow)r   )r   r   r   putChildrD   r   r    r   r   	listenTCPr   r   )clsr   r/   serverFactorys   &&  r(   r   WebSocketFixture.new   s    =!! $z+<]+KLl,FG	
 	}mo>w(9(9:OOT$--F	
 r+   c                    V ^8  d   QhRRRR/# )r"   r   rk   r$   r   r&   )r'   s   "r(   r)   r      s     8 8 8 8r+   c                	   "   \         P                  ! V P                  V4      pVP                  V P                  4      G R j  xL
 #  L5ir=   )rB   r   r   connectr   )r/   r   clients   && r(   r   WebSocketFixture.connect   s6     (,,T\\3?^^D$6$67777s   A A	AA	c                    V ^8  d   QhRRRR/# )r"   greetboolr$   r   r&   )r'   s   "r(   r)   r      s      d f r+   c                j    \        V P                  4      pVP                  VR7      pVf   Q R4       hV# )a   
There should be a single websocket connection in progress; complete it.

@param greet: Should we immediately issue a greeting, i.e. deliver any
    pending buffered TCP data, upon connection?  If C{False}, the
    caller must call C{flush} on the result to deliver any pending
    data.
r   zConnection not in progress.)r   r   succeedOnce)r/   r   	completer	succeededs   &&  r(   completeWebSocketFixture.complete   s=     (5	)))6	$C&CC$r+   r&   N)zhttp://localhost:80/connect)T)r2   r3   r4   r5   r   r   r   r   r   r   r   r   r   r   r8   r   classmethodr   r   r   r6   r&   r+   r(   r   r      sx    16M.8"'8J"KGKx8Hh8Jt4GY4$)/$JL/J 8 r+   r   z'wsproto library required for websocketsc                      ] tR t^tR R ltR R ltR R ltR R ltR	 R
 ltR R lt	R R lt
R R ltR R ltRtR# )WebSocketTestsc                   V ^8  d   QhRR/# rU   r&   )r'   s   "r(   r)   WebSocketTests.__annotate__   s     F F Fr+   c                L   \         P                  \        4       4      p\        P                  ! VP                  4       4      pV P                  V4       V P                  \        VP                  P                  4      ^4       V P                  \        VP                  P                  4      ^4       VP                  4       pV P                  V4      pVP                  4       pV P                  V4       VP                  4        V P                  V P                  V4      R4       R# )z
Connecting to a websocket server (installed with L{WebSocketResource})
from a websocket client (connected with L{WebSocketClientEndpoint})
results in a websocket connection.
rm   N)r   r   r   r
   fromCoroutiner   assertNoResultassertEquallenr   
tcpServers
tcpClientsr   successResultOfrz   flush)r/   r   	connectedpumpwsClient	requesteds   &     r(   test_websocketWebSocketTests.test_websocket   s     #&&'89**7??+<=	I&W__778!<W__778!<!''	2((*	I&

--i8*Er+   c                   V ^8  d   QhRR/# rU   r&   )r'   s   "r(   r)   r      s     T T4 Tr+   c                n   \         P                  \        4       4      p\        P                  ! VP                  4       4      pVP                  4       pV P                  V4      pVP                  4       pV P                  V4       VP                  4        V P                  V P                  V4      R4       R# )zr
Connecting to a websocket server and sending it a bytes message results
in C{bytesMessageReceived} being called.
rc   N)r   r   r   r
   r   r   r   r   r~   r   r   r   )r/   r   r   r   r   
bRequesteds   &     r(   test_bytesMessage WebSocketTests.test_bytesMessage   s    
 #&&'89**7??+<=	!''	2**,
J'

--j9;RSr+   c                   V ^8  d   QhRR/# rU   r&   )r'   s   "r(   r)   r      s      4  44  4r+   c                  a \         P                  \        4       4      p\        P                  ! VP                  4       4       VP                  4       p. o\        \        4       ! V3R lR4      4       pV! 4       pVP                  ^ ,          P                  P                  V4       V P                  S. 4       VP                  P                  P                  4        V P                  SR.4       VP                  P                  P!                  4        V P                  SRR.4       VP                  P                  P#                  4        V P                  S. RO4       V P%                  VP                  P                  V4       VP                  ^ ,          P                  P'                  4        V P%                  VP                  P                  R4       R# )zW
Websocket transports can notify a producer of backpressure events via
attachProducer.
c                  N   < ] tR t^tR V 3R lltR V 3R lltR V 3R lltRtR# )	6WebSocketTests.test_backpressure.<locals>.TestProducerc                   V ^8  d   QhRR/# rU   r&   )r'   s   "r(   r)   CWebSocketTests.test_backpressure.<locals>.TestProducer.__annotate__   s     ( ( (r+   c                	*   < SP                  R 4       R# )pausedNru   r/   eventss   &r(   pauseProducingEWebSocketTests.test_backpressure.<locals>.TestProducer.pauseProducing   s    h'r+   c                   V ^8  d   QhRR/# rU   r&   )r'   s   "r(   r)   r      s     ) ) )r+   c                	*   < SP                  R 4       R# )resumedNr   r   s   &r(   resumeProducingFWebSocketTests.test_backpressure.<locals>.TestProducer.resumeProducing       i(r+   c                   V ^8  d   QhRR/# rU   r&   )r'   s   "r(   r)   r      s     ) )t )r+   c                	*   < SP                  R 4       R# )stoppedNr   r   s   &r(   stopProducingDWebSocketTests.test_backpressure.<locals>.TestProducer.stopProducing   r   r+   r&   N)r2   r3   r4   r5   r   r   r   r6   )r   s   r(   TestProducerr      s!    ( () )) )r+   r   r   r   N)r   r   r   )r   r   r   r
   r   r   r   r	   r   r   rN   attachProducerr   serverIOproducerr   r   r   assertIsdetachProducer)r/   r   r   r   testProducerr   s   &    @r(   test_backpressure WebSocketTests.test_backpressure   sW   
 #&&'89w01!	]	#	) 	) 
$	) $~$$33LA$--/(,..0(I!67,,.!ABdmm,,l;$$335dmm,,d3r+   c                   V ^8  d   QhRR/# rU   r&   )r'   s   "r(   r)   r      s     3 3t 3r+   c                   \         P                  \        4       4      p\        P                  ! VP                  4       4      pVP                  4       pV P                  V4      pVP                  P                  R4       V P                  VP                  . 4       VP                  4        V P                  VP                  R.4       R# )a  
Internally, Twisted's websocket resource responds to all ping requests
with a pong as required by the spec, so peers can issue a C{ping} and
receive a C{pong} message with the same payload; our websocket
transport also implements a C{ping} method to send that message.
s   123N)r   r   r   r
   r   r   r   r   rN   pingr   rK   r   r/   r   r   r   r   s   &    r(   test_pingPongWebSocketTests.test_pingPong   s     #&&'89**7??+<=	!''	2',

&2r+   c                   V ^8  d   QhRR/# rU   r&   )r'   s   "r(   r)   r     s     1 14 1r+   c                   \         P                  \        4       4      p\        P                  ! VP                  4       4      pVP                  4       pV P                  V4      pV P                  VP                  ^ ,          P                  R4       V P                  VP                  R4       VP                  P                  4        V P                  VP                  ^ ,          P                  R4       V P                  VP                  R4       VP                  4        V P                  VP                  ^ ,          P                  R4       V P                  VP                  R4       R# )z]
When the underlying TCP connection is lost,
L{WebSocketProtocol.connectionLost} is invoked.
N)r   r   r   r
   r   r   r   r   r   r   rL   rN   loseConnectionr   assertIsNotr   s   &    r(   test_serverConnectionLost(WebSocketTests.test_serverConnectionLost  s    
 #&&'89**7??+<=	!''	2gooa(00$7h&&-))+gooa(00$7h&&-

+33T:))40r+   c                   V ^8  d   QhRR/# rU   r&   )r'   s   "r(   r)   r     s     
 
$ 
r+   c                   \         P                  \        4       4      p\        VP                  4      pVP                  RR4      pV P                  V4       VP                  4        V P                  V4      pV P                  VP                  \        4       \        V4      pV P                  V P                  V4      R4       R# )z
Attempting to issue an C{HTTP GET} against a websocket server
(installed with L{WebSocketResource}) results in a C{BAD_REQUEST}
response.
s   GETs   http://localhost/connects$   websocket protocol negotiation errorN)r   r   r   r   r   r#   r   r   r   r   coder   r   )r/   r   agentrm   rbodys   &     r(   test_badWebSocketTests.test_bad  s     #&&'89goo&==)DEH%  *-{  &(O	
r+   c                   V ^8  d   QhRR/# rU   r&   )r'   s   "r(   r)   r   0  s     < < <r+   c                   \         P                  \        4       4      p\        P                  ! VP                  R4      4      pVP                  RR7      pV P                  V4       VP                  4        V P                  V\        4       R# )
Attempting to connect to a regular HTTP resource that does not support
websockets will result in the Deferred returned from
L{WebSocketClientEndpoint} failing.
zhttp://localhost/emptyFr   Nr   r   r   r
   r   r   r   r   r   failureResultOfrA   r/   r   r   r   s   &   r(   test_connectionRefused%WebSocketTests.test_connectionRefused0  sl     #&&'89**7??;S+TU	e,I&

Y(:;r+   c                   V ^8  d   QhRR/# rU   r&   )r'   s   "r(   r)   r   =  s     < <T <r+   c                   \         P                  \        4       4      p\        P                  ! VP                  R4      4      pVP                  RR7      pV P                  V4       VP                  4        V P                  V\        4       R# )r  zhttp://localhost/processingFr   Nr  r  s   &   r(   test_connectionRefusedWeird*WebSocketTests.test_connectionRefusedWeird=  sq     #&&'89**OO9:
	 e,I&

Y(:;r+   c                   V ^8  d   QhRR/# rU   r&   )r'   s   "r(   r)   r   L  s     > >D >r+   c                   \         P                  \        4       4      p\        P                  ! VP                  R4      4      pVP                  RR7      pV P                  V4       VP                  P                  P                  R4       VP                  4        V P                  V\        4      pVP                  P                  pVP!                  \#        4       ;p4       V P%                  VP&                  R4       VP                  P                  pVP                  R4       VP                  4        V P%                  VP&                  R4       VP                  R4       VP                  4        V P%                  VP&                  R4       VP)                  4        V P+                  VP,                  R4       VP                  4        VP,                  f   Q R	4       hV P%                  VP,                  P.                  \0        4       R# )
z
When the client connection is refused by an asynchronous HTTP response,
the websocket client will not be notified until the response arrives.
zhttp://localhost/slowTr   r+   s   hellos   worlds
   helloworldNzshould be closed now)r   r   r   r
   r   r   r   r   r   r#   writer   r  rA   valuerm   deliverBodyr   r   r`   finishr   closedReasontyper   )r/   r   r   r   rejectedresppreqs   &       r(   test_connectionRefusedSlow)WebSocketTests.test_connectionRefusedSlowL  sy   
 #&&'89**7??;R+ST	d+I&$$**3/

''	3EF~~&&2445%""**		(

*		(

/

annd+

~~)A+AA),,n=r+   r&   N)r2   r3   r4   r5   r   r   r   r   r   r   r  r
  r  r6   r&   r+   r(   r   r      s7    F$T 4D3 1$
$<<> >r+   r   N)@r?   
__future__r   dataclassesr   r   typingr   r   r   unittestr   zope.interfacer	   twisted.internet.deferr
   twisted.internet.errorr   twisted.internet.interfacesr   twisted.internet.testingr   r   twisted.python.failurer   twisted.test.iosimr   r   twisted.trial.unittestr   twisted.web._responsesr   twisted.web.clientr   r   twisted.web.iwebr   twisted.web.resourcer   twisted.web.serverr   r   r   twisted.web.staticr   r   
shouldSkipr    r8   
__import__twisted.web.websocketrA   rB   rC   r   rD   rE   rF   rH   r   r   ImportErrorr   r   r&   r+   r(   <module>r0     s.   # ( ( (  & + 1 5 M * : 6 . . % ) : : #e./
H  	h 	Ky   /"! /" /"b*51 07 
 (ws| ( (V 
=>o>( o> ?o>s  Js   "D   	D,+D,