Your IP : 216.73.216.213


Current Path : /opt/alt/python39/lib64/python3.9/test/__pycache__/
Upload File :
Current File : //opt/alt/python39/lib64/python3.9/test/__pycache__/test_urllib2.cpython-39.opt-1.pyc

a

XC?h�:�@sddlZddlmZddlmZddlmZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Zddl
mZmZmZmZmZmZmZmZddlmZddlZddlZGdd�dej�ZGd	d
�d
ej�ZGdd�d�ZGd
d�d�Z Gdd�de!�Z"Gdd�dej#�Z$Gdd�d�Z%Gdd�d�Z&Gdd�dej'�Z(Gdd�d�Z)Gdd�d�Z*dd�Z+dd �Z,Gd!d"�d"ej-j.�Z/Gd#d$�d$ej-j0�Z1Gd%d&�d&ej-j.�Z2Gd'd(�d(�Z3Gd)d*�d*ej�Z4d+d,�Z5Gd-d.�d.ej�Z6Gd/d0�d0ej�Z7Gd1d2�d2ej�Z8e9d3k�re�:�dS)4�N)�support)�
socket_helper)�test_urllib)�Request�OpenerDirector�HTTPBasicAuthHandler�HTTPPasswordMgrWithPriorAuth�_parse_proxy�_proxy_bypass_winreg_override�_proxy_bypass_macosx_sysconf�AbstractDigestAuthHandler)�urlparsec@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�TrivialTestsc
Cs�dD]�}i}td||�|d=|dkrdtjdkrd|�d�|�d�}}|�|jd�|�|jd�|��D](\}}|�|jd	|d
|||jf�qlqdS)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$�6/opt/alt/python39/lib64/python3.9/test/test_urllib2.py�test___all__"s
��zTrivialTests.test___all__cCs�|�tjj�|�ttjjd�tj�	tjj
��tjd�}tj
dkrNd|}nd|}tj�|��}|��Wd�n1s�0YdS)Nz	bogus url�/rz
file:///%s�	file://%s)�
addCleanup�urllibr�
urlcleanup�assertRaises�
ValueError�urlopenr�path�abspath�__file__�replace�sepr�read)rZfnameZfile_url�fr$r$r%�test_trivial1s

zTrivialTests.test_trivialcCsRdgd�fdddgfdgd�fdgd	�fg}|D]\}}|�tj�|�|�q0dS)
Nza,b,c)�a�b�czpath"o,l"og"i"cal, examplezpath"o,l"og"i"calZexampleza, b, "c", "d", "e,f", g, h)r7r8z"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*rZparse_http_list)r�tests�string�listr$r$r%�test_parse_http_listDs

���z!TrivialTests.test_parse_http_listcCs"tj�d�}|�|jt|��dS)N�reason)r*r�URLError�assertInr@�str)r�errr$r$r%�test_URLError_reasonstrOsz$TrivialTests.test_URLError_reasonstrN)�__name__r�__qualname__r&r6r?rEr$r$r$r%r src@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.

        �http://example.com�	Spam-eggs�blah��headersz	spam-EggSN)rrrM)r�urlr$r$r%�test_request_headers_dictVs
������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
        rIrJrKrL)rJrKzFoo-Bar�baz)zFoo-barrPz	Not-there�defaultzUnredirected-spamZEggsN)r�
assertTrue�
has_headerrZheader_items�
add_header�sorted�assertFalse�assertIsNone�
get_headerZ
remove_header�add_unredirected_header)rrN�reqr$r$r%�test_request_headers_methodsks"�

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	ddd�|�|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)dd*d+�|d)dd,d-�|�|d)d�d.�|d/dd0d/�|�|d/d�d1�|�|d/d2�d"�|�|dd3�d"�|�|dd4�d5�|�|dd�d5�|�|dd
�d6�|�|dd
�d7�dS)8Nz
Some Realm�http://example.com/�joe�passwordzhttp://example.com/ni�nizhttp://c.example.com:3128�3r9z
d.example.com�4�dze.example.com:3128�5�e�example.com)r]r^rIzhttp://example.com/spamzhttp://example.com/spam/spamzhttp://example.com/foo�foozhttp://example.com/bar�bar�ninizhttp://example.com/foo/barZfoobarZnibar)rfr_)rgrhzhttp://example.com/foo/zhttp://example.com/foo/bazzhttp://example.com/foobar�NNzhttp://example.com/baz/rP�nininizhttp://example.com/baz)rPrjzhttp://example.com/baz/barr8�firstrK�second�spam)rlrmr7�1)rnr7zhttp://a.example.com/z
c.example.comzc.example.com:3128)r`r9)rarb)rcrd�r*rZHTTPPasswordMgr�add_password�find_user_passwordr�rZmgr�addZfind_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.
        r5zhttp://g.example.com:80�10�jr:zhttp://h.example.com�11r"r;zi.example.com:80�12�l�iz
j.example.com�13�mzg.example.com:100rizg.example.com:80)rurvz
g.example.comzhttp://g.example.com:100zhttp://g.example.comz
h.example.com)rwr"zh.example.com:80zhttp://h.example.com:80z
i.example.com)rxryzhttp://i.example.com:80)r{r|zj.example.com:80zhttp://j.example.comzhttp://j.example.com:80Nrorrr$r$r%�"test_password_manager_default_port�sZ
�����������z3RequestHdrsTests.test_password_manager_default_portN)rFrrGrOr[rtr}r$r$r$r%rHTs$VrHc@s(eZdZgZdejfdd�Zdd�ZdS)�
MockOpenerNcCs||||_|_|_dS�N)rZ�data�timeout)rrZr�r�r$r$r%�openszMockOpener.opencGs|||_|_dSr)�proto�args)rr�r�r$r$r%rszMockOpener.error)rFrrG�
addheaders�socket�_GLOBAL_DEFAULT_TIMEOUTr�rr$r$r$r%r~sr~c@s(eZdZddd�Zd	dd�Zdd�ZdS)
�MockFileNcCsdSrr$�r�countr$r$r%r4sz
MockFile.readcCsdSrr$r�r$r$r%�readlineszMockFile.readlinecCsdSrr$�rr$r$r%�close szMockFile.close)N)N)rFrrGr4r�r�r$r$r$r%r�s

r�c@seZdZdd�ZdS)�MockHeaderscCst|���Sr)r>�values)rrr$r$r%�
getheaders%szMockHeaders.getheadersN)rFrrGr�r$r$r$r%r�$sr�c@s&eZdZddd�Zdd�Zdd�ZdS)	�MockResponseNcCs.tj�||�||||f\|_|_|_|_dSr)�io�StringIO�__init__�code�msgrMrN)rr�r�rMr�rNr$r$r%r�*szMockResponse.__init__cCs|jSrrLr�r$r$r%�info.szMockResponse.infocCs|jSr�rNr�r$r$r%�geturl1szMockResponse.geturl)N)rFrrGr�r�r�r$r$r$r%r�)s
r�c@seZdZdd�Zdd�ZdS)�
MockCookieJarcCs
||_dSr)�ach_req)rrr$r$r%�add_cookie_header6szMockCookieJar.add_cookie_headercCs|||_|_dSr)�ec_req�ec_r)rrrr$r$r%�extract_cookies9szMockCookieJar.extract_cookiesN)rFrrGr�r�r$r$r$r%r�5sr�c@seZdZdd�Zdd�ZdS)�
FakeMethodcCs||_||_||_dSr)�	meth_name�handle�action)rr�r�r�r$r$r%r�>szFakeMethod.__init__cGs|j|j|jg|�R�Sr)r�r�r�)rr�r$r$r%�__call__CszFakeMethod.__call__N)rFrrGr�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�Hs
zMockHTTPResponse.__init__cCsdS)N�r$r�r$r$r%r4OszMockHTTPResponse.readcCsiSrr$r�r$r$r%r�RszMockHTTPResponse.infocCs|jSrr�r�r$r$r%r�UszMockHTTPResponse.geturlN)rFrrGr�r4r�r�r$r$r$r%r�Gsr�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_headersr��raise_on_endheadersZsock�_tunnel_headersr�r$r$r%r�ZszMockHTTPClass.__init__cCs||_||_|Sr)�hostr�)rr�r�r$r$r%r�bszMockHTTPClass.__call__cCs
||_dSr)r�)rr�r$r$r%�set_debuglevelgszMockHTTPClass.set_debuglevelNcCs&||_||_|r||_n
|j��dSr)�_tunnel_hostZ_tunnel_portr��clear)rr��portrMr$r$r%�
set_tunneljs
zMockHTTPClass.set_tunnelF)�encode_chunkedcCsP||_||_|dur&|j|��7_|j��|r:||_||_|jrLt��dSr)	�method�selectorr�r�sortr�r�r��OSError)rr�rN�bodyrMr�r$r$r%rrs
zMockHTTPClass.requestcCstt�idd�S)Nr��OK)r�r�r�r$r$r%�getresponseszMockHTTPClass.getresponsecCsdSrr$r�r$r$r%r��szMockHTTPClass.close)NN)NN)rFrrGr�r�r�r�r�r�rr�r�r$r$r$r%r�Ys
�
r�c@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�MockHandler��cCs|�|�dSr)�_define_methods)r�methodsr$r$r%r��szMockHandler.__init__cCsJ|D]@}t|�dkr|\}}n
|d}}t|||j�}t|j||�qdS)N�)�lenr�r��setattr�	__class__)rr��specrr�Zmethr$r$r%r��s

zMockHandler._define_methodscOs�|jj�||||f�|dur"dS|dkr.|S|dkrHtddid�}|S|dkrXtd�S|�d�r�||�d	�d
d�}zt|�}Wnty�Yn0tddid�}|j�	d|d||di�S|d
kr�t
j	�d��dS)N�return self�return responser�r�r��return requestzhttp://blah/r� ��httpr�raiserK)�parent�calls�appendr�r�
startswith�rfind�intr-rr*rA)rZfn_namer�r��kwds�resr�r$r$r%r��s*
zMockHandler.handlecCsdSrr$r�r$r$r%r��szMockHandler.closecCs||_g|j_dSr)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)
rFrrGr�r�r�r�r�r�r�r$r$r$r%r��s	r�cCsbg}d}|D]P}Gdd�dt�}||�}|j|7_|�|�|d}|�|�|�|�q|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�rFrrGr$r$r$r%�MockHandlerSubclass�sr�r�)r�r�r�r��add_handler)�opener�	meth_spec�handlersr��methsr�r;r$r$r%�add_ordered_mock_handlers�s

r�cGst�}|D]}|�|�q
|Sr)rr�)Zhandler_instancesr�r;r$r$r%�build_test_opener�sr�c@s$eZdZdd�Zdd�Zdd�ZdS)�MockHTTPHandlercCs||_||_|��dSr)r�rM�reset)rr�rMr$r$r%r��szMockHTTPHandler.__init__cCsd|_g|_dS)Nr)�_count�requestsr�r$r$r%r��szMockHTTPHandler.resetcCs�ddl}ddl}|j�|�|��|jdkrl|jd|_tjj|j	}|�
|j�}|j�
d|t�|j	||�S||_|�
d�}tdd|d|���SdS)Nrr�r��

r�r�r�)�email�copyr�r�Zdeepcopyr�r��client�	responsesr�Zmessage_from_stringrMr�rr�rZr��get_full_url)rrZr�r�rr�r$r$r%�	http_open�s
�
zMockHTTPHandler.http_openN�rFrrGr�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|�Sr)�do_openr��rrZr$r$r%�
https_open	szMockHTTPSHandler.https_openN)r)rFrrGr�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|_dSr�)r�r�r$r$r%r�szMockHTTPHandlerCheckAuth.resetcCs6|�d�rd|_tjj|j}t|j|t�d|���S)N�
AuthorizationTr�)	rSr�r�r�r�r�r�r�r�)rrZrr$r$r%r�s
z"MockHTTPHandlerCheckAuth.http_openNr�r$r$r$r%r�
sr�c@seZdZdd�Zdd�ZdS)�MockPasswordManagercCs||_||_||_||_dSr)�realmrN�userr^)rr�Zurir�r^r$r$r%rp!sz MockPasswordManager.add_passwordcCs||_||_|j|jfSr)�target_realm�
target_urlr�r^)rr�Zauthurir$r$r%rq'sz&MockPasswordManager.find_user_passwordN)rFrrGrprqr$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>.NonHandlerNr�r$r$r$r%�
NonHandler0sr)�objectr,�	TypeErrorrr�)rrr$r$r%�test_add_non_handler/s
�z(OpenerDirectorTests.test_add_non_handlercCs\ddlm}t�}ddgdgg}t||�|�tj���dD]}|�||j	|d�q>dS)Nr)rA)r�r�)Z
proxy_openr�)Zredirect_requestr�)Zdo�proxy�redirectz://example.com/)
�urllib.errorrArr�r�r*rZUnknownHandlerr,r�)rrA�or��schemer$r$r%�test_badly_named_methods5s	�
z,OpenerDirectorTests.test_badly_named_methodsc
Cs�t�}gd�dgdgdgg}t||�}td�}|�|�}|�||d�|ddf|ddfg}t||j�D]2\}}|\}	}
}}|�|	|
f|�|�||f�qldS)N)r��ftp_open�http_error_302r�r�r�r\r�rr�)rr�rr�r�zipr�)
rrr�r�rZ�rr��expected�got�handlerrr�r�r$r$r%�test_handledJs�

z OpenerDirectorTests.test_handledcCs�t�}g}dgdfdgdffD]:\}}Gdd�dt�}||�}||_|�|�|�|�q|�d�|�|jdd|d�|�|jdd|d�dS)	Nr
r�r�rc@seZdZdS)zCOpenerDirectorTests.test_handler_order.<locals>.MockHandlerSubclassNr�r$r$r$r%r�isr�r\r�)rr�r�r�r�r�rr�)rrr�r�r�r�r;r$r$r%�test_handler_orderds�

z&OpenerDirectorTests.test_handler_ordercCsZt�}dgdgg}t||�}td�}|�tjj|j|�|�|j	|dd|fifg�dS)N)r�r�r
r\rr�)
rr�rr,r*rrAr�rr�)rrr�r�rZr$r$r%�
test_raisevs�
zOpenerDirectorTests.test_raisecCs�t�}dgddggd�dgg}t||�}td�}|�|�|dd|ff|dd|tjd	d
iffg}t||j�D]:\}}|\}}	}
|�||	f|dd��|�|
|d�qpdS)N)r�z	error 302)Zhttp_error_400r�r�))rr�Zhttp_error_303Z
http_errorrr\rr��.r�)	rr�rr�rZ	ALWAYS_EQrr�r)rrr�r�rZr�rrrZmethod_namer�r$r$r%�test_http_error�s$�

��
z#OpenerDirectorTests.test_http_errorcCst�}ddgddgg}t||�}td�}|�|�|ddf|ddf|ddf|ddfg}t|j�D]�\}\}}}	}
|dkr�|�||f||�|�t|	�d�|�|	dt�qh|�||f||�|�t|	�d�|�|	dt�|	ddurh|�|	dt	�qhdS)	N)�http_requestr�)�
http_responser�r\rrr�rr�)
rr�rr��	enumerater�rr�ZassertIsInstancer�)rrr�r�rZr�rzrrr�r�r$r$r%�test_processors�s0���

�z#OpenerDirectorTests.test_processorsN)
rFrrGrr
rrrrrr$r$r$r%r-s
rcCs\z|�d�Wnty*t�d��Yn0tj�|�}tjdkrX|�	d�rX|dd�}|S)Nzutf-8zpath is not encodable to utf8rz///r�)
�encode�UnicodeEncodeError�unittestZSkipTestr*rrrrr�)r/�urlpathr$r$r%�sanepathname2url�sr c@sxeZdZdd�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#e� e$j%d=kd>�d?d@��Z&dAdB�Z'dCdD�Z(dEdF�Z)dGdH�Z*dIdJ�Z+dKdL�Z,dMdN�Z-dOdP�Z.dQdR�Z/dSdT�Z0dUdV�Z1dWS)X�HandlerTestscs�Gdd�d��G�fdd�dtjj�}ddl}d}||�}t�|_dd|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	fD]�\	}}}}}	}
}}}
t|�}d|_|�	|�}|�
|j|�|�
|j|	�|�
|j
t�|��|�
|j|�|�
|j|�|�
|jj|�|�
|jj|
�|��}|�
|�d�|
�|�
t|d�t|��q�dS)Nc@s$eZdZdd�Zdd�Zdd�ZdS)z-HandlerTests.test_ftp.<locals>.MockFTPWrappercSs
||_dSr�r��rr�r$r$r%r��sz6HandlerTests.test_ftp.<locals>.MockFTPWrapper.__init__cSs$|||_|_t�|j�t|j�fSr)�filename�filetyper�r�r�r�)rr$r%r$r$r%�retrfile�sz6HandlerTests.test_ftp.<locals>.MockFTPWrapper.retrfilecSsdSrr$r�r$r$r%r��sz3HandlerTests.test_ftp.<locals>.MockFTPWrapper.closeN)rFrrGr�r&r�r$r$r$r%�MockFTPWrapper�sr'cs&eZdZdd�Zejf�fdd�	ZdS)z-HandlerTests.test_ftp.<locals>.NullFTPHandlercSs
||_dSrr"r#r$r$r%r��sz6HandlerTests.test_ftp.<locals>.NullFTPHandler.__init__cs4|||_|_|||_|_||_�|j�|_|jSr)r��passwdr�r��dirsr��
ftpwrapper)rr�r(r�r�r)r��r'r$r%�connect_ftp�s
z9HandlerTests.test_ftp.<locals>.NullFTPHandler.connect_ftpN)rFrrGr�r�r�r,r$r+r$r%�NullFTPHandler�s�r-rzrheum rhaponicumz ftp://localhost/foo/bar/baz.html�	localhostr��Irfrgzbaz.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.gif�Content-type�Content-length)r*rZ
FTPHandler�ftplibr~r�ZFTP_PORTrr�rrr�r(r�r��
gethostbynamer�r)r*r$r%r��getr�r�)rr-r5r�r;rNr�r�r�r(�type_r)r$ZmimetyperZrrMr$r+r%�test_ftp�sX
�
�
�
�

���
zHandlerTests.test_ftpc
Cs�ddl}tj��}t�}|_tj}tt	j
�|��}d}d|d|dt�
d�|fg}zt�
t���}Wntjy�d}Yn0|r�|�d||f�|D]�}	t|d�}
z�z|
�|�W|
��n
|
��0|�t|	��}z$|��}|��}
|��}W|��n
|��0t	�|�}|jj|jd	d
�}Wt	�|�nt	�|�0|�||�|�|
dd�|�|
d
d�|�|
d|�|�||	�q�d|dddt�
d�t	��|fdt	��|ffD]f}	zRt|d�}
z|
�|�W|
��n
|
��0|� tj!j"|jt|	��Wt	�|�nt	�|�0�q�tj��}t�}|_dD]r\}	}t|	�}z|�|�Wn"tj!j"�y�|�#|�Yn0|�$|j%|�|�|j&d�|�|j&dk|��qBdS)Nrs
hello, world
zfile://localhost%sr(zfile://%s%sr.r��wbT)Zusegmtr3z
text/plainr4r{z
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*rZFileHandlerr~r�r�TESTFNr rr/r0r�r6ZgethostnameZgaierrorr�r��writer�Z	file_openrr4r�r��statZutilsZ
formatdate�st_mtime�remover�getcwdr,rrArV�assertIsrZ�type)rr�r;rr<rZtowrite�urlsZ	localaddrrNr5rr�rMZrespurlZstatsZmodifiedr;rZr$r$r%�	test_files�
�




�
��	


�
zHandlerTests.test_filecCsftj��}t�}|_d}dD]�\}}t||ddi�}d|_|�dd�t�}|�	||�}|j
|j|j|j
|j|jdkdf|��}	|	j|	j|�|�
�|�|�|jd	�|�|jd
�|�|j|�|�|jd�|�|jgd��|�|j|�qd
|_|�tjj|j	||�tdd�}|�t|j|�dg|_dD�]}td|�}t ddid�}|�|�}
|du�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��qDdS)Nr\))�GETN)�POSTsblah�Foorg�Spam�eggsr�r�rerr'))Z
Connectionr�)rHrg�rIrJTZbadpostrK)�Nr�r4r3�0z!application/x-www-form-urlencoded�HostrfrP)#r*rr�r~r�rr�rYr�r�r4r�r�r�r�r�r7�__contains__rr�r�r�r�r�r�r�r,rrAr�do_request_r�r��assertNotIn�unredirected_hdrs)rr;rrNr�r�rZr�r�hdrs�newreqr$r$r%�	test_httphsd
�




�
zHandlerTests.test_httpc	Cstj��}t�}|_tjddd�}|j}|��|�	t
j|�t|d��L}t
d|i�}|�|�}|�d�}|�|d�|�|�d��Wd�n1s�0Yt|d��P}t
d|d	d
i�}|�|�}|�t|�d��d
�|�|�d��Wd�n1�s0YdS)Nzw+bF)�mode�delete�rbr\�Transfer-encoding�chunkedr4�Content-Length�)r*rr�r~r��tempfileZNamedTemporaryFilerr�r)r�unlinkr�rrPrXrrVrSr�)	rr;r�file_objZ	file_pathr5rZrTZter$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)Nr\rYrZr4r[r\)r*rr�r~r�r��BytesIOrrPrrXrVrSr�r�)rr;rr_rZrTrMr$r$r%�test_http_body_fileobj�s


z#HandlerTests.test_http_body_fileobjc	Cs�tj��}t�}|_tjddg}iddifD]�}tj|tj	d��h}t
d|j|�}|�|�}|s�|�
|�d�d�|�
|�d�d	�n|�
t|�d��d�Wd�q.1s�0Yq.dS)
Nz-c�passr[r\)�stdoutr\r4rYrZ)r*rr�r~r��sys�
executable�
subprocess�Popen�PIPErrdrPrrXr�)rr;r�cmdrM�procrZrTr$r$r%�test_http_body_pipe�s

��z HandlerTests.test_http_body_pipecCs�tj��}t�}|_dd�}iddifD]\}td|�|�}|�|�}|sp|�|�d�d�|�|�d�d�q*|�t	|�d��d�q*dS)	Ncss
dVdS)Nsoner$r$r$r$r%�
iterable_body�sz;HandlerTests.test_http_body_iterable.<locals>.iterable_bodyr[�r\r4rYrZ)
r*rr�r~r�rrPrrXr�)rr;rrmrMrZrTr$r$r%�test_http_body_iterable�s

�z$HandlerTests.test_http_body_iterablecCsHtj��}t�|_|�tdd��}|�|�d�d�|�	|�
d��dS)Nr\r$rYrZr4)r*rr�r~r�rPrrrXrVrS)rr;rZr$r$r%�test_http_body_empty_seqs

z%HandlerTests.test_http_body_empty_seqcCshtj��}t�}|_t�dgd��}iddifD]0}td||�}|�|�}|�t	|�
d��d�q2dS)Nr/)r�r���r[�r\r4)r*rr�r~r��arrayrrPrr�rX)rr;rZiterable_arrayrMrZrTr$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�r�rZ_debuglevel)rrr;r$r$r%�test_http_handler_debuglevels



z)HandlerTests.test_http_handler_debuglevelcCsztj��}t�|_d}gd�}|D]R}t||�}|�|�}|�|jdd�|�	dd�|�|�}|�|jdd�q"dS)NrL)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.htmlrNrezsomeproxy:3128)
r*rr�r~r�rrPrrR�	set_proxy)rr;r�Zds_urlsZds_urlZds_reqZ	np_ds_reqZp_ds_reqr$r$r%�test_http_doubleslash s



z"HandlerTests.test_http_doubleslashcCshgd�}td�}|D]N}||_t|�}|�|��|�|�|jpBd|j�|�t|���j|j�qdS)N)zhttp://example.com?foo=bar#bazz)http://example.com?foo=bar&spam=eggs#bashrIrIr�)r�full_urlr
rr��fragmentZquery)rrDrrNZparsedr$r$r%�test_full_url_setter;sz!HandlerTests.test_full_url_settercCs6td�}|`|�|j�|�|j�|�|jd�dS)N�http://www.example.comr�)rryrWrzrr�)rrr$r$r%�test_full_url_deleterRs
z"HandlerTests.test_full_url_deletercCsztj��}t�|_d}t|�}|�|�}|�|jd�|�|j	d�d}t|�}|�|�}|�|jd�|�|j	d�dS)Nzhttp://www.python.org?getspam�www.python.orgz	/?getspam�http://www.python.orgr�)
r*rr�r~r�rrPrr�r�)rr;Z	weird_urlrZrTZurl_without_pathr$r$r%�test_fixpath_in_weirdurlsYs


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)
Nr\r�r�r�r���ZAccepted��zPartial contenti�zBad gatewayr�)r*rZHTTPErrorProcessorr~r�rr�rrBrVr�rWrr�r�)rr;rrNrZr�newrr$r$r%�test_errorsls(
zHandlerTests.test_errorscCs�t�}tj�|�}t�|_td�}tddid�}|�|�}|�	|j
|�|�	|j
|�|�|jd�|�
|j�|�||�}|�	|j|�|�	|j|�|�	||�dS)Nr\r�r�r�re)r�r*r�HTTPCookieProcessorr~r�rr�rrBr�r�origin_req_hostrVZunverifiablerr�r�)r�cjr;rZrrTr�r$r$r%�test_cookies�s
zHandlerTests.test_cookiesc
Csdd}d}tj��}t�}|_dD�]Z}dD�]N}t|d|�}t||�}tj|_	|�
dd�|durz|�
dtt|���|�
d	d
�z||t�|dtd|i��Wn,tjjy�|�|d
�|�|�Yn0|�|j��|�z|�|j��d�Wn"t�y|�|jj�Yn0dd�|jjD�}	|�d|	�|�d|	�|�|jjdd�|�d	|jj�|�d	|jj�q,q"t|�}tj|_	|fdd�}
t|dd�}d}tj|_	z|
||d�|d}�q�Wn*tjj�y�|�|tjjj�Yn0t|dd�}d}tj|_	z |
||d|�|d}�qWn*tjj�y^|�|tjjj�Yn0dS)N�http://example.com/a.htmlzhttp://example.com/b.html)i-ri/�3)Nz
blah
blah
z
http_error_%sZNonsensezviking=withholdr[rIrm�Blah�locationr�rFcSsg|]}|���qSr$)�lower)�.0�xr$r$r%�
<listcomp>�rLz.HandlerTests.test_redirect.<locals>.<listcomp>zcontent-lengthzcontent-typec	Ss |�|t�ddtd|i��dS)Nrr�r�)rr�r�)r;rZrNr$r$r%r�s
�z,HandlerTests.test_redirect.<locals>.redirectre)r�rr\r�zhttp://example.com/%d)r*r�HTTPRedirectHandlerr~r��getattrrr�r�r�rTrCr�rYr�r�r�	HTTPErrorr�assertIsNotNonerZr��
get_method�AttributeErrorrVr�rMrQrRZmax_repeatsZmax_redirections)r�from_urlZto_urlr;rr�r�r�rZrMrr�r$r$r%�
test_redirect�sl




���zHandlerTests.test_redirectcCs�d}gd�}gd�}d}tj��}t�}|_t|�}tj|_|D]6}|d|}	|�	tj
j|j|t
�ddtd|	i��qB|D]>}|d|}
|�|t
�dd	td|
i��|�|j��|
�q~dS)
Nr�)r��httpsr;)�fileZimapZldapzexample.com/b.htmlz://rzSecurity Loopholer��That's fine)r*rr�r~r�rr�r�r�r,rr�rr�r�rrZr�)rr�Z
valid_schemesZinvalid_schemesZschemeless_urlr;rrZr	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)Nr�z/b.htmlrr�r�)r*rr�r~r�rr�r�r�rZurljoinrr�r�rrZr�)rr�Zrelative_urlr;rrZr�r$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=eggsrz%Location: http://www.cracker.com/

ZCookie)Zhttp.cookiejarr�Ztest.test_http_cookiejarr�r�r*r�HTTPDefaultErrorHandlerr�r�r�r�rVrZrS)	rr�r�r��hh�hdeh�hrhZcprr$r$r%�test_cookie_redirects


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

rz
Location: r|)
r�r*rr�r�r�r�rr��strip)rZredirected_urlr�r�r�rr�r$r$r%�test_redirect_fragments


z#HandlerTests.test_redirect_fragmentcs|��tjj�tjj�d}t�|�tj_��t	tjd��t
d�����fdd�}|tjj_tj�d�}��|�
�d�dS)Ns(HTTP/1.1 302 Found
Location: ?query

�HTTPConnection)z/pathz/path?querycs8��|t����j|||g|�Ri|��d|j_dS)NsHTTP/1.1 200 OK

Hello!)r�nextrr�Zfakedata)�connr�rN�pos�kw�Z
real_classrrDr$r%r&sz3HandlerTests.test_redirect_no_path.<locals>.requestzhttp://python.org/pathzhttp://python.org/path?query)r)r*rr+r�r�r�r�fakehttpr��iterr.rr�)rZ	response1rr�r$r�r%�test_redirect_no_paths
z"HandlerTests.test_redirect_no_pathc
	Cs�Gdd�dtjj�}|�}tj�|�}d}|D]|\}}|�t|���Vt�d|d�|_|�	d�}d|d}|j
}	|�|	�|�t|	��Wd�q.1s�0Yq.dS)	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��
connectionZbuf�last_bufrr�)rrZ�resultr$r$r%r�3s
�z>HandlerTests.test_redirect_encoding.<locals>.Handler.http_openN)rFrrGr�r$r$r$r%�Handler2sr�))s/på-dansk/s/p%C3%A5-dansk/)�/spaced%20path/r�)s
/spaced path/r�)s/?på-dansks/?p%C3%A5-dansks!HTTP/1.1 302 Redirect
Location: s

r\sGET � )
r*r�HTTPHandler�build_opener�subTest�reprrr�r�r�r�rRr�)
rr�rr�r<r�r�rrrr$r$r%�test_redirect_encoding/s"���
z#HandlerTests.test_redirect_encodingcCs�d}t|d�t|d�fD]�}t�}tj�|�}|�|�dgg}t||�}td�}|�|j	d�|�
|�|�|j	|�|�|ddfgd	d
�|jD��qdS)N�proxy.example.com:3128�r�)ZHTTP)r�r�zhttp://acme.example.com/zacme.example.comrr�cSsg|]}|dd��qS�rr�r$�r��tupr$r$r%r�brLz+HandlerTests.test_proxy.<locals>.<listcomp>)�dictrr*r�ProxyHandlerr�r�rrr�r�r�)rr rbr�phr�r�rZr$r$r%�
test_proxyRs
�

�zHandlerTests.test_proxycCs�dtjd<t�}tj�tdd��}|�|�td�}|�	|j
d�|�|�|�	|j
d�td�}|�	|j
d�|�|�|�	|j
d�tjd=dS)	Nz
python.org�no_proxy�proxy.example.comr�zhttp://www.perl.org/�www.perl.orgrr~�r�environrr*rr�r�r�rrr�r��rrr�rZr$r$r%�test_proxy_no_proxyds



z HandlerTests.test_proxy_no_proxycCsfdtjd<t�}tj�tdd��}|�|�td�}|�	|j
d�|�|�|�	|j
d�tjd=dS)N�*r�r�r�rr~r�r�r$r$r%�test_proxy_no_proxy_allss


z$HandlerTests.test_proxy_no_proxy_allcCs�t�}tj�tdd��}|�|�dgg}t||�}td�}|�|j	d�|�
|�|�|j	d�|�|ddfgdd	�|jD��dS)
Nr��r�)r�r��https://www.example.com/�www.example.comrr�cSsg|]}|dd��qSr�r$r�r$r$r%r��rLz1HandlerTests.test_proxy_https.<locals>.<listcomp>)rr*rr�r�r�r�rrr�r�r�)rrr�r�r�rZr$r$r%�test_proxy_https~s
�

�zHandlerTests.test_proxy_httpscCs�t�}tj�tdd��}|�|�t�}|�|�td�}|�dd�|�dd�|�	|j
d�|�|j�|�
|�|�d	|jj�|�d
|jj�|�|j�|�	|j
d�|�	|�d�d�dS)Nr�r�r��Proxy-Authorization�FooBar�
User-Agent�Grailr�)r�r�)r�r��Proxy-authorization)rr*rr�r�r�r�rrTrr�rWr�r�rQr�r�rBr�rX)rrr�Z
https_handlerrZr$r$r%�$test_proxy_https_proxy_authorization�s(


��z1HandlerTests.test_proxy_https_proxy_authorizationrzonly relevant for WindowscCs�d}t}dD]}|�|||�d|�qdD]}|�|||�d|�q,d}|�|d|�d|�|�|d|�d|�d	D]}|�|||�d
|�q|dS)Nz*www.example.com;*.example.net; 192.168.0.1)r�zwww.example.netz192.168.0.1z expected bypass of %s to be true)rezwww.example.org�example.netz192.168.0.2�!expected bypass of %s to be Falsezexample.com; <local>rer�)�testr.z.expect <local> to bypass intranet address '%s')r
rRrV)rZproxy_overrideZproxy_bypassr�r$r$r%�test_winreg_proxy_bypass�s.������z%HandlerTests.test_winreg_proxy_bypass�darwinzonly relevant for OSXcCs�dgd�d�}dD]}|�t||�d|�qdD]}|�t||�d|�q2dgd�}|�td	|��dd
dgd�}d}|�t||�d|�d
}|�t||�d|�dS)NF)�foo.barz	*.bar.com�	127.0.0.1z10.10z10.0/16)Zexclude_simple�
exceptions)r�zwww.bar.comr�z	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.1Znotinbypassr�Tr�z10.0.0.0/40z172.19.10.0/24z172.19.10.5z10.0.1.5)rRrrV)rZbypassr�r$r$r%�test_osx_proxy_bypass�s0���
���z"HandlerTests.test_osx_proxy_bypasscCs�|j||d��lt�}t�}tj�|�}d�|�d}td|�}|�|�|�|�|�	||d|||dd�Wd�n1s�0YdS)N)r�rMz
r��r��!http://acme.example.com/protected)
r�rr�r*rr�joinr�r��_test_basic_auth)rrMr�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�}d|�d�d|�d�d|�d	�|�d
|��|�d
|��|�d
|��|�d
|��|�d
|��fD]}d|��g}|�||�q�t�dtf��(d
|��g}|�||�Wd�n1s�0Y||g||g||gfD] }	dd�|	D�}|�||��qdS)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, �WWW-Authenticate: zBasic Auth Realm was unquotedzWWW-Authenticate: Basic realm=cSsg|]}d|���qS)r�r$)r�Z	challenger$r$r%r�s�z0HandlerTests.test_basic_auth.<locals>.<listcomp>)r�rZcheck_warnings�UserWarning)
rr�Zrealm2�basicZbasic2Zother_no_realm�digestZ	realm_strrMZ
challengesr$r$r%�test_basic_auth�s<


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

r�z&http://acme.example.com:3128/protected)
rr*rr�r�r�r�ZProxyBasicAuthHandlerr�r�)rr�r�r�r�r�r�r$r$r%�test_proxy_basic_auths 
�


�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|_dSr)rr��recordedr�r$r$r%r�5s
zZHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.__init__cSs|j�|�dSr)r�r�)rr�r$r$r%�record9szXHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.recordN)rFrrGr�r�r$r$r$r%�RecordingOpenerDirector4sr�c@seZdZdd�ZdS)zOHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandlerc_s,|j�d�tjjj|g|�Ri|��dS)Nr�)r�r�r*r�HTTPDigestAuthHandler�http_error_401�rr�r�r$r$r%r�=s��z^HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandler.http_error_401N�rFrrGr�r$r$r$r%�TestDigestAuthHandler<sr�c@seZdZdd�ZdS)zNHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandlerc_s,|j�d�tjjj|g|�Ri|��dS)Nr�)r�r�r*rrr�r�r$r$r%r�Cs��z]HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandler.http_error_401Nr�r$r$r$r%�TestBasicAuthHandlerBsr�r�r��&WWW-Authenticate: Basic realm="%s"

r�r�r�r�r�)rr*rr�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)Nr�zWWW-Authenticate: Kerberos

r|)	rr*rr�r�r�r,r-r�)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)Nr�zWWW-Authenticate: NTLM

r|)	rr*rrr�r�r,r-r�)rr�Zbasic_auth_handlerr�r$r$r%�#test_unsupported_auth_basic_handlergs�

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�ZwileZcoyoter�z%s:%s�asciizBasic r�)�base64rprr�rNr�r^r�r�r�r�r�rVrS�bytesZencodebytesr��decoderXrRr�)rr�r�Zauth_headerr�r�r��request_urlZ
protected_urlrr�r^ZuserpassZauth_hdr_valuer$r$r%r�qs6
���
zHandlerTests._test_basic_authcCs�d\}}d}td�}t�}t|�}|jd|||dd�|�|�|��|�|�|d��|�|�|d��t�}|�|�|�|�|�	|�|�|j
�dS)Nr�r�r�T)�is_authenticatedz/nestedZplain)r�rrrprRrrVrr�r�r�)rr�r^rr��pwd_manager�auth_prior_handlerr�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)Nr�r�ZACMEr�r�r�)rrrprrVrr�r�r�rRr�r�)
rr�r^rr�rrZis_authr�r�r$r$r%�.test_basic_prior_auth_send_after_first_success�s0


�






z;HandlerTests.test_basic_prior_auth_send_after_first_successc	Cs�dD]�\}}d�|�}t�|��|�}tj��}td�}d|_|�	||��}|�
�Wd�n1sj0Y|�|jj
d�|��qdS)z=Test the connection is cleaned up when the response is closed))zConnection: close�data)zTransfer-Encoding: chunkeds4
data
0

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

�
http://dummy/NzConnection not closed with {!r})�formatrr�rr*rr�rr�r�r4rR�fakesock�closed)rZtransferr��headerr�rrZZrespr$r$r%�test_http_closed�s

&
�zHandlerTests.test_http_closedcCspt�d�}tj��}td�}d|_|�tj	j
��|�||�Wd�n1sR0Y|�|j
jd�dS)z;Test the connection is cleaned up after an invalid responserLrNzConnection not closed)rr�r*rr�rr�r,r�r�Z
BadStatusLiner�rRr
r)rr�rrZr$r$r%�test_invalid_closed�s

*z HandlerTests.test_invalid_closedN)2rFrrGr9rErUr`rbrlrorprurvrxr{r}r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rZ
skipUnlessrrr�re�platformr�r�r�r�r�r�r�r�rr	rrr$r$r$r%r!�sV@\B
J
	#

!-2

%(r!c@s4eZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�	MiscTestscs"|�t�fdd�|jD���dS)Nc3s|]}|j�kVqdSr)r�)r�r;��
handler_classr$r%�	<genexpr>�s�z/MiscTests.opener_has_handler.<locals>.<genexpr>)rR�anyr�)rr�rr$rr%�opener_has_handler�s�zMiscTests.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>.MyHTTPHandlerNr�r$r$r$r%�
MyHTTPHandler�src@seZdZdd�ZdS)z/MiscTests.test_build_opener.<locals>.FooHandlercSsdSrr$r�r$r$r%�foo_opensz8MiscTests.test_build_opener.<locals>.FooHandler.foo_openN)rFrrGrr$r$r$r%�
FooHandler�src@seZdZdd�ZdS)z/MiscTests.test_build_opener.<locals>.BarHandlercSsdSrr$r�r$r$r%�bar_opensz8MiscTests.test_build_opener.<locals>.BarHandler.bar_openN)rFrrGrr$r$r$r%�
BarHandlersrc@seZdZdS)z7MiscTests.test_build_opener.<locals>.MyOtherHTTPHandlerNr�r$r$r$r%�MyOtherHTTPHandler sr)r*rr��BaseHandlerr�r)rrrrr�rrr$r$r%�test_build_opener�s,

zMiscTests.test_build_openercCs�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@rMzHTTP Error %s: %sz<HTTPError %s: %r>)r*rr�rRr�rr@rMr�r�rCr�)rr�rNr�r�rSrDZexpected_errmsgr$r$r%�test_HTTPError_interface'sz"MiscTests.test_HTTPError_interfacecCs:gd�}|D]\}}|�t|�|�q|�ttd�fdS)N)�r�)NNNr��r�)NNNr�r"r#)zjoe:password@proxy.example.com)Nr]r^r�)z#joe:password@proxy.example.com:3128)Nr]r^r�)zhttp://proxy.example.com/)r�NNr�)zhttp://proxy.example.com:3128/)r�NNr�)z&http://joe:password@proxy.example.com/�r�r]r^r�)z*http://joe:password@proxy.example.com:3128)r�r]r^r�)z1ftp://joe:password@proxy.example.com/rubbish:3128)r;r]r^r�)z%http://joe:password@proxy.example.comr$)z&http://user/name:password@localhost:22)r��	user/namer^�localhost:22)z&http://username:pass/word@localhost:22)r�Zusername�	pass/wordr&)z'http://user/name:pass/word@localhost:22)r�r%r'r&zfile:/ftp.example.com)rr	r,r-)rZparse_proxy_test_casesZtcrr$r$r%�test_parse_proxy9s'zMiscTests.test_parse_proxycCsPt�}|�t��}|�d�Wd�n1s00Y|�t|j�d�dS)NZinvalidz5Unsupported digest authentication algorithm 'invalid')rr,r-Zget_algorithm_implsrrCZ	exception)rr�excr$r$r%�test_unsupported_algorithmfs(�z$MiscTests.test_unsupported_algorithmN)rFrrGrr r!r(r*r$r$r$r%r�s
,-rc@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)rFrrGr�r$r$r$r%�
PutRequestqsr-cCsLtd�|_tddddid�|_tddd�|_|�d�|_|jddd�|_dS)	N�http://www.python.org/~jeremy/r�zX-Testr�rL�HEAD)r�rG)rr7�post�headr-�put�
force_postr�r$r$r%�setUpts
��zRequestTests.setUpcCs^|�d|j���|�d|j���|�d|j���|�d|j���|�d|j���dS)NrGrFr/r,)rr0r�r7r1r2r3r�r$r$r%�test_method~s
zRequestTests.test_methodcCsL|�|jj�|�d|j���d|j_|�|jj�|�d|j���dS)NrFrmrG)rVr7r�rr�rRr�r$r$r%�	test_data�s
zRequestTests.test_datacCsN|�d|jj�|j�dd�|�d|jjd�d|j_|�d|jj�dS)Nr4�*rm)rQr7rRrYrr�r�r$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)Nr4rfrq)rQr7rRr�rYrr�r$r$r%�/test_deleting_data_should_remove_content_length�sz<RequestTests.test_deleting_data_should_remove_content_lengthcCs|�d|j���dS)Nr.)rr7r�r�r$r$r%�test_get_full_url�s�zRequestTests.test_get_full_urlcCs*|�d|jj�td�}|�d|j�dS)Nz	/~jeremy/zhttp://www.python.org/r')rr7r�rr�r$r$r%�
test_selector�szRequestTests.test_selectorcCs|�d|jj�dS)Nr�)rr7rCr�r$r$r%�
test_get_type�szRequestTests.test_get_typecCs|�d|jj�dS)Nr~)rr7r�r�r$r$r%�
test_get_host�szRequestTests.test_get_hostcCstd�}|�d|j�dS)Nzhttp://www.%70ython.org/r~�rrr�r�r$r$r%�test_get_host_unquote�sz"RequestTests.test_get_host_unquotecCsR|�|j���|j�dd�|�|j���|�d|jj�|�d|jj�dS)Nr�r�r~)rVr7Z	has_proxyrwrRrr�r�r�r$r$r%r��s
zRequestTests.test_proxycCstd�}|�d|j�dS)Nz<URL:http://www.python.org>r~r>r�r$r$r%�test_wrapped_url�szRequestTests.test_wrapped_urlcCsLtd�}|�d|j�td�}|�d|j�d}t|�}|�|��|�dS)N�-http://www.python.org/?qs=query#fragment=truez
/?qs=queryzhttp://www.python.org/#fun=truer'�.http://docs.python.org/library/urllib2.html#OK)rrr�r�)rrZrNr$r$r%�test_url_fragment�szRequestTests.test_url_fragmentcCs0gd�}|D]}t|�}|�|��|j�qdS)N)zhttp://docs.python.orgrBrA)rrr�ry)rrDrNrZr$r$r%�test_url_fullurl_get_full_url�sz*RequestTests.test_url_fullurl_get_full_urlN)rFrrGrr-r4r5r6r8r9r:r;r<r=r?r�r@rCrDr$r$r$r%r+ps

r+�__main__);rr�rZtest.supportrrrr�r�rtrer]rgZurllib.requestr*rrrrr	r
rrZurllib.parser
rZhttp.clientr�ZTestCaserrHr~r�r�r�r�r�r�r��IOBaser�r�r�r�r�rrr�r�r�r�r�rr r!rr+rF�mainr$r$r$r%�<module>sb(4<

-6"
3{a