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.10/test/__pycache__/test_urllib2.cpython-310.opt-1.pyc
o

�i�;�@s8ddlZddlmZddlmZddlmZddlmZddlmZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlmZmZmZmZmZmZmZmZddlmZddlZddlZGd	d
�d
ej�ZGdd�dej�Z Gd
d�d�Z!Gdd�d�Z"Gdd�de#�Z$Gdd�de	j%�Z&Gdd�d�Z'Gdd�d�Z(Gdd�de	j)�Z*Gdd�d�Z+Gdd�d�Z,dd �Z-d!d"�Z.Gd#d$�d$ej/j0�Z1Gd%d&�d&ej/j2�Z3Gd'd(�d(ej/j0�Z4Gd)d*�d*�Z5Gd+d,�d,ej�Z6d-d.�Z7Gd/d0�d0ej�Z8Gd1d2�d2ej�Z9Gd3d4�d4ej�Z:e;d5k�re�<�dSdS)6�N)�support)�	os_helper)�
socket_helper)�warnings_helper)�test_urllib)�Request�OpenerDirector�HTTPBasicAuthHandler�HTTPPasswordMgrWithPriorAuth�_parse_proxy�_proxy_bypass_winreg_override�_proxy_bypass_macosx_sysconf�AbstractDigestAuthHandler)�urlparsec@�,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�TrivialTestsc
Cs�dD]I}i}td||�|d=|dkr2tjdkr2|�d�|�d�}}|�|jd�|�|jd�|��D]\}}|�|jd	|d
|||jf�q6qdS)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)�./usr/local/lib/python3.10/test/test_urllib2.py�test___all__$s 
����zTrivialTests.test___all__cCs�|�tjj�|�ttjjd�tj�	tjj
��tjd�}tj
dkr'd|}nd|}tj�|��
}|��Wd�dS1sAwYdS)Nz	bogus url�/rz
file:///%s�	file://%s)�
addCleanup�urllibr�
urlcleanup�assertRaises�
ValueError�urlopenr�path�abspath�__file__�replace�sepr�read)r"ZfnameZfile_url�fr)r)r*�test_trivial3s


"�zTrivialTests.test_trivialcCsRdgd�fdddgfdgd�fdgd	�fg}|D]\}}|�tj�|�|�qdS)
Nza,b,c)�a�b�czpath"o,l"og"i"cal, examplezpath"o,l"og"i"calZexampleza, b, "c", "d", "e,f", g, h)r<r=z"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_listFs

����z!TrivialTests.test_parse_http_listcCs"tj�d�}|�|jt|��dS)N�reason)r/r�URLError�assertInrE�str)r"�errr)r)r*�test_URLError_reasonstrQsz$TrivialTests.test_URLError_reasonstrN)�__name__r �__qualname__r+r;rDrJr)r)r)r*r"s
rc@r)
�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)rrrR)r"�urlr)r)r*�test_request_headers_dictXs
������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
        rNrOrPrQ)rOrPzFoo-Bar�baz)zFoo-barrUz	Not-there�defaultzUnredirected-spamZEggsN)r�
assertTrue�
has_headerrZheader_items�
add_header�sorted�assertFalse�assertIsNone�
get_headerZ
remove_header�add_unredirected_header)r"rS�reqr)r)r*�test_request_headers_methodsms"�

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�3r>z
d.example.com�4�dze.example.com:3128�5�e�example.com)rbrcrNzhttp://example.com/spamzhttp://example.com/spam/spamzhttp://example.com/foo�foozhttp://example.com/bar�bar�ninizhttp://example.com/foo/barZfoobarZnibar)rkrd)rlrmzhttp://example.com/foo/zhttp://example.com/foo/bazzhttp://example.com/foobar�NNzhttp://example.com/baz/rU�nininizhttp://example.com/baz)rUrozhttp://example.com/baz/barr=�firstrP�second�spam)rqrrr<�1)rsr<zhttp://a.example.com/z
c.example.comzc.example.com:3128)rer>)rfrg)rhri�r/rZHTTPPasswordMgr�add_password�find_user_passwordr�r"Zmgr�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.
        r:zhttp://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:100rnzg.example.com:80)rzr{z
g.example.comzhttp://g.example.com:100zhttp://g.example.comz
h.example.com)r|r'zh.example.com:80zhttp://h.example.com:80z
i.example.com)r}r~zhttp://i.example.com:80)r�r�zj.example.com:80zhttp://j.example.comzhttp://j.example.com:80Nrtrwr)r)r*�"test_password_manager_default_port�sZ
�����������z3RequestHdrsTests.test_password_manager_default_portN)rKr rLrTr`ryr�r)r)r)r*rMVs
$VrMc@s(eZdZgZdejfdd�Zdd�ZdS)�
MockOpenerNcCs||||_|_|_dS�N)r_�data�timeout)r"r_r�r�r)r)r*�openszMockOpener.opencGs|||_|_dSr�)�proto�args)r"r�r�r)r)r*r�zMockOpener.error)rKr rL�
addheaders�socket�_GLOBAL_DEFAULT_TIMEOUTr�rr)r)r)r*r�sr�c@s(eZdZddd�Zddd�Zdd�ZdS)	�MockFileNcC�dSr�r)�r"�countr)r)r*r9�z
MockFile.readcCr�r�r)r�r)r)r*�readliner�zMockFile.readlinecCr�r�r)�r"r)r)r*�close"r�zMockFile.closer�)rKr rLr9r�r�r)r)r)r*r�s

r�c@�eZdZdd�ZdS)�MockHeaderscCst|���Sr�)rC�values)r"rr)r)r*�
getheaders'szMockHeaders.getheadersN)rKr rLr�r)r)r)r*r�&�r�c@s&eZdZddd�Zdd�Zdd�ZdS)	�MockResponseNcCs.tj�||�||||f\|_|_|_|_dSr�)�io�StringIO�__init__�code�msgrRrS)r"r�r�rRr�rSr)r)r*r�,s zMockResponse.__init__cC�|jSr�rQr�r)r)r*�info0�zMockResponse.infocCr�r��rSr�r)r)r*�geturl3r�zMockResponse.geturlr�)rKr rLr�r�r�r)r)r)r*r�+s
r�c@�eZdZdd�Zdd�ZdS)�
MockCookieJarcC�
||_dSr�)�ach_req)r"rr)r)r*�add_cookie_header8�
zMockCookieJar.add_cookie_headercCs|||_|_dSr�)�ec_req�ec_r)r"rrr)r)r*�extract_cookies;r�zMockCookieJar.extract_cookiesN)rKr rLr�r�r)r)r)r*r�7sr�c@r�)�
FakeMethodcCs||_||_||_dSr�)�	meth_name�handle�action)r"r�r�r�r)r)r*r�@s
zFakeMethod.__init__cGs|j|j|jg|�R�Sr�)r�r�r�)r"r�r)r)r*�__call__EszFakeMethod.__call__N)rKr rLr�r�r)r)r)r*r�?sr�c@r)
�MockHTTPResponsecCs"||_||_||_||_d|_dS)N��)�fpr��statusrEr�)r"r�r�r�rEr)r)r*r�Js

zMockHTTPResponse.__init__cCsdS)N�r)r�r)r)r*r9Qr�zMockHTTPResponse.readcCsiSr�r)r�r)r)r*r�Tr�zMockHTTPResponse.infocCr�r�r�r�r)r)r*r�Wr�zMockHTTPResponse.geturlN)rKr rLr�r9r�r�r)r)r)r*r�Is
r�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�\s
zMockHTTPClass.__init__cCs||_||_|Sr�)�hostr�)r"r�r�r)r)r*r�dszMockHTTPClass.__call__cCr�r�)r�)r"r�r)r)r*�set_debuglevelir�zMockHTTPClass.set_debuglevelNcCs(||_||_|r
||_dS|j��dSr�)�_tunnel_hostZ_tunnel_portr��clear)r"r��portrRr)r)r*�
set_tunnells

zMockHTTPClass.set_tunnelF)�encode_chunkedcCsP||_||_|dur|j|��7_|j��|r||_||_|jr&t��dSr�)	�method�selectorr�r!�sortr�r�r��OSError)r"r�rS�bodyrRr�r)r)r*rts
�zMockHTTPClass.requestcCstt�idd�S)Nr��OK)r�r�r�r)r)r*�getresponse��zMockHTTPClass.getresponsecCr�r�r)r�r)r)r*r��r�zMockHTTPClass.closern)rKr rLr�r�r�r�r�r�rr�r�r)r)r)r*r�[s
�
r�c@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�MockHandler��cCs|�|�dSr�)�_define_methods)r"�methodsr)r)r*r���zMockHandler.__init__cCsJ|D] }t|�dkr|\}}n|d}}t|||j�}t|j||�qdS)N�)�lenr�r��setattr�	__class__)r"r��specrr��methr)r)r*r��s

�zMockHandler._define_methodscOs�|jj�||||f�|durdS|dkr|S|dkr$tddid�}|S|dkr,td�S|�d�r`||�d	�d
d�}zt|�}Wn	tyKYnwtddid�}|j�	d|d||di�S|d
krjt
j	�d��dS)N�return self�return responser�r�r��return requestzhttp://blah/r� ��httpr�raiserP)�parent�calls�appendr�r�
startswith�rfind�intr2rr/rF)r"Zfn_namer�r��kwds�resr�r)r)r*r��s,
��zMockHandler.handlecCr�r�r)r�r)r)r*r��r�zMockHandler.closecCs||_g|j_dSr�)r�r�)r"r�r)r)r*�
add_parent�szMockHandler.add_parentcCst|d�sdS|j|jkS)N�
handler_orderT)�hasattrr�)r"�otherr)r)r*�__lt__�s
zMockHandler.__lt__N)
rKr rLr�r�r�r�r�r�r�r)r)r)r*r��s	r�cCsbg}d}|D](}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@�eZdZdS)z6add_ordered_mock_handlers.<locals>.MockHandlerSubclassN�rKr rLr)r)r)r*�MockHandlerSubclass��r�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@�$eZdZdd�Zdd�Zdd�ZdS)�MockHTTPHandlercCs||_||_|��dSr�)r�rR�reset)r"r�rRr)r)r*r���zMockHTTPHandler.__init__cCsd|_g|_dS)Nr)�_count�requestsr�r)r)r*r���
zMockHTTPHandler.resetcCs�ddl}ddl}|j�|�|��|jdkr6|jd|_tjj|j	}|�
|j�}|j�
d|t�|j	||�S||_|�
d�}tdd|d|���S)Nrr�r��

r�r�r�)�email�copyr�r��deepcopyr�r��client�	responsesr�Zmessage_from_stringrRr�rr�r_r��get_full_url)r"r_rrrr�r)r)r*�	http_open�s
�
zMockHTTPHandler.http_openN�rKr rLr�r�rr)r)r)r*r��sr�c@seZdZddd�Zdd�ZdS)�MockHTTPSHandlerrcCstjjj||d�t�|_dS)N��
debuglevel)r/r�AbstractHTTPHandlerr�r��httpconn)r"r
r)r)r*r�szMockHTTPSHandler.__init__cCs|�|j|�Sr�)�do_openr�r"r_r)r)r*�
https_openr�zMockHTTPSHandler.https_openN)r)rKr rLr�rr)r)r)r*rs
rc@r�)�MockHTTPHandlerCheckAuthcCs||_d|_dS�NF)r��has_auth_header)r"r�r)r)r*r�r�z!MockHTTPHandlerCheckAuth.__init__cCs
d|_dSr)rr�r)r)r*r�r�zMockHTTPHandlerCheckAuth.resetcCs6|�d�rd|_tjj|j}t|j|t�d|���S)N�
AuthorizationTr�)	rXrr�rrr�r�r�r)r"r_rr)r)r*rs
z"MockHTTPHandlerCheckAuth.http_openNrr)r)r)r*rsrc@r�)�MockPasswordManagercCs||_||_||_||_dSr�)�realmrS�userrc)r"rZurirrcr)r)r*ru#s
z MockPasswordManager.add_passwordcCs||_||_|j|jfSr�)�target_realm�
target_urlrrc)r"rZauthurir)r)r*rv)r�z&MockPasswordManager.find_user_passwordN)rKr rLrurvr)r)r)r*r"src@sDeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dS)�OpenerDirectorTestscCs(Gdd�dt�}|�tt�j|��dS)Nc@r�)z<OpenerDirectorTests.test_add_non_handler.<locals>.NonHandlerNr�r)r)r)r*�
NonHandler2r�r)�objectr1�	TypeErrorrr�)r"rr)r)r*�test_add_non_handler1s
�z(OpenerDirectorTests.test_add_non_handlercCs\ddlm}t�}ddgdgg}t||�|�tj���dD]}|�||j	|d�qdS)Nr)rF)r
r�)Z
proxy_openr�)Zredirect_requestr�)Zdo�proxy�redirectz://example.com/)
�urllib.errorrFrr�r�r/rZUnknownHandlerr1r�)r"rF�or��schemer)r)r*�test_badly_named_methods7s	�
�z,OpenerDirectorTests.test_badly_named_methodsc
Cs�t�}gd�dgdgdgg}t||�}td�}|�|�}|�||d�|ddf|ddfg}t||j�D]\}}|\}	}
}}|�|	|
f|�|�||f�q6dS)N)r�ftp_open�http_error_302r$�rr�rar�rr)rr�rr�r�zipr�)
r"r!r�r�r_�rr��expected�got�handlerrr�r�r)r)r*�test_handledLs �

�z OpenerDirectorTests.test_handledcCs�t�}g}dgdfdgdffD]\}}Gdd�dt�}||�}||_|�|�|�|�q|�d�|�|jdd|d�|�|jdd|d�dS)	Nr&r�rrc@r�)zCOpenerDirectorTests.test_handler_order.<locals>.MockHandlerSubclassNr�r)r)r)r*r�kr�r�rar�)rr�r�r�r�r�rr�)r"r!r�r�r�r�r@r)r)r*�test_handler_orderfs�

z&OpenerDirectorTests.test_handler_ordercCsZt�}dgdgg}t||�}td�}|�tjj|j|�|�|j	|dd|fifg�dS)N)rr�r&rarr)
rr�rr1r/rrFr�rr�)r"r!r�r�r_r)r)r*�
test_raisexs�
"zOpenerDirectorTests.test_raisecCs�t�}dgddggd�dgg}t||�}td�}|�|�|dd|ff|dd|tjd	d
iffg}t||j�D]\}}|\}}	}
|�||	f|dd��|�|
|d�q8dS)N)rz	error 302)Zhttp_error_400r�r))r%r�Zhttp_error_303Z
http_errorr%rarr��.r�)	rr�rr�rZ	ALWAYS_EQr'r�r)r"r!r�r�r_r�r)r*r+Zmethod_namer�r)r)r*�test_http_error�s&�

��
�z#OpenerDirectorTests.test_http_errorcCst�}ddgddgg}t||�}td�}|�|�|ddf|ddf|ddf|ddfg}t|j�D]O\}\}}}	}
|dkr[|�||f||�|�t|	�d�|�|	dt�q4|�||f||�|�t|	�d�|�|	dt�|	ddur�|�|	dt	�q4dS)	N)�http_requestr�)�
http_responser�rarr1r�r2r�)
rr�rr��	enumerater�rr��assertIsInstancer�)r"r!r�r�r_r�rr+rr�r�r)r)r*�test_processors�s4���

���z#OpenerDirectorTests.test_processorsN)
rKr rLrr#r,r-r.r0r5r)r)r)r*r/s
rcCsXz|�d�Wntyt�d��wtj�|�}tjdkr*|�	d�r*|dd�}|S)Nzutf-8zpath is not encodable to utf8rz///r�)
�encode�UnicodeEncodeError�unittestZSkipTestr/rrrrr�)r4�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]f\	}}}}}	}
}}}
t|�}d|_|�	|�}|�
|j|�|�
|j|	�|�
|j
t�|��|�
|j|�|�
|j|�|�
|jj|�|�
|jj|
�|��}|�
|�d�|
�|�
t|d�t|��qndS)Nc@r�)z-HandlerTests.test_ftp.<locals>.MockFTPWrappercSr�r��r��r"r�r)r)r*r��r�z6HandlerTests.test_ftp.<locals>.MockFTPWrapper.__init__cSs$|||_|_t�|j�t|j�fSr�)�filename�filetyper�r�r�r�)r"r>r?r)r)r*�retrfile�sz6HandlerTests.test_ftp.<locals>.MockFTPWrapper.retrfilecSr�r�r)r�r)r)r*r��r�z3HandlerTests.test_ftp.<locals>.MockFTPWrapper.closeN)rKr rLr�r@r�r)r)r)r*�MockFTPWrapper�srAcs&eZdZdd�Zejf�fdd�	ZdS)z-HandlerTests.test_ftp.<locals>.NullFTPHandlercSr�r�r<r=r)r)r*r��r�z6HandlerTests.test_ftp.<locals>.NullFTPHandler.__init__cs4|||_|_|||_|_||_�|j�|_|jSr�)r�passwdr�r��dirsr��
ftpwrapper)r"rrBr�r�rCr��rAr)r*�connect_ftp�s
z9HandlerTests.test_ftp.<locals>.NullFTPHandler.connect_ftpN)rKr rLr�r�r�rFr)rEr)r*�NullFTPHandler�s�rGrzrheum rhaponicumz ftp://localhost/foo/bar/baz.html�	localhostr��Irkrlzbaz.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�r$rrrBr�r��
gethostbynamer�rCrDr>r?r��getr�r�)r"rGrOr�r@rSr�r�rrBZtype_rCr>Zmimetyper_r(rRr)rEr*�test_ftp�sZ
�
�
�
�

���
�zHandlerTests.test_ftpc
Cs�ddl}tj��}t�}|_tj}tt	j
�|��}d}d|d|dt�
d�|fg}z	t�
t���}WntjyAd}Ynw|rM|�d||f�|D]w}	t|d�}
zEz|
�|�W|
��n|
��w|�t|	��}z|��}|��}
|��}W|��n|��wt	�|�}|jj|jd	d
�}Wt	�|�nt	�|�w|�||�|�|
dd�|�|
d
d�|�|
d|�|�||	�qOd|dddt�
d�t	��|fdt	��|ffD]1}	z)t|d�}
z|
�|�W|
��n|
��w|� tj!j"|jt|	��Wt	�|�q�t	�|�wtj��}t�}|_dD]9\}	}t|	�}z|�|�Wntj!j"�y?|�#|�Ynw|�$|j%|�|�|j&d�|�|j&dk|��qdS)Nrs
hello, world
zfile://localhost%sr-zfile://%s%srHr��wbT)ZusegmtrMz
text/plainrNr�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/r�FileHandlerr�r�r�TESTFNr:rr4r5r�rP�gethostname�gaierrorr�r��writer�Z	file_openrr9r�r��statZutilsZ
formatdate�st_mtime�remover�getcwdr1rrFr[�assertIsr_�type)r"rr@r!rVr9Ztowrite�urlsZ	localaddrrSr:r(r�rRZrespurlZstatsZmodifiedrTr_r)r)r*�	test_files�
��



�
��	


�
��zHandlerTests.test_filecCs`tj��}t�}|_d}dD]q\}}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�}|�|�}
|dur�|�!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�q�dS)Nra))�GETN)�POSTsblah�Foorl�Spam�eggsr�r�rjrr,))�
Connectionr�)rdrl�rerfTZbadpostrh)�Nr�rNrM�0z!application/x-www-form-urlencoded�HostrkrU)#r/rrr�r�rr�r^r�r
r9r�r�r�r�r�rQ�__contains__rr�r�r�r�r�r�r�r1rrFr�do_request_r�r��assertNotIn�unredirected_hdrs)r"r@r!rSr�r�r_r�r(�hdrs�newreqr)r)r*�	test_httpjsf
�


�
�zHandlerTests.test_httpc	Cstj��}t�}|_tjddd�}|j}|��|�	t
j|�t|d��&}t
d|i�}|�|�}|�d�}|�|d�|�|�d��Wd�n1sNwYt|d��)}t
d|d	d
i�}|�|�}|�t|�d��d
�|�|�d��Wd�dS1s�wYdS)Nzw+bF)�mode�delete�rbra�Transfer-encoding�chunkedrN�Content-Length�)r/rrr�r��tempfile�NamedTemporaryFilerr�r.r�unlinkr�rrmr]rr[rXr�)	r"r@r!�file_objZ	file_pathr:r_rq�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)NrarvrwrNrxry)r/rrr�r�r��BytesIOrrmrr]r[rXr�r�)r"r@r!r}r_rqrRr)r)r*�test_http_body_fileobj�s


z#HandlerTests.test_http_body_fileobjc	Cs�tj��}t�}|_tjddg}iddifD]F}tj|tj	d��4}t
d|j|�}|�|�}|sC|�
|�d�d�|�
|�d�d	�n|�
t|�d��d�Wd�n1sXwYqdS)
Nz-c�passrxry)�stdoutrarNrvrw)r/rrr�r��sys�
executable�
subprocess�Popen�PIPErr�rmrr]r�)r"r@r!�cmdrR�procr_rqr)r)r*�test_http_body_pipe�s&

������z HandlerTests.test_http_body_pipecCs�tj��}t�}|_dd�}iddifD].}td|�|�}|�|�}|s8|�|�d�d�|�|�d�d�q|�t	|�d��d�qdS)	Ncss�dVdS)Nsoner)r)r)r)r*�
iterable_body�s�
z;HandlerTests.test_http_body_iterable.<locals>.iterable_bodyrx�rarNrvrw)
r/rrr�r�rrmrr]r�)r"r@r!r�rRr_rqr)r)r*�test_http_body_iterable�s

��z$HandlerTests.test_http_body_iterablecCsHtj��}t�|_|�tdd��}|�|�d�d�|�	|�
d��dS)Nrar)rvrwrN)r/rrr�r�rmrrr]r[rX)r"r@r_r)r)r*�test_http_body_empty_seqs

z%HandlerTests.test_http_body_empty_seqcCshtj��}t�}|_t�dgd��}iddifD]}td||�}|�|�}|�t	|�
d��d�qdS)NrI)r�r���rx�rarN)r/rrr�r��arrayrrmrr�r])r"r@r!Ziterable_arrayrRr_rqr)r)r*�test_http_body_arrays

�z!HandlerTests.test_http_body_arraycCs6t�}tdd�}|�|�|�d�|�|jd�dS)Nr�r	zhttps://www.example.com)rrr�r�rZ_debuglevel)r"r!r@r)r)r*�test_http_handler_debuglevels



z)HandlerTests.test_http_handler_debuglevelcCsztj��}t�|_d}gd�}|D])}t||�}|�|�}|�|jdd�|�	dd�|�|�}|�|jdd�qdS)Nri)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.htmlrkrjzsomeproxy:3128)
r/rrr�r�rrmrro�	set_proxy)r"r@r�Zds_urlsZds_urlZds_reqZ	np_ds_reqZp_ds_reqr)r)r*�test_http_doubleslash"s



�z"HandlerTests.test_http_doubleslashcCshgd�}td�}|D]'}||_t|�}|�|��|�|�|jp!d|j�|�t|���j|j�q
dS)N)zhttp://example.com?foo=bar#bazz)http://example.com?foo=bar&spam=eggs#bashrNrNr�)r�full_urlrrr�fragment�query)r"r`r(rS�parsedr)r)r*�test_full_url_setter=s�z!HandlerTests.test_full_url_settercCs6td�}|`|�|j�|�|j�|�|jd�dS)N�http://www.example.comr�)rr�r\r�rr�)r"r(r)r)r*�test_full_url_deleterTs
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/rrr�r�rrmrr�r�)r"r@Z	weird_urlr_rqZurl_without_pathr)r)r*�test_fixpath_in_weirdurls[s


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)
Nrar�r�r�r���ZAccepted��zPartial contenti�zBad gatewayr�)r/rZHTTPErrorProcessorr�r�rr�r2r^r[r�r\rr�r�)r"r@r!rSr_r(�newrr)r)r*�test_errorsns(
zHandlerTests.test_errorscCs�t�}tj�|�}t�|_td�}tddid�}|�|�}|�	|j
|�|�	|j
|�|�|jd�|�
|j�|�||�}|�	|j|�|�	|j|�|�	||�dS)Nrar�r�r�rj)r�r/r�HTTPCookieProcessorr�r�rr�r1r^r�r�origin_req_hostr[Zunverifiabler2r�r�)r"�cjr@r_r(rqr�r)r)r*�test_cookies�s
zHandlerTests.test_cookiesc
CsTd}d}tj��}t�}|_dD]�}dD]�}t|d|�}t||�}tj|_	|�
dd�|dur;|�
dtt|���|�
d	d
�z||t�|dtd|i��Wntjjyf|�|d
�|�|�Ynw|�|j��|�z|�|j��d�Wnty�|�|jj�Ynwdd�|jjD�}	|�d|	�|�d|	�|�|jjdd�|�d	|jj�|�d	|jj�qqt|�}tj|_	|fdd�}
t|dd�}d}tj|_	z	|
||d�|d}q�tjjy�|�|tjjj�Ynwt|dd�}d}tj|_	z	|
||d|�|d}�qtjj�y)|�|tjjj�YdSw)N�http://example.com/a.htmlzhttp://example.com/b.html)i-r/i/�3)Nz
blah
blah
z
http_error_%sZNonsensezviking=withholdrxrerr�Blah�locationr�rbcSsg|]}|���qSr))�lower)�.0�xr)r)r*�
<listcomp>�sz.HandlerTests.test_redirect.<locals>.<listcomp>zcontent-lengthzcontent-typec	Ss |�|t�ddtd|i��dS)Nr/r�r�)r%r�r�)r@r_rSr)r)r*r�s
�z,HandlerTests.test_redirect.<locals>.redirectrj)r�rr�razhttp://example.com/%d)r/r�HTTPRedirectHandlerr�r��getattrrr�r�r�rYrHr�r^r�r�r�	HTTPErrorr�assertIsNotNoner_r�
get_method�AttributeErrorr[r�rRrnroZmax_repeatsZmax_redirections)r"�from_urlZto_urlr@r!r�r�r�r_rRrr�r)r)r*�
test_redirect�s~


�����!���
��zHandlerTests.test_redirectcCs�d}gd�}gd�}d}tj��}t�}|_t|�}tj|_|D]}|d|}	|�	tj
j|j|t
�ddtd|	i��q!|D]}|d|}
|�|t
�dd	td|
i��|�|j��|
�q?dS)
Nr�)r��httpsrT)�file�imapZldapzexample.com/b.htmlz://r/zSecurity Loopholer��That's fine)r/rr�r�r�rr�r�r�r1rr�r%r�r�rr_r)r"r�Z
valid_schemesZinvalid_schemesZschemeless_urlr@r!r_r"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.htmlr/r�r�)r/rr�r�r�rr�r�r�r�urljoinr%r�r�rr_r)r"r�Zrelative_urlr@r!r_r�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=eggsr/z%Location: http://www.cracker.com/

�Cookie)Zhttp.cookiejarr�Ztest.test_http_cookiejarr�r�r/r�HTTPDefaultErrorHandlerr�r�r�r�r[r_rX)	r"r�r�r��hh�hdeh�hrhZcpr!r)r)r*�test_cookie_redirects


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

r/z
Location: r�)
r�r/rr�r�r�r�rr��strip)r"Zredirected_urlr�r�r�r!r�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�rS�pos�kw�Z
real_classr"r`r)r*r(sz3HandlerTests.test_redirect_no_path.<locals>.requestzhttp://python.org/pathzhttp://python.org/path?query)r.r/rr0r�rr�r�fakehttpr��iterr3rr�)r"Z	response1rr�r)r�r*�test_redirect_no_paths
z"HandlerTests.test_redirect_no_pathc
	Cs�Gdd�dtjj�}|�}tj�|�}d}|D]>\}}|�t|���+t�d|d�|_|�	d�}d|d}|j
}	|�|	�|�t|	��Wd�n1sPwYqdS)	Nc@r�)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�)r"r_�resultr)r)r*r5s
�z>HandlerTests.test_redirect_encoding.<locals>.Handler.http_openN)rKr rLrr)r)r)r*�Handler4r�r�))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

rasGET � )
r/r�HTTPHandler�build_opener�subTest�reprrr�r�r�r�rWr�)
r"r�r+r�rAr�r�rr)rr)r)r*�test_redirect_encoding1s(���
���z#HandlerTests.test_redirect_encodingcCs�d}t|d�t|d�fD]A}t�}tj�|�}|�|�dgg}t||�}td�}|�|j	d�|�
|�|�|j	|�|�|ddfgd	d
�|jD��qdS)N�proxy.example.com:3128�r�)ZHTTP)rr�zhttp://acme.example.com/zacme.example.comrrcS�g|]}|dd��qS�rr�r)�r��tupr)r)r*r�d�z+HandlerTests.test_proxy.<locals>.<listcomp>)�dictrr/r�ProxyHandlerr�r�rrr�r�r�)r"r%rgr!�phr�r�r_r)r)r*�
test_proxyTs 
�

��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��r"r!r�r_r)r)r*�test_proxy_no_proxyfs



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_allus


z$HandlerTests.test_proxy_no_proxy_allcCs�t�}tj�tdd��}|�|�dgg}t||�}td�}|�|j	d�|�
|�|�|j	d�|�|ddfgdd	�|jD��dS)
Nr��r�)rr��https://www.example.com/�www.example.comrrcSr�r�r)r�r)r)r*r��r�z1HandlerTests.test_proxy_https.<locals>.<listcomp>)rr/rr�r�r�r�rrr�r�r�)r"r!r�r�r�r_r)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�rrrYrr�r\r�r�rnrr�rGr�r])r"r!r�Z
https_handlerr_r)r)r*�$test_proxy_https_proxy_authorization�s(


��z1HandlerTests.test_proxy_https_proxy_authorizationrzonly relevant for WindowscCs�d}t}dD]
}|�|||�d|�qdD]
}|�|||�d|�qd}|�|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)rjzwww.example.org�example.netz192.168.0.2�!expected bypass of %s to be Falsezexample.com; <local>rjr�)�testrHz.expect <local> to bypass intranet address '%s')rrWr[)r"Zproxy_overrideZproxy_bypassr�r)r)r*�test_winreg_proxy_bypass�s0�������z%HandlerTests.test_winreg_proxy_bypass�darwinzonly relevant for OSXcCs�dgd�d�}dD]
}|�t||�d|�q	dD]
}|�t||�d|�qdgd�}|�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)rzwww.bar.comrz	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.1ZnotinbypassrTrz10.0.0.0/40z172.19.10.0/24z172.19.10.5z10.0.1.5)rWr
r[)r"Zbypassr�r)r)r*�test_osx_proxy_bypass�s0���
���z"HandlerTests.test_osx_proxy_bypasscCs�|j||d��7t�}t�}tj�|�}d�|�d}td|�}|�|�|�|�|�	||d|||dd�Wd�dS1sAwYdS)N)rrRz
r��r�!http://acme.example.com/protected)
r�rrr/rr	�joinr�r��_test_basic_auth)r"rRrr��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}|�||�qGt�dtf��d
|��g}|�||�Wd�n1stwY||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)rr))r�Z	challenger)r)r*r�s�z0HandlerTests.test_basic_auth.<locals>.<listcomp>)rrZcheck_warnings�UserWarning)
r"rZrealm2�basicZbasic2Zother_no_realm�digestZ	realm_strrRZ
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�rZProxyBasicAuthHandlerr�r)r"r�r�rr
rrr)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@r�)zQHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirectorcSst�|�g|_dSr�)rr��recordedr�r)r)r*r�7s

zZHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.__init__cSs|j�|�dSr�)rr�)r"r�r)r)r*�record;r�zXHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.recordN)rKr rLr�rr)r)r)r*�RecordingOpenerDirector6src@r�)zOHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandlerc_�,|j�d�tjjj|g|�Ri|��dS)Nr)r�rr/r�HTTPDigestAuthHandler�http_error_401�r"r�r�r)r)r*r?��
�z^HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandler.http_error_401N�rKr rLrr)r)r)r*�TestDigestAuthHandler>r�r"c@r�)zNHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandlerc_r)Nr)r�rr/rr	rrr)r)r*rEr z]HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandler.http_error_401Nr!r)r)r)r*�TestBasicAuthHandlerDr�r#rr�&WWW-Authenticate: Basic realm="%s"

rr	rrr�)rr/rrr	rr�r�rrr)
r"rr"r#r�rZdigest_handlerZ
basic_handlerrrr)r)r*�#test_basic_and_digest_auth_handlers-s(	�



�z0HandlerTests.test_basic_and_digest_auth_handlerscC�Dt�}tj�d�}tdd�}|�|�|�|�|�t|jd�dS)NrzWWW-Authenticate: Kerberos

r�)	rr/rrr�r�r1r2r�)r"r�Zdigest_auth_handlerrr)r)r*�$test_unsupported_auth_digest_handler_s�

z1HandlerTests.test_unsupported_auth_digest_handlercCr&)NrzWWW-Authenticate: NTLM

r�)	rr/rr	r�r�r1r2r�)r"r�Zbasic_auth_handlerrr)r)r*�#test_unsupported_auth_basic_handleris�

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�)�base64rurrrSrrcr�rrr�r�r[rX�bytesZencodebytesr��decoder]ror�)r"r�r
Zauth_headerrrr�request_urlZ
protected_urlr+rrcZuserpassZauth_hdr_valuer)r)r*rss6
���
zHandlerTests._test_basic_authcCs�d\}}d}td�}t�}t|�}|jd|||dd�|�|�|��|�|�|d��|�|�|d��t�}|�|�|�|�|�	|�|�|j
�dS)Nr)r	r�T)�is_authenticatedz/nestedZplain)rr
r	rurWr/r[rr�r�r)r"rrcr.r�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	ZACMErr$r�)r
r	rur/r[rr�r�r�rWrr)
r"rrcr.rr0r1Zis_authr�rr)r)r*�.test_basic_prior_auth_send_after_first_success�s0


�






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

�
http://dummy/NzConnection not closed with {!r})�formatrr�r6r/rrrr�r
r9rW�fakesock�closed)r"Ztransferr��headerr�r+r_Zrespr)r)r*�test_http_closed�s


�
��zHandlerTests.test_http_closedcCspt�d�}tj��}td�}d|_|�tj	j
��|�||�Wd�n1s)wY|�|j
jd�dS)z;Test the connection is cleaned up after an invalid responserir5NzConnection not closed)rr�r/rrrr�r1r�rZ
BadStatusLiner
rWr7r8)r"r�r+r_r)r)r*�test_invalid_closed�s

�z HandlerTests.test_invalid_closedN)2rKr rLrRrarrrr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r8Z
skipUnlessrrrr��platformrrrrr%r'r(rr2r3r:r;r)r)r)r*r;�sX@\B
J
	#

!-2

%(r;c@s<eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)�	MiscTestscs"|�t�fdd�|jD���dS)Nc3s�|]}|j�kVqdSr�)r�)r�r@��
handler_classr)r*�	<genexpr>�s��z/MiscTests.opener_has_handler.<locals>.<genexpr>)rW�anyr�)r"r�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@r�)z2MiscTests.test_build_opener.<locals>.MyHTTPHandlerNr�r)r)r)r*�
MyHTTPHandler�r�rCc@r�)z/MiscTests.test_build_opener.<locals>.FooHandlercSr�r�r)r�r)r)r*�foo_openr�z8MiscTests.test_build_opener.<locals>.FooHandler.foo_openN)rKr rLrDr)r)r)r*�
FooHandlerr�rEc@r�)z/MiscTests.test_build_opener.<locals>.BarHandlercSr�r�r)r�r)r)r*�bar_openr�z8MiscTests.test_build_opener.<locals>.BarHandler.bar_openN)rKr rLrFr)r)r)r*�
BarHandlerr�rGc@r�)z7MiscTests.test_build_opener.<locals>.MyOtherHTTPHandlerNr�r)r)r)r*�MyOtherHTTPHandler"r�rH)r/rr��BaseHandlerr�rB)r"rCrErGr�r!rHr)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: 42rErRzHTTP Error %s: %sz<HTTPError %s: %r>)r/rr�rWr�rrErRr�r�rHr�)r"r�rSr�r�rprIZexpected_errmsgr)r)r*�test_HTTPError_interface)sz"MiscTests.test_HTTPError_interfacecCs>tj�ddddd�}|�t|dd�d�|�|j��t�dS)NrSi�zMETHOD NOT ALLOWEDZ	__notes__r))	r/rr�rr�r4r�r9r,)r"r�r)r)r*�
test_gh_98778;szMiscTests.test_gh_98778cCs:gd�}|D]\}}|�t|�|�q|�ttd�fdS)N)�r�)NNNr��r�)NNNr�rMrN)zjoe:password@proxy.example.com)Nrbrcr�)z#joe:password@proxy.example.com:3128)Nrbrcr�)zhttp://proxy.example.com/)r�NNr�)zhttp://proxy.example.com:3128/)r�NNr�)z&http://joe:password@proxy.example.com/�r�rbrcr�)z*http://joe:password@proxy.example.com:3128)r�rbrcr�)z1ftp://joe:password@proxy.example.com/rubbish:3128)rTrbrcr�)z%http://joe:password@proxy.example.comrO)z&http://user/name:password@localhost:22)r��	user/namerc�localhost:22)z&http://username:pass/word@localhost:22)r��username�	pass/wordrQ)z'http://user/name:pass/word@localhost:22)r�rPrSrQzfile:/ftp.example.com)rrr1r2)r"Zparse_proxy_test_casesZtcr)r)r)r*�test_parse_proxy@s'zMiscTests.test_parse_proxycCsPt�}|�t��
}|�d�Wd�n1swY|�t|j�d�dS)N�invalidz5Unsupported digest authentication algorithm 'invalid')rr1r2Zget_algorithm_implsrrH�	exception)r"r+�excr)r)r*�test_unsupported_algorithmms��z$MiscTests.test_unsupported_algorithmN)	rKr rLrBrJrKrLrTrXr)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)rKr rLr�r)r)r)r*�
PutRequestxsr[cCsLtd�|_tddddid�|_tddd�|_|�d�|_|jddd�|_dS)	N�http://www.python.org/~jeremy/r�zX-TestrrQ�HEAD)r�rc)rrQ�post�headr[�put�
force_postr�r)r)r*�setUp{s
��zRequestTests.setUpcCs^|�d|j���|�d|j���|�d|j���|�d|j���|�d|j���dS)Nrcrbr]rZ)rr^r�rQr_r`rar�r)r)r*�test_method�s
zRequestTests.test_methodcCsL|�|jj�|�d|j���d|j_|�|jj�|�d|j���dS)Nrbrrrc)r[rQr�rr�rWr�r)r)r*�	test_data�s
zRequestTests.test_datacCsN|�d|jj�|j�dd�|�d|jjd�d|j_|�d|jj�dS)NrN�*rr)rnrQror^rr�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)NrNrkr�)rnrQror�r^rr�r)r)r*�/test_deleting_data_should_remove_content_length�sz<RequestTests.test_deleting_data_should_remove_content_lengthcCs|�d|j���dS)Nr\)rrQrr�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,)rrQr�rrr)r)r*�
test_selector�szRequestTests.test_selectorcC�|�d|jj�dS)Nr�)rrQr_r�r)r)r*�
test_get_type��zRequestTests.test_get_typecCrj)Nr�)rrQr�r�r)r)r*�
test_get_host�rlzRequestTests.test_get_hostcC�td�}|�d|j�dS)Nzhttp://www.%70ython.org/r��rrr�rr)r)r*�test_get_host_unquote��z"RequestTests.test_get_host_unquotecCsR|�|j���|j�dd�|�|j���|�d|jj�|�d|jj�dS)Nr�r�r�)r[rQZ	has_proxyr�rWrr�r�r�r)r)r*r��s
zRequestTests.test_proxycCrn)Nz<URL:http://www.python.org>r�rorr)r)r*�test_wrapped_url�rqzRequestTests.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)r"r_rSr)r)r*�test_url_fragment�szRequestTests.test_url_fragmentcCs0gd�}|D]}t|�}|�|��|j�qdS)N)zhttp://docs.python.orgrtrs)rrrr�)r"r`rSr_r)r)r*�test_url_fullurl_get_full_url�s
�z*RequestTests.test_url_fullurl_get_full_urlN)rKr rLrr[rbrcrdrfrgrhrirkrmrpr�rrrurvr)r)r)r*rYws 

rY�__main__)=r8rrZtest.supportrrrrrr�r�r�r�rzr�Zurllib.requestr/rrr	r
rrr
r�urllib.parserr Zhttp.clientr�ZTestCaserrMr�r�r�r�r�r�r�r��IOBaser�r�r�r�r�rrIr�rrrrrr:r;r=rYrK�mainr)r)r)r*�<module>sl(4<

-6"
3
a�