Server : LiteSpeed
System : Linux server51.dnsbootclub.com 4.18.0-553.62.1.lve.el8.x86_64 #1 SMP Mon Jul 21 17:50:35 UTC 2025 x86_64
User : nandedex ( 1060)
PHP Version : 8.1.33
Disable Function : NONE
Directory :  /opt/cppython/lib/python3.8/test/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]


Current File : //opt/cppython/lib/python3.8/test/__pycache__/test_urllib2.cpython-38.opt-1.pyc
U

>��g�=�@sddlZddlmZddlmZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlm
Z
mZmZmZmZmZmZmZddlmZddlZddlZGdd�dej�ZGdd	�d	ej�ZGd
d�d�ZGdd
�d
�ZGdd�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!ej+j,�Z-Gd"d#�d#ej+j.�Z/Gd$d%�d%ej+j,�Z0Gd&d'�d'�Z1Gd(d)�d)ej�Z2d*d+�Z3Gd,d-�d-ej�Z4Gd.d/�d/ej�Z5Gd0d1�d1ej�Z6e7d2k�re�8�dS)3�N)�support)�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�error�robotparserzfrom urllib.%s import *�__builtins__r�nt�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%�0/opt/cppython/lib/python3.8/test/test_urllib2.py�test___all__!s
��zTrivialTests.test___all__c	Csl|�ttjjd�tj�tjj��	tj
d�}tjdkr@d|}nd|}tj�|��}|��W5QRXdS)Nz	bogus url�/rz
file:///%s�	file://%s)
�assertRaises�
ValueError�urllibr�urlopenr�path�abspath�__file__�replace�sepr�read)rZfnameZfile_url�fr%r%r&�test_trivial0s

zTrivialTests.test_trivialc
Cs`ddddgfdddgfdddd	d
ddd
gfddddgfg}|D]\}}|�tj�|�|�q>dS)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,rZparse_http_list)r�tests�string�listr%r%r&�test_parse_http_list@s
���z!TrivialTests.test_parse_http_listcCs"tj�d�}|�|jt|��dS)N�reason)r,r�URLError�assertInr?�str)r�errr%r%r&�test_URLError_reasonstrKsz$TrivialTests.test_URLError_reasonstrN)�__name__r�__qualname__r'r5r>rDr%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.

        �http://example.com�	Spam-eggs�blah��headersz	spam-EggSN)rrrL)r�urlr%r%r&�test_request_headers_dictRs
������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
        rHrIrJrK)rIrJzFoo-Bar�baz)zFoo-barrOz	Not-there�defaultzUnredirected-spamZEggsN)r�
assertTrue�
has_headerrZheader_items�
add_header�sorted�assertFalse�assertIsNone�
get_headerZ
remove_header�add_unredirected_header)rrM�reqr%r%r&�test_request_headers_methodsgs"�

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�3r8z
d.example.com�4�dze.example.com:3128�5�e�example.com)r\r]rHzhttp://example.com/spamzhttp://example.com/spam/spamzhttp://example.com/foo�foozhttp://example.com/bar�bar�ninizhttp://example.com/foo/barZfoobarZnibar)rer^)rfrgzhttp://example.com/foo/zhttp://example.com/foo/bazzhttp://example.com/foobar�NNzhttp://example.com/baz/rO�nininizhttp://example.com/baz)rOrizhttp://example.com/baz/barr7�firstrJ�second�spam)rkrlr6�1)rmr6zhttp://a.example.com/z
c.example.comzc.example.com:3128)r_r8)r`ra)rbrc�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.
        r4zhttp://g.example.com:80�10�jr9zhttp://h.example.com�11r#r:zi.example.com:80�12�l�iz
j.example.com�13�mzg.example.com:100rhzg.example.com:80)rtruz
g.example.comzhttp://g.example.com:100zhttp://g.example.comz
h.example.com)rvr#zh.example.com:80zhttp://h.example.com:80z
i.example.com)rwrxzhttp://i.example.com:80)rzr{zj.example.com:80zhttp://j.example.comzhttp://j.example.com:80Nrnrqr%r%r&�"test_password_manager_default_port�sZ
�����������z3RequestHdrsTests.test_password_manager_default_portN)rErrFrNrZrsr|r%r%r%r&rGPs$VrGc@s(eZdZgZdejfdd�Zdd�ZdS)�
MockOpenerNcCs||||_|_|_dS�N)rY�data�timeout)rrYrr�r%r%r&�openszMockOpener.opencGs|||_|_dSr~)�proto�args)rr�r�r%r%r&rszMockOpener.error)rErrF�
addheaders�socket�_GLOBAL_DEFAULT_TIMEOUTr�rr%r%r%r&r}sr}c@s(eZdZddd�Zd	dd�Zdd�ZdS)
�MockFileNcCsdSr~r%�r�countr%r%r&r3sz
MockFile.readcCsdSr~r%r�r%r%r&�readlineszMockFile.readlinecCsdSr~r%�rr%r%r&�closeszMockFile.close)N)N)rErrFr3r�r�r%r%r%r&r�s

r�c@seZdZdd�ZdS)�MockHeaderscCst|���Sr~)r=�values)rrr%r%r&�
getheaders!szMockHeaders.getheadersN)rErrFr�r%r%r%r&r� sr�c@s&eZdZddd�Zdd�Zdd�ZdS)	�MockResponseNcCs.tj�||�||||f\|_|_|_|_dSr~)�io�StringIO�__init__�code�msgrLrM)rr�r�rLrrMr%r%r&r�&szMockResponse.__init__cCs|jSr~rKr�r%r%r&�info*szMockResponse.infocCs|jSr~�rMr�r%r%r&�geturl-szMockResponse.geturl)N)rErrFr�r�r�r%r%r%r&r�%s
r�c@seZdZdd�Zdd�ZdS)�
MockCookieJarcCs
||_dSr~)�ach_req)rrr%r%r&�add_cookie_header2szMockCookieJar.add_cookie_headercCs|||_|_dSr~)�ec_req�ec_r)rrrr%r%r&�extract_cookies5szMockCookieJar.extract_cookiesN)rErrFr�r�r%r%r%r&r�1sr�c@seZdZdd�Zdd�ZdS)�
FakeMethodcCs||_||_||_dSr~)�	meth_name�handle�action)rr�r�r�r%r%r&r�:szFakeMethod.__init__cGs|j|j|jf|��Sr~)r�r�r�)rr�r%r%r&�__call__?szFakeMethod.__call__N)rErrFr�r�r%r%r%r&r�9sr�c@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�MockHTTPResponsecCs"||_||_||_||_d|_dS)N��)�fpr��statusr?r�)rr�r�r�r?r%r%r&r�Ds
zMockHTTPResponse.__init__cCsdS)N�r%r�r%r%r&r3KszMockHTTPResponse.readcCsiSr~r%r�r%r%r&r�NszMockHTTPResponse.infocCs|jSr~r�r�r%r%r&r�QszMockHTTPResponse.geturlN)rErrFr�r3r�r�r%r%r%r&r�Csr�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_endheaders�sock�_tunnel_headersr�r%r%r&r�VszMockHTTPClass.__init__cCs||_||_|Sr~)�hostr�)rr�r�r%r%r&r�^szMockHTTPClass.__call__cCs
||_dSr~)r�)rr�r%r%r&�set_debuglevelcszMockHTTPClass.set_debuglevelNcCs&||_||_|r||_n
|j��dSr~)�_tunnel_hostZ_tunnel_portr��clear)rr��portrLr%r%r&�
set_tunnelfs
zMockHTTPClass.set_tunnelF)�encode_chunkedcCsP||_||_|dk	r&|j|��7_|j��|r:||_||_|jrLt��dSr~)	�method�selectorr�r�sortrr�r��OSError)rr�rM�bodyrLr�r%r%r&rns
zMockHTTPClass.requestcCstt�idd�S)Nr��OK)r�r�r�r%r%r&�getresponse{szMockHTTPClass.getresponsecCsdSr~r%r�r%r%r&r�~szMockHTTPClass.close)NN)NN)rErrFr�r�r�r�r�r�rr�r�r%r%r%r&r�Us
�
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��methr%r%r&r��s

zMockHandler._define_methodscOs�|jj�||||f�|dkr"dS|dkr.|S|dkrHtddid�}|S|dkrXtd�S|�d�r�||�d	�d
d�}zt|�}Wntk
r�YnXtddid�}|j�	d|d||di�S|d
kr�t
j	�d��dS)N�return self�return responser�r�r��return requestzhttp://blah/r� ��httpr�raiserJ)�parent�calls�appendr�r�
startswith�rfind�intr+rr,r@)rZfn_namer�r��kwds�resr�r%r%r&r��s*
zMockHandler.handlecCsdSr~r%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)
rErrFr�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�rErrFr%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�rL�reset)rr�rLr%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��deepcopyr�r��client�	responsesr�Zmessage_from_stringrLr�rr�rYr��get_full_url)rrYr�r�rr�r%r%r&�	http_open�s"
�
zMockHTTPHandler.http_openN�rErrFr�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��rrYr%r%r&�
https_openszMockHTTPSHandler.https_openN)r)rErrFr�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�)	rRr�r�r�r�r�r�r�r�)rrYrr%r%r&r�s
z"MockHTTPHandlerCheckAuth.http_openNr�r%r%r%r&r�	sr�c@seZdZdd�Zdd�ZdS)�MockPasswordManagercCs||_||_||_||_dSr~)�realmrM�userr])rr�Zurirr]r%r%r&rosz MockPasswordManager.add_passwordcCs||_||_|j|jfSr~)�target_realm�
target_urlrr])rr�Zauthurir%r%r&rp#sz&MockPasswordManager.find_user_passwordN)rErrFrorpr%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&�
NonHandler,sr)�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)r@)r�r�)Z
proxy_openr�)Zredirect_requestr�)Zdo�proxy�redirectz://example.com/)
�urllib.errorr@rr�r�r,rZUnknownHandlerr*r�)rr@�or��schemer%r%r&�test_badly_named_methods1s	�
z,OpenerDirectorTests.test_badly_named_methodsc
Cs�t�}dddgdgdgdgg}t||�}td�}|�|�}|�||d�|ddf|ddfg}t||j�D]2\}}|\}	}
}}|�|	|
f|�|�||f�qndS)Nr��ftp_open�http_error_302�r�r�r[r�r)rr�rr�r�zipr�)
rrr�r�rY�rr��expected�got�handlerrr�r�r%r%r&�test_handledFs�

z OpenerDirectorTests.test_handledcCs�t�}g}dgdfdgdffD]:\}}Gdd�dt�}||�}||_|�|�|�|�q|�d�|�|jdd|d�|�|jdd|d�dS)	Nrr�r�rc@seZdZdS)zCOpenerDirectorTests.test_handler_order.<locals>.MockHandlerSubclassNr�r%r%r%r&r�esr�r[r�)rr�r�r�r�r�rr�)rrr�r�r�r�r:r%r%r&�test_handler_order`s�

z&OpenerDirectorTests.test_handler_ordercCsZt�}dgdgg}t||�}td�}|�tjj|j|�|�|j	|dd|fifg�dS)N)r�r�rr[rr�)
rr�rr*r,rr@r�rr�)rrr�r�rYr%r%r&�
test_raisers�
zOpenerDirectorTests.test_raisecCs�t�}dgddgdddgdgg}t||�}Gdd	�d	�}td
�}|�|�|dd|ff|dd||�d
diffg}t||j�D]:\}}|\}	}
}|�|	|
f|dd��|�||d�q�dS)N)r�z	error 302)Zhttp_error_400r�r�)rr�Zhttp_error_303Z
http_errorrc@seZdZdd�ZdS)z4OpenerDirectorTests.test_http_error.<locals>.UnknowncSsdS)NTr%r�r%r%r&�__eq__�sz;OpenerDirectorTests.test_http_error.<locals>.Unknown.__eq__N)rErrFrr%r%r%r&�Unknown�srr[rr��.r�)rr�rr�rr�r)rrr�r�rrYr�rrrZmethod_namer�r%r%r&�test_http_errors*��

��
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�|	ddk	rh|�|	dt	�qhdS)	N)�http_requestr�)�
http_responser�r[rrr�rr�)
rr�rr��	enumerater�rr�ZassertIsInstancer�)rrr�r�rYr�ryrrr�r�r%r%r&�test_processors�s4���





�z#OpenerDirectorTests.test_processorsN)
rErrFrr
rrrrr r%r%r%r&r)s
rcCs^z|�d�Wntk
r,t�d��YnXtj�|�}tjdkrZ|�	d�rZ|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�rrr%r%r&r��sz6HandlerTests.test_ftp.<locals>.MockFTPWrapper.__init__cSs$|||_|_t�|j�t|j�fSr~)�filename�filetyper�r�rr�)rr)r*r%r%r&�retrfile�sz6HandlerTests.test_ftp.<locals>.MockFTPWrapper.retrfilecSsdSr~r%r�r%r%r&r��sz3HandlerTests.test_ftp.<locals>.MockFTPWrapper.closeN)rErrFr�r+r�r%r%r%r&�MockFTPWrapper�sr,cs&eZdZdd�Zejf�fdd�	ZdS)z-HandlerTests.test_ftp.<locals>.NullFTPHandlercSs
||_dSr~r'r(r%r%r&r��sz6HandlerTests.test_ftp.<locals>.NullFTPHandler.__init__cs4|||_|_|||_|_||_�|j�|_|jSr~)r�passwdr�r��dirsr�
ftpwrapper)rrr-r�r�r.r��r,r%r&�connect_ftp�s
z9HandlerTests.test_ftp.<locals>.NullFTPHandler.connect_ftpN)rErrFr�r�r�r1r%r0r%r&�NullFTPHandler�s�r2rzrheum rhaponicumz ftp://localhost/foo/bar/baz.html�	localhostr��Irerfzbaz.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�rrrr-r�r��
gethostbynamer�r.r/r)r*r��getr�r�)rr2r:rr:rMr�r�rr-Ztype_r.r)ZmimetyperYrrLr%r0r&�test_ftp�s��������
zHandlerTests.test_ftpc
Cs�ddl}tj��}t�}|_tj}tt	j
�|��}d}d|d|dt�
d�|fg}zt�
t���}Wntjk
r�d}YnX|r�|�d||f�|D]�}	t|d�}
zpz|
�|�W5|
��X|�t|	��}z|��}|��}
|��}W5|��Xt	�|�}|jj|jd	d
�}W5t	�|�X|�||�|�|
dd�|�|
d
d�|�|
d|�|�||	�q�d|dddt�
d�t	��|fdt	��|ffD]T}	z@t|d�}
z|
�|�W5|
��X|� tj!j"|jt|	��W5t	�|�X�q�tj��}t�}|_dD]t\}	}t|	�}z|�|�Wn$tj!j"k
�rZ|�#|�YnX|�$|j%|�|�|j&d�|�|j&dk|��qdS)Nrs
hello, world
zfile://localhost%sr)zfile://%s%sr3r��wbT)Zusegmtr8z
text/plainr9rzz
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�FileHandlerr}r�r�TESTFNr%rr.r/r�r;�gethostname�gaierrorr�r��remover��writeZ	file_openrr3r�r��statZutilsZ
formatdate�st_mtimer�getcwdr*rr@rU�assertIsrY�type)rr�r:rrAr$Ztowrite�urlsZ	localaddrrMr4rrrLZrespurlZstatsZmodifiedr?rYr%r%r&�	test_file
s�
�





�
��	


�
zHandlerTests.test_filecCshtj��}t�}|_d}dD]�\}}t||ddi�}d|_|�dd�t�}|�	||�}|j
|j|j|j
|j|jdkdf|��}	|	j|	j|�|�
�|�|�|jd	�|�|jd
�|�|j|�|�|jd�|�|jdd
dg�|�|j|�qd|_|�tjj|j	||�tdd�}|�t|j|�dg|_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��qFdS)Nr[))�GETN)�POSTsblah�Foorf�Spam�eggsr�r�rdrr()�
Connectionr�)rOrf)rPrQTZbadpost)�Nr�r9r8�0z!application/x-www-form-urlencoded�HostrerO)#r,rr�r}r�rr�rXr�r�r3r�r�r�r�r�r<�__contains__rr�r�r�r�r�rr�r*rr@r�do_request_r�r��assertNotIn�unredirected_hdrs)rr:rrMr�rrYr�r�hdrs�newreqr%r%r&�	test_httpisp
��




�
zHandlerTests.test_httpc		Cs�tj��}t�}|_tjddd�}|j}|��|�	t
j|�t|d��B}t
d|i�}|�|�}|�d�}|�|d�|�|�d��W5QRXt|d��F}t
d|d	d
i�}|�|�}|�t|�d��d
�|�|�d��W5QRXdS)Nzw+bF)�mode�delete�rbr[�Transfer-encoding�chunkedr9�Content-Length�)r,rr�r}r��tempfile�NamedTemporaryFilerr��
addCleanupr�unlinkr�rrWrWrrUrRr�)	rr:r�file_objZ	file_pathr4rYr[�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)Nr[r`rar9rbrc)r,rr�r}r�r��BytesIOrrWrrWrUrRr�r�)rr:rrhrYr[rLr%r%r&�test_http_body_fileobj�s


z#HandlerTests.test_http_body_fileobjc
Cs�tj��}t�}|_tjddg}iddifD]x}tj|tj	d��^}t
d|j|�}|�|�}|s�|�
|�d�d�|�
|�d�d	�n|�
t|�d��d�W5QRXq.dS)
Nz-c�passrbrc)�stdoutr[r9r`ra)r,rr�r}r��sys�
executable�
subprocess�Popen�PIPErrnrWrrWr�)rr:r�cmdrL�procrYr[r%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_bodyrb�r[r9r`ra)
r,rr�r}r�rrWrrWr�)rr:rrwrLrYr[r%r%r&�test_http_body_iterable�s

�z$HandlerTests.test_http_body_iterablecCsHtj��}t�|_|�tdd��}|�|�d�d�|�	|�
d��dS)Nr[r%r`rar9)r,rr�r}r�rWrrrWrUrR)rr:rYr%r%r&�test_http_body_empty_seqs

z%HandlerTests.test_http_body_empty_seqcCsltj��}t�}|_t�dddddg�}iddifD]0}td||�}|�|�}|�t	|�
d	��d�q6dS)
Nr4r�r���rb�r[r9)r,rr�r}r��arrayrrWrr�rW)rr:rZiterable_arrayrLrYr[r%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_debuglevelcCs~tj��}t�|_d}ddddg}|D]R}t||�}|�|�}|�|jdd�|�	dd�|�|�}|�|jdd�q&dS)	NrSz#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.htmlrUrdzsomeproxy:3128)
r,rr�r}r�rrWrrY�	set_proxy)rr:rZds_urlsZds_urlZds_reqZ	np_ds_reqZp_ds_reqr%r%r&�test_http_doubleslash!s
�


z"HandlerTests.test_http_doubleslashcCsjdddg}td�}|D]N}||_t|�}|�|��|�|�|jpDd|j�|�t|���j|j�qdS)Nzhttp://example.com?foo=bar#bazz)http://example.com?foo=bar&spam=eggs#bashrHr�)r�full_urlrrr��fragmentZquery)rrKrrMZparsedr%r%r&�test_full_url_setter<s�z!HandlerTests.test_full_url_settercCs6td�}|`|�|j�|�|j�|�|jd�dS)N�http://www.example.comr�)rr�rVr�rr�)rrr%r%r&�test_full_url_deleterSs
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�rrWrr�r�)rr:Z	weird_urlrYr[Zurl_without_pathr%r%r&�test_fixpath_in_weirdurlsZs


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�rrIrUr�rVrr�r�)rr:rrMrYr�newrr%r%r&�test_errorsms(
zHandlerTests.test_errorscCs�t�}tj�|�}t�|_td�}tddid�}|�|�}|�	|j
|�|�	|j
|�|�|jd�|�
|j�|�||�}|�	|j|�|�	|j|�|�	||�dS)Nr[r�r�r�rd)r�r,r�HTTPCookieProcessorr}r�rr�rrIr�r�origin_req_hostrUZunverifiablerr�r�)r�cjr:rYrr[r�r%r%r&�test_cookies�s
zHandlerTests.test_cookiesc
Csld}d}tj��}t�}|_dD�]^}dD�]R}t|d|�}t||�}tj|_	|�
dd�|dk	rz|�
dtt|���|�
d	d
�z||t�|dtd|i��Wn.tjjk
r�|�|d
�|�|�YnX|�|j��|�z|�|j��d�Wn$tk
�r |�|jj�YnXdd�|jjD�}	|�d|	�|�d|	�|�|jjdd�|�d	|jj�|�d	|jj�q,q"t|�}tj|_	|fdd�}
t|dd�}d}tj|_	z|
||d�|d}�q�Wn,tjjk
�r|�|tjjj�YnXt|dd�}d}tj|_	z |
||d|�|d}�qWn,tjjk
�rf|�|tjjj�YnXdS)N�http://example.com/a.htmlzhttp://example.com/b.html)i-ri/�3)Nz
blah
blah
z
http_error_%sZNonsensezviking=withholdrbrPrl�Blah�locationr�rMcSsg|]}|���qSr%)�lower)�.0�xr%r%r&�
<listcomp>�sz.HandlerTests.test_redirect.<locals>.<listcomp>zcontent-lengthzcontent-typec	Ss |�|t�ddtd|i��dS)Nrr�r�)rr�r�)r:rYrMr%r%r&r	�s
�z,HandlerTests.test_redirect.<locals>.redirectrd)r�rr[r�zhttp://example.com/%d)r,r�HTTPRedirectHandlerr}r��getattrrr�r�r�rSrBr�rXr�r�r�	HTTPErrorr�assertIsNotNonerYr��
get_method�AttributeErrorrUrrLrXrYZmax_repeatsZmax_redirections)r�from_urlZto_urlr:rr�rr�rYrLr	r�r%r%r&�
test_redirect�sl




���zHandlerTests.test_redirectcCs�d}dddg}dddg}d}tj��}t�}|_t|�}tj|_|D]6}|d	|}	|�	tj
j|j|t
�d
dtd|	i��qF|D]>}|d	|}
|�|t
�d
d
td|
i��|�|j��|
�q�dS)Nr�r��httpsr?�file�imapZldapzexample.com/b.htmlz://rzSecurity Loopholer��That's fine)r,rr�r}r�rr�r�r�r*rr�rr�r�rrYr�)rr�Z
valid_schemesZinvalid_schemesZschemeless_urlr:rrYr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�rZurljoinrr�r�rrYr�)rr�Zrelative_urlr:rrYr�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_netscape�http://www.example.com/z	spam=eggsrz%Location: http://www.cracker.com/

�Cookie)Zhttp.cookiejarr�Ztest.test_http_cookiejarr�r�r,r�HTTPDefaultErrorHandlerr�r�r�r�rUrYrR)	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_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�nextrr�Zfakedata)�connr�rM�pos�kw�Z
real_classrrKr%r&r#sz3HandlerTests.test_redirect_no_path.<locals>.requestzhttp://python.org/pathzhttp://python.org/path?query)
r�r�r�r�fakehttprfr��iterrr,r-rr�)rZ	response1rr�r%r�r&�test_redirect_no_paths
z"HandlerTests.test_redirect_no_pathc

Cs�Gdd�dtjj�}|�}tj�|�}d}|D]h\}}|�t|���Lt�d|d�|_|�	d�}d|d}|j
}	|�|	�|�t|	��W5QRXq.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��
connection�buf�last_bufrr�)rrY�resultr%r%r&r�0s
�z>HandlerTests.test_redirect_encoding.<locals>.Handler.http_openN)rErrFr�r%r%r%r&�Handler/sr�))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�rQr�)
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�_sz+HandlerTests.test_proxy.<locals>.<listcomp>)�dictrr,r�ProxyHandlerr�r�rrr�r�r�)rr!rar�phr�r�rYr%r%r&�
test_proxyOs
�

�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.orgr�r��r�environrr,rr�r�r�rrr�r��rrr�rYr%r%r&�test_proxy_no_proxyas



z HandlerTests.test_proxy_no_proxycCsfdtjd<t�}tj�tdd��}|�|�td�}|�	|j
d�|�|�|�	|j
d�tjd=dS)N�*r�r�r�r�r�r�r�r%r%r&�test_proxy_no_proxy_allps


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��sz1HandlerTests.test_proxy_https.<locals>.<listcomp>)rr,rr�r�r�r�rrr�r�r�)rrr�r�r�rYr%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�rrSrr�rVr�r�rXr�r�rAr�rW)rrr�Z
https_handlerrYr%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)rdzwww.example.org�example.netz192.168.0.2�!expected bypass of %s to be Falsezexample.com; <local>rdr�)�testr3z.expect <local> to bypass intranet address '%s')r	rQrU)rZproxy_overrideZproxy_bypassr�r%r%r&�test_winreg_proxy_bypass�s.������z%HandlerTests.test_winreg_proxy_bypass�darwinzonly relevant for OSXcCs�ddddddgd�}dD]}|�t||�d	|�qd
D]}|�t||�d|�q8dgd�}|�td
|��dddgd�}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)rQr
rU)rZbypassr�r%r%r&�test_osx_proxy_bypass�s4����
���z"HandlerTests.test_osx_proxy_bypasscCsz|j||d��bt�}t�}tj�|�}d�|�d}td|�}|�|�|�|�|�	||d|||dd�W5QRXdS)N)r�rLz
r��r��!http://acme.example.com/protected)
r�rr�r,rr�joinr�r��_test_basic_auth)rrLr�r��password_manager�auth_handlerr��http_handlerr%r%r&�check_basic_auth�s



�zHandlerTests.check_basic_authc

Csd}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}|�||�W5QRX||g||g||gfD]}	dd�|	D�}|�||�q�dS)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_strrLZ
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�2s
zZHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.__init__cSs|j�|�dSr~)rr�)rr�r%r%r&�record6szXHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.recordN)rErrFr�rr%r%r%r&�RecordingOpenerDirector1src@seZdZdd�ZdS)zOHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandlerc_s&|j�d�tjjj|f|�|�dS)Nr�)r�rr,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�rErrFrr%r%r%r&�TestDigestAuthHandler9src@seZdZdd�ZdS)zNHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandlerc_s&|j�d�tjjj|f|�|�dS)Nr�)r�rr,rrrrr%r%r&r@s��z]HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandler.http_error_401Nrr%r%r%r&�TestBasicAuthHandler?srr�r��&WWW-Authenticate: Basic realm="%s"

r�r�r�r�r�)rr,rrrr�r�r�r�rr)
rrrrr�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,rrr�r�r*r+r�)rr�Zdigest_auth_handlerr�r%r%r&�$test_unsupported_auth_digest_handlerZs�

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_handlerds�

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�)�base64rorr�rMrr]r�rrr�r�rUrR�bytesZencodebytesr��decoderWrYr�)rr�r�Zauth_headerr�r�r��request_urlZ
protected_urlrrr]ZuserpassZauth_hdr_valuer%r%r&r�ns6
���
zHandlerTests._test_basic_authcCs�d\}}d}td�}t�}t|�}|jd|||dd�|�|�|��|�|�|d��|�|�|d��t�}|�|�|�|�|�	|�|�|j
�dS)Nr
r�r�T)�is_authenticatedz/nestedZplain)r�rrrorQrrUrr�r�r�)rrr]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�)rrrorrUrr�r�r�rQr�r�)
rrr]rr�rrZis_authr�r�r%r%r&�.test_basic_prior_auth_send_after_first_success�s2


�






z;HandlerTests.test_basic_prior_auth_send_after_first_successc
Cs|dD]r\}}d�|�}t�|��|�}tj��}td�}d|_|�	||��}|�
�W5QRX|�|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: 4rzHTTP/1.1 200 OK
{}

�
http://dummy/NzConnection not closed with {!r})�formatrr�r!r,rr�rr�r�r3rQ�fakesock�closed)rZtransferr�headerr�rrYZrespr%r%r&�test_http_closed�s


�zHandlerTests.test_http_closedc	Cs\t�d�}tj��}td�}d|_|�tj	j
��|�||�W5QRX|�|j
jd�dS)z;Test the connection is cleaned up after an invalid responserSrNzConnection not closed)rr�r,rr�rr�r*r�r�Z
BadStatusLiner�rQrr)rr�rrYr%r%r&�test_invalid_closed�s

z HandlerTests.test_invalid_closedN)2rErrFr=rLr\rjrlrvryrzrr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r#�
skipUnlessrrr�ro�platformr�r�r�r�r
rrr�rrrrr%r%r%r&r&�sV@\B
J
	#

!-2

%(r&c@sZeZdZdd�Zdd�Ze�e�d�d�e�	dd�d	d
���Z
dd�Zd
d�Zdd�Z
dS)�	MiscTestscs"|�t�fdd�|jD���dS)Nc3s|]}|j�kVqdSr~)r�)r�r:��
handler_classr%r&�	<genexpr>�s�z/MiscTests.opener_has_handler.<locals>.<genexpr>)rQ�anyr�)rr�r$r%r#r&�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�sr(c@seZdZdd�ZdS)z/MiscTests.test_build_opener.<locals>.FooHandlercSsdSr~r%r�r%r%r&�foo_open�sz8MiscTests.test_build_opener.<locals>.FooHandler.foo_openN)rErrFr)r%r%r%r&�
FooHandler�sr*c@seZdZdd�ZdS)z/MiscTests.test_build_opener.<locals>.BarHandlercSsdSr~r%r�r%r%r&�bar_opensz8MiscTests.test_build_opener.<locals>.BarHandler.bar_openN)rErrFr+r%r%r%r&�
BarHandlersr,c@seZdZdS)z7MiscTests.test_build_opener.<locals>.MyOtherHTTPHandlerNr�r%r%r%r&�MyOtherHTTPHandlersr-)r,rr��BaseHandlerr�r')rr(r*r,r�rr-r%r%r&�test_build_opener�s,

zMiscTests.test_build_openerZnetworkztest requires network accessTz5POST request to http://www.example.com/ fail randomlyc	Cs�t�d���tj��}tj�d�}|�d|j�|�|d�	d��|�d|j�|�d|�
d��|�|d�	d��|�d|j�|�d|�
d��W5QRXdS)	Nr�rmzus-ascii�1r9Z
1234567890s
1234567890rt)rZtransient_internetr,rr�rrrr�r!rW)rr�rr%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?rLzHTTP Error %s: %sz<HTTPError %s: %r>)r,rr�rQr�rr?rLr�r�rBr�)rr�rMr�r�rZrCZexpected_errmsgr%r%r&�test_HTTPError_interface6sz"MiscTests.test_HTTPError_interfacecCsTddddddddddd	d
ddd
g}|D]\}}|�t|�|�q&|�ttd�fdS)N)r�)NNNr�)r�)NNNr�)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.comr3)z&http://user/name:password@localhost:22)r��	user/namer]�localhost:22)z&http://username:pass/word@localhost:22)r�Zusername�	pass/wordr5)z'http://user/name:pass/word@localhost:22)r�r4r6r5zfile:/ftp.example.com)rrr*r+)rZparse_proxy_test_casesZtcrr%r%r&�test_parse_proxyHs&�'zMiscTests.test_parse_proxyc	Cs<t�}|�t��}|�d�W5QRX|�t|j�d�dS)N�invalidz5Unsupported digest authentication algorithm 'invalid')rr*r+Zget_algorithm_implsrrB�	exception)rr�excr%r%r&�test_unsupported_algorithmus�z$MiscTests.test_unsupported_algorithmN)rErrFr'r/r#r rZis_resource_enabledZskipIfr1r2r7r;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)rErrFr�r%r%r%r&�
PutRequest�sr>cCsLtd�|_tddddid�|_tddd�|_|�d�|_|jddd�|_dS)	N�http://www.python.org/~jeremy/rzX-Testr�rK�HEAD)r�rN)rr<�post�headr>�put�
force_postr�r%r%r&�setUp�s
��zRequestTests.setUpcCs^|�d|j���|�d|j���|�d|j���|�d|j���|�d|j���dS)NrNrMr@r=)rrAr�r<rBrCrDr�r%r%r&�test_method�s
zRequestTests.test_methodcCsL|�|jj�|�d|j���d|j_|�|jj�|�d|j���dS)NrMrlrN)rUr<rrr�rQr�r%r%r&�	test_data�s
zRequestTests.test_datacCsN|�d|jj�|j�dd�|�d|jjd�d|j_|�d|jj�dS)Nr9�*rl)rXr<rYrXrrr�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)Nr9rer{)rXr<rYrrXrr�r%r%r&�/test_deleting_data_should_remove_content_length�sz<RequestTests.test_deleting_data_should_remove_content_lengthcCs|�d|j���dS)Nr?)rr<r�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()rr<r�rr�r%r%r&�
test_selector�szRequestTests.test_selectorcCs|�d|jj�dS)Nr�)rr<rJr�r%r%r&�
test_get_type�szRequestTests.test_get_typecCs|�d|jj�dS)Nr�)rr<r�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�)rUr<Z	has_proxyr�rQrr�r�r�r%r%r&r��s
zRequestTests.test_proxycCstd�}|�d|j�dS)Nz<URL:http://www.python.org>r�rOr�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�)rrYrMr%r%r&�test_url_fragment�szRequestTests.test_url_fragmentcCs2dddg}|D]}t|�}|�|��|j�qdS)Nzhttp://docs.python.orgrSrR)rrr�r�)rrKrMrYr%r%r&�test_url_fullurl_get_full_url�s�z*RequestTests.test_url_fullurl_get_full_urlN)rErrFrr>rErFrGrIrJrKrLrMrNrPr�rQrTrUr%r%r%r&r<s

r<�__main__)9r#r�rrrr�r�r~rordrqZurllib.requestr,rrrrrr	r
rZurllib.parserr
Zhttp.clientr�ZTestCaser
rGr}r�r�r�r�r�r�r��IOBaser�r�r�r�r�rr.r�r�r�r�r�rr%r&r"r<rE�mainr%r%r%r&�<module>sb(1<

-6"
/a


F1le Man4ger