U
    •uÄcj6  ã                   @   s  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ƒdd„ ƒZdZdZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZ dS )é    NZUSE_TWISTEDF)Úunittest)ÚIPv4Address)ÚClock)ÚWebSocketServerProtocol)ÚWebSocketServerFactory)ÚWebSocketClientProtocol)ÚWebSocketClientFactory)ÚPerMessageDeflate)ÚFakeTransport)Ú	MagicMockÚpatch)Úreplace_loop)Ú	b64decodezbase64.b64encodec                    s|   d| _ tdgd�}t|_| ¡  | tdddƒ¡}tƒ |_| 	¡  t
|_| ¡  g ‰ ‡ fdd„}||_|jf |Ž 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©Ú	protocolsÚTCPz	127.0.0.9éþÿ  c                    s   ˆ   | ¡ d S ©N)Úappend)ÚdÚargs©Údata© úwc:\users\psant\downloads\hotstarbackend\school\venv38\Lib\site-packages\autobahn\websocket\test\test_websocket_frame.pyÚcollectI   s    z$create_client_frame.<locals>.collectó    )Zreturn_valuer   r   ÚprotocolÚdoStartÚbuildProtocolr   r
   Ú	transportÚconnectionMadeÚmock_handshake_serverr   ÚprocessHandshakeZsendDataZ	sendFrameÚjoin)Zb64patchÚkwargsÚfactoryÚprotor   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_serverÚserver_no_context_takeoverÚclient_no_context_takeoverÚserver_max_window_bitsÚclient_max_window_bitsÚ	mem_levelZmax_message_sizeó   «¨£`Œ‚Q0
FÁP Ï@©®s
   xxxxxxxxxx©r	   Zstart_decompress_messageZdecompress_message_dataÚassertEqual©ÚselfÚdecoderZcompressed_datar   r   r   r   Útest_max_sizeW   s    ù
zTestDeflate.test_max_sizec                 C   s:   t dddddd d�}d}| ¡  | |¡}|  |d¡ d S )NFr+   )r.   r/   r0   r1   r2   r3   r4   sÐ  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxr5   r7   r   r   r   Útest_no_max_sizel   s    ú

zTestDeflate.test_no_max_sizeN)Ú__name__Ú
__module__Ú__qualname__r:   r;   r   r   r   r   r*   U   s   r*   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
Ú
TestClientc                 C   sV   t dgd�| _t| j_| j ¡  | j tdddƒ¡| _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"   ©r8   r   r   r   ÚsetUp   s    

zTestClient.setUpc                 C   s*   | j jr| j j ¡  | j ¡  | `| ` d S r   ©r(   ZopenHandshakeTimeoutCallÚcancelr'   ZdoStoprA   r   r   r   ÚtearDown‰   s
    
zTestClient.tearDownc                 C   s8   d| j _| j j| j _d| j _tƒ | j _| j  dd ¡ d S )NTé   éè  )r(   ZechoCloseCodeReasonÚ
STATE_OPENÚstateZwebsocket_versionr   ÚsendCloseFrameÚonCloseFramerA   r   r   r   Útest_missing_reason_raw‘   s
    
z"TestClient.test_missing_reason_rawc              	   C   sî   tdƒ| j_t| j_| j ¡  |  | jjt	j
¡ |  | jjdk¡ ttƒ ƒ�˜}| j ¡  | j dd¡ |  t|jƒd¡ |  |jd j| jj¡ |  |jd  ¡ | jj¡ | | jjd ¡ |  | jj ¡ ¡ |  | jjt	j¡ W 5 Q R X dS )zR
            make a delayed call to drop the connection (client-side)
            z6Jid6RgXpH0RVegaNSs/4g==r   rG   s
   raw reasonrF   N)Úprintr   r(   Zwebsocket_keyr#   r   r$   r6   rI   r   rH   Ú
assertTrueZserverConnectionDropTimeoutr   r   rJ   rK   ÚlenÚcallsÚfuncZonServerConnectionDropTimeoutZgetTimeÚadvanceÚcloseHandshakeTimeoutr!   Zabort_calledÚSTATE_CLOSED©r8   Úreactorr   r   r   Útest_unclean_timeout_client›   s    	

z&TestClient.test_unclean_timeout_clientN)r<   r=   r>   rB   rE   rL   rW   r   r   r   r   r?   ~   s   

r?   c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚTestPingc                 C   sV   t dgd�| _t| j_| j ¡  | j tdddƒ¡| _tƒ | _	| j	| j_	| j 
¡  d S r@   )r   r'   r   r   r   r    r   r(   r   r!   r"   rA   r   r   r   rB   ¾   s    

zTestPing.setUpc                 C   s*   | j jr| j j ¡  | j ¡  | `| ` d S r   rC   rA   r   r   r   rE   È   s
    
zTestPing.tearDownc              	   C   s¬   t | j_| j ¡  |  | jjtjk¡ tt	ƒ ƒ�r}| j 
¡  |  t|jƒd¡ | | jjd ¡ |  dt| jjjd ƒ¡ |  | jjjj¡ |  | jjtj¡ W 5 Q R X dS )zD
            make a delayed call to drop the connection
            rF   zcall.abortConnection()éÿÿÿÿN)Úmock_handshake_clientr(   r   r$   rN   rI   r   rH   r   r   rJ   r6   rO   rP   rR   rS   Ústrr!   Zmethod_callsZabortConnectionÚcalledrT   rU   r   r   r   Útest_unclean_timeoutÐ   s    

zTestPing.test_unclean_timeoutc              	   C   s  d| j _d| j _ttƒ ƒ�Þ}t| j _| j  ¡  |  | j j	t
jk¡ |  dt|jƒ¡ | d¡ |  | jjj¡ | jjjd d }t|d gƒ}t d|¡\}|d@ }|  d|¡ |  dt|jƒ¡ |  | j j	| j j¡ | d¡ |  | j j	| j j¡ W 5 Q R X d	S )
z>
            autoping and autoping-timeout timing
            é   é   rF   r   ÚBiÿÿÿé	   é   N)r(   ÚautoPingIntervalÚautoPingTimeoutr   r   rZ   r   r$   rN   rI   r   rH   r6   rO   rP   rR   r!   Úwriter\   Z	call_argsÚbytesÚstructÚunpackÚassertNotEqualrT   )r8   rV   r   Ú_dataÚopcoder   r   r   Útest_auto_pingpong_timeoutð   s$    


z#TestPing.test_auto_pingpong_timeoutc              	   C   sÀ   d| j _d| j _ttƒ ƒ�œ}t| j _| j  ¡  |  | j j	t
jk¡ |  dt|jƒ¡ | d¡ |  dt|jƒ¡ |jd }td| j jd�}|| j _| j  ¡  | j  ¡  |  |j¡ W 5 Q R X dS )zF
            auto-ping with correct reply cancels timeout
            r^   r_   rF   r   r-   )rk   ÚpayloadN)r(   rc   rd   r   r   rZ   r   r$   rN   rI   r   rH   r6   rO   rP   rR   r)   ZautoPingPendingZprocessDataÚ	cancelled)r8   rV   Ztimeout_callÚframer   r   r   Útest_auto_ping_got_pong  s    




z TestPing.test_auto_ping_got_pongN)r<   r=   r>   rB   rE   r]   rl   rp   r   r   r   r   rX   ½   s
   
 +rX   )!Úosrg   ÚenvironÚgetZtwisted.trialr   Ztwisted.internet.addressr   Ztwisted.internet.taskr   Zautobahn.twisted.websocketr   r   r   r   Z#autobahn.websocket.compress_deflater	   Zautobahn.testutilr
   Zunittest.mockr   r   Ztxaio.testutilr   Úbase64r   r)   rZ   r#   ÚTestCaser*   r?   rX   r   r   r   r   Ú<module>   s*   
 )?