o
    @Bi@$                     @   s   d dl mZmZ d dlZd dlZd dlmZ ejddrFd dl	m
Z
 d dlmZ d dlmZ eeG d	d
 d
eZG dd dejZdS zd dlZd dlm
Z
mZ W n eyi   d dlZd dl	m
Z
mZ Y nw d dlmZ G dd dejZdS )    )absolute_importprint_functionN)replace_loopUSE_TWISTEDF)patch)implementer)IReactorTimec                   @   s<   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dS )FakeReactorzP
        This just fakes out enough reactor methods so .run() can work.
        Fc                 C   s   d| _ || _g | _d S )NF)stop_calledto_raisedelayed)selfr    r   ^/var/www/html/Trade-python/venv/lib/python3.10/site-packages/autobahn/wamp/test/test_runner.py__init__-   s   
zFakeReactor.__init__c                 O   s   | j N)r   r   argskwr   r   r   run2   s   zFakeReactor.runc                 C   s
   d| _ d S )NT)r
   )r   r   r   r   stop5   s   
zFakeReactor.stopc                 O   s   | j ||||f d S r   )r   append)r   delayfuncr   kwargsr   r   r   	callLater8   s   zFakeReactor.callLaterc                 O   s   t d)NzConnectTCP shouldn't get called)RuntimeErrorr   r   r   r   
connectTCP;   s   zFakeReactor.connectTCPN)
__name__
__module____qualname____doc__r
   r   r   r   r   r   r   r   r   r   r	   &   s    r	   c                   @   s,   e Zd Zedededdd ZdS )TestWampTwistedRunnerztxaio.use_twistedztxaio.start_loggingztxaio.configc                 G   s   zddl m} ddlm} ddlm} W n ty    tdw |dd}|d}t	d	t
|}| j||jd
d dd | |j W d   dS 1 sOw   Y  dS )z
            Ensure the runner doesn't swallow errors and that it exits the
            reactor properly if there is one.
            r   ApplicationRunner)ConnectionRefusedError)reactorz
No twistedzws://localhost:1realmzIt's a trap!ztwisted.internet.reactorc                 S   s   d S r   r   )_r   r   r   <lambda>[   s    z:TestWampTwistedRunner.test_connect_error.<locals>.<lambda>T)start_reactorN)autobahn.twisted.wampr$   twisted.internet.errorr%   twisted.internetr&   ImportErrorunittestSkipTestr   r	   assertRaisesr   
assertTruer
   )r   r   r$   r%   r&   runner	exceptionmockreactorr   r   r   test_connect_errorD   s    

"z(TestWampTwistedRunner.test_connect_errorN)r   r   r    r   r6   r   r   r   r   r"   >   s
    r"   )r   Mockr#   c                   @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )TestApplicationRunnerzI
        Test the autobahn.asyncio.wamp.ApplicationRunner class.
        c                 O   sF   z| j  W n ty   | j}Y nw | j }|||g|R i | d S r   )assertRaisesRegexAttributeErrorassertRaisesRegexp)r   r4   errorr   r   fr   r   r   _assertRaisesRegexn   s   

z(TestApplicationRunner._assertRaisesRegexc              	   C   s   t t M}tjtd|d, tt t fd|_i }tdd|d}|d | ||j	j
d d  W d	   n1 s=w   Y  W d	   d	S W d	   d	S 1 sUw   Y  d	S )
z
            Ensure that loop.create_connection is called with the exact SSL
            context object that is passed (as ssl) to the __init__ method of
            ApplicationRunner.
            get_event_loopreturn_valuews://127.0.0.1:8080/wsr'   ssl_unused_   rD   Nr   r7   r   objectasynciorun_until_completer$   r   assertIscreate_connection	call_args)r   looprD   r3   r   r   r   test_explicit_SSLContextw   s   
"z.TestApplicationRunner.test_explicit_SSLContextc              	   C   s   t t I}tjtd|d( tt t fd|_tdd}|d | d|j	j
d d  W d	   n1 s9w   Y  W d	   d	S W d	   d	S 1 sQw   Y  d	S )
z
            Ensure that loop.create_connection is called with ssl=False
            if no ssl argument is passed to the __init__ method of
            ApplicationRunner and the websocket URL starts with "ws:".
            r?   r@   rB   r'   rE   FrF   rD   NrG   r   rN   r3   r   r   r    test_omitted_SSLContext_insecure   s   

"z6TestApplicationRunner.test_omitted_SSLContext_insecurec              	   C   s   t t J}tjtd|d) tt t fd|_tdd}|| j | 	d|j
jd d  W d   n1 s:w   Y  W d   dS W d   dS 1 sRw   Y  dS )	z
            Ensure that loop.create_connection is called with ssl=True
            if no ssl argument is passed to the __init__ method of
            ApplicationRunner and the websocket URL starts with "wss:".
            r?   r@   zwss://127.0.0.1:8080/wssr'   TrF   rD   N)r   r7   r   rH   rI   rJ   r$   r   failrK   rL   rM   rP   r   r   r   test_omitted_SSLContext_secure   s   
"z4TestApplicationRunner.test_omitted_SSLContext_securec                 C   sh   t t %}tt t fd|_tdddd}d}| t||jd W d   dS 1 s-w   Y  dS )	z
            ApplicationRunner must raise an exception if given an ssl value of True
            but only a "ws:" URL.
            r@   ws://127.0.0.1:8080/wssr'   TrC   ^ssl argument value passed to ApplicationRunner conflicts with the "ws:" prefix of the url argument\. Did you mean to use "wss:"\?$rE   N)r   r7   rJ   r$   r>   	Exceptionr   )r   rN   r3   r<   r   r   r   "test_conflict_SSL_True_with_ws_url   s   "z8TestApplicationRunner.test_conflict_SSL_True_with_ws_urlc                 C   s   ddl }z|j W n ty   d}Y nw | }tt %}tt t fd|_tdd|d}d}| t||j	d	 W d   dS 1 sFw   Y  dS )
z
            ApplicationRunner must raise an exception if given an ssl value that is
            an instance of SSLContext, but only a "ws:" URL.
            r   NTr@   rT   r'   rC   rU   rE   )
rD   create_default_contextr:   r   r7   rJ   r$   r>   rV   r   )r   rD   contextrN   r3   r<   r   r   r   $test_conflict_SSLContext_with_ws_url   s   
"z:TestApplicationRunner.test_conflict_SSLContext_with_ws_urlN)
r   r   r    r!   r>   rO   rQ   rS   rW   rZ   r   r   r   r   r8   j   s    	r8   )
__future__r   r   osr/   txaio.testutilr   environgetmockr   zope.interfacer   twisted.internet.interfacesr   rH   r	   TestCaser"   rI   unittest.mockr7   r.   trolliusautobahn.asyncio.wampr$   r8   r   r   r   r   <module>   s(   "