
     ke0                        d dl Z d dlZ d dlZd dlZd dlZd dlZ	 e j                  j                  dd      dk(  Z	e j                  j                  dd      Z
e
dk7  ZdZ G d d	ej                        Zed
k(  r ej                           yy)    NTEST_WITH_INTERNET01LOCAL_WS_SERVER_PORTz-1Tc                      e Zd Z G d d      Zd Zd Z ej                  ed      d        Z	 ej                  dd      d	        Z
 ej                  ed      d
        Z ej                  ed      d        Z ej                  ed      d        Z ej                  ed      d        Z ej                  ed      d        Z ej                  ed      d        Z ej                  ed      d        Z ej                  ed      d        Z ej                  ed      d        Z ej                  ed      d        Z ej                  ed      d        Zy)WebSocketAppTestc                       e Zd ZdZy)WebSocketAppTest.NotSetYetz?A marker class for signalling that a value hasn't been set yet.N)__name__
__module____qualname____doc__     :/usr/lib/python3/dist-packages/websocket/tests/test_app.py	NotSetYetr
   '   s    Mr   r   c                    t        j                  t               t        j	                         t        _        t        j	                         t        _        t        j	                         t        _        t        j	                         t        _        y N)	wsenableTrace	TRACEABLEr   r   keep_running_openkeep_running_closeget_mask_key_idon_error_dataselfs    r   setUpzWebSocketAppTest.setUp*   sV    
y!-=-G-G-I*.>.H.H.J++;+E+E+G()9)C)C)E&r   c                     t         j                         t         _        t         j                         t         _        t         j                         t         _        t         j                         t         _        y r   )r   r   r   r   r   r   r   s    r   tearDownzWebSocketAppTest.tearDown2   sJ    -=-G-G-I*.>.H.H.J++;+E+E+G()9)C)C)E&r   z/Tests using local websocket server are disabledc                 |     d } fd}d }t        j                  dt         |||      }|j                          y){A WebSocketApp should keep running as long as its self.keep_running
        is not False (in the boolean context).
        c                 ^    | j                  d       | j                  t        _        d| _        y)zmSet the keep_running flag for later inspection and immediately
            close the connection.
            hello!FN)sendkeep_runningr   r   r   argskwargss      r   on_openz1WebSocketAppTest.testKeepRunning.<locals>.on_open@   s'     IIh151B1B. %Dr   c                 <    t        |       j                          y r   printclosewsappmessager   s     r   
on_messagez4WebSocketAppTest.testKeepRunning.<locals>.on_messageH       'NJJLr   c                 .    | j                   t        _        y)z.Set the keep_running flag for the test to use.N)r&   r   r   r'   s      r   on_closez2WebSocketAppTest.testKeepRunning.<locals>.on_closeL   s    262C2C/r   ws://127.0.0.1:)r*   r5   r2   Nr   WebSocketAppr   run_forever)r   r*   r2   r5   apps   `    r   testKeepRunningz WebSocketAppTest.testKeepRunning8   sD    	&		D oo234!	
 	r   Fz$Test disabled for now (requires rel)c                 x     d } fd}t        j                  dt         ||      }|j                  d       y)r"   c                 h    | j                  d       | j                          | j                  d       y)z*Send a message, receive, and send one morer$   zgoodbye!N)r%   recvr'   s      r   r*   z:WebSocketAppTest.testRunForeverDispatcher.<locals>.on_open_   s#    IIhIIKIIj!r   c                 <    t        |       j                          y r   r,   r/   s     r   r2   z=WebSocketAppTest.testRunForeverDispatcher.<locals>.on_messagee   r3   r   r6   )r*   r2   
Dispatcher)
dispatcherNr7   )r   r*   r2   r:   s   `   r   testRunForeverDispatcherz)WebSocketAppTest.testRunForeverDispatcherY   s?    	"	 oo234!

 	<0r   c                     t        j                  dt               }t        j                  d|j
                        j                          |j                         }| j                  |d       y)zaThe WebSocketApp.run_forever() method should return `False` when the application ends gracefully.r6   g?)intervalfunctionFN)	r   r8   r   	threadingTimerr.   startr9   assertEqual)r   r:   teardowns      r   testRunForeverTeardownCleanExitz0WebSocketAppTest.testRunForeverTeardownCleanExits   sR    
 oo0D/EFGsyy9??A??$5)r   z%Internet-requiring tests are disabledc                     d }t        j                  d|      }| j                  t        |j                        t        |             y)zhA WebSocketApp should forward the received mask_key function down
        to the actual socket.
        c                       y)Nz    r   r   r   r   my_mask_key_funcz:WebSocketAppTest.testSockMaskKey.<locals>.my_mask_key_func   s    %r   wss://api-pub.bitfinex.com/ws/1)get_mask_keyN)r   r8   rI   idrP   )r   rN   r:   s      r   testSockMaskKeyz WebSocketAppTest.testSockMaskKey}   s@    	& oo-<L
 	C,,-r2B/CDr   c                     d }d }t        j                  d||      }| j                  t         j                  |j                  dddt
        j                  i       y	)
z7Test exception handling if ping_interval < ping_timeoutc                 :    t        d       | j                          y NzGot a ping!r,   r:   msgs     r   on_pingzDWebSocketAppTest.testInvalidPingIntervalPingTimeout.<locals>.on_ping       - IIKr   c                 :    t        d       | j                          y NzGot a pong! No need to respondr,   rV   s     r   on_pongzDWebSocketAppTest.testInvalidPingIntervalPingTimeout.<locals>.on_pong       23IIKr   rO   rX   r\         	cert_reqsping_intervalping_timeoutssloptNr   r8   assertRaisesWebSocketExceptionr9   ssl	CERT_NONEr   rX   r\   r:   s       r   "testInvalidPingIntervalPingTimeoutz3WebSocketAppTest.testInvalidPingIntervalPingTimeout   sZ    		 oo-w
 	!!OO/ 	 	
r   c                     d }d }t        j                  d||      }|j                  dddt        j                  i       y	)
z+Test WebSocketApp proper ping functionalityc                 :    t        d       | j                          y rU   r,   rV   s     r   rX   z2WebSocketAppTest.testPingInterval.<locals>.on_ping   rY   r   c                 :    t        d       | j                          y r[   r,   rV   s     r   r\   z2WebSocketAppTest.testPingInterval.<locals>.on_pong   r]   r   rO   r^   r`   r_   ra   rb   N)r   r8   r9   ri   rj   rk   s       r   testPingIntervalz!WebSocketAppTest.testPingInterval   sF    		 oo-w
 	![#--4P 	 	
r   c                 V    t        j                  d      }|j                  ddd       y)zTest WebSocketApp close opcode'wss://tsock.us1.twilio.com/v3/wsconnectr`   r_   zPing payload)rc   rd   ping_payloadN)r   r8   r9   r   r:   s     r   testOpcodeClosez WebSocketAppTest.testOpcodeClose   s%     ooGHaanUr   c                     t        j                  d      }| j                  t         j                  |j                  ddt
        j                  i       y)z1A WebSocketApp handling of negative ping_intervalrO   ra   )rc   re   Nrf   rt   s     r   testBadPingIntervalz$WebSocketAppTest.testBadPingInterval   sE     oo?@!!OO/	 	 	
r   c                     t        j                  d      }| j                  t         j                  |j                  ddt
        j                  i       y)z0A WebSocketApp handling of negative ping_timeoutrO   ra   )rd   re   Nrf   rt   s     r   testBadPingTimeoutz#WebSocketAppTest.testBadPingTimeout   sE     oo?@!!OO/	 	 	
r   c                    d }t        j                  d|      }t        j                  t         j                  j                  d      }| j	                  ddg|j                  |             t        j                  t         j                  j                  d      }| j	                  d	d	g|j                  |             t        j                  d      }t        j                  t         j                  j                  d      }| j	                  d	d	g|j                  |             | j                  t         j                  |j                  d
       y	)zKTest extraction of close frame status code and close reason in WebSocketAppc                     t        d       y )Nzon_close reached)r-   )r0   close_status_code	close_msgs      r   r5   z6WebSocketAppTest.testCloseStatusCode.<locals>.on_close   s    $%r   rr   )r5   s   no-init-from-client)opcodedatai  zno-init-from-clientr   Nztest if connection is closed)r   )	r   r8   ABNFOPCODE_CLOSErI   _get_close_argsrg   "WebSocketConnectionClosedExceptionr%   )r   r5   r:   
closeframeapp2s        r   testCloseStatusCodez$WebSocketAppTest.testCloseStatusCode   s   	& oo5
 WW77''.L

 	$ 568K8KJ8WXWWBGG$8$8sC
$s':'::'FGHIWWBGG$8$8sC
$t';';J'GH11HH/ 	 	
r   c                    ddd }fd}d }t        j                  dt         |||      }|j                  dd	       | j	                  |       | j                  t               | j	                  t              d
       y)z)Test callback function exception handlingNc                     t        d      NCallback failedRuntimeError)r:   s    r   r*   z?WebSocketAppTest.testCallbackFunctionException.<locals>.on_open  s    011r   c                     | |y r   r   )r:   errexc
passed_apps     r   on_errorz@WebSocketAppTest.testCallbackFunctionException.<locals>.on_error  s    JCr   c                 $    | j                          y r   r.   rV   s     r   r\   z?WebSocketAppTest.testCallbackFunctionException.<locals>.on_pong  s    IIKr   r6   r*   r   r\   r`   r_   rc   rd   r   )r   r8   r   r9   rI   assertIsInstancer   str)r   r*   r   r\   r:   r   r   s        @@r   testCallbackFunctionExceptionz.WebSocketAppTest.testCallbackFunctionException   s     
	2		 oo234	
 	aa8S)c<0S#45r   c                      G d d      } |       }| j                  |j                  |j                         | j                  |j                  t
               | j                  t        |j                        d       y)z'Test callback method exception handlingc                   $    e Zd Zd Zd Zd Zd Zy)?WebSocketAppTest.testCallbackMethodException.<locals>.Callbacksc                     d | _         d | _        t        j                  dt         | j
                  | j                  | j                        | _        | j                  j                  dd       y )Nr6   r   r`   r_   r   )
r   r   r   r8   r   r*   r   r\   r:   r9   r   s    r   __init__zHWebSocketAppTest.testCallbackMethodException.<locals>.Callbacks.__init__!  sZ    "&??%&:%;< LL!]] LL	 $$11$Er   c                     t        d      r   r   rt   s     r   r*   zGWebSocketAppTest.testCallbackMethodException.<locals>.Callbacks.on_open,  s    "#455r   c                      || _         || _        y r   )r   r   )r   r:   r   s      r   r   zHWebSocketAppTest.testCallbackMethodException.<locals>.Callbacks.on_error/  s    "%r   c                 $    |j                          y r   r   )r   r:   rW   s      r   r\   zGWebSocketAppTest.testCallbackMethodException.<locals>.Callbacks.on_pong3  s    		r   N)r   r   r   r   r*   r   r\   r   r   r   	Callbacksr      s    	F6r   r   r   N)rI   r   r:   r   r   r   r   )r   r   	callbackss      r   testCallbackMethodExceptionz,WebSocketAppTest.testCallbackMethodException  s]    	 	, K	--y}}=imm\:Y]]+->?r   c                 $   ddfd}fd}t        j                  dt         ||      }|j                  ddd	
       | j	                  d       | j                  t         j                         | j	                  t              d       y)zTest reconnectr   Nc                 
    |y r   r   )r:   r   r   s     r   r   z0WebSocketAppTest.testReconnect.<locals>.on_errorD  s    Cr   c                 z    dz  dk(  r| j                   j                          dk\  r| j                          y y )Nr_   r`   )sockshutdownr.   )r:   rW   
pong_counts     r   r\   z/WebSocketAppTest.testReconnect.<locals>.on_pongH  s8    !OJQ!!#Q		 r   r6   )r\   r   r`   r_      )rc   rd   	reconnectzping/pong timed out)r   r8   r   r9   rI   r   WebSocketTimeoutExceptionr   )r   r   r\   r:   r   r   s       @@r   testReconnectzWebSocketAppTest.testReconnect<  s    
 
		 oo234gPX
 	aa1EQ'c2#?#?@S#89r   N)r   r   r   r   r   r    unittest
skipUnlessTEST_WITH_LOCAL_SERVERr;   rB   rK   r   rR   rl   rp   ru   rx   r{   r   r   r   r   r   r   r   r   r   &   s   N NFF X Q< X FG1 H12 X Q** X+-TUE VE  X+-TU
 V
, X+-TU
 V
$ X+-TUV VV X+-TU
 V
 X+-TU
 V
 X+-TU
 V
6 X Q66< X Q@@> X Q::r   r   __main__)osos.pathri   rF   r   	websocketr   environgetr   r   r   r   TestCaser   r   mainr   r   r   <module>r      s    
  
   ( ZZ^^$8#>#E zz~~&<dC -5 	s:x(( s:l	 zHMMO r   