o
    >hj6                     @   s  d dl Z d dlZe jddrd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dl
mZ d d	l
mZ d d
l
mZ d dlmZ d dlmZ d dlmZmZ d dlmZ d dlmZ eddd ZdZdZG dd dejZG dd dejZG dd dejZ dS dS )    NUSE_TWISTEDF)unittest)IPv4Address)Clock)WebSocketServerProtocol)WebSocketServerFactory)WebSocketClientProtocol)WebSocketClientFactory)PerMessageDeflate)FakeTransport)	MagicMockpatch)replace_loop)	b64decodezbase64.b64encodec                    s   d| _ tdgd}t|_|  |tddd}t |_|	  t
|_|  g   fdd}||_|jdi | d	 S )a[  
        Kind-of hack-y; maybe better to re-factor the Protocol to have a
        frame-encoder method-call? Anyway, makes a throwaway protocol
        encode a frame for us, collects the .sendData call and returns
        the data that would have gone out. Accepts all the kwargs that
        WebSocketClientProtocol.sendFrame() accepts.
        s   QIatSt9QkZPyS4QQfdufO8TgkL0=wamp.2.json	protocolsTCPz	127.0.0.9  c                    s     |  d S N)append)dargsdata /var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/autobahn/websocket/test/test_websocket_frame.pycollectI   s   z$create_client_frame.<locals>.collect    Nr   )return_valuer	   r   protocoldoStartbuildProtocolr   r   	transportconnectionMademock_handshake_serverr   processHandshakesendData	sendFramejoin)b64patchkwargsfactoryprotor   r   r   r   create_client_frame0   s   
r.   s
  GET / HTTP/1.1
User-Agent: AutobahnPython/0.10.2
Host: localhost:80
Upgrade: WebSocket
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: 6Jid6RgXpH0RVegaNSs/4g==
Sec-WebSocket-Protocol: wamp.2.json
Sec-WebSocket-Version: 13

s  HTTP/1.1 101 Switching Protocols
Server: AutobahnPython/0.10.2
X-Powered-By: AutobahnPython/0.10.2
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Protocol: wamp.2.json
Sec-WebSocket-Accept: QIatSt9QkZPyS4QQfdufO8TgkL0=

~[1,"crossbar",{"roles":{"subscriber":{"features":{"publisher_identification":true,"pattern_based_subscription":true,"subscription_revocation":true}},"publisher":{"features":{"publisher_identification":true,"publisher_exclusion":true,"subscriber_blackwhite_listing":true}},"caller":{"features":{"caller_identification":true,"progressive_call_results":true}},"callee":{"features":{"progressive_call_results":true,"pattern_based_registration":true,"registration_revocation":true,"shared_registration":true,"caller_identification":true}}}}]c                   @   s   e Zd Zdd Zdd ZdS )TestDeflatec              	   C   s<   t dddddddd}d}|  ||}| |d d S )NF      
   )	is_serverserver_no_context_takeoverclient_no_context_takeoverserver_max_window_bitsclient_max_window_bits	mem_levelmax_message_size   `Q0
FP @s
   xxxxxxxxxxr
   start_decompress_messagedecompress_message_dataassertEqualselfdecodercompressed_datar   r   r   r   test_max_sizeW   s   
zTestDeflate.test_max_sizec                 C   s:   t dddddd d}d}|  ||}| |d d S )NFr0   )r3   r4   r5   r6   r7   r8   r:   s  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxr;   r?   r   r   r   test_no_max_sizel   s   

zTestDeflate.test_no_max_sizeN)__name__
__module____qualname__rC   rD   r   r   r   r   r/   U   s    r/   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )

TestClientc                 C   V   t dgd| _t| j_| j  | jtddd| _t | _	| j	| j_	| j
  d S Nr   r   r   z	127.0.0.1r   )r	   r,   r   r    r!   r"   r   r-   r   r#   r$   r@   r   r   r   setUp      

zTestClient.setUpc                 C   *   | j jr
| j j  | j  | `| ` d S r   r-   openHandshakeTimeoutCallcancelr,   doStoprK   r   r   r   tearDown   
   
zTestClient.tearDownc                 C   s8   d| j _| j j| j _d| j _t | j _| j dd  d S )NT     )r-   echoCloseCodeReason
STATE_OPENstatewebsocket_versionr   sendCloseFrameonCloseFramerK   r   r   r   test_missing_reason_raw   s
   
z"TestClient.test_missing_reason_rawc                 C   s  	 td| j_t| j_| j  | 	| jj
tj | | jjdk tt R}| j  | jdd | 	t|jd | 	|jd j| jj | 	|jd  | jj || jjd  | | jj  | 	| jj
tj W d   dS 1 s|w   Y  dS )zR
            make a delayed call to drop the connection (client-side)
            Fz6Jid6RgXpH0RVegaNSs/4g==r   rV   s
   raw reasonrU   N)printr-   r,   _logr   websocket_keyr%   r   r&   r>   rY   r   rX   
assertTrueserverConnectionDropTimeoutr   r   r[   r\   lencallsfunconServerConnectionDropTimeoutgetTimeadvancecloseHandshakeTimeoutr#   abort_calledSTATE_CLOSEDr@   reactorr   r   r   test_unclean_timeout_client   s    

"z&TestClient.test_unclean_timeout_clientN)rE   rF   rG   rL   rS   r]   rn   r   r   r   r   rH   ~   s
    

rH   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestPingc                 C   rI   rJ   )r   r,   r   r    r!   r"   r   r-   r   r#   r$   rK   r   r   r   rL      rM   zTestPing.setUpc                 C   rN   r   rO   rK   r   r   r   rS      rT   zTestPing.tearDownc                 C   s   t | j_| j  | | jjtjk tt	 ?}| j
  | t|jd || jjd  | dt| jjjd  | | jjjj | | jjtj W d   dS 1 sZw   Y  dS )zD
            make a delayed call to drop the connection
            rU   zcall.abortConnection()N)mock_handshake_clientr-   r   r&   ra   rY   r   rX   r   r   r[   r>   rc   rd   rh   ri   strr#   method_callsabortConnectioncalledrk   rl   r   r   r   test_unclean_timeout   s   

"zTestPing.test_unclean_timeoutc                 C   s  d| j _d| j _tt u}t| j _| j   | | j j	t
jk | dt|j |d | | jjj | jjjd d }t|d g}td|\}|d@ }| d| | dt|j | | j j	| j j |d | | j j	| j j W d	   d	S 1 sw   Y  d	S )
z>
            autoping and autoping-timeout timing
                  rU   r   Bi	      N)r-   autoPingIntervalautoPingTimeoutr   r   rq   r   r&   ra   rY   r   rX   r>   rc   rd   rh   r#   writeru   	call_argsbytesstructunpackassertNotEqualrk   )r@   rm   r   _dataopcoder   r   r   test_auto_pingpong_timeout   s&   


"z#TestPing.test_auto_pingpong_timeoutc                 C   s   d| j _d| j _tt T}t| j _| j   | | j j	t
jk | dt|j |d | dt|j |jd }td| j jd}|| j _| j   | j   | |j W d   dS 1 sdw   Y  dS )zF
            auto-ping with correct reply cancels timeout
            rw   rx   rU   r   r2   )r   payloadN)r-   r|   r}   r   r   rq   r   r&   ra   rY   r   rX   r>   rc   rd   rh   r.   autoPingPendingprocessData	cancelled)r@   rm   timeout_callframer   r   r   test_auto_ping_got_pong  s    




"z TestPing.test_auto_ping_got_pongN)rE   rF   rG   rL   rS   rv   r   r   r   r   r   r   ro      s    
 +ro   )!osr   environgettwisted.trialr   twisted.internet.addressr   twisted.internet.taskr   autobahn.twisted.websocketr   r   r   r	   #autobahn.websocket.compress_deflater
   autobahn.testutilr   unittest.mockr   r   txaio.testutilr   base64r   r.   rq   r%   TestCaser/   rH   ro   r   r   r   r   <module>   s0   
 )? 