a
    mzf                     @   s   d 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Z	ddl
ZddlZddlmZ eeZejejeZddddZG dd dejZG d	d
 d
ZG dd dZG dd dZdS )z9A library of helper functions for the Cheroot test suite.    N)webtest)z	127.0.0.1i7  wsgi)	bind_addrserverZwsgi_appc                   @   sh   e Zd ZdZdZdZejjej	j
dZedd Zedd Zed	d
 Zedd ZdZdddZdS )CherootWebCasez&Helper class for a web app test suite. http)r   Znativec                 C   s   t  }|t| di  |dd}| j|}|du rJtd|d  |f i || _| jj	\| _
| _| jjdu rd}d| _nd}tjj| _d	| _tj d
 }td| tdtj td| jj| tdt  t| dr|   |   dS )z)Create and run one HTTP server per class.configr   r   NzUnknown server in config: %sr   r   z (ssl)httpsr   z/Python version used to run this test script: %szCheroot version: %szHTTP server version: %s%szPID: %ssetup_server)r	   copyupdategetattrpopavailable_serversgetRuntimeError
httpserverr   ZHOSTZPORTZssl_adapterschemer   clientHTTPSConnectionZ	HTTP_CONNsysversionsplitloginfocheroot__version__protocolosgetpidhasattrr   start)clsconfZs_classZserver_factorysslv r'   A/var/www/media/lib/python3.9/site-packages/cheroot/test/helper.pysetup_class'   s,    

zCherootWebCase.setup_classc                 C   s   t | dr|   dS )zCleanup HTTP server.r   N)r!   stopr#   r'   r'   r(   teardown_classH   s    
zCherootWebCase.teardown_classc                 C   s,   t j| jjd  | jjs(td qdS )zLoad and start the HTTP server.)targetg?N)	threadingThreadr   Z
safe_startr"   readytimesleepr+   r'   r'   r(   r"   N   s    zCherootWebCase.startc                 C   s$   | j   t| dd}|r |  dS )zTerminate HTTP server.ZteardownN)r   r*   r   )r#   tdr'   r'   r(   r*   U   s    
zCherootWebCase.stop   Nc                 C   sN   |du r| j }||kr || }n|| }|tj|dk sJtd|||f dS )z2Assert ``abs(dt1 - dt2)`` is within ``Y`` seconds.N)secondsz$%r and %r are not within %r seconds.)date_tolerancedatetime	timedeltaAssertionError)selfZdt1Zdt2r5   diffr'   r'   r(   assertEqualDates_   s    
zCherootWebCase.assertEqualDates)N)__name__
__module____qualname____doc__script_namer   r   r   Serverr   Z
HTTPServerr   classmethodr)   r,   r"   r*   r6   r<   r'   r'   r'   r(   r      s    
 


r   c                   @   s   e Zd ZdZdd ZdS )RequestzHTTP request container.c                 C   s
   || _ dS )zInitialize HTTP request.N)environ)r:   rE   r'   r'   r(   __init__r   s    zRequest.__init__N)r=   r>   r?   r@   rF   r'   r'   r'   r(   rD   o   s   rD   c                   @   s    e Zd ZdZdd Zdd ZdS )ResponsezHTTP response container.c                 C   s   d| _ ddi| _d| _dS )zInitialize HTTP response.z200 OKzContent-Typez	text/htmlN)statusheadersbodyr:   r'   r'   r(   rF   z   s    
zResponse.__init__c                 C   sP   | j du rg S t| j tr(| j dgS t| j tr<| j gS dd | j D S dS )z'Generate iterable response body object.N
iso-8859-1c                 S   s   g | ]}| d qS )rL   )encode).0xr'   r'   r(   
<listcomp>       z#Response.output.<locals>.<listcomp>)rJ   
isinstancestrrM   bytesrK   r'   r'   r(   output   s    
zResponse.outputN)r=   r>   r?   r@   rF   rU   r'   r'   r'   r(   rG   w   s   rG   c                   @   s   e Zd ZdZdd ZdS )
ControllerzWSGI app for tests.c                    s   t |t  } z| j|d  }W n ty:   d _Y nl0 || }|durt fdddD s| _z jdt	t
| W n  ty   t|tjs Y n0 | j j    S )zWSGI request handler.Z	PATH_INFOz404 Not FoundNc                 3   s   | ]} j |V  qd S )N)rH   
startswith)rN   status_coderespr'   r(   	<genexpr>   s   z&Controller.__call__.<locals>.<genexpr>)Z204Z304zContent-Length)rD   rG   handlersKeyErrorrH   anyrJ   rI   
setdefaultrS   len	TypeErrorrR   typesGeneratorTypeitemsrU   )r:   rE   Zstart_responsereqhandlerrU   r'   rY   r(   __call__   s(    
zController.__call__N)r=   r>   r?   r@   rg   r'   r'   r'   r(   rV      s   rV   )r@   r7   loggingr   r   r1   r.   rb   http.clientr   Zcheroot.serverr   Zcheroot.wsgiZcheroot.testr   	getLoggerr=   r   pathabspathdirname__file__Zthisdirr	   ZWebCaser   rD   rG   rV   r'   r'   r'   r(   <module>   s*   
S