a
    mzf                     @   s
  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	d
 Ze
jdd Ze
jdd Ze
jdd Ze
jdd ZG dd dZ dd Z!dd Z"dd Z#dS )zAPytest fixtures and other helpers for doing testing by end-users.    )closingcontextmanagerN)webtestz0.0.0.0z::)	bind_addrZwsgi_app)r   Zgatewayc              	   c   s   t |   }|dd }ttfD ]:}z||f}| f d|i|}W n tyV   Y q"0  q^q"d|_tj|j	d}|
  |jstd qzz||fV  W |  |  n|  |  0 dS )z/Set up and tear down a Cheroot server instance.r   r   )targetg?N)configcopypopANY_INTERFACE_IPV6ANY_INTERFACE_IPV4OSErrorZshutdown_timeout	threadingThreadZ
safe_startstartreadytimesleepstopjoin)Zserver_factoryconfZ	bind_port	interfaceZactual_bind_addrZ
httpserverserver_thread r   =/var/www/media/lib/python3.9/site-packages/cheroot/testing.pycheroot_server!   s0    

r   c                  c   s>   t tjj\} }| |fV  W d   n1 s00    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   srvr   r   r   thread_and_wsgi_serverC   s    r!   c                  c   s>   t tjj\} }| |fV  W d   n1 s00    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   r   r   r   thread_and_native_serverM   s    r$   c                 C   s   | \}}|S )z4Set up and tear down a Cheroot WSGI server instance.r   )r!   _server_threadr    r   r   r   wsgi_serverW   s    r&   c                 C   s   | \}}|S )z4Set up and tear down a Cheroot HTTP server instance.r   )r$   r%   r    r   r   r   native_server^   s    r'   c                   @   s.   e Zd Zdd Zdd Zddd	Zd
d ZdS )_TestClientc                 C   s*   t |j\| _| _| _|| _|  | _d S )N)_get_conn_datar   
_interface_host_portserver_instanceget_connection_http_connection)selfr"   r   r   r   __init__f   s
    z_TestClient.__init__c                 C   s6   dj | j| jd}| jjd u r&tjjntjj}||S )Nz{interface}:{port})r   port)	formatr*   r,   r-   Zssl_adapterhttpclientHTTPConnectionHTTPSConnection)r0   nameZconn_clsr   r   r   r.   m   s    

z_TestClient.get_connectionGETNHTTP/1.1c              	   C   s"   t j|||| j| j|p| j|dS )N)methodheadershostr2   	http_connprotocol)r   ZopenURLr+   r,   r/   )r0   urir;   r<   r>   r?   r   r   r   requesty   s    z_TestClient.requestc                    s    fdd}|S )Nc                    s      }j| fd|i|S )Nr;   )upperrA   )r@   kwargsZhttp_method	attr_namer0   r   r   _wrapper   s    z)_TestClient.__getattr__.<locals>._wrapperr   )r0   rE   rF   r   rD   r   __getattr__   s    z_TestClient.__getattr__)r9   NNr:   )__name__
__module____qualname__r1   r.   rA   rG   r   r   r   r   r(   e   s     
r(   c              
   C   s   zDt tjtjd}|| df W d    n1 s80    Y  W n2 tyv } z|jtjkrb W Y d }~nd }~0 0 dS dS )N)familyr   TF)r   socketAF_INET6bindr   errnoZEADDRNOTAVAIL)r   sockZsock_errr   r   r   _probe_ipv6_sock   s    0rQ   c                 C   sR   t | tr| \}}n
| d }}t|}d|v rHt|sHd}d|v rH|}|||fS )Nr   :z	127.0.0.1)
isinstancetupler   r   rQ   )r   r=   r2   r   r   r   r   r)      s    



r)   c                 C   s   t | S )z5Create and return a test client for the given server.)r(   )r"   r   r   r   get_server_client   s    rU   )$__doc__
contextlibr   r   rO   rL   r   r   http.clientr4   ZpytestZcheroot.serverr   Zcheroot.testr   Zcheroot.wsgiZEPHEMERAL_PORTZNO_INTERFACEr   r   r   r   r"   r#   ZGatewayr   r   Zfixturer!   r$   r&   r'   r(   rQ   r)   rU   r   r   r   r   <module>   sJ   
!
	
	

(