HEX
Server: Apache
System: Linux zacp120.webway.host 4.18.0-553.50.1.lve.el8.x86_64 #1 SMP Thu Apr 17 19:10:24 UTC 2025 x86_64
User: govancoz (1003)
PHP: 8.3.26
Disabled: exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: //usr/local/lib/python3.7/test/__pycache__/test_urllib2.cpython-37.pyc
B

��g�/�@sddlZddlmZddlmZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlm
Z
mZmZmZmZmZmZddlmZddlZddlZGdd�dej�ZGdd	�d	ej�ZGd
d�d�ZGdd
�d
�ZGdd�de�ZGdd�dej �Z!Gdd�d�Z"Gdd�d�Z#Gdd�dej$�Z%Gdd�d�Z&Gdd�d�Z'dd�Z(dd�Z)Gd d!�d!ej*j+�Z,Gd"d#�d#ej*j-�Z.Gd$d%�d%ej*j+�Z/Gd&d'�d'�Z0Gd(d)�d)ej�Z1d*d+�Z2Gd,d-�d-ej�Z3Gd.d/�d/ej�Z4Gd0d1�d1ej�Z5e6d2k�re�7�dS)3�N)�support)�test_urllib)�Request�OpenerDirector�HTTPBasicAuthHandler�HTTPPasswordMgrWithPriorAuth�_parse_proxy�_proxy_bypass_macosx_sysconf�AbstractDigestAuthHandler)�urlparsec@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�TrivialTestsc
Cs�x�dD]�}i}td||�|d=|dkrftjdkrf|�d�|�d�}}|�|jd�|�|jd�x4|��D](\}}|�|jd	|d
|||jf�qpWqWdS)N)�request�response�parse�errorZrobotparserzfrom 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"�-/usr/local/lib/python3.7/test/test_urllib2.py�test___all__ s
zTrivialTests.test___all__cCsh|�ttjjd�tj�tjj��	tj
d�}tjdkr@d|}nd|}tj�|�}|��|�
�dS)Nz	bogus url�/rz
file:///%sz	file://%s)�assertRaises�
ValueError�urllibr
�urlopenr�path�abspath�__file__�replace�sepr�read�close)rZfnameZfile_url�fr"r"r#�test_trivial/s

zTrivialTests.test_trivialc
Csdddddgfdddgfdddd	d
ddd
gfddddgfg}x$|D]\}}|�tj�|�|�q@WdS)Nza,b,c�a�b�czpath"o,l"og"i"cal, examplezpath"o,l"og"i"calZexampleza, b, "c", "d", "e,f", g, hz"c"z"d"z"e,f"�g�hza="b\"c", d="e\,f", g="h\\i"za="b"c"zd="e,f"zg="h\i")rr(r
Zparse_http_list)r�tests�string�listr"r"r#�test_parse_http_listAs
z!TrivialTests.test_parse_http_listcCs"tj�d�}|�|jt|��dS)N�reason)r(r�URLError�assertInr<�str)r�errr"r"r#�test_URLError_reasonstrLsz$TrivialTests.test_URLError_reasonstrN)�__name__r�__qualname__r$r2r;rAr"r"r"r#rsrc@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�RequestHdrsTestscCsDd}|�t|ddid�jdd�|�t|ddid�jdd�dS)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.

        zhttp://example.comz	Spam-eggs�blah)�headersz	spam-EggSN)rrrF)r�urlr"r"r#�test_request_headers_dictSs

z*RequestHdrsTests.test_request_headers_dictcCs�d}t|ddid�}|�|�d��|�|��dg�|�dd�|�t|���ddg�|�|�d	��|�|�	d	��|�|�	d	d
�d
�|�
d�|�|�d��|�dd�|�|�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
        zhttp://example.comz	Spam-eggsrE)rF)z	Spam-eggsrEzFoo-Bar�baz)zFoo-barrIz	Not-there�defaultzUnredirected-spamZEggsN)r�
assertTrue�
has_headerrZheader_items�
add_header�sorted�assertFalse�assertIsNone�
get_headerZ
remove_header�add_unredirected_header)rrG�reqr"r"r#�test_request_headers_methodshs 


z-RequestHdrsTests.test_request_headers_methodscCs�tj��}|j}|j}|dddd�|dddd�|dddd	�|dd
dd�|dd
dd�|�|dd�d�|�|dd�d�|�|dd�d�|�|dd�d�|�|dd�d�|�|dd�d�|d	ddd�|d	ddd�|�|d	d�d�|�|d	d�d�|dddd�|dddd �|�|dd�d!�|d"dd#d"�|�|d"d�d$�|�|d"d%�d&�|�|dd'�d&�|�|dd(�d)�|�|dd�d)�|�|dd
�d*�|�|dd
�d+�dS),Nz
Some Realmzhttp://example.com/�joe�passwordzhttp://example.com/ni�nizhttp://c.example.com:3128�3r5z
d.example.com�4�dze.example.com:3128�5�ezexample.com)rUrVzhttp://example.comzhttp://example.com/spamzhttp://example.com/spam/spamzhttp://example.com/foo�foozhttp://example.com/bar�bar�nini)r]rW)r^r_r4�firstrE�second�spam)rarbr3�1)rcr3zhttp://a.example.com/)NNz
c.example.comzc.example.com:3128)rXr5)rYrZ)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_managercCsrtj��}|j}|j}|dddd�|dddd�|d	d
dd�|d
ddd�|�|dd�d�|�|dd�d�|�|dd�d�|�|dd�d�|�|dd�d�|�|dd�d�|�|dd�d�|�|dd�d�|�|dd�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.
        r1zhttp://g.example.com:80�10�jr6zhttp://h.example.com�11r r7zi.example.com:80�12�l�iz
j.example.com�13�mzg.example.com:100)NNzg.example.com:80)rkrlz
g.example.comzhttp://g.example.com:100zhttp://g.example.comz
h.example.com)rmr zh.example.com:80zhttp://h.example.com:80z
i.example.com)rnrozhttp://i.example.com:80)rqrrzj.example.com:80zhttp://j.example.comzhttp://j.example.com:80N)r(r
rdrerfr)rrgrhrir"r"r#�"test_password_manager_default_port�sD
z3RequestHdrsTests.test_password_manager_default_portN)rBrrCrHrTrjrsr"r"r"r#rDQs$FrDc@s(eZdZgZdejfdd�Zdd�ZdS)�
MockOpenerNcCs||||_|_|_dS)N)rS�data�timeout)rrSrurvr"r"r#�open�szMockOpener.opencGs|||_|_dS)N)�proto�args)rrxryr"r"r#rszMockOpener.error)rBrrC�
addheaders�socket�_GLOBAL_DEFAULT_TIMEOUTrwrr"r"r"r#rt�srtc@s(eZdZddd�Zd	dd�Zdd�ZdS)
�MockFileNcCsdS)Nr")r�countr"r"r#r/sz
MockFile.readcCsdS)Nr")rr~r"r"r#�readline
szMockFile.readlinecCsdS)Nr")rr"r"r#r0
szMockFile.close)N)N)rBrrCr/rr0r"r"r"r#r}s

r}c@seZdZdd�ZdS)�MockHeaderscCst|���S)N)r:�values)rrr"r"r#�
getheadersszMockHeaders.getheadersN)rBrrCr�r"r"r"r#r�sr�c@s&eZdZddd�Zdd�Zdd�ZdS)	�MockResponseNcCs.tj�||�||||f\|_|_|_|_dS)N)�io�StringIO�__init__�code�msgrFrG)rr�r�rFrurGr"r"r#r�szMockResponse.__init__cCs|jS)N)rF)rr"r"r#�infoszMockResponse.infocCs|jS)N)rG)rr"r"r#�geturlszMockResponse.geturl)N)rBrrCr�r�r�r"r"r"r#r�s
r�c@seZdZdd�Zdd�ZdS)�
MockCookieJarcCs
||_dS)N)�ach_req)rr
r"r"r#�add_cookie_header#szMockCookieJar.add_cookie_headercCs|||_|_dS)N)�ec_req�ec_r)rrr
r"r"r#�extract_cookies&szMockCookieJar.extract_cookiesN)rBrrCr�r�r"r"r"r#r�"sr�c@seZdZdd�Zdd�ZdS)�
FakeMethodcCs||_||_||_dS)N)�	meth_name�handle�action)rr�r�r�r"r"r#r�+szFakeMethod.__init__cGs|j|j|jf|��S)N)r�r�r�)rryr"r"r#�__call__0szFakeMethod.__call__N)rBrrCr�r�r"r"r"r#r�*sr�c@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�MockHTTPResponsecCs"||_||_||_||_d|_dS)N��)�fpr��statusr<r�)rr�r�r�r<r"r"r#r�5s
zMockHTTPResponse.__init__cCsdS)N�r")rr"r"r#r/<szMockHTTPResponse.readcCsiS)Nr")rr"r"r#r�?szMockHTTPResponse.infocCs|jS)N)rG)rr"r"r#r�BszMockHTTPResponse.geturlN)rBrrCr�r/r�r�r"r"r"r#r�4sr�c@sTeZdZdd�Zejfdd�Zdd�Zddd	�Zdd
d�dd
�Z	dd�Z
dd�ZdS)�
MockHTTPClasscCs(d|_g|_d|_d|_d|_i|_dS)NrF)�level�req_headersru�raise_on_endheadersZsock�_tunnel_headers)rr"r"r#r�GszMockHTTPClass.__init__cCs||_||_|S)N)�hostrv)rr�rvr"r"r#r�OszMockHTTPClass.__call__cCs
||_dS)N)r�)rr�r"r"r#�set_debuglevelTszMockHTTPClass.set_debuglevelNcCs&||_||_|r||_n
|j��dS)N)�_tunnel_hostZ_tunnel_portr��clear)rr��portrFr"r"r#�
set_tunnelWs
zMockHTTPClass.set_tunnelF)�encode_chunkedcCsP||_||_|dk	r&|j|��7_|j��|r:||_||_|jrLt��dS)N)	�method�selectorr�r�sortrur�r��OSError)rr�rG�bodyrFr�r"r"r#r
_s
zMockHTTPClass.requestcCstt�idd�S)Nr��OK)r�r})rr"r"r#�getresponselszMockHTTPClass.getresponsecCsdS)Nr")rr"r"r#r0oszMockHTTPClass.close)NN)NN)rBrrCr�r{r|r�r�r�r
r�r0r"r"r"r#r�Fs
r�c@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�MockHandleri�cCs|�|�dS)N)�_define_methods)r�methodsr"r"r#r�xszMockHandler.__init__cCsNxH|D]@}t|�dkr |\}}n
|d}}t|||j�}t|j||�qWdS)N�)�lenr�r��setattr�	__class__)rr��specrr�Zmethr"r"r#r�{s


zMockHandler._define_methodscOs�|jj�||||f�|dkr"dS|dkr.|S|dkrHtddid�}|S|dkrXtd�S|�d�r�||�d	�d
d�}yt|�}Wntk
r�YnXtddid�}|j�	d|d||di�S|d
kr�t
j	�d��ds�t�dS)Nzreturn selfzreturn responser�r�r�zreturn requestzhttp://blah/r� ��httpr�raiserEF)
�parent�calls�appendr�r�
startswith�rfind�intr'rr(r=�AssertionError)rZfn_namer�ry�kwdsZresr�r"r"r#r��s*
zMockHandler.handlecCsdS)Nr")rr"r"r#r0�szMockHandler.closecCs||_g|j_dS)N)r�r�)rr�r"r"r#�
add_parent�szMockHandler.add_parentcCst|d�sdS|j|jkS)N�
handler_orderT)�hasattrr�)r�otherr"r"r#�__lt__�s
zMockHandler.__lt__N)
rBrrCr�r�r�r�r0r�r�r"r"r"r#r�ss	r�cCsfg}d}xX|D]P}Gdd�dt�}||�}|j|7_|�|�|d}|�|�|�|�qW|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.

    rc@seZdZdS)z6add_ordered_mock_handlers.<locals>.MockHandlerSubclassN)rBrrCr"r"r"r#�MockHandlerSubclass�sr�r�)r�r�r�r��add_handler)�opener�	meth_spec�handlersr~�methsr�r7r"r"r#�add_ordered_mock_handlers�s


r�cGs"t�}x|D]}|�|�qW|S)N)rr�)Zhandler_instancesr�r7r"r"r#�build_test_opener�s
r�c@s$eZdZdd�Zdd�Zdd�ZdS)�MockHTTPHandlercCs||_||_|��dS)N)r�rF�reset)rr�rFr"r"r#r��szMockHTTPHandler.__init__cCsd|_g|_dS)Nr)�_count�requests)rr"r"r#r��szMockHTTPHandler.resetcCs�ddl}ddl}|j�|�|��|jdkrl|jd|_tjj|j	}|�
|j�}|j�
d|t�|j	||�S||_|�
d�}tdd|d|���SdS)Nrr�r�z

r�r�r�)�email�copyr�r�Zdeepcopyr�r��client�	responsesr�Zmessage_from_stringrFr�rr}rSr��get_full_url)rrSr�r�rr�r"r"r#�	http_open�s

zMockHTTPHandler.http_openN)rBrrCr�r�r�r"r"r"r#r��sr�c@seZdZddd�Zdd�ZdS)�MockHTTPSHandlerrcCstjjj||d�t�|_dS)N)�
debuglevel)r(r
�AbstractHTTPHandlerr�r��httpconn)rr�r"r"r#r��szMockHTTPSHandler.__init__cCs|�|j|�S)N)�do_openr�)rrSr"r"r#�
https_open�szMockHTTPSHandler.https_openN)r)rBrrCr�r�r"r"r"r#r��s
r�c@s$eZdZdd�Zdd�Zdd�ZdS)�MockHTTPHandlerCheckAuthcCs||_d|_dS)NF)r��has_auth_header)rr�r"r"r#r��sz!MockHTTPHandlerCheckAuth.__init__cCs
d|_dS)NF)r�)rr"r"r#r�szMockHTTPHandlerCheckAuth.resetcCs6|�d�rd|_tjj|j}t|j|t�d|���S)N�
AuthorizationTr�)	rLr�r�r�r�r�r�r}r�)rrSrr"r"r#r�s
z"MockHTTPHandlerCheckAuth.http_openN)rBrrCr�r�r�r"r"r"r#r��sr�c@seZdZdd�Zdd�ZdS)�MockPasswordManagercCs||_||_||_||_dS)N)�realmrG�userrV)rr�Zurir�rVr"r"r#resz MockPasswordManager.add_passwordcCs||_||_|j|jfS)N)�target_realm�
target_urlr�rV)rr�Zauthurir"r"r#rfsz&MockPasswordManager.find_user_passwordN)rBrrCrerfr"r"r"r#r�
sr�c@sDeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dS)�OpenerDirectorTestscCs(Gdd�dt�}|�tt�j|��dS)Nc@seZdZdS)z<OpenerDirectorTests.test_add_non_handler.<locals>.NonHandlerN)rBrrCr"r"r"r#�
NonHandlersr�)�objectr&�	TypeErrorrr�)rr�r"r"r#�test_add_non_handlersz(OpenerDirectorTests.test_add_non_handlercCs`ddlm}t�}ddgdgg}t||�|�tj���x dD]}|�||j	|d�q@WdS)Nr)r=)r�zreturn self)Z
proxy_openzreturn self)Zredirect_requestzreturn self)Zdo�proxy�redirectz://example.com/)
�urllib.errorr=rr�r�r(r
ZUnknownHandlerr&rw)rr=�or��schemer"r"r#�test_badly_named_methods"s	

z,OpenerDirectorTests.test_badly_named_methodsc
Cs�t�}dddgdgdgdgg}t||�}td�}|�|�}|�||d�|ddf|ddfg}xBt||j�D]2\}}|\}	}
}}|�|	|
f|�|�||f�qpWdS)Nr��ftp_open�http_error_302)r�zreturn selfzhttp://example.com/r�r)rr�rrwr�zipr�)
rr�r�r�rS�rr��expected�got�handlerrryr�r"r"r#�test_handled7s

z OpenerDirectorTests.test_handledcCs�t�}g}xRdgdfdgdfgD]:\}}Gdd�dt�}||�}||_|�|�|�|�q W|�d�|�|jdd|d�|�|jdd|d�dS)	N)r�zreturn selfi�r�rc@seZdZdS)zCOpenerDirectorTests.test_handler_order.<locals>.MockHandlerSubclassN)rBrrCr"r"r"r#r�Vsr�zhttp://example.com/r�)rr�r�r�r�rwrr�)rr�r�r�r�r�r7r"r"r#�test_handler_orderQs


z&OpenerDirectorTests.test_handler_ordercCsZt�}dgdgg}t||�}td�}|�tjj|j|�|�|j	|dd|fifg�dS)N)r�r�)r�zreturn selfzhttp://example.com/rr�)
rr�rr&r(rr=rwrr�)rr�r�r�rSr"r"r#�
test_raisecs
zOpenerDirectorTests.test_raisecCs�t�}dgddgdddgdgg}t||�}Gdd	�d	�}td
�}|�|�t|j�dks\t�|dd|ff|dd||�d
diffg}xJt||j�D]:\}}|\}	}
}|�|	|
f|dd��|�||d�q�WdS)N)r�z	error 302)Zhttp_error_400r�r�)r�zreturn responseZhttp_error_303Z
http_errorr�c@seZdZdd�ZdS)z4OpenerDirectorTests.test_http_error.<locals>.UnknowncSsdS)NTr")rr�r"r"r#�__eq__~sz;OpenerDirectorTests.test_http_error.<locals>.Unknown.__eq__N)rBrrCr�r"r"r"r#�Unknown}sr�zhttp://example.com/r�ri.r�)	rr�rrwr�r�r�r�r)rr�r�r�r�rSr�r�r�r�Zmethod_nameryr"r"r#�test_http_errorps$


z#OpenerDirectorTests.test_http_errorcCst�}ddgddgg}t||�}td�}|�|�|ddf|ddf|ddf|ddfg}x�t|j�D]�\}\}}}	}
|dkr�|�||f||�|�t|	�d�|�|	dt�qj|�||f||�|�t|	�d�|�|	dt�|	ddk	rj|�|	dt	�qjWdS)	N)�http_requestzreturn request)�
http_responsezreturn responsezhttp://example.com/rr�r�rr�)
rr�rrw�	enumerater�rr�ZassertIsInstancer�)rr�r�r�rSr�rpr�rryr�r"r"r#�test_processors�s(

z#OpenerDirectorTests.test_processorsN)
rBrrCr�r�r�r�r�r�rr"r"r"r#r�s
r�cCs^y|�d�Wntk
r,t�d��YnXtj�|�}tjdkrZ|�	d�rZ|dd�}|S)Nzutf-8zpath is not encodable to utf8rz///r�)
�encode�UnicodeEncodeError�unittestZSkipTestr(r
rrrr�)r*�urlpathr"r"r#�sanepathname2url�src@s^eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Zd7d8�Ze� e!j"d9kd:�d;d<��Z#d=d>�Z$d?d@�Z%dAdB�Z&dCdD�Z'dEdF�Z(dGdH�Z)dIdJ�Z*dKdL�Z+dMdN�Z,dOdP�Z-dQdR�Z.dSS)T�HandlerTestscs�Gdd�d��G�fdd�dtjj�}ddl}d}||�}t�|_�xjdd|jd	d	d
ddgd
df	dd|jdd	d
ddgd
df	dd|jdd	d
ddgd
df	dd|jdd	d
ddgd
df	dddd	d	dddgd	df	dd|jd	d	dgddf	gD]�\	}}}}}	}
}}}
t|�}d|_|�	|�}|�
|j|�|�
|j|	�|�
|j
t�|��|�
|j|�|�
|j|�|�
|jj|�|�
|jj|
�|��}|�
|�d�|
�|�
t|d�t|��q�WdS)Nc@s$eZdZdd�Zdd�Zdd�ZdS)z-HandlerTests.test_ftp.<locals>.MockFTPWrappercSs
||_dS)N)ru)rrur"r"r#r��sz6HandlerTests.test_ftp.<locals>.MockFTPWrapper.__init__cSs$|||_|_t�|j�t|j�fS)N)�filename�filetyper�r�rur�)rr	r
r"r"r#�retrfile�sz6HandlerTests.test_ftp.<locals>.MockFTPWrapper.retrfilecSsdS)Nr")rr"r"r#r0�sz3HandlerTests.test_ftp.<locals>.MockFTPWrapper.closeN)rBrrCr�rr0r"r"r"r#�MockFTPWrapper�srcs&eZdZdd�Zejf�fdd�	ZdS)z-HandlerTests.test_ftp.<locals>.NullFTPHandlercSs
||_dS)N)ru)rrur"r"r#r��sz6HandlerTests.test_ftp.<locals>.NullFTPHandler.__init__cs4|||_|_|||_|_||_�|j�|_|jS)N)r��passwdr�r��dirsru�
ftpwrapper)rr�r
r�r�rrv)rr"r#�connect_ftp�s
z9HandlerTests.test_ftp.<locals>.NullFTPHandler.connect_ftpN)rBrrCr�r{r|rr")rr"r#�NullFTPHandler�srrzrheum rhaponicumz ftp://localhost/foo/bar/baz.html�	localhostr��Ir]r^zbaz.htmlz	text/htmlz'ftp://parrot@localhost/foo/bar/baz.htmlZparrotz*ftp://%25parrot@localhost/foo/bar/baz.htmlz%parrotz,ftp://%2542parrot@localhost/foo/bar/baz.htmlz	%42parrotzftp://localhost:80/foo/bar/�P�Dzftp://localhost/baz.gif;type=a�Azbaz.gifzContent-typezContent-length)r(r
Z
FTPHandler�ftplibrtr�ZFTP_PORTrrvr�rr�r
r�r{�
gethostbynamer�rrr	r
r��getr�r�)rrrrur7rGr�r�r�r
Ztype_rr	ZmimetyperSr�rFr")rr#�test_ftp�sL
"
zHandlerTests.test_ftpc
Cs�ddl}tj��}t�}|_tj}tt	j
�|��}d}d|d|dt�
d�|fg}yt�
t���}Wntjk
r�d}YnX|r�|�d||f�x�|D]�}	t|d�}
zpz|
�|�Wd|
��X|�t|	��}z|��}|��}
|��}Wd|��Xt	�|�}|jj|jd	d
�}Wdt	�|�X|�||�|�|
dd�|�|
d
d�|�|
d|�|�||	�q�Wx�d|dddt�
d�t	��|fdt	��|fgD]T}	z@t|d�}
z|
�|�Wd|
��X|� tj!j"|jt|	��Wdt	�|�X�q�Wtj��}t�}|_x|dD]t\}	}t|	�}y|�|�Wn$tj!j"k
�rd|�#|�YnX|�$|j%|�|�|j&d�|�|j&dk|��q"WdS)Nrs
hello, world
zfile://localhost%sz	file://%szfile://%s%srr��wbT)ZusegmtzContent-typez
text/plainzContent-lengthrqz
Last-modifiedzfile://localhost:80%szfile:///file_does_not_exist.txtz)file://not-a-local-host.com//dir/file.txtzfile://%s:80%s/%sz,file://somerandomhost.ontheinternet.com%s/%s))zfile://ftp.example.com//foo.txtF)z file://ftp.example.com///foo.txtF)zfile://ftp.example.com/foo.txtF)z"file://somehost//foo/something.txtF)z#file://localhost//foo/something.txtF�ftp)'Zemail.utilsr(r
ZFileHandlerrtr�r�TESTFNrrr*r+r{rZgethostnameZgaierrorr�rw�writer0Z	file_openrr/r�r��statZutilsZ
formatdate�st_mtime�remover�getcwdr&rr=rO�assertIsrS�type)rr�r7r�rrZtowrite�urlsZ	localaddrrGr1r�rurFZrespurlZstatsZmodifiedrrSr"r"r#�	test_file�s|











zHandlerTests.test_filecCsrtj��}t�}|_d}x�dD]�\}}t||ddi�}d|_|�dd�t�}|�	||�}|j
|j|j|j
|j|jdkdf|��}	|	j|	j|�|�
�|�|�|jd	�|�|jd
�|�|j|�|�|jd�|�|jdd
dg�|�|j|�q Wd|_|�tjj|j	||�tdd�}|�t|j|�dg|_�x$dD�]}td|�}t ddid�}|�|�}
|dk�r�|�!d|j"�|�!d|j"�n$|�|j"dd�|�|j"dd�|�|j"dd	�|�|j"dd�|�dd�|�dd�|�dd�|�dd�|�|�}
|�|j"dd�|�|j"dd�|�|j"dd�|�|j"dd��qNWdS)Nzhttp://example.com/))�GETN)�POSTsblah�Foor^�Spam�eggsr�r�zexample.comrr%)Z
Connectionr0)r)r^)r*r+TZbadpost)�Nr�zContent-lengthzContent-type�0z!application/x-www-form-urlencoded�Hostr]rI)#r(r
r�rtr�rrvrRr�r�r/rr�r�r�r�r�__contains__rr�r�r�r�r�rur�r&rr=r��do_request_rzr��assertNotIn�unredirected_hdrs)rr7r�rGr�rurSr�r��hdrs�newreqr"r"r#�	test_httpZsh






zHandlerTests.test_httpc		Cs�tj��}t�}|_tjddd�}|j}|��|�	t
j|�t|d��B}t
d|i�}|�|�}|�d�}|�|d�|�|�d��WdQRXt|d��F}t
d|d	d
i�}|�|�}|�t|�d��d
�|�|�d��WdQRXdS)Nzw+bF)�mode�delete�rbzhttp://example.com/zTransfer-encoding�chunkedzContent-lengthzContent-Length�)r(r
r�rtr��tempfileZNamedTemporaryFilerr0�
addCleanupr�unlinkrwrr0rQrrOrLr�)	rr7r��file_objZ	file_pathr1rSr4�ter"r"r#�test_http_body_file�s"



z HandlerTests.test_http_body_filecCs�tj��}t�}|_t��}td|i�}|�|�}|�	|�
d�d�|�|�d��ddi}td||�}|�|�}|�	t
|�
d��d�|�|�d��|��dS)Nzhttp://example.com/zTransfer-encodingr9zContent-lengthzContent-Lengthr:)r(r
r�rtr�r��BytesIOrr0rrQrOrLr�r0)rr7r�r>rSr4rFr"r"r#�test_http_body_fileobj�s


z#HandlerTests.test_http_body_fileobjc
Cs�tj��}t�}|_tjddg}x�iddifD]x}tj|tj	d��^}t
d|j|�}|�|�}|s�|�
|�d�d�|�
|�d�d	�n|�
t|�d��d�WdQRXq0WdS)
Nz-c�passzContent-Lengthr:)�stdoutzhttp://example.com/zContent-lengthzTransfer-encodingr9)r(r
r�rtr��sys�
executable�
subprocess�Popen�PIPErrDr0rrQr�)rr7r��cmdrF�procrSr4r"r"r#�test_http_body_pipe�s

z HandlerTests.test_http_body_pipecCs�tj��}t�}|_dd�}xliddifD]\}td|�|�}|�|�}|sr|�|�d�d�|�|�d�d�q,|�t	|�d��d�q,WdS)	Ncss
dVdS)Nsoner"r"r"r"r#�
iterable_body�sz;HandlerTests.test_http_body_iterable.<locals>.iterable_bodyzContent-Length�zhttp://example.com/zContent-lengthzTransfer-encodingr9)
r(r
r�rtr�rr0rrQr�)rr7r�rMrFrSr4r"r"r#�test_http_body_iterable�s

z$HandlerTests.test_http_body_iterablecCsHtj��}t�|_|�tdd��}|�|�d�d�|�	|�
d��dS)Nzhttp://example.com/r"zTransfer-encodingr9zContent-length)r(r
r�rtr�r0rrrQrOrL)rr7rSr"r"r#�test_http_body_empty_seq�s

z%HandlerTests.test_http_body_empty_seqcCsptj��}t�}|_t�dddddg�}x@iddifD]0}td||�}|�|�}|�t	|�
d	��d�q8WdS)
Nrr�r���zContent-Length�zhttp://example.com/zContent-length)r(r
r�rtr��arrayrr0rr�rQ)rr7r�Ziterable_arrayrFrSr4r"r"r#�test_http_body_array�s

z!HandlerTests.test_http_body_arraycCs6t�}tdd�}|�|�|�d�|�|jd�dS)Nr�)r�zhttps://www.example.com)rr�r�rwrZ_debuglevel)rr�r7r"r"r#�test_http_handler_debuglevels



z)HandlerTests.test_http_handler_debuglevelcCs�tj��}t�|_d}ddddg}xZ|D]R}t||�}|�|�}|�|jdd�|�	dd�|�|�}|�|jdd�q(WdS)	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.zexample.comzsomeproxy:3128)
r(r
r�rtr�rr0rr2�	set_proxy)rr7ruZds_urlsZds_urlZds_reqZ	np_ds_reqZp_ds_reqr"r"r#�test_http_doubleslashs




z"HandlerTests.test_http_doubleslashcCsndddg}td�}xV|D]N}||_t|�}|�|��|�|�|jpFd|j�|�t|���j|j�qWdS)Nzhttp://example.com?foo=bar#bazz)http://example.com?foo=bar&spam=eggs#bashzhttp://example.comr�)r�full_urlrrr��fragmentZquery)rr%r�rGZparsedr"r"r#�test_full_url_setter-s
z!HandlerTests.test_full_url_settercCs6td�}|`|�|j�|�|j�|�|jd�dS)Nzhttp://www.example.comr�)rrYrPrZrr�)rr�r"r"r#�test_full_url_deleterDs
z"HandlerTests.test_full_url_deletercCsztj��}t�|_d}t|�}|�|�}|�|jd�|�|j	d�d}t|�}|�|�}|�|jd�|�|j	d�dS)Nzhttp://www.python.org?getspamzwww.python.orgz	/?getspamzhttp://www.python.orgr�)
r(r
r�rtr�rr0rr�r�)rr7Z	weird_urlrSr4Zurl_without_pathr"r"r#�test_fixpath_in_weirdurlsKs


z&HandlerTests.test_fixpath_in_weirdurlscCstj��}t�}|_d}t|�}tddid|�}|�||�}|�||�|�	t
|d��tddid|�}|�||�}|�||�|�	t
|d��tdd	id|�}|�||�}|�||�|�	t
|d��td
did|�}|�|�||��|�|j
d�|�|j||d
dif�dS)
Nzhttp://example.com/r�r�r�rx��ZAccepted��zPartial contenti�zBad gatewayr�)r(r
ZHTTPErrorProcessorrtr�rr�rr#rOr�rPrrxry)rr7r�rGrSr��newrr"r"r#�test_errors^s(
zHandlerTests.test_errorscCs�t�}tj�|�}t�|_td�}tddid�}|�|�}|�	|j
|�|�	|j
|�|�|jd�|�
|j�|�||�}|�	|j|�|�	|j|�|�	||�dS)Nzhttp://example.com/r�r�r�zexample.com)r�r(r
�HTTPCookieProcessorrtr�rr�r�r#r�r�origin_req_hostrOZunverifiablerr�r�)r�cjr7rSr�r4r`r"r"r#�test_cookiesws
zHandlerTests.test_cookiesc
Cs�d}d}tj��}t�}|_�xndD�]d}�x\dD�]R}t|d|�}t||�}tj|_	|�
dd�|dk	r�|�
dtt|���|�
d	d
�y||t�|dtd|i��Wn.tjjk
r�|�|d
�|�|�YnX|�|j��|�y|�|j��d�Wn$tk
�r(|�|jj�YnXdd�|jjD�}	|�d|	�|�d|	�|�|jjdd�|�d	|jj�|�d	|jj�q4Wq&Wt|�}tj|_	|fdd�}
t|dd�}d}tj|_	y x|
||d�|d}�q�WWn,tjjk
�r|�|tjjj�YnXt|dd�}d}tj|_	y$x|
||d|�|d}�q.WWn,tjjk
�rz|�|tjjj�YnXdS)Nzhttp://example.com/a.htmlzhttp://example.com/b.html)i-i.i/i3)Nz
blah
blah
z
http_error_%sZNonsensezviking=withholdzContent-Lengthr*rb�Blah�locationi3r'cSsg|]}|���qSr")�lower)�.0�xr"r"r#�
<listcomp>�sz.HandlerTests.test_redirect.<locals>.<listcomp>zcontent-lengthzcontent-typec	Ss |�|t�ddtd|i��dS)Ni.rfrg)r�r}r�)r7rSrGr"r"r#r��sz,HandlerTests.test_redirect.<locals>.redirectzexample.com)rcrzhttp://example.com/r�zhttp://example.com/%d)r(r
�HTTPRedirectHandlerrtr��getattrrr{r|rvrMr?r�rRr}r�r�	HTTPErrorr�assertIsNotNonerSr��
get_method�AttributeErrorrOrurFr1r2Zmax_repeatsZmax_redirections)r�from_urlZto_urlr7r�r�rur�rSrFr�r~r"r"r#�
test_redirect�sj

zHandlerTests.test_redirectcCs�d}dddg}dddg}d}tj��}t�}|_t|�}tj|_x>|D]6}|d	|}	|�	tj
j|j|t
�d
dtd|	i��qHWxF|D]>}|d	|}
|�|t
�d
d
td|
i��|�|j��|
�q�WdS)Nzhttp://example.com/a.htmlr��httpsr�fileZimapZldapzexample.com/b.htmlz://i.zSecurity LoopholergzThat's fine)r(r
rlrtr�rr{r|rvr&rrnr�r}r�rrSr�)rrrZ
valid_schemesZinvalid_schemesZschemeless_urlr7r�rSr�Zinvalid_url�	valid_urlr"r"r#�test_invalid_redirect�s$





z"HandlerTests.test_invalid_redirectc	Csnd}d}tj��}t�}|_t|�}tj|_tj	�
||�}|�|t�ddt
d|i��|�|j��|�dS)Nzhttp://example.com/a.htmlz/b.htmli.zThat's finerg)r(r
rlrtr�rr{r|rvrZurljoinr�r}r�rrSr�)rrrZrelative_urlr7r�rSrvr"r"r#�test_relative_redirect�s
z#HandlerTests.test_relative_redirectc	Cs�ddlm}ddlm}|�}||dd�tdd�}tj��}tj��}tj�	|�}t
||||�}|�d�|�|j
�d��dS)	Nr)�	CookieJar)�interact_netscapezhttp://www.example.com/z	spam=eggsi.z%Location: http://www.cracker.com/

ZCookie)Zhttp.cookiejarryZtest.test_http_cookiejarrzr�r(r
�HTTPDefaultErrorHandlerrlrbr�rwrOrSrL)	rryrzrd�hh�hdeh�hrhZcpr�r"r"r#�test_cookie_redirect�s



z!HandlerTests.test_cookie_redirectcCsTd}tdd|�}tj��}tj��}t|||�}|�d�}|�|��|�	��dS)Nz(http://www.example.com/index.html#OK

i.z
Location: zhttp://www.example.com)
r�r(r
r{rlr�rwrr��strip)rZredirected_urlr|r}r~r�r�r"r"r#�test_redirect_fragments


z#HandlerTests.test_redirect_fragmentcsntjj�d}t�|�tj_��ttjd��td�����fdd�}|tjj_t	j�
d�}��|��d�dS)Ns(HTTP/1.1 302 Found
Location: ?query

�HTTPConnection)z/pathz/path?querycs2��|t����j|||f|�|�d|j_dS)NsHTTP/1.1 200 OK

Hello!)r�nextr
r�Zfakedata)�connr�rG�pos�kw)�
real_classrr%r"r#r
sz3HandlerTests.test_redirect_no_path.<locals>.requestzhttp://python.org/pathzhttp://python.org/path?query)
r�r�r�r�fakehttpr<r��iterr
r(r)rr�)rZ	response1r
r�r")r�rr%r#�test_redirect_no_path
s
z"HandlerTests.test_redirect_no_pathc

Cs�Gdd�dtjj�}|�}tj�|�}d}xp|D]h\}}|�t|���Lt�d|d�|_|�	d�}d|d}|j
}	|�|	�|�t|	��WdQRXq0WdS)	Nc@seZdZdd�ZdS)z4HandlerTests.test_redirect_encoding.<locals>.HandlercSs(|�|j|�}|jj|_t�d�|_|S)Ns)HTTP/1.1 200 OK
Content-Length: 3

123)r��
connection�buf�last_bufrr�)rrS�resultr"r"r#r�!s

z>HandlerTests.test_redirect_encoding.<locals>.Handler.http_openN)rBrrCr�r"r"r"r#�Handler sr�))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

zhttp://example.com/sGET � )
r(r
�HTTPHandler�build_opener�subTest�reprrr�r�rwr�rKr�)
rr�r�r�r8rgr�rr�r
r"r"r#�test_redirect_encodings
z#HandlerTests.test_redirect_encodingcCs�t�}tj�tdd��}|�|�dgg}t||�}td�}|�|j	d�|�
|�|�|j	d�|�|ddfgdd	�|jD��dS)
Nzproxy.example.com:3128)r�)r�zreturn responsezhttp://acme.example.com/zacme.example.comrr�cSsg|]}|dd��qS)rr�r")ri�tupr"r"r#rkOsz+HandlerTests.test_proxy.<locals>.<listcomp>)rr(r
�ProxyHandler�dictr�r�rrr�rwr�)rr��phr�r�rSr"r"r#�
test_proxy@s


zHandlerTests.test_proxycCs�dtjd<t�}tj�tdd��}|�|�td�}|�	|j
d�|�|�|�	|j
d�td�}|�	|j
d�|�|�|�	|j
d�tjd=dS)	Nz
python.org�no_proxyzproxy.example.com)r�zhttp://www.perl.org/zwww.perl.orgzhttp://www.python.orgzwww.python.org)r�environrr(r
r�r�r�rrr�rw)rr�r�rSr"r"r#�test_proxy_no_proxyQs



z HandlerTests.test_proxy_no_proxycCsfdtjd<t�}tj�tdd��}|�|�td�}|�	|j
d�|�|�|�	|j
d�tjd=dS)N�*r�zproxy.example.com)r�zhttp://www.python.orgzwww.python.org)rr�rr(r
r�r�r�rrr�rw)rr�r�rSr"r"r#�test_proxy_no_proxy_all`s


z$HandlerTests.test_proxy_no_proxy_allcCs�t�}tj�tdd��}|�|�dgg}t||�}td�}|�|j	d�|�
|�|�|j	d�|�|ddfgdd	�|jD��dS)
Nzproxy.example.com:3128)rt)r�zreturn responsezhttps://www.example.com/zwww.example.comrr�cSsg|]}|dd��qS)rr�r")rir�r"r"r#rkysz1HandlerTests.test_proxy_https.<locals>.<listcomp>)rr(r
r�r�r�r�rrr�rwr�)rr�r�r�r�rSr"r"r#�test_proxy_httpsks


zHandlerTests.test_proxy_httpscCs�t�}tj�tdd��}|�|�t�}|�|�td�}|�dd�|�dd�|�	|j
d�|�|j�|�
|�|�d	|jj�|�d
|jj�|�|j�|�	|j
d�|�	|�d�d�dS)Nzproxy.example.com:3128)rtzhttps://www.example.com/zProxy-Authorization�FooBarz
User-Agent�Grailzwww.example.com)zProxy-Authorizationr�)z
User-Agentr�zProxy-authorization)rr(r
r�r�r�r�rrMrr�rPr�rwr1r�r�r>rorQ)rr�r�Z
https_handlerrSr"r"r#�$test_proxy_https_proxy_authorization{s$




z1HandlerTests.test_proxy_https_proxy_authorization�darwinzonly relevant for OSXcCszddddddgd�}x"dD]}|�t||�d	|�qWx"d
D]}|�t||�d|�q>Wdgd�}|�td
|��dS)NFzfoo.barz	*.bar.comz	127.0.0.1z10.10z10.0/16)Zexclude_simple�
exceptions)zfoo.barzwww.bar.comz	127.0.0.1z	10.10.0.1z10.0.0.1z expected bypass of %s to be True)zabc.foo.barzbar.comz	127.0.0.2z	10.11.0.1Znotinbypassz!expected bypass of %s to be FalseT�test)rKr	rO)rZbypassr�r"r"r#�test_osx_proxy_bypass�s



z"HandlerTests.test_osx_proxy_bypasscCsz|j||d��bt�}t�}tj�|�}d�|�d}td|�}|�|�|�|�|�	||d|||dd�WdQRXdS)N)r�rFz
z

i�r�z!http://acme.example.com/protected)
r�rr�r(r
r�joinr�r��_test_basic_auth)rrFr�r��password_manager�auth_handlerr��http_handlerr"r"r#�check_basic_auth�s



zHandlerTests.check_basic_authc

Cs d}d}d|�d�}d|�d�}d}d|�d�}x~d|�d�d|�d�d|�d	�|�d
|��|�d
|��|�d
|��|�d
|��|�d
|��fD]}d|��g}|�||�q�Wt�dtf��d
|��g}|�||�WdQRXx8||g||g||gfD]}	dd�|	D�}|�||�q�WdS)Nzrealm2@example.comz
Basic realm="�"zOtherscheme xxxzDigest realm="zn", qop="auth, auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41"z
Basic realm='�'z", charset="UTF-8"z, zWWW-Authenticate: zBasic Auth Realm was unquotedzWWW-Authenticate: Basic realm=cSsg|]}d|���qS)zWWW-Authenticate: r")riZ	challenger"r"r#rk�sz0HandlerTests.test_basic_auth.<locals>.<listcomp>)r�rZcheck_warnings�UserWarning)
rr�Zrealm2�basicZbasic2Zother_no_realm�digestZ	realm_strrFZ
challengesr"r"r#�test_basic_auth�s8



zHandlerTests.test_basic_authc
Csvt�}tj�tdd��}|�|�t�}tj�|�}d}tdd|�}|�|�|�|�|�	||d|||dd�dS)Nzproxy.example.com:3128)r�z
ACME Networksi�z(Proxy-Authenticate: Basic realm="%s"

zProxy-authorizationz&http://acme.example.com:3128/protected)
rr(r
r�r�r�r�ZProxyBasicAuthHandlerr�r�)rr�r�r�r�r�r�r"r"r#�test_proxy_basic_auth�s



z"HandlerTests.test_proxy_basic_authc

Cs�Gdd�dt�}Gdd�dtjj�}Gdd�dtjj�}|�}t�}||�}||�}d}tdd	|�}	|�|�|�|�|�|	�|�||d
||	|dd�|�	|j
dd
gd�dS)Nc@seZdZdd�Zdd�ZdS)zQHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirectorcSst�|�g|_dS)N)rr��recorded)rr"r"r#r��s
zZHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.__init__cSs|j�|�dS)N)r�r�)rr�r"r"r#�recordszXHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.recordN)rBrrCr�r�r"r"r"r#�RecordingOpenerDirector�sr�c@seZdZdd�ZdS)zOHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandlerc_s&|j�d�tjjj|f|�|�dS)Nr�)r�r�r(r
�HTTPDigestAuthHandler�http_error_401)rryr�r"r"r#r�sz^HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandler.http_error_401N)rBrrCr�r"r"r"r#�TestDigestAuthHandlersr�c@seZdZdd�ZdS)zNHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandlerc_s&|j�d�tjjj|f|�|�dS)Nr�)r�r�r(r
rr�)rryr�r"r"r#r�sz]HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandler.http_error_401N)rBrrCr�r"r"r"r#�TestBasicAuthHandlersr�z
ACME Networksi�z&WWW-Authenticate: Basic realm="%s"

r�z!http://acme.example.com/protectedr�r�r�)rr(r
r�rr�r�r�r�rr�)
rr�r�r�r�r�Zdigest_handlerZ
basic_handlerr�r�r"r"r#�#test_basic_and_digest_auth_handlers�s$	



z0HandlerTests.test_basic_and_digest_auth_handlerscCsDt�}tj�d�}tdd�}|�|�|�|�|�t|jd�dS)Ni�zWWW-Authenticate: Kerberos

zhttp://www.example.com)	rr(r
r�r�r�r&r'rw)rr�Zdigest_auth_handlerr�r"r"r#�$test_unsupported_auth_digest_handler&s

z1HandlerTests.test_unsupported_auth_digest_handlercCsDt�}tj�d�}tdd�}|�|�|�|�|�t|jd�dS)Ni�zWWW-Authenticate: NTLM

zhttp://www.example.com)	rr(r
rr�r�r&r'rw)rr�Zbasic_auth_handlerr�r"r"r#�#test_unsupported_auth_basic_handler0s

z0HandlerTests.test_unsupported_auth_basic_handlerc	CsHddl}	d\}
}|�|||
|�|�||j�|�||j�|�|
|j�|�||j�|�|�|�|j|�|�|j	|�|�t
|j�d�|�|jd�
|��td|
|fd�}d|	�|�����}
|�|jd�|�|
�|�|jdj||
�d|_|_|��|�|�|�t
|j�d�|�|jd�
|��dS)Nr)�wile�coyoter�z%s:%s�asciizBasic r�)�base64rerr�rGr�rVrwr�r�r�r�rOrL�bytesZencodebytesr��decoderQr2r�)rr�r�Zauth_headerr�r�r��request_urlZ
protected_urlr�r�rVZuserpassZauth_hdr_valuer"r"r#r�:s0

zHandlerTests._test_basic_authc	Cs~d\}}d}td�}t�}t|�}|jd|||dd�|�|�}|�|�t�}|�|�|�|�|�|�|�|j	�dS)N)r�r�z!http://acme.example.com/protectedr�T)�is_authenticated)
r�rrrer�rKrr�rwr�)	rr�rVr�r��pwd_manager�auth_prior_handler�is_authr�r"r"r#�test_basic_prior_auth_auto_send_s




z,HandlerTests.test_basic_prior_auth_auto_sendc
Cs�d\}}d}d}t�}t|�}|�||||�|�|�}|�|�t�}|�|�tddd�}	|�|	�|�|�|�|�}|�	|�t
d�}	|�|	j�t�}|�|�|�|	�|�|�|�	|	j�dS)N)r�r�z!http://acme.example.com/protectedZACMEi�z&WWW-Authenticate: Basic realm="%s"

r�)rrrer�rOrr�r�rwrKr�r�)
rr�rVr�r�r�r�r�r�r�r"r"r#�.test_basic_prior_auth_send_after_first_successys.









z;HandlerTests.test_basic_prior_auth_send_after_first_successc
Cs�xzdD]r\}}d�|�}t�|��|�}tj��}td�}d|_|�	||��}|�
�WdQRX|�|jj
d�|��qWdS)z=Test the connection is cleaned up when the response is closed))zConnection: closesdata)zTransfer-Encoding: chunkeds4
data
0

)zContent-Length: 4sdatazHTTP/1.1 200 OK
{}

z
http://dummy/NzConnection not closed with {!r})�formatrr�rr(r
r�rrvr�r/rK�fakesock�closed)rZtransferru�headerr�r�rSZrespr"r"r#�test_http_closed�s


zHandlerTests.test_http_closedc	Cs\t�d�}tj��}td�}d|_|�tj	j
��|�||�WdQRX|�|j
jd�dS)z;Test the connection is cleaned up after an invalid responser,z
http://dummy/NzConnection not closed)rr�r(r
r�rrvr&r�r�Z
BadStatusLiner�rKr�r�)rr�r�rSr"r"r#�test_invalid_closed�s

z HandlerTests.test_invalid_closedN)/rBrrCrr&r5r@rBrLrOrPrUrVrXr[r\r]rarersrwrxrr�r�r�r�r�r�r�r�r�
skipUnlessrE�platformr�r�r�r�r�r�r�r�r�r�r�r�r"r"r"r#r�sP@\B
J
	#-2

%(rc@sNeZdZdd�Zdd�Ze�e�d�d�dd��Z	d	d
�Z
dd�Zd
d�ZdS)�	MiscTestscs"|�t�fdd�|jD���dS)Nc3s|]}|j�kVqdS)N)r�)rir7)�
handler_classr"r#�	<genexpr>�sz/MiscTests.opener_has_handler.<locals>.<genexpr>)rK�anyr�)rr�r�r")r�r#�opener_has_handler�szMiscTests.opener_has_handlercCs(Gdd�dtjj�}Gdd�dtjj�}Gdd�dtjj�}tjj}|||�}|�||�|�||�|||��}|�||�|�||�||�}|�||�|�}|�|tjj�|tjj�}|�|tjj�|tj���}|�|tjj�Gdd�dtjj�}|||�}|�||�|�||�dS)	Nc@seZdZdS)z2MiscTests.test_build_opener.<locals>.MyHTTPHandlerN)rBrrCr"r"r"r#�
MyHTTPHandler�sr�c@seZdZdd�ZdS)z/MiscTests.test_build_opener.<locals>.FooHandlercSsdS)Nr")rr"r"r#�foo_open�sz8MiscTests.test_build_opener.<locals>.FooHandler.foo_openN)rBrrCr�r"r"r"r#�
FooHandler�sr�c@seZdZdd�ZdS)z/MiscTests.test_build_opener.<locals>.BarHandlercSsdS)Nr")rr"r"r#�bar_open�sz8MiscTests.test_build_opener.<locals>.BarHandler.bar_openN)rBrrCr�r"r"r"r#�
BarHandler�sr�c@seZdZdS)z7MiscTests.test_build_opener.<locals>.MyOtherHTTPHandlerN)rBrrCr"r"r"r#�MyOtherHTTPHandler�sr�)r(r
r��BaseHandlerr�r�)rr�r�r�r�r�r�r"r"r#�test_build_opener�s,

zMiscTests.test_build_openerZnetworkztest requires network accessc	Cs�t�d���tj��}tj�d�}|�d|j�|�|d�	d��|�d|j�|�d|�
d��|�|d�	d��|�d|j�|�d|�
d��WdQRXdS)	Nzhttp://www.example.com/rczus-ascii�1zContent-lengthZ
1234567890s
1234567890rk)rZtransient_internetr(r
r�rrrurwrrQ)rr�r
r"r"r#�test_issue16464�s
zMiscTests.test_issue16464cCs�d}d}}}d}tj�|||||�}|�t|d��|�|jd�|�t|d��|�|jd�d|j|j	f}|�t
|�|�d|j|j	f}|�t|�|�dS)z�
        Issue 13211 reveals that HTTPError didn't implement the URLError
        interface even though HTTPError is a subclass of URLError.
        zsomething bad happenedNzContent-Length: 42r<rFzHTTP Error %s: %sz<HTTPError %s: %r>)r(rrnrKr�rr<rFr�r�r?r�)rr�rGr�r�r3r@Zexpected_errmsgr"r"r#�test_HTTPError_interface�sz"MiscTests.test_HTTPError_interfacecCsRddddddddddd	d
g}x |D]\}}|�t|�|�q"W|�ttd�fdS)N)zproxy.example.com)NNNzproxy.example.com)zproxy.example.com:3128)NNNzproxy.example.com:3128)zjoe:password@proxy.example.com)NrUrVzproxy.example.com)z#joe:password@proxy.example.com:3128)NrUrVzproxy.example.com:3128)zhttp://proxy.example.com/)r�NNzproxy.example.com)zhttp://proxy.example.com:3128/)r�NNzproxy.example.com:3128)z&http://joe:password@proxy.example.com/)r�rUrVzproxy.example.com)z*http://joe:password@proxy.example.com:3128)r�rUrVzproxy.example.com:3128)z1ftp://joe:password@proxy.example.com/rubbish:3128)rrUrVzproxy.example.com)z%http://joe:password@proxy.example.com)r�rUrVzproxy.example.comzfile:/ftp.example.com)rrr&r')rZparse_proxy_test_casesZtcr�r"r"r#�test_parse_proxyszMiscTests.test_parse_proxyc	Cs<t�}|�t��}|�d�WdQRX|�t|j�d�dS)NZinvalidz5Unsupported digest authentication algorithm 'invalid')r
r&r'Zget_algorithm_implsrr?Z	exception)rr��excr"r"r#�test_unsupported_algorithm6sz$MiscTests.test_unsupported_algorithmN)
rBrrCr�r�rr�rZis_resource_enabledr�r�r�r�r"r"r"r#r��s,%r�c@s�eZdZGdd�de�Zdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�ZdS) �RequestTestsc@seZdZdZdS)zRequestTests.PutRequest�PUTN)rBrrCr�r"r"r"r#�
PutRequestAsr�cCsLtd�|_tddddid�|_tddd�|_|�d�|_|jddd�|_dS)	Nzhttp://www.python.org/~jeremy/ruzX-Testr�)rF�HEAD)r�r()rr�post�headr��put�
force_post)rr"r"r#�setUpDs
zRequestTests.setUpcCs^|�d|j���|�d|j���|�d|j���|�d|j���|�d|j���dS)Nr(r'r�r�)rr�rprr�r�r�)rr"r"r#�test_methodNs
zRequestTests.test_methodcCsL|�|jj�|�d|j���d|j_|�|jj�|�d|j���dS)Nr'rbr()rOrrurrprK)rr"r"r#�	test_dataUs
zRequestTests.test_datacCsN|�d|jj�|j�dd�|�d|jjd�d|j_|�d|jj�dS)NzContent-length�*rb)r1rr2rRrru)rr"r"r#�.test_setting_data_should_remove_content_length_s
z;RequestTests.test_setting_data_should_remove_content_lengthcCsT|�d|jj�d|j_|j�dd�|�d|jjd�|j`|�d|jj�dS)NzContent-lengthr]rQ)r1rr2rurRr)rr"r"r#�/test_deleting_data_should_remove_content_lengthgsz<RequestTests.test_deleting_data_should_remove_content_lengthcCs|�d|j���dS)Nzhttp://www.python.org/~jeremy/)rrr�)rr"r"r#�test_get_full_urloszRequestTests.test_get_full_urlcCs*|�d|jj�td�}|�d|j�dS)Nz	/~jeremy/zhttp://www.python.org/r%)rrr�r)rrSr"r"r#�
test_selectorsszRequestTests.test_selectorcCs|�d|jj�dS)Nr�)rrr$)rr"r"r#�
test_get_typexszRequestTests.test_get_typecCs|�d|jj�dS)Nzwww.python.org)rrr�)rr"r"r#�
test_get_host{szRequestTests.test_get_hostcCstd�}|�d|j�dS)Nzhttp://www.%70ython.org/zwww.python.org)rrr�)rrSr"r"r#�test_get_host_unquote~sz"RequestTests.test_get_host_unquotecCsR|�|j���|j�dd�|�|j���|�d|jj�|�d|jj�dS)Nzwww.perl.orgr�zwww.python.org)rOrZ	has_proxyrWrKrrcr�)rr"r"r#r��s
zRequestTests.test_proxycCstd�}|�d|j�dS)Nz<URL:http://www.python.org>zwww.python.org)rrr�)rrSr"r"r#�test_wrapped_url�szRequestTests.test_wrapped_urlcCsLtd�}|�d|j�td�}|�d|j�d}t|�}|�|��|�dS)Nz-http://www.python.org/?qs=query#fragment=truez
/?qs=queryzhttp://www.python.org/#fun=truer%z.http://docs.python.org/library/urllib2.html#OK)rrr�r�)rrSrGr"r"r#�test_url_fragment�szRequestTests.test_url_fragmentcCs6dddg}x&|D]}t|�}|�|��|j�qWdS)Nzhttp://docs.python.orgz.http://docs.python.org/library/urllib2.html#OKz-http://www.python.org/?qs=query#fragment=true)rrr�rY)rr%rGrSr"r"r#�test_url_fullurl_get_full_url�s
z*RequestTests.test_url_fullurl_get_full_urlN)rBrrCrr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r"r"r"r#r�@s

r��__main__)8rr�rrrr�r{rTrEr;rGZurllib.requestr(rrrrrr	r
Zurllib.parserr�Zhttp.clientr�ZTestCaserrDrtr}r�r�r�r�r�r��IOBaser�r�r�r�r�r
r�r�r�r�r�r�r�rrr�r�rB�mainr"r"r"r#�<module>sb$3,

-6"
	a