U
    d                     @   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-fekwu36z/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    Z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r5   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  rA   rB   rC   r   r   r   r-   O   s    zCustomFailReasonHandler.getNrD   r   r   r   r   rE   N   s   rE   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_clientrC   	__class__r   r   rH   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?   rE   rC   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   rI   g   s     z$CurlHTTPClientTestCase.create_clientc                 C   s$   | j ddddd}| |jd d S )NrM   r>   rN   rO   r7   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 )NrR   rA   )rY   rZ   reasonr\   r   r   r   test_custom_reasonr   s    
z)CurlHTTPClientTestCase.test_custom_reasonc                 C   s    |  d}| t|jd d S )NrS   zHTTP 400: Custom reason)rY   rZ   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 )NrP   r>   rN   rQ   rV   rW   rX   r\   r   r   r   test_digest_auth_non_asciiz   s    z1CurlHTTPClientTestCase.test_digest_auth_non_ascii)r   r   r   rH   rT   rI   r]   r_   rb   rc   __classcell__r   r   rK   r   rF   S   s   rF   )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?   rE   rF   r   r   r   r   <module>   s$   
-