
    afj6                        d dl Z d dlZe j                            dd          rd 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  ed          d             ZdZdZ G d dej                  Z G d dej                  Z G d dej                  Z dS dS )    NUSE_TWISTEDF)unittest)IPv4Address)Clock)WebSocketServerProtocol)WebSocketServerFactory)WebSocketClientProtocol)WebSocketClientFactory)PerMessageDeflate)FakeTransport)	MagicMockpatch)replace_loop)	b64decodezbase64.b64encodec                    d| _         t          dg          }t          |_        |                                 |                    t          ddd                    }t                      |_        |	                                 t          |_        |                                 g fd}||_         |j        d	i | 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                 2                         |            d S N)append)dargsdatas     ^/var/www/html/env/lib/python3.11/site-packages/autobahn/websocket/test/test_websocket_frame.pycollectz$create_client_frame.<locals>.collectI   s    KKNNNNN    r    )return_valuer
   r	   protocoldoStartbuildProtocolr   r   	transportconnectionMademock_handshake_serverr   processHandshakesendData	sendFramejoin)b64patchkwargsfactoryprotor   r   s        @r   create_client_framer0   0   s     !@(M?CCC2%%k%e&L&LMM'//*
   	 	 	 	 	 !!&!!!xx~~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                       e Zd Zd Zd ZdS )TestDeflatec           	          t          ddddddd          }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   s       r   test_max_sizezTestDeflate.test_max_sizeW   sv    '+0+0')')!#  G bO,,...22?CCD T9-----r   c                     t          dddddd           }d}|                                 |                    |          }|                     |d           d S )NFr4   )r7   r8   r9   r:   r;   r<   r>   s  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxr?   rC   s       r   test_no_max_sizezTestDeflate.test_no_max_sizel   sq    '+0+0')')  G bO,,...22?CCDT;/////r   N)__name__
__module____qualname__rG   rI   r    r   r   r2   r2   U   s2        	. 	. 	.*	0 	0 	0 	0 	0r   r2   c                   &    e Zd Zd Zd Zd Zd ZdS )
TestClientc                 \   t          dg          | _        t          | j        _        | j                                         | j                            t          ddd                    | _        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&   rD   s    r   setUpzTestClient.setUp   s    1]OLLLDL$;DL!L  """33K{TY4Z4Z[[DJ*__DN#'>DJ J%%'''''r   c                     | j         j        r| j         j                                         | j                                         | `| ` d S r   r/   openHandshakeTimeoutCallcancelr.   doStoprQ   s    r   tearDownzTestClient.tearDown   H    z2 =
3::<<<L!!!


r   c                     d| j         _        | j         j        | j         _        d| j         _        t                      | j         _        | j                             dd            d S )NT     )r/   echoCloseCodeReason
STATE_OPENstatewebsocket_versionr   sendCloseFrameonCloseFramerQ   s    r   test_missing_reason_rawz"TestClient.test_missing_reason_raw   sQ    -1DJ*#z4DJ+,DJ((1DJ%J##D$/////r   c                    	 t	          d          | j        _        t          | j        _        | j                                         | 	                    | j        j
        t          j                   |                     | j        j        dk               t          t!                                5 }| j                                         | j                            dd           | 	                    t'          |j                  d           | 	                    |j        d         j        | j        j                   | 	                    |j        d                                         | j        j                   |                    | j        j        dz              |                     | j        j                                                   | 	                    | j        j
        t          j                   ddd           dS # 1 swxY w Y   dS )zR
            make a delayed call to drop the connection (client-side)
            Fz6Jid6RgXpH0RVegaNSs/4g==r   r\   s
   raw reasonr[   N)printr/   r.   _logr   websocket_keyr'   r   r(   rB   r_   r   r^   
assertTrueserverConnectionDropTimeoutr   r   ra   rb   lencallsfunconServerConnectionDropTimeoutgetTimeadvancecloseHandshakeTimeoutr%   abort_calledSTATE_CLOSEDrD   reactors     r   test_unclean_timeout_clientz&TestClient.test_unclean_timeout_client   s   
0 (11K'L'LDJ$3DJOJ'')))TZ-/F/QRRROODJBQFGGGegg&& Y'
))+++
''m<<<   W]!3!3Q777  q!1!6
8`aaa  q!1!9!9!;!;TZ=cddd 
 @1 DEEE 
 4 A A C CDDD  !13J3WXXX#Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Ys   -EHHHN)rJ   rK   rL   rR   rX   rc   ru   r    r   r   rN   rN   ~   sU        	( 	( 	(	 	 		0 	0 	0 	Y  	Y  	Y  	Y  	Yr   rN   c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )TestPingc                 \   t          dg          | _        t          | j        _        | j                                         | j                            t          ddd                    | _        t                      | _	        | j	        | j        _	        | j        
                                 d S rP   )r   r.   r   r"   r#   r$   r   r/   r   r%   r&   rQ   s    r   rR   zTestPing.setUp   s    1]OLLLDL$;DL!L  """33K{TY4Z4Z[[DJ&[[DN#'>DJ J%%'''''r   c                     | j         j        r| j         j                                         | j                                         | `| ` d S r   rT   rQ   s    r   rX   zTestPing.tearDown   rY   r   c                    t           | j        _        | j                                         |                     | j        j        t          j        k               t          t                                5 }| j        
                                 |                     t          |j                  d           |                    | j        j        dz              |                     dt!          | j        j        j        d                              |                     | j        j        j        j                   |                     | j        j        t          j                   ddd           dS # 1 swxY w Y   dS )zD
            make a delayed call to drop the connection
            r[   zcall.abortConnection()N)mock_handshake_clientr/   r   r(   rh   r_   r   r^   r   r   ra   rB   rj   rk   ro   rp   strr%   method_callsabortConnectioncalledrr   rs   s     r   test_unclean_timeoutzTestPing.test_unclean_timeout   s    4DJOJ'')))OODJ,0G0RRSSSegg&& Y'
))+++   W]!3!3Q777 
 @1 DEEE   !93tz?S?`ac?d;e;efff
 4 D KLLL  !13J3WXXXY Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Ys   3C/E//E36E3c                    d| j         _        d| j         _        t          t	                                5 }t
          | j         _        | j                                          |                     | j         j	        t          j        k               |                     dt          |j                             |                    d           |                     | j        j        j                   | j        j        j        d         d         }t)          |d         g          }t+          j        d|          \  }|dz  }|                     d|           |                     dt          |j                             |                     | j         j	        | j         j                   |                    d           |                     | j         j	        | j         j                   d	d	d	           d	S # 1 swxY w Y   d	S )
z>
            autoping and autoping-timeout timing
                  r[   r   Bi	      N)r/   autoPingIntervalautoPingTimeoutr   r   r|   r   r(   rh   r_   r   r^   rB   rj   rk   ro   r%   writer   	call_argsbytesstructunpackassertNotEqualrr   )rD   rt   r   _dataopcodes        r   test_auto_pingpong_timeoutz#TestPing.test_auto_pingpong_timeout   s   
 +,DJ')*DJ&egg&& !L'"7

++---
 04K4V VWWW   C$6$6777 """  4 ;<<<~+5a8;tAwi(( #M#u55	5)   F+++   C$6$6777##DJ$4dj6MNNN """  !14:3JKKKC!L !L !L !L !L !L !L !L !L !L !L !L !L !L !L !L !L !Ls   F/G00G47G4c                    d| j         _        d| j         _        t          t	                                5 }t
          | j         _        | j                                          |                     | j         j	        t          j        k               |                     dt          |j                             |                    d           |                     dt          |j                             |j        d         }t!          d| j         j                  }|| j         _        | j                                          | j                                          |                     |j                   ddd           dS # 1 swxY w Y   dS )zF
            auto-ping with correct reply cancels timeout
            r   r   r[   r   r6   )r   payloadN)r/   r   r   r   r   r|   r   r(   rh   r_   r   r^   rB   rj   rk   ro   r0   autoPingPendingprocessData	cancelled)rD   rt   timeout_callframes       r   test_auto_ping_got_pongz TestPing.test_auto_ping_got_pong  s   
 +,DJ')*DJ&egg&& 8'"7

++---
 04K4V VWWW   C$6$6777 """   C$6$6777&}Q/ ,2tz?YZZZ"'

&&(((
&&(((  677798 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8s   D=E>>FFN)rJ   rK   rL   rR   rX   r   r   r   r    r   r   rw   rw      sg        	( 	( 	(	 	 		Y 	Y 	Y@)	L )	L )	LV$	8 $	8 $	8 $	8 $	8r   rw   )!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   r0   r|   r'   TestCaser2   rN   rw   r    r   r   <module>r      s  6 
			  :>>-'' `8&&&&&&444444++++++BBBBBBAAAAAABBBBBBAAAAAAEEEEEE//////........++++++      
U  @ @ C'0 '0 '0 '0 '0h' '0 '0 '0R=Y =Y =Y =Y =YX& =Y =Y =Y~B8 B8 B8 B8 B88$ B8 B8 B8 B8 B8}`8 `8r   