B
    ˦Z^,                @   s  d dl Z d dlmZ d dl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Zd dlmZmZmZmZmZmZmZ d dlmZ d dlZd dlZG dd de jZG dd	 d	e jZG d
d dZG dd dZG dd deZG dd dej Z!G dd dZ"G dd dZ#G dd dej$Z%G dd dZ&G dd dZ'dd Z(dd Z)G d d! d!ej*j+Z,G d"d# d#ej*j-Z.G d$d% d%ej*j+Z/G d&d' d'Z0G d(d) d)e jZ1d*d+ Z2G d,d- d-e jZ3G d.d/ d/e jZ4G d0d1 d1e jZ5e6d2kre 7  dS )3    N)support)test_urllib)RequestOpenerDirectorHTTPBasicAuthHandlerHTTPPasswordMgrWithPriorAuth_parse_proxy_proxy_bypass_macosx_sysconfAbstractDigestAuthHandler)urlparsec               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TrivialTestsc          
   C   s   xdD ]}i }t d| | |d= |dkrftjdkrf|d|d }}| |jd | |jd x4| D ](\}}| |jd	| d
|||jf  qpW qW d S )N)requestresponseparseerrorZrobotparserzfrom urllib.%s import *__builtins__r   ntZurl2pathnamepathname2urlZ
nturl2pathz	urllib.%sz.%r is exposed in 'urllib.%s' but defined in %r)execosnamepopassertEqual
__module__items)selfmodulecontextupkv r"   C/work/yifan.wang/lscsoft/opt/python37/lib/python3.7/test_urllib2.pytest___all__    s    
zTrivialTests.test___all__c             C   sh   |  ttjjd tjtjj	tj
d}tjdkr@d| }nd| }tj|}|  |  d S )Nz	bogus url/r   z
file:///%sz	file://%s)assertRaises
ValueErrorurllibr   urlopenr   pathabspath__file__replacesepr   readclose)r   ZfnameZfile_urlfr"   r"   r#   test_trivial/   s    

zTrivialTests.test_trivialc          
   C   sd   ddddgfdddgfdddd	d
dddgfddddgfg}x$|D ]\}}|  tj|| q@W d S )Nza,b,cabczpath"o,l"og"i"cal, examplezpath"o,l"og"i"calZexampleza, b, "c", "d", "e,f", g, hz"c"z"d"z"e,f"ghza="b\"c", d="e\,f", g="h\\i"za="b"c"zd="e,f"zg="h\i")r   r(   r   Zparse_http_list)r   testsstringlistr"   r"   r#   test_parse_http_listA   s    
z!TrivialTests.test_parse_http_listc             C   s"   t jd}| |jt| d S )Nreason)r(   r   URLErrorassertInr<   str)r   errr"   r"   r#   test_URLError_reasonstrL   s    z$TrivialTests.test_URLError_reasonstrN)__name__r   __qualname__r$   r2   r;   rA   r"   r"   r"   r#   r      s   r   c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
RequestHdrsTestsc             C   sD   d}|  t|ddidjd d |  t|ddidjd d dS )a(  
        The Request.headers dictionary is not a documented interface.  It
        should stay that way, because the complete set of headers are only
        accessible through the .get_header(), .has_header(), .header_items()
        interface.  However, .headers pre-dates those methods, and so real code
        will be using the dictionary.

        The introduction in 2.4 of those methods was a mistake for the same
        reason: code that previously saw all (urllib2 user)-provided headers in
        .headers now sees only a subset.

        zhttp://example.comz	Spam-eggsblah)headersz	spam-EggSN)r   r   rF   )r   urlr"   r"   r#   test_request_headers_dictS   s    
z*RequestHdrsTests.test_request_headers_dictc             C   s   d}t |ddid}| |d | | dg |dd | t| ddg | |d	 | |	d	 | |	d	d
d
 |
d | |d |dd | |d |
d | |d dS )a;  
        Note the case normalization of header names here, to
        .capitalize()-case.  This should be preserved for
        backwards-compatibility.  (In the HTTP case, normalization to
        .title()-case is done by urllib2 before sending headers to
        http.client).

        Note that e.g. r.has_header("spam-EggS") is currently False, and
        r.get_header("spam-EggS") returns None, but that could be changed in
        future.

        Method r.remove_header should remove items both from r.headers and
        r.unredirected_hdrs dictionaries
        zhttp://example.comz	Spam-eggsrE   )rF   )z	Spam-eggsrE   zFoo-Barbaz)zFoo-barrI   z	Not-theredefaultzUnredirected-spamZEggsN)r   
assertTrue
has_headerr   Zheader_items
add_headersortedassertFalseassertIsNone
get_headerZremove_headeradd_unredirected_header)r   rG   reqr"   r"   r#   test_request_headers_methodsh   s     


z-RequestHdrsTests.test_request_headers_methodsc             C   s  t j }|j}|j}|dddd |dddd |dddd	 |dd
dd |dddd | |ddd | |ddd | |ddd | |ddd | |ddd | |ddd |d	ddd |d	ddd | |d	dd | |d	dd |dddd |dddd  | |ddd! |d"dd#d" | |d"dd$ | |d"d%d& | |dd'd& | |dd(d) | |ddd) | |dd
d* | |ddd+ d S ),Nz
Some Realmzhttp://example.com/joepasswordzhttp://example.com/ninizhttp://c.example.com:31283r5   zd.example.com4dze.example.com:31285ezexample.com)rU   rV   zhttp://example.comzhttp://example.com/spamzhttp://example.com/spam/spamzhttp://example.com/foofoozhttp://example.com/barbarnini)r]   rW   )r^   r_   r4   firstrE   secondspam)ra   rb   r3   1)rc   r3   zhttp://a.example.com/)NNzc.example.comzc.example.com:3128)rX   r5   )rY   rZ   )r[   r\   )r(   r   HTTPPasswordMgradd_passwordfind_user_passwordr   )r   mgraddfind_user_passr"   r"   r#   test_password_manager   s`    
z&RequestHdrsTests.test_password_managerc             C   sr  t j }|j}|j}|dddd |dddd |d	d
dd |dddd | |ddd | |ddd | |ddd | |ddd | |ddd | |ddd | |ddd | |ddd | |ddd | |d	dd | |d	d
d | |d	dd | |ddd | |dd d | |dd!d | |dd"d d#S )$z
        The point to note here is that we can't guess the default port if
        there's no scheme.  This applies to both add_password and
        find_user_password.
        r1   zhttp://g.example.com:8010jr6   zhttp://h.example.com11r    r7   zi.example.com:8012lizj.example.com13mzg.example.com:100)NNzg.example.com:80)rk   rl   zg.example.comzhttp://g.example.com:100zhttp://g.example.comzh.example.com)rm   r    zh.example.com:80zhttp://h.example.com:80zi.example.com)rn   ro   zhttp://i.example.com:80)rq   rr   zj.example.com:80zhttp://j.example.comzhttp://j.example.com:80N)r(   r   rd   re   rf   r   )r   rg   rh   ri   r"   r"   r#   "test_password_manager_default_port   sD    
z3RequestHdrsTests.test_password_manager_default_portN)rB   r   rC   rH   rT   rj   rs   r"   r"   r"   r#   rD   Q   s   $FrD   c               @   s(   e Zd Zg ZdejfddZdd ZdS )
MockOpenerNc             C   s   |||  | _ | _| _d S )N)rS   datatimeout)r   rS   ru   rv   r"   r"   r#   open   s    zMockOpener.openc             G   s   || | _ | _d S )N)protoargs)r   rx   ry   r"   r"   r#   r     s    zMockOpener.error)rB   r   rC   
addheaderssocket_GLOBAL_DEFAULT_TIMEOUTrw   r   r"   r"   r"   r#   rt      s   rt   c               @   s(   e Zd ZdddZd	ddZdd ZdS )
MockFileNc             C   s   d S )Nr"   )r   countr"   r"   r#   r/     s    zMockFile.readc             C   s   d S )Nr"   )r   r~   r"   r"   r#   readline
  s    zMockFile.readlinec             C   s   d S )Nr"   )r   r"   r"   r#   r0     s    zMockFile.close)N)N)rB   r   rC   r/   r   r0   r"   r"   r"   r#   r}     s   

r}   c               @   s   e Zd Zdd ZdS )MockHeadersc             C   s   t |  S )N)r:   values)r   r   r"   r"   r#   
getheaders  s    zMockHeaders.getheadersN)rB   r   rC   r   r"   r"   r"   r#   r     s   r   c               @   s&   e Zd ZdddZdd Zdd ZdS )	MockResponseNc             C   s.   t j| | ||||f\| _| _| _| _d S )N)ioStringIO__init__codemsgrF   rG   )r   r   r   rF   ru   rG   r"   r"   r#   r     s    zMockResponse.__init__c             C   s   | j S )N)rF   )r   r"   r"   r#   info  s    zMockResponse.infoc             C   s   | j S )N)rG   )r   r"   r"   r#   geturl  s    zMockResponse.geturl)N)rB   r   rC   r   r   r   r"   r"   r"   r#   r     s   
r   c               @   s   e Zd Zdd Zdd ZdS )MockCookieJarc             C   s
   || _ d S )N)ach_req)r   r   r"   r"   r#   add_cookie_header#  s    zMockCookieJar.add_cookie_headerc             C   s   || | _ | _d S )N)ec_reqec_r)r   r   r   r"   r"   r#   extract_cookies&  s    zMockCookieJar.extract_cookiesN)rB   r   rC   r   r   r"   r"   r"   r#   r   "  s   r   c               @   s   e Zd Zdd Zdd ZdS )
FakeMethodc             C   s   || _ || _|| _d S )N)	meth_namehandleaction)r   r   r   r   r"   r"   r#   r   +  s    zFakeMethod.__init__c             G   s   | j | j| jf| S )N)r   r   r   )r   ry   r"   r"   r#   __call__0  s    zFakeMethod.__call__N)rB   r   rC   r   r   r"   r"   r"   r#   r   *  s   r   c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
MockHTTPResponsec             C   s"   || _ || _|| _|| _d| _d S )N   )fpr   statusr<   r   )r   r   r   r   r<   r"   r"   r#   r   5  s
    zMockHTTPResponse.__init__c             C   s   dS )N r"   )r   r"   r"   r#   r/   <  s    zMockHTTPResponse.readc             C   s   i S )Nr"   )r   r"   r"   r#   r   ?  s    zMockHTTPResponse.infoc             C   s   | j S )N)rG   )r   r"   r"   r#   r   B  s    zMockHTTPResponse.geturlN)rB   r   rC   r   r/   r   r   r"   r"   r"   r#   r   4  s   r   c               @   sT   e Zd Zdd ZejfddZdd Zddd	Zdd
dddZ	dd Z
dd ZdS )MockHTTPClassc             C   s(   d| _ g | _d | _d| _d | _i | _d S )Nr   F)levelreq_headersru   raise_on_endheadersZsock_tunnel_headers)r   r"   r"   r#   r   G  s    zMockHTTPClass.__init__c             C   s   || _ || _| S )N)hostrv   )r   r   rv   r"   r"   r#   r   O  s    zMockHTTPClass.__call__c             C   s
   || _ d S )N)r   )r   r   r"   r"   r#   set_debuglevelT  s    zMockHTTPClass.set_debuglevelNc             C   s&   || _ || _|r|| _n
| j  d S )N)_tunnel_hostZ_tunnel_portr   clear)r   r   portrF   r"   r"   r#   
set_tunnelW  s
    zMockHTTPClass.set_tunnelF)encode_chunkedc            C   sP   || _ || _|d k	r&|  j| 7  _| j  |r:|| _|| _| jrLt d S )N)	methodselectorr   r   sortru   r   r   OSError)r   r   rG   ZbodyrF   r   r"   r"   r#   r   _  s    
zMockHTTPClass.requestc             C   s   t t i ddS )Nr   OK)r   r}   )r   r"   r"   r#   getresponsel  s    zMockHTTPClass.getresponsec             C   s   d S )Nr"   )r   r"   r"   r#   r0   o  s    zMockHTTPClass.close)NN)NN)rB   r   rC   r   r{   r|   r   r   r   r   r   r0   r"   r"   r"   r#   r   F  s   
r   c               @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )MockHandleri  c             C   s   |  | d S )N)_define_methods)r   methodsr"   r"   r#   r   x  s    zMockHandler.__init__c             C   sN   xH|D ]@}t |dkr |\}}n
|d  }}t||| j}t| j|| qW d S )N   )lenr   r   setattr	__class__)r   r   specr   r   Zmethr"   r"   r#   r   {  s    


zMockHandler._define_methodsc             O   s   | j j| |||f |d kr"d S |dkr.| S |dkrHtddi d}|S |dkrXtdS |dr||d	d
 d  }yt|}W n tk
r   Y nX tddi d}| j 	d|d ||di S |dkrt
j	ddstd S )Nzreturn selfzreturn responser   r   r   zreturn requestzhttp://blah/r       httpr   raiserE   F)parentcallsappendr   r   
startswithrfindintr'   r   r(   r=   AssertionError)r   Zfn_namer   ry   kwdsZresr   r"   r"   r#   r     s*    
zMockHandler.handlec             C   s   d S )Nr"   )r   r"   r"   r#   r0     s    zMockHandler.closec             C   s   || _ g | j _d S )N)r   r   )r   r   r"   r"   r#   
add_parent  s    zMockHandler.add_parentc             C   s   t |dsdS | j|jk S )Nhandler_orderT)hasattrr   )r   otherr"   r"   r#   __lt__  s    
zMockHandler.__lt__N)
rB   r   rC   r   r   r   r   r0   r   r   r"   r"   r"   r#   r   s  s   	r   c             C   sf   g }d}xX|D ]P}G dd dt }||}| j|7  _||  |d }|| | | qW |S )a  Create MockHandlers and add them to an OpenerDirector.

    meth_spec: list of lists of tuples and strings defining methods to define
    on handlers.  eg:

    [["http_error", "ftp_open"], ["http_open"]]

    defines methods .http_error() and .ftp_open() on one handler, and
    .http_open() on another.  These methods just record their arguments and
    return None.  Using a tuple instead of a string causes the method to
    perform some action (see MockHandler.handle()), eg:

    [["http_error"], [("http_open", "return request")]]

    defines .http_error() on one handler (which simply returns None), and
    .http_open() on another handler, which returns a Request object.

    r   c               @   s   e Zd ZdS )z6add_ordered_mock_handlers.<locals>.MockHandlerSubclassN)rB   r   rC   r"   r"   r"   r#   MockHandlerSubclass  s   r   r   )r   r   r   r   add_handler)opener	meth_spechandlersr~   methsr   r7   r"   r"   r#   add_ordered_mock_handlers  s    


r   c              G   s"   t  }x| D ]}|| qW |S )N)r   r   )Zhandler_instancesr   r7   r"   r"   r#   build_test_opener  s    
r   c               @   s$   e Zd Zdd Zdd Zdd ZdS )MockHTTPHandlerc             C   s   || _ || _|   d S )N)r   rF   reset)r   r   rF   r"   r"   r#   r     s    zMockHTTPHandler.__init__c             C   s   d| _ g | _d S )Nr   )_countrequests)r   r"   r"   r#   r     s    zMockHTTPHandler.resetc             C   s   dd l }dd l}| j|| | jdkrl| jd | _tjj| j	 }|
| j}| jd|t | j	||S || _|
d}tdd|d| S d S )Nr   r   r   z

r   r   r   )emailcopyr   r   Zdeepcopyr   r   client	responsesr   Zmessage_from_stringrF   r   r   r}   rS   r   get_full_url)r   rS   r   r   r   r   r"   r"   r#   	http_open  s    

zMockHTTPHandler.http_openN)rB   r   rC   r   r   r   r"   r"   r"   r#   r     s   r   c               @   s   e Zd ZdddZdd ZdS )MockHTTPSHandlerr   c             C   s   t jjj| |d t | _d S )N)
debuglevel)r(   r   AbstractHTTPHandlerr   r   httpconn)r   r   r"   r"   r#   r     s    zMockHTTPSHandler.__init__c             C   s   |  | j|S )N)do_openr   )r   rS   r"   r"   r#   
https_open  s    zMockHTTPSHandler.https_openN)r   )rB   r   rC   r   r   r"   r"   r"   r#   r     s   
r   c               @   s$   e Zd Zdd Zdd Zdd ZdS )MockHTTPHandlerCheckAuthc             C   s   || _ d| _d S )NF)r   has_auth_header)r   r   r"   r"   r#   r     s    z!MockHTTPHandlerCheckAuth.__init__c             C   s
   d| _ d S )NF)r   )r   r"   r"   r#   r     s    zMockHTTPHandlerCheckAuth.resetc             C   s6   | drd| _tjj| j }t| j|t d| S )NAuthorizationTr   )	rL   r   r   r   r   r   r   r}   r   )r   rS   r   r"   r"   r#   r     s    
z"MockHTTPHandlerCheckAuth.http_openN)rB   r   rC   r   r   r   r"   r"   r"   r#   r     s   r   c               @   s   e Zd Zdd Zdd ZdS )MockPasswordManagerc             C   s   || _ || _|| _|| _d S )N)realmrG   userrV   )r   r   Zurir   rV   r"   r"   r#   re     s    z MockPasswordManager.add_passwordc             C   s   || _ || _| j| jfS )N)target_realm
target_urlr   rV   )r   r   Zauthurir"   r"   r#   rf     s    z&MockPasswordManager.find_user_passwordN)rB   r   rC   re   rf   r"   r"   r"   r#   r     s   r   c               @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )OpenerDirectorTestsc             C   s(   G dd dt }| tt j|  d S )Nc               @   s   e Zd ZdS )z<OpenerDirectorTests.test_add_non_handler.<locals>.NonHandlerN)rB   r   rC   r"   r"   r"   r#   
NonHandler  s   r   )objectr&   	TypeErrorr   r   )r   r   r"   r"   r#   test_add_non_handler  s    z(OpenerDirectorTests.test_add_non_handlerc             C   s`   ddl m} t }ddgdgg}t|| |tj  x dD ]}| ||j	|d  q@W d S )Nr   )r=   )r   zreturn self)Z
proxy_openzreturn self)Zredirect_requestzreturn self)Zdoproxyredirectz://example.com/)
urllib.errorr=   r   r   r   r(   r   ZUnknownHandlerr&   rw   )r   r=   or   schemer"   r"   r#   test_badly_named_methods"  s    	

z,OpenerDirectorTests.test_badly_named_methodsc             C   s   t  }dddgdgdgdgg}t||}td}||}| ||d  |d df|d dfg}xBt||jD ]2\}}|\}	}
}}| |	|
f| | ||f qpW d S )Nr   ftp_openhttp_error_302)r   zreturn selfzhttp://example.com/r   r   )r   r   r   rw   r   zipr   )r   r   r   r   rS   rr   expectedgothandlerr   ry   r   r"   r"   r#   test_handled7  s    

z OpenerDirectorTests.test_handledc             C   s   t  }g }xRdgdfdgdfgD ]:\}}G dd dt}||}||_|| || q W |d | |jd d |d  | |jd d |d  d S )	N)r   zreturn selfi  r   r   c               @   s   e Zd ZdS )zCOpenerDirectorTests.test_handler_order.<locals>.MockHandlerSubclassN)rB   r   rC   r"   r"   r"   r#   r   V  s   r   zhttp://example.com/r   )r   r   r   r   r   rw   r   r   )r   r   r   r   r   r   r7   r"   r"   r#   test_handler_orderQ  s    


z&OpenerDirectorTests.test_handler_orderc             C   sZ   t  }dgdgg}t||}td}| tjj|j| | |j	|d d|fi fg d S )N)r   r   )r   zreturn selfzhttp://example.com/r   r   )
r   r   r   r&   r(   r   r=   rw   r   r   )r   r   r   r   rS   r"   r"   r#   
test_raisec  s    
zOpenerDirectorTests.test_raisec             C   s   t  }dgddgdddgdgg}t||}G dd	 d	}td
}|| t|jdks\t|d d|ff|d d|| ddi ffg}xJt||jD ]:\}}|\}	}
}| |	|
f|d d  | ||d  qW d S )N)r   z	error 302)Zhttp_error_400r   r   )r   zreturn responseZhttp_error_303Z
http_errorr   c               @   s   e Zd Zdd ZdS )z4OpenerDirectorTests.test_http_error.<locals>.Unknownc             S   s   dS )NTr"   )r   r   r"   r"   r#   __eq__~  s    z;OpenerDirectorTests.test_http_error.<locals>.Unknown.__eq__N)rB   r   rC   r   r"   r"   r"   r#   Unknown}  s   r   zhttp://example.com/r   r   i.  r   )	r   r   r   rw   r   r   r   r   r   )r   r   r   r   r   rS   r   r   r   r   Zmethod_namery   r"   r"   r#   test_http_errorp  s$    


z#OpenerDirectorTests.test_http_errorc             C   s  t  }ddgddgg}t||}td}|| |d df|d df|d df|d dfg}xt|jD ]\}\}}}	}
|dk r| ||f||  | t|	d | |	d t qj| ||f||  | t|	d | |	d t |	d d k	rj| |	d t	 qjW d S )	N)http_requestzreturn request)http_responsezreturn responsezhttp://example.com/r   r   r   r   r   )
r   r   r   rw   	enumerater   r   r   ZassertIsInstancer   )r   r   r   r   rS   r   rp   r   r   ry   r   r"   r"   r#   test_processors  s(    

z#OpenerDirectorTests.test_processorsN)
rB   r   rC   r   r   r   r   r   r   r  r"   r"   r"   r#   r     s   r   c             C   s^   y|  d W n tk
r,   tdY nX tj| }tjdkrZ|	drZ|dd  }|S )Nzutf-8zpath is not encodable to utf8r   z///r   )
encodeUnicodeEncodeErrorunittestZSkipTestr(   r   r   r   r   r   )r*   urlpathr"   r"   r#   sanepathname2url  s    r  c               @   sh  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Ze e!j"d9kd:d;d< Z#dWd>d?Z$d@dA Z%dBdC Z&dDdE Z'dFdG Z(dHdI Z)dJdK Z*dLdM Z+dNdO Z,dPdQ Z-dRdS Z.dTdU Z/dVS )XHandlerTestsc                s  G dd d G  fdddt jj}dd l}d}||}t |_xjdd|jd	d	d
ddgddf	dd|jdd	d
ddgddf	dd|jdd	d
ddgddf	dd|jdd	d
ddgddf	dddd	d	dddgd	d f	dd|jd	d	dg dd f	gD ]\	}}}}}	}
}}}t|}d |_|	|}| 
|j| | 
|j|	 | 
|jt| | 
|j| | 
|j| | 
|jj| | 
|jj|
 | }| 
|d| | 
t|d t| qW d S )Nc               @   s$   e Zd Zdd Zdd Zdd ZdS )z-HandlerTests.test_ftp.<locals>.MockFTPWrapperc             S   s
   || _ d S )N)ru   )r   ru   r"   r"   r#   r     s    z6HandlerTests.test_ftp.<locals>.MockFTPWrapper.__init__c             S   s$   || | _ | _t| jt| jfS )N)filenamefiletyper   r   ru   r   )r   r  r	  r"   r"   r#   retrfile  s    z6HandlerTests.test_ftp.<locals>.MockFTPWrapper.retrfilec             S   s   d S )Nr"   )r   r"   r"   r#   r0     s    z3HandlerTests.test_ftp.<locals>.MockFTPWrapper.closeN)rB   r   rC   r   r
  r0   r"   r"   r"   r#   MockFTPWrapper  s   r  c                   s&   e Zd Zdd Zejf fdd	ZdS )z-HandlerTests.test_ftp.<locals>.NullFTPHandlerc             S   s
   || _ d S )N)ru   )r   ru   r"   r"   r#   r     s    z6HandlerTests.test_ftp.<locals>.NullFTPHandler.__init__c                s4   || | _ | _|| | _| _|| _ | j| _| jS )N)r   passwdr   r   dirsru   
ftpwrapper)r   r   r  r   r   r  rv   )r  r"   r#   connect_ftp  s
    z9HandlerTests.test_ftp.<locals>.NullFTPHandler.connect_ftpN)rB   r   rC   r   r{   r|   r  r"   )r  r"   r#   NullFTPHandler  s   r  r   zrheum rhaponicumz ftp://localhost/foo/bar/baz.html	localhostr   Ir]   r^   zbaz.htmlz	text/htmlz'ftp://parrot@localhost/foo/bar/baz.htmlZparrotz*ftp://%25parrot@localhost/foo/bar/baz.htmlz%parrotz,ftp://%2542parrot@localhost/foo/bar/baz.htmlz	%42parrotzftp://localhost:80/foo/bar/P   Dzftp://localhost/baz.gif;type=aAzbaz.gifzContent-typezContent-length)r(   r   Z
FTPHandlerftplibrt   r   ZFTP_PORTr   rv   r   r   r   r  r   r{   gethostbynamer   r  r  r  r	  r   getr   r   )r   r  r  ru   r7   rG   r   r   r   r  Ztype_r  r  ZmimetyperS   r   rF   r"   )r  r#   test_ftp  sL    
"
zHandlerTests.test_ftpc          
   C   s  dd l }tj }t  }|_tj}tt	j
|}d}d| d| dtd|f g}ytt }W n tjk
r   d}Y nX |r|d||f  x|D ]}	t|d}
zpz|
| W d |
  X |t|	}z| }| }| }W d |  X t	|}|jj|jd	d
}W d t	| X | || | |d d | |d d | |d | | ||	 qW xd| dddtdt	 |f dt	 |f gD ]T}	z@t|d}
z|
| W d |
  X |  tj!j"|jt|	 W d t	| X qW tj }t  }|_x|dD ]t\}	}t|	}y|| W n$ tj!j"k
rd   | #| Y nX | $|j%| | |j&d | |j&dk| q"W d S )Nr   s   hello, world
zfile://localhost%sz	file://%szfile://%s%sr  r   wbT)ZusegmtzContent-typez
text/plainzContent-lengthrq   zLast-modifiedzfile://localhost:80%szfile:///file_does_not_exist.txtz)file://not-a-local-host.com//dir/file.txtzfile://%s:80%s/%sz,file://somerandomhost.ontheinternet.com%s/%s))zfile://ftp.example.com//foo.txtF)z file://ftp.example.com///foo.txtF)zfile://ftp.example.com/foo.txtF)z"file://somehost//foo/something.txtF)z#file://localhost//foo/something.txtFftp)'Zemail.utilsr(   r   ZFileHandlerrt   r   r   TESTFNr  r   r*   r+   r{   r  ZgethostnameZgaierrorr   rw   writer0   Z	file_openr   r/   r   r   statZutilsZ
formatdatest_mtimeremover   getcwdr&   r   r=   rO   assertIsrS   type)r   r   r7   r   r  r  ZtowriteurlsZ	localaddrrG   r1   r   ru   rF   ZrespurlZstatsZmodifiedr  rS   r"   r"   r#   	test_file  s|    











zHandlerTests.test_filec             C   sr  t j }t  }|_d}xdD ]\}}t||ddi}d |_|dd t }|	||}|j
 |j |j |j |j|jdkdf | }	|	j |	j | | | | |jd	 | |jd
 | |j| | |jd | |jdddg | |j| q W d|_| t jj|j	|| tdd}| t|j| dg|_x$dD ]}td|}t ddi d}||}
|d kr| !d|j" | !d|j" n$| |j"d d | |j"d d | |j"d d	 | |j"d d |dd |dd |dd |dd ||}
| |j"d d | |j"d d | |j"d d | |j"d d qNW d S )Nzhttp://example.com/))GETN)POSTs   blahFoor^   Spameggsr   r   zexample.comr   r%   )Z
Connectionr0   )r(  r^   )r)  r*  TZbadpost)    Nr   zContent-lengthzContent-type0z!application/x-www-form-urlencodedHostr]   rI   )#r(   r   r   rt   r   r   rv   rR   r   r   r/   r   r   r   r   r   r  __contains__r   r   r   r   r   r   ru   r   r&   r   r=   r   do_request_rz   r   assertNotInunredirected_hdrs)r   r7   r   rG   r   ru   rS   r   r   hdrsnewreqr"   r"   r#   	test_httpZ  sh    
   





zHandlerTests.test_httpc       	   	   C   s   t j }t  }|_tjddd}|j}|  | 	t
j| t|dB}td|i }||}|d}| |d | |d W d Q R X t|dF}td|d	d
i}||}| t|dd
 | |d W d Q R X d S )Nzw+bF)modedeleterbzhttp://example.com/zTransfer-encodingchunkedzContent-lengthzContent-Length   )r(   r   r   rt   r   tempfileZNamedTemporaryFiler   r0   
addCleanupr   unlinkrw   r   r/  rQ   r   rO   rL   r   )	r   r7   r   file_objZ	file_pathr1   rS   r3  ter"   r"   r#   test_http_body_file  s"    



z HandlerTests.test_http_body_filec             C   s   t j }t  }|_t }td|i }||}| 	|
dd | |d ddi}td||}||}| 	t|
dd | |d |  d S )Nzhttp://example.com/zTransfer-encodingr8  zContent-lengthzContent-Lengthr9  )r(   r   r   rt   r   r   BytesIOr   r/  r   rQ   rO   rL   r   r0   )r   r7   r   r=  rS   r3  rF   r"   r"   r#   test_http_body_fileobj  s    


z#HandlerTests.test_http_body_fileobjc          
   C   s   t j }t  }|_tjddg}xi ddifD ]x}tj|tj	d^}t
d|j|}||}|s| |dd  | |dd	 n| t|dd W d Q R X q0W d S )
Nz-cpasszContent-Lengthr9  )stdoutzhttp://example.com/zContent-lengthzTransfer-encodingr8  )r(   r   r   rt   r   sys
executable
subprocessPopenPIPEr   rC  r/  r   rQ   r   )r   r7   r   cmdrF   procrS   r3  r"   r"   r#   test_http_body_pipe  s    

z HandlerTests.test_http_body_pipec             C   s   t j }t  }|_dd }xli ddifD ]\}td| |}||}|sr| |dd  | |dd q,| t	|dd q,W d S )	Nc               s   s
   dV  d S )Ns   oner"   r"   r"   r"   r#   iterable_body  s    z;HandlerTests.test_http_body_iterable.<locals>.iterable_bodyzContent-Length   zhttp://example.com/zContent-lengthzTransfer-encodingr8  )
r(   r   r   rt   r   r   r/  r   rQ   r   )r   r7   r   rL  rF   rS   r3  r"   r"   r#   test_http_body_iterable  s    

z$HandlerTests.test_http_body_iterablec             C   sH   t j }t |_|tdd}| |dd | 	|
d d S )Nzhttp://example.com/r"   zTransfer-encodingr8  zContent-length)r(   r   r   rt   r   r/  r   r   rQ   rO   rL   )r   r7   rS   r"   r"   r#   test_http_body_empty_seq  s
    
z%HandlerTests.test_http_body_empty_seqc             C   sp   t j }t  }|_tdddddg}x@i ddifD ]0}td||}||}| t	|
d	d q8W d S )
Nr  r   r         zContent-Length   zhttp://example.com/zContent-length)r(   r   r   rt   r   arrayr   r/  r   r   rQ   )r   r7   r   Ziterable_arrayrF   rS   r3  r"   r"   r#   test_http_body_array  s    

z!HandlerTests.test_http_body_arrayc             C   s6   t  }tdd}|| |d | |jd d S )Nr   )r   zhttps://www.example.com)r   r   r   rw   r   Z_debuglevel)r   r   r7   r"   r"   r#   test_http_handler_debuglevel  s
    


z)HandlerTests.test_http_handler_debuglevelc             C   s   t j }t |_d}ddddg}xZ|D ]R}t||}||}| |jd d |	dd  ||}| |jd d q(W d S )	Nr+  z#http://example.com/foo/bar/baz.htmlz$http://example.com//foo/bar/baz.htmlz$http://example.com/foo//bar/baz.htmlz$http://example.com/foo/bar//baz.htmlr-  zexample.comzsomeproxy:3128)
r(   r   r   rt   r   r   r/  r   r1  	set_proxy)r   r7   ru   Zds_urlsZds_urlZds_reqZ	np_ds_reqZp_ds_reqr"   r"   r#   test_http_doubleslash  s    




z"HandlerTests.test_http_doubleslashc             C   sn   dddg}t d}xV|D ]N}||_t|}| | | | |jpFd|j | t| j|j qW d S )Nzhttp://example.com?foo=bar#bazz)http://example.com?foo=bar&spam=eggs#bashzhttp://example.comr   )r   full_urlr   r   r   fragmentZquery)r   r$  r   rG   Zparsedr"   r"   r#   test_full_url_setter-  s    
z!HandlerTests.test_full_url_setterc             C   s6   t d}|`| |j | |j | |jd d S )Nzhttp://www.example.comr   )r   rX  rP   rY  r   r   )r   r   r"   r"   r#   test_full_url_deleterD  s
    z"HandlerTests.test_full_url_deleterc             C   sz   t j }t |_d}t|}||}| |jd | |j	d d}t|}||}| |jd | |j	d d S )Nzhttp://www.python.org?getspamzwww.python.orgz	/?getspamzhttp://www.python.orgr   )
r(   r   r   rt   r   r   r/  r   r   r   )r   r7   Z	weird_urlrS   r3  Zurl_without_pathr"   r"   r#   test_fixpath_in_weirdurlsK  s    


z&HandlerTests.test_fixpath_in_weirdurlsc             C   s  t j }t  }|_d}t|}tddi d|}|||}| || | 	t
|d tddi d|}|||}| || | 	t
|d tdd	i d|}|||}| || | 	t
|d td
di d|}| ||| | |jd | |j||d
di f d S )Nzhttp://example.com/r   r   r   rx      ZAccepted   zPartial contenti  zBad gatewayr   )r(   r   ZHTTPErrorProcessorrt   r   r   r   r   r"  rO   r   rP   r   rx   ry   )r   r7   r   rG   rS   r   newrr"   r"   r#   test_errors^  s(    
zHandlerTests.test_errorsc             C   s   t  }tj|}t |_td}tddi d}||}| 	|j
| | 	|j
| | |jd | |j |||}| 	|j| | 	|j| | 	|| d S )Nzhttp://example.com/r   r   r   zexample.com)r   r(   r   HTTPCookieProcessorrt   r   r   r   r   r"  r   r   origin_req_hostrO   Zunverifiabler   r   r   )r   cjr7   rS   r   r3  r_  r"   r"   r#   test_cookiesw  s    
zHandlerTests.test_cookiesc          
   C   s  d}d}t j }t  }|_xndD ]d}x\dD ]R}t|d| }t||}tj|_	|
dd |d k	r|
dtt| |d	d
 y||t |dtd|i W n. t jjk
r   | |d | | Y nX | |j | y| |j d W n$ tk
r(   | |jj Y nX dd |jjD }	| d|	 | d|	 | |jjd d | d	|jj | d	|jj q4W q&W t|}tj|_	|fdd}
t|dd}d}tj|_	y x|
||d |d }qW W n, t jjk
r   | |t jjj Y nX t|dd}d}tj|_	y$x|
||d|  |d }q.W W n, t jjk
rz   | |t jjj Y nX d S )Nzhttp://example.com/a.htmlzhttp://example.com/b.html)i-  i.  i/  i3  )Nz
blah
blah
zhttp_error_%sZNonsensezviking=withholdzContent-Lengthr)  rb   Blahlocationi3  r&  c             S   s   g | ]}|  qS r"   )lower).0xr"   r"   r#   
<listcomp>  s    z.HandlerTests.test_redirect.<locals>.<listcomp>zcontent-lengthzcontent-typec          	   S   s    |  |t ddtd|i d S )Ni.  re  rf  )r   r}   r   )r7   rS   rG   r"   r"   r#   r     s    z,HandlerTests.test_redirect.<locals>.redirectzexample.com)rb  r   zhttp://example.com/r   zhttp://example.com/%d)r(   r   HTTPRedirectHandlerrt   r   getattrr   r{   r|   rv   rM   r?   r   rR   r}   r   r   	HTTPErrorr   assertIsNotNonerS   r   
get_methodAttributeErrorrO   ru   rF   r0  r1  Zmax_repeatsZmax_redirections)r   from_urlZto_urlr7   r   r   ru   r   rS   rF   r   r~   r"   r"   r#   test_redirect  sj    

zHandlerTests.test_redirectc             C   s   d}dddg}dddg}d}t j }t  }|_t|}tj|_x>|D ]6}|d	 | }	| 	t j
j|j|t d
dtd|	i qHW xF|D ]>}|d	 | }
||t d
dtd|
i | |j |
 qW d S )Nzhttp://example.com/a.htmlr   httpsr  fileZimapZldapzexample.com/b.htmlz://i.  zSecurity Loopholerf  zThat's fine)r(   r   rk  rt   r   r   r{   r|   rv   r&   r   rm  r   r}   r   r   rS   r   )r   rq  Zvalid_schemesZinvalid_schemesZschemeless_urlr7   r   rS   r   Zinvalid_url	valid_urlr"   r"   r#   test_invalid_redirect  s$    





z"HandlerTests.test_invalid_redirectc          	   C   sn   d}d}t j }t  }|_t|}tj|_t j	
||}||t ddtd|i | |j | d S )Nzhttp://example.com/a.htmlz/b.htmli.  zThat's finerf  )r(   r   rk  rt   r   r   r{   r|   rv   r   Zurljoinr   r}   r   r   rS   r   )r   rq  Zrelative_urlr7   r   rS   ru  r"   r"   r#   test_relative_redirect  s    
z#HandlerTests.test_relative_redirectc       	      C   s   ddl m} ddlm} | }||dd tdd}tj }tj }tj	|}t
||||}|d | |jd d S )	Nr   )	CookieJar)interact_netscapezhttp://www.example.com/z	spam=eggsi.  z%Location: http://www.cracker.com/

ZCookie)Zhttp.cookiejarrx  Ztest.test_http_cookiejarry  r   r(   r   HTTPDefaultErrorHandlerrk  ra  r   rw   rO   rS   rL   )	r   rx  ry  rc  hhhdehhrhZcpr   r"   r"   r#   test_cookie_redirect  s    



z!HandlerTests.test_cookie_redirectc             C   sT   d}t dd| }tj }tj }t|||}|d}| | |	  d S )Nz(http://www.example.com/index.html#OK

i.  z
Location: zhttp://www.example.com)
r   r(   r   rz  rk  r   rw   r   r   strip)r   Zredirected_urlr{  r|  r}  r   r   r"   r"   r#   test_redirect_fragment  s    


z#HandlerTests.test_redirect_fragmentc                sn   t jj d}t|t j_tt jd  td fdd}|t jj_t	j
d}| d d S )Ns(   HTTP/1.1 302 Found
Location: ?query

HTTPConnection)z/pathz/path?queryc                s2    |t  j| ||f|| d| j_d S )Ns   HTTP/1.1 200 OK

Hello!)r   nextr   r   Zfakedata)connr   rG   poskw)
real_classr   r$  r"   r#   r     s    z3HandlerTests.test_redirect_no_path.<locals>.requestzhttp://python.org/pathzhttp://python.org/path?query)r   r   r  r   fakehttpr;  r   iterr   r(   r)   r   r   )r   Z	response1r   r   r"   )r  r   r$  r#   test_redirect_no_path  s    
z"HandlerTests.test_redirect_no_pathc       
   
   C   s   G dd dt jj}| }t j|}d}xp|D ]h\}}| t|L td| d |_|	d}d| d }|j
}	| |	|t|	 W d Q R X q0W d S )	Nc               @   s   e Zd Zdd ZdS )z4HandlerTests.test_redirect_encoding.<locals>.Handlerc             S   s(   |  | j|}| jj| _td| _|S )Ns)   HTTP/1.1 200 OK
Content-Length: 3

123)r   
connectionbuflast_bufr   r  )r   rS   resultr"   r"   r#   r   !  s
    
z>HandlerTests.test_redirect_encoding.<locals>.Handler.http_openN)rB   r   rC   r   r"   r"   r"   r#   Handler   s   r  ))s   /på-dansk/s   /p%C3%A5-dansk/)s   /spaced%20path/s   /spaced%20path/)s   /spaced path/s   /spaced%20path/)s   /?på-dansks   /?p%C3%A5-dansks!   HTTP/1.1 302 Redirect
Location: s   

zhttp://example.com/s   GET     )r(   r   HTTPHandlerbuild_openerZsubTestreprr   r  r  rw   r  rK   r   )
r   r  r   r   r8   rf  r  r   r   r   r"   r"   r#   test_redirect_encoding  s    
z#HandlerTests.test_redirect_encodingc             C   s   t  }tjtdd}|| dgg}t||}td}| |j	d |
| | |j	d | |d dfgdd	 |jD  d S )
Nzproxy.example.com:3128)r   )r   zreturn responsezhttp://acme.example.com/zacme.example.comr   r   c             S   s   g | ]}|d d qS )r   r   r"   )rh  tupr"   r"   r#   rj  O  s    z+HandlerTests.test_proxy.<locals>.<listcomp>)r   r(   r   ProxyHandlerdictr   r   r   r   r   rw   r   )r   r   phr   r   rS   r"   r"   r#   
test_proxy@  s    


zHandlerTests.test_proxyc             C   s   dt jd< t }tjtdd}|| td}| 	|j
d || | 	|j
d td}| 	|j
d || | 	|j
d t jd= d S )	Nz
python.orgno_proxyzproxy.example.com)r   zhttp://www.perl.org/zwww.perl.orgzhttp://www.python.orgzwww.python.org)r   environr   r(   r   r  r  r   r   r   r   rw   )r   r   r  rS   r"   r"   r#   test_proxy_no_proxyQ  s    



z HandlerTests.test_proxy_no_proxyc             C   sf   dt jd< t }tjtdd}|| td}| 	|j
d || | 	|j
d t jd= d S )N*r  zproxy.example.com)r   zhttp://www.python.orgzwww.python.org)r   r  r   r(   r   r  r  r   r   r   r   rw   )r   r   r  rS   r"   r"   r#   test_proxy_no_proxy_all`  s    


z$HandlerTests.test_proxy_no_proxy_allc             C   s   t  }tjtdd}|| dgg}t||}td}| |j	d |
| | |j	d | |d dfgdd	 |jD  d S )
Nzproxy.example.com:3128)rs  )r   zreturn responsezhttps://www.example.com/zwww.example.comr   r   c             S   s   g | ]}|d d qS )r   r   r"   )rh  r  r"   r"   r#   rj  y  s    z1HandlerTests.test_proxy_https.<locals>.<listcomp>)r   r(   r   r  r  r   r   r   r   r   rw   r   )r   r   r  r   r   rS   r"   r"   r#   test_proxy_httpsk  s    


zHandlerTests.test_proxy_httpsc             C   s   t  }tjtdd}|| t }|| td}|dd |dd | 	|j
d | |j || | d	|jj | d
|jj | |j | 	|j
d | 	|dd d S )Nzproxy.example.com:3128)rs  zhttps://www.example.com/zProxy-AuthorizationFooBarz
User-AgentGrailzwww.example.com)zProxy-Authorizationr  )z
User-Agentr  zProxy-authorization)r   r(   r   r  r  r   r   r   rM   r   r   rP   r   rw   r0  r   r   r>   rn  rQ   )r   r   r  Zhttps_handlerrS   r"   r"   r#   $test_proxy_https_proxy_authorization{  s$    




z1HandlerTests.test_proxy_https_proxy_authorizationdarwinzonly relevant for OSXc             C   sz   ddddddgd}x"dD ]}|  t||d	|  qW x"d
D ]}| t||d|  q>W dg d}|  td| d S )NFzfoo.barz	*.bar.comz	127.0.0.1z10.10z10.0/16)Zexclude_simple
exceptions)zfoo.barzwww.bar.comz	127.0.0.1z	10.10.0.1z10.0.0.1z expected bypass of %s to be True)zabc.foo.barzbar.comz	127.0.0.2z	10.11.0.1Znotinbypassz!expected bypass of %s to be FalseTtest)rK   r	   rO   )r   Zbypassr   r"   r"   r#   test_osx_proxy_bypass  s    



z"HandlerTests.test_osx_proxy_bypass"c          
   C   s`   t  }t }tj|}d}tdd|||f }|| || | ||d|||dd d S )NzACME Widget Storei  z(WWW-Authenticate: Basic realm=%s%s%s

r   z!http://acme.example.com/protected)r   r   r(   r   r   r   r   _test_basic_auth)r   
quote_charr   password_managerauth_handlerr   http_handlerr"   r"   r#   test_basic_auth  s    


zHandlerTests.test_basic_authc             C   s   | j dd d S )N')r  )r  )r   r"   r"   r#   (test_basic_auth_with_single_quoted_realm  s    z5HandlerTests.test_basic_auth_with_single_quoted_realmc             C   sp   t  }t }tj|}d}tdd| }|| || | t | 	||d|||dd W d Q R X d S )NzACME Widget Storei  z$WWW-Authenticate: Basic realm=%s

r   z!http://acme.example.com/protected)
r   r   r(   r   r   r   r   ZassertWarnsUserWarningr  )r   r   r  r  r   r  r"   r"   r#   #test_basic_auth_with_unquoted_realm  s    


z0HandlerTests.test_basic_auth_with_unquoted_realmc          
   C   sv   t  }tjtdd}|| t }tj|}d}tdd| }|| || | 	||d|||dd d S )Nzproxy.example.com:3128)r   zACME Networksi  z(Proxy-Authenticate: Basic realm="%s"

zProxy-authorizationz&http://acme.example.com:3128/protected)
r   r(   r   r  r  r   r   ZProxyBasicAuthHandlerr   r  )r   r   r  r  r  r   r  r"   r"   r#   test_proxy_basic_auth  s    



z"HandlerTests.test_proxy_basic_authc       
   
   C   s   G dd dt }G dd dtjj}G dd dtjj}| }t }||}||}d}tdd	| }	|| || ||	 | ||d
||	|dd | 	|j
ddgd  d S )Nc               @   s   e Zd Zdd Zdd ZdS )zQHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirectorc             S   s   t |  g | _d S )N)r   r   recorded)r   r"   r"   r#   r     s    
zZHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.__init__c             S   s   | j | d S )N)r  r   )r   r   r"   r"   r#   record  s    zXHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.recordN)rB   r   rC   r   r  r"   r"   r"   r#   RecordingOpenerDirector  s   r  c               @   s   e Zd Zdd ZdS )zOHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandlerc             _   s&   | j d tjjj| f|| d S )Ndigest)r   r  r(   r   HTTPDigestAuthHandlerhttp_error_401)r   ry   r   r"   r"   r#   r    s    z^HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandler.http_error_401N)rB   r   rC   r  r"   r"   r"   r#   TestDigestAuthHandler  s   r  c               @   s   e Zd Zdd ZdS )zNHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandlerc             _   s&   | j d tjjj| f|| d S )Nbasic)r   r  r(   r   r   r  )r   ry   r   r"   r"   r#   r    s    z]HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandler.http_error_401N)rB   r   rC   r  r"   r"   r"   r#   TestBasicAuthHandler  s   r  zACME Networksi  z&WWW-Authenticate: Basic realm="%s"

r   z!http://acme.example.com/protectedr  r  r   )r   r(   r   r  r   r   r   r   r  r   r  )
r   r  r  r  r   r  Zdigest_handlerZbasic_handlerr   r  r"   r"   r#   #test_basic_and_digest_auth_handlers  s$    	



z0HandlerTests.test_basic_and_digest_auth_handlersc             C   sD   t  }tjd }tdd}|| || | t|jd d S )Ni  zWWW-Authenticate: Kerberos

zhttp://www.example.com)	r   r(   r   r  r   r   r&   r'   rw   )r   r   Zdigest_auth_handlerr  r"   r"   r#   $test_unsupported_auth_digest_handler  s    

z1HandlerTests.test_unsupported_auth_digest_handlerc             C   sD   t  }tjd }tdd}|| || | t|jd d S )Ni  zWWW-Authenticate: NTLM

zhttp://www.example.com)	r   r(   r   r   r   r   r&   r'   rw   )r   r   Zbasic_auth_handlerr  r"   r"   r#   #test_unsupported_auth_basic_handler  s    

z0HandlerTests.test_unsupported_auth_basic_handlerc	             C   sH  dd l }	d\}
}||||
| | ||j | ||j | |
|j | ||j || | |j| | |j	| | t
|jd | |jd | td|
|f d}d|	|   }| |jd || | |jd j| | d  |_|_|  || | t
|jd | |jd | d S )Nr   )wilecoyoter   z%s:%sasciizBasic r   )base64re   r   r   rG   r   rV   rw   r   r   r   r   rO   rL   bytesZencodebytesr  decoderQ   r1  r   )r   r   r  Zauth_headerr   r  r  request_urlZprotected_urlr  r   rV   ZuserpassZauth_hdr_valuer"   r"   r#   r  "  s0    

zHandlerTests._test_basic_authc       	      C   s~   d\}}d}t d}t }t|}|jd |||dd ||}| | t }|| || || | |j	 d S )N)r  r  z!http://acme.example.com/protectedr   T)is_authenticated)
r   r   r   re   r  rK   r   r   rw   r   )	r   r   rV   r  r  pwd_managerauth_prior_handleris_authr   r"   r"   r#   test_basic_prior_auth_auto_sendG  s    




z,HandlerTests.test_basic_prior_auth_auto_sendc       
      C   s   d\}}d}d}t  }t|}||||| ||}| | t }|| tddd  }	||	 || ||}| 	| t
d}	| |	j t }|| ||	 || | 	|	j d S )N)r  r  z!http://acme.example.com/protectedZACMEi  z&WWW-Authenticate: Basic realm="%s"

r   )r   r   re   r  rO   r   r   r   rw   rK   r   r   )
r   r   rV   r  r   r  r  r  r   r  r"   r"   r#   .test_basic_prior_auth_send_after_first_successa  s.    









z;HandlerTests.test_basic_prior_auth_send_after_first_successc          
   C   s   xzdD ]r\}}d |}t| | }tj }td}d|_|	||}|
  W dQ R X | |jjd | qW dS )z=Test the connection is cleaned up when the response is closed))zConnection: closes   data)zTransfer-Encoding: chunkeds   4
data
0

)zContent-Length: 4s   datazHTTP/1.1 200 OK
{}

zhttp://dummy/NzConnection not closed with {!r})formatr   r  r  r(   r   r   r   rv   r   r/   rK   fakesockclosed)r   Ztransferru   headerr  r   rS   Zrespr"   r"   r#   test_http_closed  s    


zHandlerTests.test_http_closedc          	   C   s\   t d}tj }td}d|_| tj	j
 ||| W dQ R X | |jjd dS )z;Test the connection is cleaned up after an invalid responser+  zhttp://dummy/NzConnection not closed)r   r  r(   r   r   r   rv   r&   r   r   ZBadStatusLiner   rK   r  r  )r   r  r   rS   r"   r"   r#   test_invalid_closed  s    

z HandlerTests.test_invalid_closedN)r  )0rB   r   rC   r  r%  r4  r?  rA  rK  rN  rO  rT  rU  rW  rZ  r[  r\  r`  rd  rr  rv  rw  r~  r  r  r  r  r  r  r  r  r  
skipUnlessrD  platformr  r  r  r  r  r  r  r  r  r  r  r  r  r"   r"   r"   r#   r    sR   @\BJ	#
2

%(r  c               @   sN   e Zd Zdd Zdd Zeedddd Z	d	d
 Z
dd Zdd ZdS )	MiscTestsc                s"   |  t fdd|jD  d S )Nc             3   s   | ]}|j  kV  qd S )N)r   )rh  r7   )handler_classr"   r#   	<genexpr>  s   z/MiscTests.opener_has_handler.<locals>.<genexpr>)rK   anyr   )r   r   r  r"   )r  r#   opener_has_handler  s    zMiscTests.opener_has_handlerc             C   s(  G dd dt jj}G dd dt jj}G dd dt jj}t jj}|||}| || | || ||| }| || | || ||}| || | }| |t jj |t jj}| |t jj |t j }| |t jj G dd dt jj}|||}| || | || d S )	Nc               @   s   e Zd ZdS )z2MiscTests.test_build_opener.<locals>.MyHTTPHandlerN)rB   r   rC   r"   r"   r"   r#   MyHTTPHandler  s   r  c               @   s   e Zd Zdd ZdS )z/MiscTests.test_build_opener.<locals>.FooHandlerc             S   s   d S )Nr"   )r   r"   r"   r#   foo_open  s    z8MiscTests.test_build_opener.<locals>.FooHandler.foo_openN)rB   r   rC   r  r"   r"   r"   r#   
FooHandler  s   r  c               @   s   e Zd Zdd ZdS )z/MiscTests.test_build_opener.<locals>.BarHandlerc             S   s   d S )Nr"   )r   r"   r"   r#   bar_open  s    z8MiscTests.test_build_opener.<locals>.BarHandler.bar_openN)rB   r   rC   r  r"   r"   r"   r#   
BarHandler  s   r  c               @   s   e Zd ZdS )z7MiscTests.test_build_opener.<locals>.MyOtherHTTPHandlerN)rB   r   rC   r"   r"   r"   r#   MyOtherHTTPHandler  s   r  )r(   r   r  BaseHandlerr  r  )r   r  r  r  r  r   r  r"   r"   r#   test_build_opener  s,    

zMiscTests.test_build_openerZnetworkztest requires network accessc          	   C   s   t d tj }tjd}| d |j ||d	d | d|j | d|
d ||d	d | d|j | d|
d W d Q R X d S )	Nzhttp://www.example.com/rc   zus-ascii   1zContent-lengthZ
1234567890s
   1234567890rk   )r   Ztransient_internetr(   r   r  r   r   ru   rw   r  rQ   )r   r   r   r"   r"   r#   test_issue16464  s    
zMiscTests.test_issue16464c             C   s   d}d } }}d}t j|||||}| t|d | |jd | t|d | |jd d|j|j	f }| t
|| d|j|j	f }| t|| dS )z
        Issue 13211 reveals that HTTPError didn't implement the URLError
        interface even though HTTPError is a subclass of URLError.
        zsomething bad happenedNzContent-Length: 42r<   rF   zHTTP Error %s: %sz<HTTPError %s: %r>)r(   r   rm  rK   r   r   r<   rF   r   r   r?   r  )r   r   rG   r   r   r2  r@   Zexpected_errmsgr"   r"   r#   test_HTTPError_interface  s    z"MiscTests.test_HTTPError_interfacec             C   sR   ddddddddddd	d
g}x |D ]\}}|  t|| q"W | ttdf d S )N)zproxy.example.com)NNNzproxy.example.com)zproxy.example.com:3128)NNNzproxy.example.com:3128)zjoe:password@proxy.example.com)NrU   rV   zproxy.example.com)z#joe:password@proxy.example.com:3128)NrU   rV   zproxy.example.com:3128)zhttp://proxy.example.com/)r   NNzproxy.example.com)zhttp://proxy.example.com:3128/)r   NNzproxy.example.com:3128)z&http://joe:password@proxy.example.com/)r   rU   rV   zproxy.example.com)z*http://joe:password@proxy.example.com:3128)r   rU   rV   zproxy.example.com:3128)z1ftp://joe:password@proxy.example.com/rubbish:3128)r  rU   rV   zproxy.example.com)z%http://joe:password@proxy.example.com)r   rU   rV   zproxy.example.comzfile:/ftp.example.com)r   r   r&   r'   )r   Zparse_proxy_test_casesZtcr   r"   r"   r#   test_parse_proxy  s    zMiscTests.test_parse_proxyc          	   C   s<   t  }| t}|d W d Q R X | t|jd d S )NZinvalidz5Unsupported digest authentication algorithm 'invalid')r
   r&   r'   Zget_algorithm_implsr   r?   Z	exception)r   r   excr"   r"   r#   test_unsupported_algorithm  s    z$MiscTests.test_unsupported_algorithmN)rB   r   rC   r  r  r  r  r   Zis_resource_enabledr  r  r  r  r"   r"   r"   r#   r    s   ,%r  c               @   s   e Zd ZG dd deZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS ) RequestTestsc               @   s   e Zd ZdZdS )zRequestTests.PutRequestPUTN)rB   r   rC   r   r"   r"   r"   r#   
PutRequest)  s   r  c             C   sL   t d| _t ddddid| _t ddd| _| d| _| jddd| _d S )	Nzhttp://www.python.org/~jeremy/ru   zX-Testr  )rF   HEAD)r   r'  )r   r  postheadr  put
force_post)r   r"   r"   r#   setUp,  s    
zRequestTests.setUpc             C   s^   |  d| j  |  d| j  |  d| j  |  d| j  |  d| j  d S )Nr'  r&  r  r  )r   r  ro  r  r  r  r  )r   r"   r"   r#   test_method6  s
    zRequestTests.test_methodc             C   sL   |  | jj | d| j  d| j_| | jj | d| j  d S )Nr&  rb   r'  )rO   r  ru   r   ro  rK   )r   r"   r"   r#   	test_data=  s
    zRequestTests.test_datac             C   sN   |  d| jj | jdd | d| jjd  d| j_|  d| jj d S )NzContent-length*   rb   )r0  r  r1  rR   r   ru   )r   r"   r"   r#   .test_setting_data_should_remove_content_lengthG  s
    z;RequestTests.test_setting_data_should_remove_content_lengthc             C   sT   |  d| jj d| j_| jdd | d| jjd  | j`|  d| jj d S )NzContent-lengthr]   rP  )r0  r  r1  ru   rR   r   )r   r"   r"   r#   /test_deleting_data_should_remove_content_lengthO  s    z<RequestTests.test_deleting_data_should_remove_content_lengthc             C   s   |  d| j  d S )Nzhttp://www.python.org/~jeremy/)r   r  r   )r   r"   r"   r#   test_get_full_urlW  s    zRequestTests.test_get_full_urlc             C   s*   |  d| jj td}|  d|j d S )Nz	/~jeremy/zhttp://www.python.org/r%   )r   r  r   r   )r   rS   r"   r"   r#   test_selector[  s    zRequestTests.test_selectorc             C   s   |  d| jj d S )Nr   )r   r  r#  )r   r"   r"   r#   test_get_type`  s    zRequestTests.test_get_typec             C   s   |  d| jj d S )Nzwww.python.org)r   r  r   )r   r"   r"   r#   test_get_hostc  s    zRequestTests.test_get_hostc             C   s   t d}| d|j d S )Nzhttp://www.%70ython.org/zwww.python.org)r   r   r   )r   rS   r"   r"   r#   test_get_host_unquotef  s    z"RequestTests.test_get_host_unquotec             C   sR   |  | j  | jdd | | j  | d| jj | d| jj d S )Nzwww.perl.orgr   zwww.python.org)rO   r  Z	has_proxyrV  rK   r   rb  r   )r   r"   r"   r#   r  j  s
    zRequestTests.test_proxyc             C   s   t d}| d|j d S )Nz<URL:http://www.python.org>zwww.python.org)r   r   r   )r   rS   r"   r"   r#   test_wrapped_urlq  s    zRequestTests.test_wrapped_urlc             C   sL   t d}| d|j t d}| d|j d}t |}| | | d S )Nz-http://www.python.org/?qs=query#fragment=truez
/?qs=queryzhttp://www.python.org/#fun=truer%   z.http://docs.python.org/library/urllib2.html#OK)r   r   r   r   )r   rS   rG   r"   r"   r#   test_url_fragmentu  s    zRequestTests.test_url_fragmentc             C   s6   dddg}x&|D ]}t |}| | |j qW d S )Nzhttp://docs.python.orgz.http://docs.python.org/library/urllib2.html#OKz-http://www.python.org/?qs=query#fragment=true)r   r   r   rX  )r   r$  rG   rS   r"   r"   r#   test_url_fullurl_get_full_url  s    
z*RequestTests.test_url_fullurl_get_full_urlN)rB   r   rC   r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r"   r"   r"   r#   r  (  s   

r  __main__)8r  r  r   r   r   r   r{   rS  rD  r:  rF  Zurllib.requestr(   r   r   r   r   r   r	   r
   Zurllib.parser   r   Zhttp.clientr   ZTestCaser   rD   rt   r}   r  r   r   r   r   r   IOBaser   r   r   r   r   r   r  r   r   r   r   r   r   r  r  r  r  rB   mainr"   r"   r"   r#   <module>   s`   $3 ,

-6"        p a
