
    Df                        d Z ddlmZmZ ddlZddlZddlZddlZddlZ	ddl
Z
ddlZddlmZ ddlZdZdZdZdZej        j        eefddej        j        eefej        j        diZed	             Ze
j        d
             Ze
j        d             Ze
j        d             Ze
j        d             Z G d d          Z d Z!d Z"d Z#dS )zAPytest fixtures and other helpers for doing testing by end-users.    )closingcontextmanagerN)webtestz0.0.0.0z::)	bind_addrwsgi_app)r   gatewayc              #   F  K   t           |                                          }|                    d          d         }t          t          fD ]"}	 ||f} | dd|i|} n# t
          $ r Y w xY wd|_        t          j        |j	                  }|
                                 |j        st          j        d           |j        	 ||fV  |                                 |                                 dS # |                                 |                                 w xY w)z/Set up and tear down a Cheroot server instance.r   r   )targetg?N )configcopypopANY_INTERFACE_IPV6ANY_INTERFACE_IPV4OSErrorshutdown_timeout	threadingThread
safe_startstartreadytimesleepstopjoin)server_factoryconf	bind_port	interfaceactual_bind_addr
httpserverserver_threads          >/var/www/media/lib/python3.11/site-packages/cheroot/testing.pycheroot_serverr%   !   sj      .!&&((D%%b)I');; 
 
			 )95'  * J E  	 	 	D	
 #$J $J,ABBBM 
3  Z'''' 	s   A
A+*A+C6 6*D c               #      K   t          t          j        j                  5 \  } }| |fV  ddd           dS # 1 swxY w Y   dS )ztSet up and tear down a Cheroot WSGI server instance.

    This emits a tuple of a thread and a server instance.
    N)r%   cherootwsgiServerr#   srvs     r$   thread_and_wsgi_serverr,   C   s       
+	,	, !0DS    ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !   
8<<c               #      K   t          t          j        j                  5 \  } }| |fV  ddd           dS # 1 swxY w Y   dS )ztSet up and tear down a Cheroot HTTP server instance.

    This emits a tuple of a thread and a server instance.
    N)r%   r'   server
HTTPServerr*   s     r$   thread_and_native_serverr1   M   s       
1	2	2 !6J}cS    ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !r-   c                     | \  }}|S )z4Set up and tear down a Cheroot WSGI server instance.r   )r,   _server_threadr+   s      r$   wsgi_serverr4   W   s     1NCJ    c                     | \  }}|S )z4Set up and tear down a Cheroot HTTP server instance.r   )r1   r3   r+   s      r$   native_serverr7   ^   s     3NCJr5   c                   ,    e Zd Zd Zd Z	 	 ddZd ZdS )	_TestClientc                     t          |j                  \  | _        | _        | _        || _        |                                 | _        d S )N)_get_conn_datar   
_interface_host_portserver_instanceget_connection_http_connection)selfr/   s     r$   __init__z_TestClient.__init__f   sH    2@3
 3
/TZ  & $ 3 3 5 5r5   c                     d                     | j        | j                  }| j        j        t
          j        j        nt
          j        j        } ||          S )Nz{interface}:{port})r    port)	formatr<   r>   r?   ssl_adapterhttpclientHTTPConnectionHTTPSConnection)rB   nameconn_clss      r$   r@   z_TestClient.get_connectionm   s^    #**o + 
 
 #/7 K&&K' 	
 x~~r5   GETNHTTP/1.1c           	      Z    t          j        |||| j        | j        |p| j        |          S )N)methodheadershostrE   	http_connprotocol)r   openURLr=   r>   rA   )rB   urirQ   rR   rT   rU   s         r$   requestz_TestClient.requesty   s<     $*84#8
 
 
 	
r5   c                       fd}|S )Nc                 L                                     } j        | fd|i|S )NrQ   )upperrX   )rW   kwargshttp_method	attr_namerB   s      r$   _wrapperz)_TestClient.__getattr__.<locals>._wrapper   s1    #//++K4<BBKB6BBBr5   r   )rB   r^   r_   s   `` r$   __getattr__z_TestClient.__getattr__   s/    	C 	C 	C 	C 	C 	C r5   )rN   NNrO   )__name__
__module____qualname__rC   r@   rX   r`   r   r5   r$   r9   r9   e   s]        6 6 6
 
 
 :>

 

 

 

    r5   r9   c                    	 t          t          j        t          j                            5 }|                    | df           d d d            n# 1 swxY w Y   dS # t          $ r }|j        t
          j        k    r Y d }~nd }~ww xY wdS )N)familyr   TF)r   socketAF_INET6bindr   errnoEADDRNOTAVAIL)r    socksock_errs      r$   _probe_ipv6_sockrm      s    V]&/:::;; 	&tIIy!n%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& t	    >U000 10000 5s:   ,A AA AA AA 
B	)BB	c                     t          | t                    r| \  }}n| d}}t          j        |          }d|v rt	          |          sd}d|v r|}|||fS )Nr   :z	127.0.0.1)
isinstancetupler   r    rm   )r   rS   rE   r    s       r$   r;   r;      sq    )U## "
ddd!$''I
i 0 ; ;	$;;DdD  r5   c                      t          |           S )z5Create and return a test client for the given server.)r9   )r/   s    r$   get_server_clientrs      s    vr5   )$__doc__
contextlibr   r   ri   rf   r   r   http.clientrH   pytestcheroot.serverr'   cheroot.testr   cheroot.wsgiEPHEMERAL_PORTNO_INTERFACEr   r   r(   r)   r/   r0   Gatewayr   r%   fixturer,   r1   r4   r7   r9   rm   r;   rs   r   r5   r$   <module>r      s   G G . . . . . . . .                                   L"N3  N"N3>)   	
   B ! ! ! ! ! !      % % % % % % % %P  ! ! !     r5   