Your IP : 216.73.216.85


Current Path : /opt/alt/python35/lib64/python3.5/test/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib64/python3.5/test/__pycache__/test_urllib2.cpython-35.opt-1.pyc



��YfP�@s�ddlZddlmZddlmZddlZddlZddlZddlZddlZddl	Z
ddl	mZmZm
Z
mZmZmZmZddlmZddlZ
ddlZGdd�dej�ZGdd	�d	ej�ZGd
d�d�ZGdd
�d
�ZGdd�de�ZGdd�dej�ZGdd�d�Z Gdd�d�Z!Gdd�dej"�Z#Gdd�d�Z$Gdd�d�Z%dd�Z&dd�Z'Gd d!�d!e
j(j)�Z*Gd"d#�d#e
j(j+�Z,Gd$d%�d%e
j(j)�Z-Gd&d'�d'�Z.Gd(d)�d)ej�Z/d*d+�Z0Gd,d-�d-ej�Z1Gd.d/�d/ej�Z2Gd0d1�d1ej�Z3e4d2kr�ej5�dS)3�N)�support)�test_urllib)�Request�OpenerDirector�HTTPBasicAuthHandler�HTTPPasswordMgrWithPriorAuth�_parse_proxy�_proxy_bypass_macosx_sysconf�AbstractDigestAuthHandler)�urlparsec@s@eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�TrivialTestsc	Cs�x�dD]�}i}td||�|d=|dkr�tjdkr�|jd	�|jd
�}}|j|jd�|j|jd�xD|j�D]6\}}|j|jd|d
|||jf�q�WqWdS)N�request�response�parse�error�robotparserzfrom urllib.%s import *�__builtins__�ntZurl2pathname�pathname2urlZ
nturl2pathz	urllib.%sz.%r is exposed in 'urllib.%s' but defined in %r)r
rrrr)�exec�os�name�pop�assertEqual�
__module__�items)�self�module�context�u�p�k�v�r#�6/opt/alt/python35/lib64/python3.5/test/test_urllib2.py�test___all__s
zTrivialTests.test___all__cCs�|jttjjd�tjjtjj�j	tj
d�}tjdkr\d|}n
d|}tjj|�}|j�|j
�dS)Nz	bogus url�/rz
file:///%sz	file://%s)�assertRaises�
ValueError�urllibr
�urlopenr�path�abspath�__file__�replace�sepr�read�close)rZfnameZfile_url�fr#r#r$�test_trivial-s'


zTrivialTests.test_trivialc
Cs�ddddgfdddgfdddd	d
ddd
gfddddgfg}x0|D](\}}|jtjj|�|�q^WdS)Nza,b,c�a�b�czpath"o,l"og"i"cal, examplezpath"o,l"og"i"calZexampleza, b, "c", "d", "e,f", g, hz"c"z"d"z"e,f"�g�hza="b\"c", d="e\,f", g="h\\i"za="b"c"zd="e,f"zg="h\i")rr)r
Zparse_http_list)r�tests�string�listr#r#r$�test_parse_http_list?sz!TrivialTests.test_parse_http_listcCs/tjjd�}|j|jt|��dS)N�reason)r)r�URLError�assertInr=�str)r�errr#r#r$�test_URLError_reasonstrJsz$TrivialTests.test_URLError_reasonstrN)�__name__r�__qualname__r%r3r<rBr#r#r#r$rsrc@s@eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�RequestHdrsTestscCs\d}|jt|dddi�jdd�|jt|dddi�jdd�dS)a(
        The Request.headers dictionary is not a documented interface.  It
        should stay that way, because the complete set of headers are only
        accessible through the .get_header(), .has_header(), .header_items()
        interface.  However, .headers pre-dates those methods, and so real code
        will be using the dictionary.

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

        zhttp://example.com�headersz	Spam-eggs�blahz	spam-EggSN)rrrF)r�urlr#r#r$�test_request_headers_dictQs
z*RequestHdrsTests.test_request_headers_dictcCs7d}t|dddi�}|j|jd��|j|j�d
g�|jdd�|jt|j��ddg�|j|jd��|j|j	d��|j|j	dd	�d	�|j
d�|j|jd��|jd
d�|j|jd
��|j
d
�|j|jd
��dS)a;
        Note the case normalization of header names here, to
        .capitalize()-case.  This should be preserved for
        backwards-compatibility.  (In the HTTP case, normalization to
        .title()-case is done by urllib2 before sending headers to
        http.client).

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

        Method r.remove_header should remove items both from r.headers and
        r.unredirected_hdrs dictionaries
        zhttp://example.comrF�	Spam-eggsrGzFoo-Bar�baz�Foo-barz	Not-there�defaultzUnredirected-spamZEggsN)rJrG)rLrK)rJrG)r�
assertTrue�
has_headerrZheader_items�
add_header�sorted�assertFalse�assertIsNone�
get_headerZ
remove_header�add_unredirected_header)rrH�reqr#r#r$�test_request_headers_methodsfs 


z-RequestHdrsTests.test_request_headers_methodscCsZtjj�}|j}|j}|dddd�|dddd�|ddd	d�|dd
dd�|d
ddd�|d
ddd�|dddd�|dddd�|dddd�|dddd�|j|dd�d#�|j|dd�d$�|j|dd�d%�|j|dd�d&�|j|dd�d'�|j|dd�d(�|j|dd
�d)�|j|d
d�d*�|j|dd�d+�|j|dd �d,�|j|dd!�d-�|j|dd"�d.�|j|dd�d/�|j|dd�d0�|j|dd�d1�dS)2Nz
Some Realmzhttp://example.com/�joe�passwordzhttp://example.com/ni�nir6zhttp://example.com/foo�foozhttp://example.com/bar�bar�ninir5�firstrG�second�spamr4zhttp://example.com�1zhttp://c.example.com:3128�3z
d.example.com�4�dze.example.com:3128�5�ezexample.comzhttp://example.com/spamzhttp://example.com/spam/spamzhttp://a.example.com/z
c.example.comzc.example.com:3128)rXrY)rXrY)rXrY)rXrY)rXrY)r[rZ)r\r])r_r`)rar4)NN)NN)rbr6)rbr6)rcrd)rerf)r)r
�HTTPPasswordMgr�add_password�find_user_passwordr)r�mgr�add�find_user_passr#r#r$�test_password_manager�s\		z&RequestHdrsTests.test_password_managercCstjj�}|j}|j}|dddd�|dddd�|d	d
dd�|d
ddd�|j|dd�d�|j|dd�d �|j|dd�d!�|j|dd�d"�|j|dd�d#�|j|dd�d$�|j|dd�d%�|j|dd�d&�|j|dd�d'�|j|d	d�d(�|j|d	d
�d)�|j|d	d�d*�|j|d
d�d+�|j|d
d�d,�|j|d
d�d-�|j|d
d�d.�dS)/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.
        r2zhttp://g.example.com:80�10�jr7zhttp://h.example.com�11r!r8zi.example.com:80�12�l�iz
j.example.com�13�mzg.example.com:100Nzg.example.com:80z
g.example.comzhttp://g.example.com:100zhttp://g.example.comz
h.example.comzh.example.com:80zhttp://h.example.com:80z
i.example.comzhttp://i.example.com:80zj.example.com:80zhttp://j.example.comzhttp://j.example.com:80)NN)rnro)NN)NN)rnro)rnro)rpr!)rpr!)rpr!)NN)rqrr)rqrr)rtru)NN)rtru)NN)r)r
rgrhrir)rrjrkrlr#r#r$�"test_password_manager_default_port�sD		z3RequestHdrsTests.test_password_manager_default_portN)rCrrDrIrWrmrvr#r#r#r$rEOs$;rEc@s7eZdZgZdejdd�Zdd�ZdS)�
MockOpenerNcCs!||||_|_|_dS)N)rV�data�timeout)rrVrxryr#r#r$�open�szMockOpener.opencGs|||_|_dS)N)�proto�args)rr{r|r#r#r$r�szMockOpener.error)rCrrD�
addheaders�socket�_GLOBAL_DEFAULT_TIMEOUTrzrr#r#r#r$rw�srwc@s:eZdZddd�Zddd�Zdd�ZdS)�MockFileNcCsdS)Nr#)r�countr#r#r$r0�sz
MockFile.readcCsdS)Nr#)rr�r#r#r$�readline�szMockFile.readlinecCsdS)Nr#)rr#r#r$r1szMockFile.close)rCrrDr0r�r1r#r#r#r$r��sr�c@seZdZdd�ZdS)�MockHeaderscCst|j��S)N)r;�values)rrr#r#r$�
getheadersszMockHeaders.getheadersN)rCrrDr�r#r#r#r$r�sr�c@s7eZdZddd�Zdd�Zdd�ZdS)�MockResponseNcCsAtjj||�||||f\|_|_|_|_dS)N)�io�StringIO�__init__�code�msgrFrH)rr�r�rFrxrHr#r#r$r�
szMockResponse.__init__cCs|jS)N)rF)rr#r#r$�infoszMockResponse.infocCs|jS)N)rH)rr#r#r$�geturlszMockResponse.geturl)rCrrDr�r�r�r#r#r#r$r�	sr�c@s(eZdZdd�Zdd�ZdS)�
MockCookieJarcCs
||_dS)N)�ach_req)rr
r#r#r$�add_cookie_headerszMockCookieJar.add_cookie_headercCs|||_|_dS)N)�ec_req�ec_r)rrr
r#r#r$�extract_cookiesszMockCookieJar.extract_cookiesN)rCrrDr�r�r#r#r#r$r�sr�c@s(eZdZdd�Zdd�ZdS)�
FakeMethodcCs||_||_||_dS)N)�	meth_name�handle�action)rr�r�r�r#r#r$r�s		zFakeMethod.__init__cGs|j|j|j|�S)N)r�r�r�)rr|r#r#r$�__call__#szFakeMethod.__call__N)rCrrDr�r�r#r#r#r$r�sr�c@s@eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�MockHTTPResponsecCs1||_||_||_||_d|_dS)N��)�fpr��statusr=r�)rr�r�r�r=r#r#r$r�(s
				zMockHTTPResponse.__init__cCsdS)N�r#)rr#r#r$r0/szMockHTTPResponse.readcCsiS)Nr#)rr#r#r$r�2szMockHTTPResponse.infocCs|jS)N)rH)rr#r#r$r�5szMockHTTPResponse.geturlN)rCrrDr�r0r�r�r#r#r#r$r�'sr�c@sveZdZdd�Zejdd�Zdd�Zdddd	�Zddd
d�Z	dd
�Z
dd�ZdS)�
MockHTTPClasscCs:d|_g|_d|_d|_d|_i|_dS)NrF)�level�req_headersrx�raise_on_endheadersZsock�_tunnel_headers)rr#r#r$r�:s					zMockHTTPClass.__init__cCs||_||_|S)N)�hostry)rr�ryr#r#r$r�Bs		zMockHTTPClass.__call__cCs
||_dS)N)r�)rr�r#r#r$�set_debuglevelGszMockHTTPClass.set_debuglevelNcCs5||_||_|r$||_n
|jj�dS)N)�_tunnel_hostZ_tunnel_portr��clear)rr��portrFr#r#r$�
set_tunnelJs
		zMockHTTPClass.set_tunnelcCse||_||_|dk	r3|j|j�7_|jj�|rO||_|jrat��dS)N)�method�selectorr�r�sortrxr��OSError)rr�rHZbodyrFr#r#r$r
Rs		
		zMockHTTPClass.requestcCstt�idd�S)Nr��OK)r�r�)rr#r#r$�getresponse]szMockHTTPClass.getresponsecCsdS)Nr#)rr#r#r$r1`szMockHTTPClass.close)rCrrDr�r~rr�r�r�r
r�r1r#r#r#r$r�9sr�c@s^eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�MockHandleri�cCs|j|�dS)N)�_define_methods)r�methodsr#r#r$r�iszMockHandler.__init__cCskxd|D]\}t|�dkr.|\}}n
|d}}t|||j�}t|j||�qWdS)N�)�lenr�r��setattr�	__class__)rr��specrr��methr#r#r$r�ls

zMockHandler._define_methodscOs%|jjj||||f�|dkr/dS|dkr?|S|dkrdtddid�}|S|dkrztd�S|jd�r||jd	�d
d�}yt|�}Wntk
r�YnXtddid�}|jj	d|d||di�S|d
kr!t
j	jd��dS)Nzreturn selfzreturn responser�r�r�zreturn requestzhttp://blah/r� ��httpr�raiserG)�parent�calls�appendr�r�
startswith�rfind�intr(rr)r>)rZfn_namer�r|�kwds�resr�r#r#r$r�us*

#zMockHandler.handlecCsdS)Nr#)rr#r#r$r1�szMockHandler.closecCs||_g|j_dS)N)r�r�)rr�r#r#r$�
add_parent�s	zMockHandler.add_parentcCs#t|d�sdS|j|jkS)N�
handler_orderT)�hasattrr�)r�otherr#r#r$�__lt__�szMockHandler.__lt__N)
rCrrDr�r�r�r�r1r�r�r#r#r#r$r�ds	r�cCs�g}d}xp|D]h}Gdd�dt�}||�}|j|7_|j|�|d}|j|�|j|�qW|S)a�Create MockHandlers and add them to an OpenerDirector.

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

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

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

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

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

    rc@seZdZdS)z6add_ordered_mock_handlers.<locals>.MockHandlerSubclassN)rCrrDr#r#r#r$�MockHandlerSubclass�sr�r�)r�r�r�r��add_handler)�opener�	meth_spec�handlersr��methsr�r8r#r#r$�add_ordered_mock_handlers�s



r�cGs+t�}x|D]}|j|�qW|S)N)rr�)Zhandler_instancesr�r8r#r#r$�build_test_opener�s	
r�c@s4eZdZdd�Zdd�Zdd�ZdS)�MockHTTPHandlercCs ||_||_|j�dS)N)r�rF�reset)rr�rFr#r#r$r��s		zMockHTTPHandler.__init__cCsd|_g|_dS)Nr)�_count�requests)rr#r#r$r��s	zMockHTTPHandler.resetcCs�ddl}ddl}|jj|j|��|jdkr�|jd|_tjj|j	}|j
|j�}|jj
d|t�|j	||�S||_|j
d�}tdd|d|j��SdS)Nrr�r�z

r�r�r�)�email�copyr�r��deepcopyr�r��client�	responsesr�Zmessage_from_stringrFr�rr�rVr��get_full_url)rrVr�r�rr�r#r#r$�	http_open�s		zMockHTTPHandler.http_openN)rCrrDr�r�r�r#r#r#r$r��sr�c@s+eZdZddd�Zdd�ZdS)�MockHTTPSHandlerrcCs)tjjj|d|�t�|_dS)N�
debuglevel)r)r
�AbstractHTTPHandlerr�r��httpconn)rr�r#r#r$r��szMockHTTPSHandler.__init__cCs|j|j|�S)N)�do_openr�)rrVr#r#r$�
https_open�szMockHTTPSHandler.https_openN)rCrrDr�r�r#r#r#r$r��sr�c@s4eZdZdd�Zdd�Zdd�ZdS)�MockHTTPHandlerCheckAuthcCs||_d|_dS)NF)r��has_auth_header)rr�r#r#r$r��s	z!MockHTTPHandlerCheckAuth.__init__cCs
d|_dS)NF)r�)rr#r#r$r��szMockHTTPHandlerCheckAuth.resetcCsM|jd�rd|_tjj|j}t|j|t�d|j��S)N�
AuthorizationTr�)	rOr�r�r�r�r�r�r�r�)rrVrr#r#r$r��s	z"MockHTTPHandlerCheckAuth.http_openN)rCrrDr�r�r�r#r#r#r$r��sr�c@s(eZdZdd�Zdd�ZdS)�MockPasswordManagercCs(||_||_||_||_dS)N)�realmrH�userrY)rr�Zurir�rYr#r#r$rh�s			z MockPasswordManager.add_passwordcCs"||_||_|j|jfS)N)�target_realm�
target_urlr�rY)rr�Zauthurir#r#r$ris		z&MockPasswordManager.find_user_passwordN)rCrrDrhrir#r#r#r$r��sr�c@sdeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dS)�OpenerDirectorTestscCs6Gdd�dt�}|jtt�j|��dS)Nc@seZdZdS)z<OpenerDirectorTests.test_add_non_handler.<locals>.NonHandlerN)rCrrDr#r#r#r$�
NonHandlersr�)�objectr'�	TypeErrorrr�)rr�r#r#r$�test_add_non_handler
s	z(OpenerDirectorTests.test_add_non_handlercCs�ddlm}t�}ddgd
gg}t||�|jtjj��x(dD] }|j||j	|d
�qXWdS)Nr)r>r��return self�
proxy_open�redirect_request�do�proxy�redirectz://example.com/)r�r�)r�r�)r�r�)r�r�r�)
�urllib.errorr>rr�r�r)r
ZUnknownHandlerr'rz)rr>�or��schemer#r#r$�test_badly_named_methodss			

z,OpenerDirectorTests.test_badly_named_methodsc
Cs�t�}dddgdgdgd	gg}t||�}td�}|j|�}|j||d�|ddf|ddfg}x[t||j�D]G\}}|\}	}
}}|j|	|
f|�|j||f�q�WdS)
Nr��ftp_open�http_error_302�return selfzhttp://example.com/r�r)r�r�)r�r�)rr�rrzr�zipr�)
rr�r�r�rV�rr��expected�got�handlerrr|r�r#r#r$�test_handled(s	 z OpenerDirectorTests.test_handledcCs�t�}g}xqd	gdfdgdfgD]Q\}}Gdd�dt�}||�}||_|j|�|j|�q.W|jd�|j|jdd|d�|j|jdd|d�dS)
Nr��return selfi�rc@seZdZdS)zCOpenerDirectorTests.test_handler_order.<locals>.MockHandlerSubclassN)rCrrDr#r#r#r$r�Gsr�zhttp://example.com/r�)r�r)rr�r�r�r�rzrr�)rr�r�r�r�r�r8r#r#r$�test_handler_orderBs		

z&OpenerDirectorTests.test_handler_ordercCst�}dgdgg}t||�}td�}|jtjj|j|�|j|j	|dd|fifg�dS)Nr�r��return selfzhttp://example.com/r)r�r�)r�r)
rr�rr'r)rr>rzrr�)rr�r�r�rVr#r#r$�
test_raiseTs	zOpenerDirectorTests.test_raisecCst�}dgddgdddgdgg}t||�}Gd	d
�d
�}td�}|j|�|dd|ff|d
d||�ddiffg}xct||j�D]O\}}|\}	}
}|j|	|
f|dd
��|j||d
�q�WdS)Nr��	error 302�http_error_400r�r��return responseZhttp_error_303Z
http_errorc@seZdZdd�ZdS)z4OpenerDirectorTests.test_http_error.<locals>.UnknowncSsdS)NTr#)rr�r#r#r$�__eq__osz;OpenerDirectorTests.test_http_error.<locals>.Unknown.__eq__N)rCrrDr	r#r#r#r$�Unknownnsr
zhttp://example.com/rr�i.r�)r�r)rr�)r�r)rr�rrzr�r�r)rr�r�r�r
rVr�r�r�rZmethod_namer|r#r#r$�test_http_erroras"		

 z#OpenerDirectorTests.test_http_errorcCsnt�}d	d
gddgg}t||�}td�}|j|�|ddf|ddf|ddf|ddfg}x�t|j�D]�\}\}}}	}
|dkr�|j||f||�|jt|	�d�|j|	dt�q�|j||f||�|jt|	�d�|j|	dt�|	ddk	r�|j|	dt	�q�WdS)
N�http_request�return request�
http_response�return responsezhttp://example.com/rr�r�)rr
)rr)rr
)rr)
rr�rrz�	enumerater�rr�ZassertIsInstancer�)rr�r�r�rVr�rsrrr|r�r#r#r$�test_processors}s(	
 (z#OpenerDirectorTests.test_processorsN)
rCrrDr�r�rrrrrr#r#r#r$r�s
r�cCsyy|jd�Wn!tk
r4tjd��YnXtjj|�}tjdkru|j	d�ru|dd�}|S)Nzutf-8zpath is not encodable to utf8rz///r�)
�encode�UnicodeEncodeError�unittestZSkipTestr)r
rrrr�)r+�urlpathr#r#r$�sanepathname2url�s
rc@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zejejd-kd.�d/d0��Zd1d2d3�Zd4d5�Zd6d7�Z d8d9�Z!d:d;�Z"d<d=�Z#d>d?�Z$d@dA�Z%dBdC�Z&dDdE�Z'dFdG�Z(dHdI�Z)dJS)K�HandlerTestscsmGdd�d��G�fdd�dtjj�}ddl}d}||�}t�|_xdd|jd	d	d
ddgd
df	dd|jdd	d
ddgd
df	dd|jdd	d
ddgd
df	dd|jdd	d
ddgd
df	dddd	d	dddgd	df	dd|jd	d	dgddf	gD]\	}}}}}	}
}}}
t|�}d|_|j	|�}|j
|j|�|j
|j|	�|j
|j
tj|��|j
|j|�|j
|j|�|j
|jj|�|j
|jj|
�|j�}|j
|jd�|
�|j
t|d�t|��qGWdS)Nc@s4eZdZdd�Zdd�Zdd�ZdS)z-HandlerTests.test_ftp.<locals>.MockFTPWrappercSs
||_dS)N)rx)rrxr#r#r$r��sz6HandlerTests.test_ftp.<locals>.MockFTPWrapper.__init__cSs2|||_|_tj|j�t|j�fS)N)�filename�filetyper�r�rxr�)rrrr#r#r$�retrfile�sz6HandlerTests.test_ftp.<locals>.MockFTPWrapper.retrfilecSsdS)Nr#)rr#r#r$r1�sz3HandlerTests.test_ftp.<locals>.MockFTPWrapper.closeN)rCrrDr�rr1r#r#r#r$�MockFTPWrapper�srcs4eZdZdd�Zej�fdd�ZdS)z-HandlerTests.test_ftp.<locals>.NullFTPHandlercSs
||_dS)N)rx)rrxr#r#r$r��sz6HandlerTests.test_ftp.<locals>.NullFTPHandler.__init__csH|||_|_|||_|_||_�|j�|_|jS)N)r��passwdr�r��dirsrx�
ftpwrapper)rr�rr�r�rry)rr#r$�connect_ftp�s
	z9HandlerTests.test_ftp.<locals>.NullFTPHandler.connect_ftpN)rCrrDr�r~rrr#)rr#r$�NullFTPHandler�sr rzrheum rhaponicumz ftp://localhost/foo/bar/baz.html�	localhostr��Ir[r\zbaz.htmlz	text/htmlz'ftp://parrot@localhost/foo/bar/baz.htmlZparrotz*ftp://%25parrot@localhost/foo/bar/baz.htmlz%parrotz,ftp://%2542parrot@localhost/foo/bar/baz.htmlz	%42parrotzftp://localhost:80/foo/bar/�P�Dzftp://localhost/baz.gif;type=a�Azbaz.gifzContent-typezContent-length)r)r
Z
FTPHandler�ftplibrwr�ZFTP_PORTrryr�rr�rr�r~�
gethostbynamer�rrrrr��getr�r�)rr r&rxr8rHr�r�r�rZtype_rrZmimetyperVr�rFr#)rr$�test_ftp�sL"1	zHandlerTests.test_ftpc#Cswddl}tjj�}t�}|_tj}tt	j
j|��}d}d|d|dtj
d�|fg}ytj
tj��}Wntjk
r�d}YnX|r�|jd||f�x"|D]}	t|d�}
z�z|
j|�Wd|
j�X|jt|	��}z(|j�}|j�}
|j�}Wd|j�Xt	j|�}|jj|jd	d
�}Wdt	j|�X|j||�|j|
dd�|j|
d
d�|j|
d|�|j||	�q�Wx�d|dddtj
d�t	j�|fdt	j�|fgD]k}	zTt|d�}
z|
j|�Wd|
j�X|j tj!j"|jt|	��Wdt	j|�Xq;Wtjj�}t�}|_x�ddddd gD]�\}	}t|	�}y|j|�Wn%tj!j"k
r/|j#|�Yn'X|j$|j%|�|j|j&d�|j|j&dk|�q�WdS)!Nrs
hello, world
zfile://localhost%sz	file://%szfile://%s%sr!r��wbZusegmtTzContent-typez
text/plainzContent-lengthrtz
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�file://ftp.example.com//foo.txtF� file://ftp.example.com///foo.txt�file://ftp.example.com/foo.txt�"file://somehost//foo/something.txt�#file://localhost//foo/something.txt�ftp)r+F)r,F)r-F)r.F)r/F)'Zemail.utilsr)r
ZFileHandlerrwr�r�TESTFNrrr+r,r~r'ZgethostnameZgaierrorr�rz�writer1Z	file_openrr0r�r��statZutilsZ
formatdate�st_mtime�remover�getcwdr'rr>rR�assertIsrV�type)rr�r8r�r1rZtowrite�urlsZ	localaddrrHr2r�rxrFZrespurlZstatsZmodifiedr0rVr#r#r$�	test_file�s�	
zHandlerTests.test_filecCs�tjj�}t�}|_d}xFd'd(gD]8\}}t||ddi�}d|_|jdd�t�}|j	||�}|j
|j|j|j
|j|jd	kd
f|j�}	|	j|	j|j|j
�|�|j|jd�|j|jd�|j|j|�|j|jd
�|j|jd)d*d+g�|j|j|�q2Wd|_|jtjj|j	||�tdd�}|jt|j|�d,g|_x}d-D]u}td|�}t d	d
id�}|j|�}
|dkr<|j!d|j"�|j!d|j"�n.|j|j"dd�|j|j"dd�|j|j"dd�|j|j"dd�|jdd�|jdd�|jdd�|jdd�|j|�}
|j|j"dd�|j|j"dd�|j|j"dd�|j|j"dd�q�Wdd�}x]iddifD]I}td|�|�}|s�|jt#|j|�qf|j|�}
qfWt$j%�}
|
j&d�xyidd ifD]e}td|
|�}|s|jt#|j|�q�|j|�}
|jt'|
j(d��d �q�W|
j)�t*j*d!d"d#d$d%g�}xZidd&ifD]F}td||�}|j|�}
|jt'|
j(d��d&�q�WdS).Nzhttp://example.com/�GET�POST�blah�Foor\�Spam�eggsr�r�zexample.comrr&�
Connectionr1TZbadpost�r�zContent-lengthzContent-type�0z!application/x-www-form-urlencoded�Hostr[rKcssdVdVdVdS)Nsonestwosthreer#r#r#r#r$�
iterable_body�sz-HandlerTests.test_http.<locals>.iterable_bodyzContent-Length�sSomething
Something
Something
�r"r�r����)r;N)r<r=)rAr1)r>r\)r?r@)r?r@)rBN)+r)r
r�rwr�rryrUr�r�r0r�r�r�r�r�r(�__contains__rr�r�r�r�r�rxr�r'rr>r��do_request_r}r��assertNotIn�unredirected_hdrsr(r��BytesIOr2r�rTr1�array)rr8r�rHr�rxrVr�r��hdrs�newreqrErFZfile_objZiterable_arrayr#r#r$�	test_httpKs�		
	

#
zHandlerTests.test_httpcCsIt�}tdd�}|j|�|jd�|j|jd�dS)Nr�r�zhttps://www.example.com)rr�r�rzrZ_debuglevel)rr�r8r#r#r$�test_http_handler_debuglevel�s
	

z)HandlerTests.test_http_handler_debuglevelcCs�tjj�}t�|_d}ddddg}xy|D]q}t||�}|j|�}|j|jdd�|j	dd�|j|�}|j|jdd�q:WdS)	NrBz#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.htmlrDzexample.comzsomeproxy:3128)
r)r
r�rwr�rrLrrN�	set_proxy)rr8rxZds_urlsZds_urlZds_reqZ	np_ds_reqZp_ds_reqr#r#r$�test_http_doubleslash�s	
z"HandlerTests.test_http_doubleslashcCs�dddg}td�}xw|D]o}||_t|�}|j|j�|�|j|jped|j�|jt|j��j|j�q"WdS)Nzhttp://example.com?foo=bar#bazz)http://example.com?foo=bar&spam=eggs#bashzhttp://example.comr�)r�full_urlrrr��fragmentZquery)rr9r�rHZparsedr#r#r$�test_full_url_setter�s	
	z!HandlerTests.test_full_url_settercCsItd�}|`|j|j�|j|j�|j|jd�dS)Nzhttp://www.example.comr�)rrWrSrXrr�)rr�r#r#r$�test_full_url_deleter�s
z"HandlerTests.test_full_url_deletercCs�tjj�}t�|_d}t|�}|j|�}|j|jd�|j|j	d�d}t|�}|j|�}|j|jd�|j|j	d�dS)Nzhttp://www.python.org?getspamzwww.python.orgz	/?getspamzhttp://www.python.orgr�)
r)r
r�rwr�rrLrr�r�)rr8Z	weird_urlrVrRZurl_without_pathr#r#r$�test_fixpath_in_weirdurls�sz&HandlerTests.test_fixpath_in_weirdurlscCs�tjj�}t�}|_d}t|�}tddid|�}|j||�}|j||�|j	t
|d��tddid|�}|j||�}|j||�|j	t
|d��tdd	id|�}|j||�}|j||�|j	t
|d��td
did|�}|j|j||��|j|j
d�|j|j||d
dif�dS)
Nzhttp://example.com/r�r�r�r{��ZAccepted��zPartial contenti�zBad gatewayr�)r)r
ZHTTPErrorProcessorrwr�rr�rr7rRr�rSrr{r|)rr8r�rHrVr��newrr#r#r$�test_errors
s(zHandlerTests.test_errorscCs�t�}tjj|�}t�|_td�}tddid�}|j|�}|j	|j
|�|j	|j
|�|j|jd�|j
|j�|j||�}|j	|j|�|j	|j|�|j	||�dS)Nzhttp://example.com/r�r�r�zexample.com)r�r)r
�HTTPCookieProcessorrwr�rr�rr7r�r�origin_req_hostrRZunverifiablerr�r�)r�cjr8rVr�rRr^r#r#r$�test_cookies#s	zHandlerTests.test_cookiescCs*d}d}tjj�}t�}|_x�dD]�}x�dD]�}t|d|�}t||�}tj|_	|j
d	d
�|dk	r�|j
dtt|���|j
dd
�y)||t�|dtd|i��Wn5tjjk
r|j|d�|j|�YnX|j|jj�|�y|j|jj�d�Wn%tk
ry|j|jj�YnXdd�|jjD�}	|jd|	�|jd|	�|j|jjd	d
�|jd|jj�|jd|jj�q?Wq2Wt|�}tj|_	|dd�}
t|dd�}d}tj|_	y%x|
||d�|d}qRWWn1tjjk
r�|j|tjjj�YnXt|dd�}d}tj|_	y)x"|
||d|�|d}q�WWn1tjjk
r%|j|tjjj�YnXdS)Nzhttp://example.com/a.htmlzhttp://example.com/b.html�-�.�/�3�
blah
blah
z
http_error_%sZNonsensezviking=withholdzContent-Lengthr?r`�Blah�locationr;cSsg|]}|j��qSr#)�lower)�.0�xr#r#r$�
<listcomp>Ss	z.HandlerTests.test_redirect.<locals>.<listcomp>zcontent-lengthzcontent-typecSs,|j|t�ddtd|i��dS)Ni.rirj)r�r�r�)r8rVrHr#r#r$r�`sz,HandlerTests.test_redirect.<locals>.redirectrazexample.comrzhttp://example.com/r�zhttp://example.com/%d)rdrerfrg)Nrh)r)r
�HTTPRedirectHandlerrwr��getattrrr~rryrPr@r�rUr�r�r�	HTTPErrorr�assertIsNotNonerVr��
get_method�AttributeErrorrRrxrFrMrNZmax_repeatsZmax_redirections)r�from_urlZto_urlr8r�r�rxr�rVrFr�r�r#r#r$�
test_redirect4sj


	zHandlerTests.test_redirectcCsd}dddg}dddg}d}tjj�}t�}|_t|�}tj|_xS|D]K}|d	|}	|j	tj
j|j|t
�d
dtd|	i��qhWx]|D]U}|d	|}
|j|t
�d
d
td|
i��|j|jj�|
�q�WdS)Nzhttp://example.com/a.htmlr��httpsr0�fileZimapZldapzexample.com/b.htmlz://i.zSecurity LoopholerjzThat's fine)r)r
rorwr�rr~rryr'rrqr�r�r�rrVr�)rruZ
valid_schemesZinvalid_schemesZschemeless_urlr8r�rVr�Zinvalid_url�	valid_urlr#r#r$�test_invalid_redirect~s$

z"HandlerTests.test_invalid_redirectcCs�d}d}tjj�}t�}|_t|�}tj|_tj	j
||�}|j|t�ddt
d|i��|j|jj�|�dS)Nzhttp://example.com/a.htmlz/b.htmli.zThat's finerj)r)r
rorwr�rr~rryrZurljoinr�r�r�rrVr�)rruZrelative_urlr8r�rVryr#r#r$�test_relative_redirect�sz#HandlerTests.test_relative_redirectc	Cs�ddlm}ddlm}|�}||dd�tdd�}tjj�}tjj�}tjj	|�}t
||||�}|jd�|j|j
jd��dS)	Nr)�	CookieJar)�interact_netscapezhttp://www.example.com/z	spam=eggsi.z%Location: http://www.cracker.com/

ZCookie)Zhttp.cookiejarr|Ztest.test_http_cookiejarr}r�r)r
�HTTPDefaultErrorHandlerror`r�rzrRrVrO)	rr|r}rb�hh�hdeh�hrh�cpr�r#r#r$�test_cookie_redirect�s	
z!HandlerTests.test_cookie_redirectcCsxd}tdd|�}tjj�}tjj�}t|||�}|jd�}|j|j�|j	��dS)Nz(http://www.example.com/index.html#OK

i.z
Location: zhttp://www.example.com)
r�r)r
r~ror�rzrr��strip)rZredirected_urlrr�r�r�r�r#r#r$�test_redirect_fragment�sz#HandlerTests.test_redirect_fragmentcs�tjj�d}tj|�tj_�jttjd��td	�����fdd�}|tjj_t	jj
d�}�j|j�d�dS)
Ns(HTTP/1.1 302 Found
Location: ?query

�HTTPConnection�/path�/path?querycs?�j|t����j|||||�d|j_dS)NsHTTP/1.1 200 OK

Hello!)r�nextr
r�Zfakedata)�connr�rH�pos�kw)�
real_classrr9r#r$r
�sz3HandlerTests.test_redirect_no_path.<locals>.requestzhttp://python.org/pathzhttp://python.org/path?query)r�r�)
r�r�r�r�fakehttpZ
addCleanupr��iterr
r)r*rr�)rZ	response1r
r�r#)r�rr9r$�test_redirect_no_path�sz"HandlerTests.test_redirect_no_pathc
Cs�Gdd�dtjj�}|�}tjj|�}d}x�|D]�\}}|jt|���dtjd	|d
�|_|j	d�}d|d
}|j
}	|j|	j|�t|	��WdQRXqDWdS)Nc@seZdZdd�ZdS)z4HandlerTests.test_redirect_encoding.<locals>.HandlercSs:|j|j|�}|jj|_tjd�|_|S)Ns)HTTP/1.1 200 OK
Content-Length: 3

123)r��
connectionZbuf�last_bufrr�)rrV�resultr#r#r$r��s
z>HandlerTests.test_redirect_encoding.<locals>.Handler.http_openN)rCrrDr�r#r#r#r$�Handler�sr��/på-dansk/�/p%C3%A5-dansk/�/spaced%20path/�
/spaced path/�/?på-dansk�/?p%C3%A5-dansks!HTTP/1.1 302 Redirect
Location: s

zhttp://example.com/sGET � �r�r��r�r��r�r��r�r�)r�r�r�r�)
r)r
�HTTPHandler�build_openerZsubTest�reprrr�r�rzr�rNr�)
rr�rr�r9rjr�rr�r
r#r#r$�test_redirect_encoding�s		z#HandlerTests.test_redirect_encodingcCs�t�}tjjtdd��}|j|�d
gg}t||�}td�}|j|j	d�|j
|�|j|j	d�|j|ddfgdd	�|jD��dS)Nr�zproxy.example.com:3128r��return responsezhttp://acme.example.com/zacme.example.comrcSs g|]}|dd��qS)rr�r#)rl�tupr#r#r$rn�s	z+HandlerTests.test_proxy.<locals>.<listcomp>)r�r�)rr)r
�ProxyHandler�dictr�r�rrr�rzr�)rr��phr�r�rVr#r#r$�
test_proxy�s	

zHandlerTests.test_proxycCs�dtjd<t�}tjjtdd��}|j|�td�}|j	|j
d�|j|�|j	|j
d�td�}|j	|j
d�|j|�|j	|j
d�tjd=dS)	Nz
python.org�no_proxyr�zproxy.example.comzhttp://www.perl.org/zwww.perl.orgzhttp://www.python.orgzwww.python.org)r�environrr)r
r�r�r�rrr�rz)rr�r�rVr#r#r$�test_proxy_no_proxy�s
	


z HandlerTests.test_proxy_no_proxycCs�dtjd<t�}tjjtdd��}|j|�td�}|j	|j
d�|j|�|j	|j
d�tjd=dS)N�*r�r�zproxy.example.comzhttp://www.python.orgzwww.python.org)rr�rr)r
r�r�r�rrr�rz)rr�r�rVr#r#r$�test_proxy_no_proxy_alls
	

z$HandlerTests.test_proxy_no_proxy_allcCs�t�}tjjtdd��}|j|�d
gg}t||�}td�}|j|j	d�|j
|�|j|j	d�|j|ddfgdd	�|jD��dS)Nrwzproxy.example.com:3128r��return responsezhttps://www.example.com/zwww.example.comrcSs g|]}|dd��qS)rr�r#)rlr�r#r#r$rn%s	z1HandlerTests.test_proxy_https.<locals>.<listcomp>)r�r�)rr)r
r�r�r�r�rrr�rzr�)rr�r�r�r�rVr#r#r$�test_proxy_httpss	

zHandlerTests.test_proxy_httpscCst�}tjjtdd��}|j|�t�}|j|�td�}|jdd�|jdd�|j	|j
d�|j|j�|j
|�|jd
|jj�|jd|jj�|j|j�|j	|j
d�|j	|jd	�d�dS)Nrwzproxy.example.com:3128zhttps://www.example.com/�Proxy-Authorization�FooBar�
User-Agent�Grailzwww.example.comzProxy-authorization)r�r�)r�r�)rr)r
r�r�r�r�rrPrr�rSr�rzrMr�r�r?rrrT)rr�r�Z
https_handlerrVr#r#r$�$test_proxy_https_proxy_authorization's$	
	

	
	
z1HandlerTests.test_proxy_https_proxy_authorization�darwinzonly relevant for OSXcCs�ddddddddgi}x+dD]#}|jt||�d|�q(Wx+dD]#}|jt||�d|�qVWdddgi}|jtd|��dS)NZexclude_simpleF�
exceptions�foo.barz	*.bar.com�	127.0.0.1z10.10z10.0/16�www.bar.com�	10.10.0.1�10.0.0.1z expected bypass of %s to be True�abc.foo.bar�bar.com�	127.0.0.2�	10.11.0.1�notinbypassz!expected bypass of %s to be FalseT�test)r�r�r�r�r�)r�r�r�r�r�)rNr	rR)rZbypassr�r#r#r$�test_osx_proxy_bypass>s

z"HandlerTests.test_osx_proxy_bypass�"c	Cs�t�}t�}tjj|�}d}tdd|||f�}|j|�|j|�|j||d|||dd�dS)NzACME Widget Storei�z(WWW-Authenticate: Basic realm=%s%s%s

r�z!http://acme.example.com/protected)rr�r)r
rr�r��_test_basic_auth)r�
quote_charr��password_manager�auth_handlerr��http_handlerr#r#r$�test_basic_authTs		

	zHandlerTests.test_basic_authcCs|jdd�dS)Nr��')r�)rr#r#r$�(test_basic_auth_with_single_quoted_realmdsz5HandlerTests.test_basic_auth_with_single_quoted_realmcCs�t�}t�}tjj|�}d}tdd|�}|j|�|j|�|jt��'|j	||d|||dd�WdQRXdS)NzACME Widget Storei�z$WWW-Authenticate: Basic realm=%s

r�z!http://acme.example.com/protected)
rr�r)r
rr�r�ZassertWarns�UserWarningr�)rr�r�r�r�r�r#r#r$�#test_basic_auth_with_unquoted_realmgs		

	z0HandlerTests.test_basic_auth_with_unquoted_realmc	Cs�t�}tjjtdd��}|j|�t�}tjj|�}d}tdd|�}|j|�|j|�|j	||d|||dd�dS)Nr�zproxy.example.com:3128z
ACME Networksi�z(Proxy-Authenticate: Basic realm="%s"

zProxy-authorizationz&http://acme.example.com:3128/protected)
rr)r
r�r�r�r�ZProxyBasicAuthHandlerr�r�)rr�r�r�r�r�r�r#r#r$�test_proxy_basic_authws	
	

	z"HandlerTests.test_proxy_basic_authc
	Cs�Gdd�dt�}Gdd�dtjj�}Gdd�dtjj�}|�}t�}||�}||�}d}tdd	|�}	|j|�|j|�|j|	�|j||d
||	|dd�|j	|j
dd
gd�dS)Nc@s(eZdZdd�Zdd�ZdS)zQHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirectorcSstj|�g|_dS)N)rr��recorded)rr#r#r$r��s
zZHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.__init__cSs|jj|�dS)N)r�r�)rr�r#r#r$�record�szXHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.recordN)rCrrDr�r�r#r#r#r$�RecordingOpenerDirector�sr�c@seZdZdd�ZdS)zOHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandlerc_s-|jjd�tjjj|||�dS)N�digest)r�r�r)r
�HTTPDigestAuthHandler�http_error_401)rr|r�r#r#r$r��sz^HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandler.http_error_401N)rCrrDr�r#r#r#r$�TestDigestAuthHandler�sr�c@seZdZdd�ZdS)zNHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandlerc_s-|jjd�tjjj|||�dS)N�basic)r�r�r)r
rr�)rr|r�r#r#r$r��sz]HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandler.http_error_401N)rCrrDr�r#r#r#r$�TestBasicAuthHandler�sr�z
ACME Networksi�z&WWW-Authenticate: Basic realm="%s"

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


	z0HandlerTests.test_basic_and_digest_auth_handlerscCs^t�}tjjd�}tdd�}|j|�|j|�|jt|jd�dS)Ni�zWWW-Authenticate: Kerberos

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

z1HandlerTests.test_unsupported_auth_digest_handlercCs^t�}tjjd�}tdd�}|j|�|j|�|jt|jd�dS)Ni�zWWW-Authenticate: NTLM

zhttp://www.example.com)	rr)r
rr�r�r'r(rz)rr�Zbasic_auth_handlerr�r#r#r$�#test_unsupported_auth_basic_handler�s	

z0HandlerTests.test_unsupported_auth_basic_handlerc	Cs�ddl}	d	\}
}|j|||
|�|j||j�|j||j�|j|
|j�|j||j�|j|�|j|j|�|j|j	|�|jt
|j�d�|j|jdj
|��td|
|fd�}d|	j|�j�j�}
|j|jdj|�|
�|j|jdj||
�d|_|_|j�|j|�|jt
|j�d�|j|jdj
|��dS)
Nr�wile�coyoter�z%s:%s�asciizBasic r�)r�r�)�base64rhrr�rHr�rYrzr�r�r�r�rRrO�bytesZencodebytesr��decoderTrNr�)rr�r�Zauth_headerr�r�r��request_urlZ
protected_urlr�r�rYZuserpassZauth_hdr_valuer#r#r$r��s0


zHandlerTests._test_basic_authc	Cs�d\}}d}td�}t�}t|�}|jd|||dd�|j|�}|j|�t�}|j|�|j|�|j|�|j|j	�dS)Nr�r�z!http://acme.example.com/protectedr��is_authenticatedT)r�r�)
r�rrrhr�rNrr�rzr�)	rr�rYr�r��pwd_manager�auth_prior_handler�is_authr�r#r#r$�test_basic_prior_auth_auto_send�s	
	


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

r�)r�r�)rrrhr�rRrr�r�rzrNr�r�)
rr�rYr�r�r�r�r�r�r�r#r#r$�.test_basic_prior_auth_send_after_first_success
s.	
	



	


z;HandlerTests.test_basic_prior_auth_send_after_first_successcCs�x�d
D]�\}}dj|�}tj|j�|�}tjj�}td�}d|_|j	||��}|j
�WdQRX|j|jj
d	j|��qWdS)z=Test the connection is cleaned up when the response is closed�Connection: close�data�Transfer-Encoding: chunked�4
data
0

�Content-Length: 4zHTTP/1.1 200 OK
{}

z
http://dummy/NzConnection not closed with {!r}�r�r��r�r��r�r�)r�r�r�)�formatrr�rr)r
r�rryr�r0rN�fakesock�closed)rZtransferrx�headerr�rrVZrespr#r#r$�test_http_closed5s	zHandlerTests.test_http_closedc
Csztjd�}tjj�}td�}d|_|jtj	j
��|j||�WdQRX|j|j
jd�dS)z;Test the connection is cleaned up after an invalid responserBz
http://dummy/NzConnection not closed)rr�r)r
r�rryr'r�r�Z
BadStatusLiner�rNr�r�)rr�rrVr#r#r$�test_invalid_closedFs	z HandlerTests.test_invalid_closedN)*rCrrDr)r:rSrTrVrYrZr[r_rcrvrzr{r�r�r�r�r�r�r�r�r�r�
skipUnless�sys�platformr�r�r�r�r�r�r�r�r�r�r�r�r�r#r#r#r$r�sF@\lJ
	#'2

%(rc@sseZdZdd�Zdd�Zejejd�d�dd��Z	d	d
�Z
dd�Zd
d�ZdS)�	MiscTestscs-|jt�fdd�|jD���dS)Nc3s|]}|j�kVqdS)N)r�)rlr8)�
handler_classr#r$�	<genexpr>Tsz/MiscTests.opener_has_handler.<locals>.<genexpr>)rN�anyr�)rr�r�r#)r�r$�opener_has_handlerSszMiscTests.opener_has_handlercCs�Gdd�dtjj�}Gdd�dtjj�}Gdd�dtjj�}tjj}|||�}|j||�|j||�|||��}|j||�|j||�||�}|j||�|�}|j|tjj�|tjj�}|j|tjj�|tjj��}|j|tjj�Gdd�dtjj�}|||�}|j||�|j||�dS)	Nc@seZdZdS)z2MiscTests.test_build_opener.<locals>.MyHTTPHandlerN)rCrrDr#r#r#r$�
MyHTTPHandlerXsr�c@seZdZdd�ZdS)z/MiscTests.test_build_opener.<locals>.FooHandlercSsdS)Nr#)rr#r#r$�foo_open\sz8MiscTests.test_build_opener.<locals>.FooHandler.foo_openN)rCrrDrr#r#r#r$�
FooHandler[src@seZdZdd�ZdS)z/MiscTests.test_build_opener.<locals>.BarHandlercSsdS)Nr#)rr#r#r$�bar_open`sz8MiscTests.test_build_opener.<locals>.BarHandler.bar_openN)rCrrDrr#r#r#r$�
BarHandler_src@seZdZdS)z7MiscTests.test_build_opener.<locals>.MyOtherHTTPHandlerN)rCrrDr#r#r#r$�MyOtherHTTPHandler|sr)r)r
r��BaseHandlerr�r�)rr�rrr�r�rr#r#r$�test_build_openerWs,	zMiscTests.test_build_openerZnetworkztest requires network accesscCs�tjd���tjj�}tjjd�}|jd|j�|j|dj	d��|jd|j�|jd|j
d��|j|dj	d��|jd|j�|jd|j
d��WdQRXdS)	Nzhttp://www.example.com/razus-ascii�1zContent-lengthZ
1234567890s
1234567890rn)rZtransient_internetr)r
r�rrrxrzrrT)rr�r
r#r#r$�test_issue16464�szMiscTests.test_issue16464cCs�d}d}}}d}tjj|||||�}|jt|d��|j|jd�|jt|d��|j|jd�d|j|j	f}|jt
|�|�d|j|j	f}|jt|�|�dS)z�
        Issue 13211 reveals that HTTPError didn't implement the URLError
        interface even though HTTPError is a subclass of URLError.
        zsomething bad happenedNzContent-Length: 42r=rFzHTTP Error %s: %sz<HTTPError %s: %r>)r)rrqrNr�rr=rFr�r�r@r�)rr�rHr�r�rQrAZexpected_errmsgr#r#r$�test_HTTPError_interface�sz"MiscTests.test_HTTPError_interfacecCsqddddddddd!d#d%d'g}x*|D]"\}}|jt|�|�q1W|jttd�fdS)(N�proxy.example.com�proxy.example.com:3128�joe:password@proxy.example.comrXrY�#joe:password@proxy.example.com:3128�http://proxy.example.com/r��http://proxy.example.com:3128/�&http://joe:password@proxy.example.com/�*http://joe:password@proxy.example.com:3128�1ftp://joe:password@proxy.example.com/rubbish:3128r0�%http://joe:password@proxy.example.comzfile:/ftp.example.com�NNNr
)r
r�NNNr)rr�NNNr
)r
r�NNNr)rr�NrXrYr
)rr�NrXrYr)r
r�r�NNr
)rr�r�NNr)rr�r�rXrYr
)rr�r�rXrYr)rr�r0rXrYr
)rr�r�rXrYr
)rr)rrr'r()rZparse_proxy_test_casesZtcr�r#r#r$�test_parse_proxy�s4	zMiscTests.test_parse_proxyc
CsLt�}|jt��}|jd�WdQRX|jt|j�d�dS)NZinvalidz5Unsupported digest authentication algorithm 'invalid')r
r'r(Zget_algorithm_implsrr@Z	exception)rr�excr#r#r$�test_unsupported_algorithm�s	z$MiscTests.test_unsupported_algorithmN)
rCrrDr�rrr�rZis_resource_enabledrr	r r"r#r#r#r$r�Qs,%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)rCrrDr�r#r#r#r$�
PutRequest�sr%cCsptd�|_tdddddi�|_tddd�|_|jd�|_|jddd�|_dS)	Nzhttp://www.python.org/~jeremy/rxrFzX-Testr�r��HEADr<)rr(�post�headr%�put�
force_post)rr#r#r$�setUp�szRequestTests.setUpcCs�|jd|jj��|jd|jj��|jd|jj��|jd|jj��|jd|jj��dS)Nr<r;r&r$)rr'rsr(r(r)r*)rr#r#r$�test_method�s
zRequestTests.test_methodcCsh|j|jj�|jd|jj��d|j_|j|jj�|jd|jj��dS)Nr;r`r<)rRr(rxrrsrN)rr#r#r$�	test_data�s
zRequestTests.test_datacCsi|jd|jj�|jjdd�|jd|jjd�d|j_|jd|jj�dS)NzContent-length�*r`)rMr(rNrUrrx)rr#r#r$�.test_setting_data_should_remove_content_length�s
z;RequestTests.test_setting_data_should_remove_content_lengthcCsr|jd|jj�d|j_|jjdd�|jd|jjd�|j`|jd|jj�dS)NzContent-lengthr[rH)rMr(rNrxrUr)rr#r#r$�/test_deleting_data_should_remove_content_length�s	z<RequestTests.test_deleting_data_should_remove_content_lengthcCs|jd|jj��dS)Nzhttp://www.python.org/~jeremy/)rr(r�)rr#r#r$�test_get_full_urls	zRequestTests.test_get_full_urlcCs9|jd|jj�td�}|jd|j�dS)Nz	/~jeremy/zhttp://www.python.org/r&)rr(r�r)rrVr#r#r$�
test_selectorszRequestTests.test_selectorcCs|jd|jj�dS)Nr�)rr(r8)rr#r#r$�
test_get_typeszRequestTests.test_get_typecCs|jd|jj�dS)Nzwww.python.org)rr(r�)rr#r#r$�
test_get_hostszRequestTests.test_get_hostcCs#td�}|jd|j�dS)Nzhttp://www.%70ython.org/zwww.python.org)rrr�)rrVr#r#r$�test_get_host_unquotesz"RequestTests.test_get_host_unquotecCso|j|jj��|jjdd�|j|jj��|jd|jj�|jd|jj�dS)Nzwww.perl.orgr�zwww.python.org)rRr(Z	has_proxyrUrNrrar�)rr#r#r$r�s
zRequestTests.test_proxycCs#td�}|jd|j�dS)Nz<URL:http://www.python.org>zwww.python.org)rrr�)rrVr#r#r$�test_wrapped_urlszRequestTests.test_wrapped_urlcCsjtd�}|jd|j�td�}|jd|j�d}t|�}|j|j�|�dS)Nz-http://www.python.org/?qs=query#fragment=truez
/?qs=queryzhttp://www.python.org/#fun=truer&z.http://docs.python.org/library/urllib2.html#OK)rrr�r�)rrVrHr#r#r$�test_url_fragment!szRequestTests.test_url_fragmentcCsIdddg}x3|D]+}t|�}|j|j�|j�qWdS)Nzhttp://docs.python.orgz.http://docs.python.org/library/urllib2.html#OKz-http://www.python.org/?qs=query#fragment=true)rrr�rW)rr9rHrVr#r#r$�test_url_fullurl_get_full_url,s	
z*RequestTests.test_url_fullurl_get_full_urlN)rCrrDrr%r+r,r-r/r0r1r2r3r4r5r�r6r7r8r#r#r#r$r#�s

r#�__main__)6rr�rrrr�r~rPr�Zurllib.requestr)rrrrrr	r
Zurllib.parserr�Zhttp.clientr�ZTestCaserrErwr�r�r�r�r�r�r��IOBaser�r�r�r�r�r
rr�r�r�r�r�r�rrr�r#rC�mainr#r#r#r$�<module>sN43�

+6"
������a