U
    luf                     @   s   d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
mZ zd dlZW n ek
rl   dZY nX edk	rd dlmZ eedkdG d	d
 d
ejZG dd deZG dd deZG dd deZeedkdG dd deZdS )    )md5N)utf8)AsyncHTTPTestCase)httpclient_test)ApplicationRequestHandler)CurlAsyncHTTPClientzpycurl module not presentc                   @   s   e Zd Zdd ZdS )CurlHTTPClientCommonTestCasec                 C   s$   t tddd}| t|t  |S )NFZ
allow_ipv6)defaults)r   dict
assertTrue
isinstance)selfclient r   E/tmp/pip-unpacked-wheel-bmg6zs32/tornado/test/curl_httpclient_test.pyget_http_client   s    z,CurlHTTPClientCommonTestCase.get_http_clientN)__name__
__module____qualname__r   r   r   r   r   r	      s   r	   c                   @   s   e Zd Zdd Zdd ZdS )DigestAuthHandlerc                 C   s   || _ || _d S Nusernamepassword)r   r   r   r   r   r   
initialize   s    zDigestAuthHandler.initializec                 C   s  d}d}d}| j jdd }|d k	rj|dd\}}|dksBti }|dD ]D}| d	d\}	}
|
d
 dkr|
d dkr|
dd }
|
||	< qP|d |kst|d |kst|d |kst|d | jkst|d | j jksttt	d| j|| j
f  }tt	d| j j| j jf  }tt	d|||f  }||d kr^| d n
| d n | d | dd|||f  d S )NtestZasdfZ1234Authorization    Digest,=r   "realmopaquenoncer   uriz%s:%s:%sz%s:%sresponseokZfaili  zWWW-Authenticatez*Digest realm="%s", nonce="%s", opaque="%s")requestheadersgetsplitAssertionErrorstripr   pathr   r   r   	hexdigestmethodwrite
set_statusZ
set_header)r   r&   r'   r(   auth_header	auth_modeparamsZ
param_dictpairkvh1h2digestr   r   r   r.   !   sB    


zDigestAuthHandler.getN)r   r   r   r   r.   r   r   r   r   r      s   r   c                   @   s   e Zd Zdd ZdS )CustomReasonHandlerc                 C   s   |  dd d S )N   Custom reasonr6   r   r   r   r   r.   J   s    zCustomReasonHandler.getNr   r   r   r.   r   r   r   r   r@   I   s   r@   c                   @   s   e Zd Zdd ZdS )CustomFailReasonHandlerc                 C   s   |  dd d S )Ni  rB   rC   rD   r   r   r   r.   O   s    zCustomFailReasonHandler.getNrE   r   r   r   r   rF   N   s   rF   c                       sL   e Zd Z fddZdd Zdd Zdd Zd	d
 Zdd Zdd Z	  Z
S )CurlHTTPClientTestCasec                    s   t    |  | _d S r   )supersetUpcreate_clienthttp_clientrD   	__class__r   r   rI   U   s    
zCurlHTTPClientTestCase.setUpc                 C   s0   t dtdddfdtdddfdtfdtfgS )	N/digestfoobarr   /digest_non_ascii   barユ£/custom_reason/custom_fail_reason)r   r   r@   rF   rD   r   r   r   get_appY   s    zCurlHTTPClientTestCase.get_appc                 K   s   t f dtddd|S )NTFr
   )Zforce_instancer   )r   r   )r   kwargsr   r   r   rJ   g   s     z$CurlHTTPClientTestCase.create_clientc                 C   s$   | j ddddd}| |jd d S )NrN   r?   rO   rP   r8   Zauth_usernameZauth_password   okfetchassertEqualbodyr   r*   r   r   r   test_digest_authl   s       z'CurlHTTPClientTestCase.test_digest_authc                 C   s   |  d}| |jd d S )NrS   rB   )rZ   r[   reasonr]   r   r   r   test_custom_reasonr   s    
z)CurlHTTPClientTestCase.test_custom_reasonc                 C   s    |  d}| t|jd d S )NrT   zHTTP 400: Custom reason)rZ   r[   strerrorr]   r   r   r   test_fail_custom_reasonv   s    
z.CurlHTTPClientTestCase.test_fail_custom_reasonc                 C   s$   | j ddddd}| |jd d S )NrQ   r?   rO   rR   rW   rX   rY   r]   r   r   r   test_digest_auth_non_asciiz   s    z1CurlHTTPClientTestCase.test_digest_auth_non_ascii)r   r   r   rI   rU   rJ   r^   r`   rc   rd   __classcell__r   r   rL   r   rG   S   s   rG   )hashlibr   ZunittestZtornado.escaper   Ztornado.testingr   Ztornado.testr   Ztornado.webr   r   ZpycurlImportErrorZtornado.curl_httpclientr   ZskipIfZHTTPClientCommonTestCaser	   r   r@   rF   rG   r   r   r   r   <module>   s$   
-