HEX
Server: Apache
System: Linux opal14.opalstack.com 3.10.0-1160.108.1.el7.x86_64 #1 SMP Thu Jan 25 16:17:31 UTC 2024 x86_64
User: curbgloabal_opal (1234)
PHP: 8.1.29
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib64/python3.6/test/__pycache__/test_urllib2.cpython-36.pyc
3

ai�/�@sddlZddlmZddlmZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlm
Z
mZmZmZmZmZmZddlmZddlZddlZGdd�dej�ZGdd	�d	ej�ZGd
d�d�ZGdd
�d
�ZGdd�de�ZGdd�dej �Z!Gdd�d�Z"Gdd�d�Z#Gdd�dej$�Z%Gdd�d�Z&Gdd�d�Z'dd�Z(dd�Z)Gd d!�d!ej*j+�Z,Gd"d#�d#ej*j-�Z.Gd$d%�d%ej*j+�Z/Gd&d'�d'�Z0Gd(d)�d)ej�Z1d*d+�Z2Gd,d-�d-ej�Z3Gd.d/�d/ej�Z4Gd0d1�d1ej�Z5e6d2k�rej7�dS)3�N)�support)�test_urllib)�Request�OpenerDirector�HTTPBasicAuthHandler�HTTPPasswordMgrWithPriorAuth�_parse_proxy�_proxy_bypass_macosx_sysconf�AbstractDigestAuthHandler)�urlparsec@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�TrivialTestsc	Cs�x�dD]�}i}td||�|d=|dkrftjdkrf|jd	�|jd
�}}|j|jd�|j|jd�x4|j�D](\}}|j|jd|d
|||jf�qpWqWdS)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#�$/usr/lib64/python3.6/test_urllib2.py�test___all__ s
zTrivialTests.test___all__cCsh|jttjjd�tjjtjj�j	tj
d�}tjdkr@d|}nd|}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
Csdddddgfdddgfdddd	d
ddd
gfddddgfg}x$|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_listAs
z!TrivialTests.test_parse_http_listcCs"tjjd�}|j|jt|��dS)N�reason)r)r�URLError�assertInr=�str)r�errr#r#r$�test_URLError_reasonstrLsz$TrivialTests.test_URLError_reasonstrN)�__name__r�__qualname__r%r3r<rBr#r#r#r$rsrc@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�RequestHdrsTestscCsDd}|jt|ddid�jdd�|jt|ddid�jdd�dS)a(
        The Request.headers dictionary is not a documented interface.  It
        should stay that way, because the complete set of headers are only
        accessible through the .get_header(), .has_header(), .header_items()
        interface.  However, .headers pre-dates those methods, and so real code
        will be using the dictionary.

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

        zhttp://example.comz	Spam-eggs�blah)�headersz	spam-EggSN)rrrG)r�urlr#r#r$�test_request_headers_dictSs

z*RequestHdrsTests.test_request_headers_dictcCs�d}t|ddid�}|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.com�	Spam-eggsrF)rGzFoo-Bar�baz�Foo-barz	Not-there�defaultzUnredirected-spamZEggsN)rJrF)rLrK)rJrF)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_methodshs 


z-RequestHdrsTests.test_request_headers_methodscCs�tjj�}|j}|j}|dddd�|dddd�|dddd	�|dd
dd�|dd
dd�|j|dd�d#�|j|dd�d$�|j|dd�d%�|j|dd�d&�|j|dd�d'�|j|dd�d(�|d	ddd�|d	ddd�|j|d	d�d)�|j|d	d�d*�|dddd�|dddd�|j|dd�d+�|dddd�|j|dd�d,�|j|dd �d-�|j|dd!�d.�|j|dd"�d/�|j|dd�d0�|j|dd
�d1�|j|dd
�d2�dS)3Nz
Some Realmzhttp://example.com/�joe�passwordzhttp://example.com/ni�nizhttp://c.example.com:3128�3r6z
d.example.com�4�dze.example.com:3128�5�ezexample.comzhttp://example.comzhttp://example.com/spamzhttp://example.com/spam/spamzhttp://example.com/foo�foozhttp://example.com/bar�bar�ninir5�firstrF�second�spamr4�1zhttp://a.example.com/z
c.example.comzc.example.com:3128)rXrY)rXrY)rXrY)rXrY)rXrY)rXrY)r`rZ)rarb)rdre)rfr4)NN)NN)r[r6)r[r6)r\r])r^r_)r)r
�HTTPPasswordMgr�add_password�find_user_passwordr)r�mgr�add�find_user_passr#r#r$�test_password_manager�s`
z&RequestHdrsTests.test_password_managercCsrtjj�}|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$rEQs$FrEc@s(eZdZgZdejfdd�Zdd�ZdS)�
MockOpenerNcCs||||_|_|_dS)N)rV�data�timeout)rrVrxryr#r#r$�open�szMockOpener.opencGs|||_|_dS)N)�proto�args)rr{r|r#r#r$rszMockOpener.error)rCrrD�
addheaders�socket�_GLOBAL_DEFAULT_TIMEOUTrzrr#r#r#r$rw�srwc@s(eZdZddd�Zd	dd�Zdd�ZdS)
�MockFileNcCsdS)Nr#)r�countr#r#r$r0sz
MockFile.readcCsdS)Nr#)rr�r#r#r$�readline
szMockFile.readlinecCsdS)Nr#)rr#r#r$r1
szMockFile.close)N)N)rCrrDr0r�r1r#r#r#r$r�s

r�c@seZdZdd�ZdS)�MockHeaderscCst|j��S)N)r;�values)rrr#r#r$�
getheadersszMockHeaders.getheadersN)rCrrDr�r#r#r#r$r�sr�c@s&eZdZddd�Zdd�Zdd�ZdS)	�MockResponseNcCs.tjj||�||||f\|_|_|_|_dS)N)�io�StringIO�__init__�code�msgrGrH)rr�r�rGrxrHr#r#r$r�szMockResponse.__init__cCs|jS)N)rG)rr#r#r$�infoszMockResponse.infocCs|jS)N)rH)rr#r#r$�geturlszMockResponse.geturl)N)rCrrDr�r�r�r#r#r#r$r�s
r�c@seZdZdd�Zdd�ZdS)�
MockCookieJarcCs
||_dS)N)�ach_req)rr
r#r#r$�add_cookie_header#szMockCookieJar.add_cookie_headercCs|||_|_dS)N)�ec_req�ec_r)rrr
r#r#r$�extract_cookies&szMockCookieJar.extract_cookiesN)rCrrDr�r�r#r#r#r$r�"sr�c@seZdZdd�Zdd�ZdS)�
FakeMethodcCs||_||_||_dS)N)�	meth_name�handle�action)rr�r�r�r#r#r$r�+szFakeMethod.__init__cGs|j|j|jf|��S)N)r�r�r�)rr|r#r#r$�__call__0szFakeMethod.__call__N)rCrrDr�r�r#r#r#r$r�*sr�c@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�MockHTTPResponsecCs"||_||_||_||_d|_dS)N��)�fpr��statusr=r�)rr�r�r�r=r#r#r$r�5s
zMockHTTPResponse.__init__cCsdS)N�r#)rr#r#r$r0<szMockHTTPResponse.readcCsiS)Nr#)rr#r#r$r�?szMockHTTPResponse.infocCs|jS)N)rH)rr#r#r$r�BszMockHTTPResponse.geturlN)rCrrDr�r0r�r�r#r#r#r$r�4sr�c@sTeZdZdd�Zejfdd�Zdd�Zddd	�Zdd
d�dd
�Z	dd�Z
dd�ZdS)�
MockHTTPClasscCs(d|_g|_d|_d|_d|_i|_dS)NrF)�level�req_headersrx�raise_on_endheadersZsock�_tunnel_headers)rr#r#r$r�GszMockHTTPClass.__init__cCs||_||_|S)N)�hostry)rr�ryr#r#r$r�OszMockHTTPClass.__call__cCs
||_dS)N)r�)rr�r#r#r$�set_debuglevelTszMockHTTPClass.set_debuglevelNcCs&||_||_|r||_n
|jj�dS)N)�_tunnel_hostZ_tunnel_portr��clear)rr��portrGr#r#r$�
set_tunnelWs
zMockHTTPClass.set_tunnelF)�encode_chunkedcCsP||_||_|dk	r&|j|j�7_|jj�|r:||_||_|jrLt��dS)N)	�method�selectorr�r�sortrxr�r��OSError)rr�rH�bodyrGr�r#r#r$r
_s
zMockHTTPClass.requestcCstt�idd�S)Nr��OK)r�r�)rr#r#r$�getresponselszMockHTTPClass.getresponsecCsdS)Nr#)rr#r#r$r1oszMockHTTPClass.close)NN)NN)rCrrDr�r~rr�r�r�r
r�r1r#r#r#r$r�Fs
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�xszMockHandler.__init__cCsNxH|D]@}t|�dkr |\}}n
|d}}t|||j�}t|j||�qWdS)N�)�lenr�r��setattr�	__class__)rr��specrr��methr#r#r$r�{s


zMockHandler._define_methodscOs�|jjj||||f�|dkr"dS|dkr.|S|dkrHtddid�}|S|dkrXtd�S|jd�r�||jd	�d
d�}yt|�}Wntk
r�YnXtddid�}|jj	d|d||di�S|d
kr�t
j	jd��ds�t�dS)Nzreturn selfzreturn responser�r�r�zreturn requestzhttp://blah/r� ��httpr�raiserFF)
�parent�calls�appendr�r�
startswith�rfind�intr(rr)r>�AssertionError)rZfn_namer�r|�kwds�resr�r#r#r$r��s*
zMockHandler.handlecCsdS)Nr#)rr#r#r$r1�szMockHandler.closecCs||_g|j_dS)N)r�r�)rr�r#r#r$�
add_parent�szMockHandler.add_parentcCst|d�sdS|j|jkS)N�
handler_orderT)�hasattrr�)r�otherr#r#r$�__lt__�s
zMockHandler.__lt__N)
rCrrDr�r�r�r�r1r�r�r#r#r#r$r�ss	r�cCsfg}d}xX|D]P}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@seZdZdS)z6add_ordered_mock_handlers.<locals>.MockHandlerSubclassN)rCrrDr#r#r#r$�MockHandlerSubclass�sr�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|�qW|S)N)rr�)Zhandler_instancesr�r8r#r#r$�build_test_opener�s
r�c@s$eZdZdd�Zdd�Zdd�ZdS)�MockHTTPHandlercCs||_||_|j�dS)N)r�rG�reset)rr�rGr#r#r$r��szMockHTTPHandler.__init__cCsd|_g|_dS)Nr)�_count�requests)rr#r#r$r��szMockHTTPHandler.resetcCs�ddl}ddl}|jj|j|��|jdkrl|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_stringrGr�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��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|j|�S)N)�do_openr�)rrVr#r#r$�
https_open�szMockHTTPSHandler.https_openN)r)rCrrDr�r�r#r#r#r$r��s
r�c@s$eZdZdd�Zdd�Zdd�ZdS)�MockHTTPHandlerCheckAuthcCs||_d|_dS)NF)r��has_auth_header)rr�r#r#r$r��sz!MockHTTPHandlerCheckAuth.__init__cCs
d|_dS)NF)r�)rr#r#r$r�szMockHTTPHandlerCheckAuth.resetcCs6|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��sr�c@seZdZdd�Zdd�ZdS)�MockPasswordManagercCs||_||_||_||_dS)N)�realmrH�userrY)rr�Zurir�rYr#r#r$rhsz MockPasswordManager.add_passwordcCs||_||_|j|jfS)N)�target_realm�
target_urlr�rY)rr�Zauthurir#r#r$risz&MockPasswordManager.find_user_passwordN)rCrrDrhrir#r#r#r$r�
sr�c@sDeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dS)�OpenerDirectorTestscCs(Gdd�dt�}|jtt�j|��dS)Nc@seZdZdS)z<OpenerDirectorTests.test_add_non_handler.<locals>.NonHandlerN)rCrrDr#r#r#r$�
NonHandlersr�)�objectr'�	TypeErrorrr�)rr�r#r#r$�test_add_non_handlersz(OpenerDirectorTests.test_add_non_handlercCs`ddlm}t�}ddgd
gg}t||�|jtjj��x dD]}|j||j	|d
�q@WdS)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_methods"s	

z,OpenerDirectorTests.test_badly_named_methodsc
Cs�t�}dddgdgdgd	gg}t||�}td�}|j|�}|j||d�|ddf|ddfg}xBt||j�D]2\}}|\}	}
}}|j|	|
f|�|j||f�qpWdS)
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_handled7s

z OpenerDirectorTests.test_handledcCs�t�}g}xRd	gdfdgdfgD]:\}}Gdd�dt�}||�}||_|j|�|j|�q W|jd�|j|jdd|d�|j|jdd|d�dS)
Nr��return selfi�rc@seZdZdS)zCOpenerDirectorTests.test_handler_order.<locals>.MockHandlerSubclassN)rCrrDr#r#r#r$r�Vsr�zhttp://example.com/r�)r�r)rr�r�r�r�rzrr�)rr�r�r�r�r�r8r#r#r$�test_handler_orderQs


z&OpenerDirectorTests.test_handler_ordercCsZt�}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_raisecs
zOpenerDirectorTests.test_raisecCs�t�}dgddgdddgdgg}t||�}Gd	d
�d
�}td�}|j|�t|j�dks\t�|d
d|ff|dd||�ddiffg}xJt||j�D]:\}}|\}	}
}|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__~sz;OpenerDirectorTests.test_http_error.<locals>.Unknown.__eq__N)rCrrDrr#r#r#r$�Unknown}sr
zhttp://example.com/r�ri.r�)r�r	)r
r�)r�r)	rr�rrzr�r�r�r�r)rr�r�r�r
rVr�rrrZmethod_namer|r#r#r$�test_http_errorps$


z#OpenerDirectorTests.test_http_errorcCst�}d	d
gddgg}t||�}td�}|j|�|ddf|ddf|ddf|ddfg}x�t|j�D]�\}\}}}	}
|dkr�|j||f||�|jt|	�d�|j|	dt�qj|j||f||�|jt|	�d�|j|	dt�|	ddk	rj|j|	dt	�qjWdS)
N�http_request�return request�
http_response�return responsezhttp://example.com/rr�r�)rr)rr)rr)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�cCs^y|jd�Wntk
r,tjd��YnXtjj|�}tjdkrZ|j	d�rZ|dd�}|S)Nzutf-8zpath is not encodable to utf8rz///r�)
�encode�UnicodeEncodeError�unittestZSkipTestr)r
rrrr�)r+�urlpathr#r#r$�sanepathname2url�src@s^eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Zd7d8�Zej e!j"d9kd:�d;d<��Z#d=d>�Z$d?d@�Z%dAdB�Z&dCdD�Z'dEdF�Z(dGdH�Z)dIdJ�Z*dKdL�Z+dMdN�Z,dOdP�Z-dQdR�Z.dSS)T�HandlerTestscs�Gdd�d��G�fdd�dtjj�}ddl}d}||�}t�|_�xjdd|jd	d	d
ddgd
df	dd|jdd	d
ddgd
df	dd|jdd	d
ddgd
df	dd|jdd	d
ddgd
df	dddd	d	dddgd	df	dd|jd	d	dgddf	gD]�\	}}}}}	}
}}}
t|�}d|_|j	|�}|j
|j|�|j
|j|	�|j
|j
tj|��|j
|j|�|j
|j|�|j
|jj|�|j
|jj|
�|j�}|j
|jd�|
�|j
t|d�t|��q�WdS)Nc@s$eZdZdd�Zdd�Zdd�ZdS)z-HandlerTests.test_ftp.<locals>.MockFTPWrappercSs
||_dS)N)rx)rrxr#r#r$r��sz6HandlerTests.test_ftp.<locals>.MockFTPWrapper.__init__cSs$|||_|_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�srcs&eZdZdd�Zejf�fdd�	ZdS)z-HandlerTests.test_ftp.<locals>.NullFTPHandlercSs
||_dS)N)rx)rrxr#r#r$r��sz6HandlerTests.test_ftp.<locals>.NullFTPHandler.__init__cs4|||_|_|||_|_||_�|j�|_|jS)N)r��passwdr�r��dirsrx�
ftpwrapper)rr�rr�r�r ry)rr#r$�connect_ftp�s
z9HandlerTests.test_ftp.<locals>.NullFTPHandler.connect_ftpN)rCrrDr�r~rr"r#)rr#r$�NullFTPHandler�sr#rzrheum rhaponicumz ftp://localhost/foo/bar/baz.html�	localhostr��Ir`razbaz.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�r r!rrr��getr�r�)rr#r)rxr8rHr�r�r�rZtype_r rZmimetyperVrrGr#)rr$�test_ftp�sL
"
zHandlerTests.test_ftpc#Cs�ddl}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�}
zpz|
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]T}	z@t|d�}
z|
j|�Wd|
j�X|j tj!j"|jt|	��Wdt	j|�X�q�Wtjj�}t�}|_x|d!D]t\}	}t|	�}y|j|�Wn$tj!j"k
�rd|j#|�YnX|j$|j%|�|j|j&d�|j|j&dk|��q"WdS)"Nrs
hello, world
zfile://localhost%sz	file://%szfile://%s%sr$r��wbT)Zusegmtz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�r0F�r1F�r2F)r4r5r6r7r8)'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�r9rZtowrite�urlsZ	localaddrrHr2rrxrGZrespurlZstatsZmodifiedr3rVr#r#r$�	test_file�s�











zHandlerTests.test_filecCsttjj�}t�}|_d}�x�dD]�\}}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|jddd g�|j|j|�q"Wd|_|jtjj|j	||�tdd�}|jt|j|�d!g|_�x$d"D�]}td|�}t d	d
id�}|j|�}
|dk�r�|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��qPWdS)#Nzhttp://example.com/�GET�POST�blah�Foora�Spam�eggsr�r�zexample.comrr&�
Connectionr1TZbadpost�r�zContent-lengthzContent-type�0z!application/x-www-form-urlencoded�Hostr`rK�rCN�rDrE)rMrN)rIr1)rFra)rGrH)rGrH)rJN)#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_hdrs)rr8r�rHr�rxrVr�r�hdrs�newreqr#r#r$�	test_httpZsh






zHandlerTests.test_httpc	Cs�tjj�}t�}|_tjddd�}|j}|j�|j	t
j|�t|d��B}t
d|i�}|j|�}|jd�}|j|d�|j|jd��WdQRXt|d��F}t
d|d	d
i�}|j|�}|jt|jd��d
�|j|jd��WdQRXdS)Nzw+bF)�mode�delete�rbzhttp://example.com/zTransfer-encoding�chunkedzContent-lengthzContent-Length�)r)r
r�rwr��tempfileZNamedTemporaryFilerr1�
addCleanupr�unlinkrzrrPrTrrRrOr�)	rr8r��file_objZ	file_pathr2rVrTZter#r#r$�test_http_body_file�s"



z HandlerTests.test_http_body_filecCs�tjj�}t�}|_tj�}td|i�}|j|�}|j	|j
d�d�|j|jd��ddi}td||�}|j|�}|j	t
|j
d��d�|j|jd��|j�dS)Nzhttp://example.com/zTransfer-encodingrYzContent-lengthzContent-LengthrZ)r)r
r�rwr�r��BytesIOrrPrrTrRrOr�r1)rr8r�r^rVrTrGr#r#r$�test_http_body_fileobj�s


z#HandlerTests.test_http_body_fileobjcCs�tjj�}t�}|_tjddg}x�iddifD]x}tj|tj	d��^}t
d|j|�}|j|�}|s�|j
|jd�d�|j
|jd�d	�n|j
t|jd��d�WdQRXq0WdS)
Nz-c�passzContent-LengthrZ)�stdoutzhttp://example.com/zContent-lengthzTransfer-encodingrY)r)r
r�rwr��sys�
executable�
subprocess�Popen�PIPErrcrPrrTr�)rr8r��cmdrG�procrVrTr#r#r$�test_http_body_pipe�s

z HandlerTests.test_http_body_pipecCs�tjj�}t�}|_dd�}xliddifD]\}td|�|�}|j|�}|sr|j|jd�d�|j|jd�d�q,|jt	|jd��d�q,WdS)	Ncss
dVdS)Nsoner#r#r#r#r$�
iterable_body�sz;HandlerTests.test_http_body_iterable.<locals>.iterable_bodyzContent-Length�zhttp://example.com/zContent-lengthzTransfer-encodingrY)
r)r
r�rwr�rrPrrTr�)rr8r�rlrGrVrTr#r#r$�test_http_body_iterable�s

z$HandlerTests.test_http_body_iterablecCsHtjj�}t�|_|jtdf��}|j|jd�d�|j	|j
d��dS)Nzhttp://example.com/zTransfer-encodingrYzContent-length)r)r
r�rwr�rPrrrTrRrO)rr8rVr#r#r$�test_http_body_empty_seq�s

z%HandlerTests.test_http_body_empty_seqcCsptjj�}t�}|_tjdddddg�}x@iddifD]0}td||�}|j|�}|jt	|j
d	��d�q8WdS)
Nr%r�r���zContent-Length�zhttp://example.com/zContent-length)r)r
r�rwr��arrayrrPrr�rT)rr8r�Ziterable_arrayrGrVrTr#r#r$�test_http_body_array�s

z!HandlerTests.test_http_body_arraycCs6t�}tdd�}|j|�|jd�|j|jd�dS)Nr�)r�zhttps://www.example.com)rr�r�rzrZ_debuglevel)rr�r8r#r#r$�test_http_handler_debuglevels



z)HandlerTests.test_http_handler_debuglevelcCs�tjj�}t�|_d}ddddg}xZ|D]R}t||�}|j|�}|j|jdd�|j	dd�|j|�}|j|jdd�q(WdS)	NrJz#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.htmlrLzexample.comzsomeproxy:3128)
r)r
r�rwr�rrPrrR�	set_proxy)rr8rxZds_urlsZds_urlZds_reqZ	np_ds_reqZp_ds_reqr#r#r$�test_http_doubleslashs




z"HandlerTests.test_http_doubleslashcCsndddg}td�}xV|D]N}||_t|�}|j|j�|�|j|jpFd|j�|jt|j��j|j�qWdS)Nzhttp://example.com?foo=bar#bazz)http://example.com?foo=bar&spam=eggs#bashzhttp://example.comr�)r�full_urlrrr��fragmentZquery)rrArrHZparsedr#r#r$�test_full_url_setter-s
z!HandlerTests.test_full_url_settercCs6td�}|`|j|j�|j|j�|j|jd�dS)Nzhttp://www.example.comr�)rrxrSryrr�)rrr#r#r$�test_full_url_deleterDs
z"HandlerTests.test_full_url_deletercCsztjj�}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�rrPrr�r�)rr8Z	weird_urlrVrTZurl_without_pathr#r#r$�test_fixpath_in_weirdurlsKs


z&HandlerTests.test_fixpath_in_weirdurlscCstjj�}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�rr?rRr�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�rr?r�r�origin_req_hostrRZunverifiablerr�r�)r�cjr8rVrrTrr#r#r$�test_cookiesws
zHandlerTests.test_cookiescCs�d}d}tjj�}t�}|_�xndD�]d}�x\dD�]R}t|d|�}t||�}tj|_	|j
d	d
�|dk	r�|j
dtt|���|j
dd
�y||t�|dtd|i��Wn.tjjk
r�|j|d�|j|�YnX|j|jj�|�y|j|jj�d�Wn$tk
�r(|j|jj�YnXdd�|jjD�}	|jd|	�|jd|	�|j|jjd	d
�|jd|jj�|jd|jj�q4Wq&Wt|�}tj|_	|fdd�}
t|dd�}d}tj|_	y x|
||d�|d}�q�WWn,tjjk
�r|j|tjjj�YnXt|dd�}d}tj|_	y$x|
||d|�|d}�q.WWn,tjjk
�rz|j|tjjj�YnXdS)Nzhttp://example.com/a.htmlzhttp://example.com/b.html�-�.�/�3�
blah
blah
z
http_error_%sZNonsensezviking=withholdzContent-LengthrGre�Blah�locationrCcSsg|]}|j��qSr#)�lower)�.0�xr#r#r$�
<listcomp>�sz.HandlerTests.test_redirect.<locals>.<listcomp>zcontent-lengthzcontent-typecSs |j|t�ddtd|i��dS)Ni.r�r�)r�r�r�)r8rVrHr#r#r$r��sz,HandlerTests.test_redirect.<locals>.redirectzexample.com)r�rzhttp://example.com/r�zhttp://example.com/%d)r�r�r�r�)Nr�)r)r
�HTTPRedirectHandlerrwr��getattrrr~rryrPr@r�rUr�r�r�	HTTPErrorr�assertIsNotNonerVr��
get_method�AttributeErrorrRrxrGrQrRZmax_repeatsZmax_redirections)r�from_urlZto_urlr8r�r�rxr�rVrGr�r�r#r#r$�
test_redirect�sj

zHandlerTests.test_redirectcCs�d}dddg}dddg}d}tjj�}t�}|_t|�}tj|_x>|D]6}|d	|}	|j	tj
j|j|t
�d
dtd|	i��qHWxF|D]>}|d	|}
|j|t
�d
d
td|
i��|j|jj�|
�q�WdS)Nzhttp://example.com/a.htmlr��httpsr3�fileZimapZldapzexample.com/b.htmlz://i.zSecurity Loopholer�zThat's fine)r)r
r�rwr�rr~rryr'rr�r�r�r�rrVr�)rr�Z
valid_schemesZinvalid_schemesZschemeless_urlr8r�rVr�Zinvalid_url�	valid_urlr#r#r$�test_invalid_redirect�s$





z"HandlerTests.test_invalid_redirectcCsnd}d}tjj�}t�}|_t|�}tj|_tj	j
||�}|j|t�ddt
d|i��|j|jj�|�dS)Nzhttp://example.com/a.htmlz/b.htmli.zThat's finer�)r)r
r�rwr�rr~rryrZurljoinr�r�r�rrVr�)rr�Zrelative_urlr8r�rVr�r#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
�HTTPDefaultErrorHandlerr�r�r�rzrRrVrO)	rr�r�r��hh�hdeh�hrhZcpr�r#r#r$�test_cookie_redirect�s



z!HandlerTests.test_cookie_redirectcCsTd}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�r�r�rzrr��strip)rZredirected_urlr�r�r�r�r�r#r#r$�test_redirect_fragments


z#HandlerTests.test_redirect_fragmentcsntjj�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?querycs2�j|t����j|||f|�|�d|j_dS)NsHTTP/1.1 200 OK

Hello!)r�nextr
r�Zfakedata)�connr�rH�pos�kw)�
real_classrrAr#r$r
sz3HandlerTests.test_redirect_no_path.<locals>.requestzhttp://python.org/pathzhttp://python.org/path?query)r�r�)
r�r�r�r�fakehttpr\r��iterr
r)r*rr�)rZ	response1r
r�r#)r�rrAr$�test_redirect_no_path
s
z"HandlerTests.test_redirect_no_pathc
Cs�Gdd�dtjj�}|�}tjj|�}d}xp|D]h\}}|jt|���Ltjd	|d
�|_|j	d�}d|d
}|j
}	|j|	j|�t|	��WdQRXq0WdS)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 sr��/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_opener�subTest�reprrr�r�rzr�rNr�)
rr�rr�r9r�r�rrr
r#r#r$�test_redirect_encodings
z#HandlerTests.test_redirect_encodingcCs�t�}tjjtdd��}|j|�d
gg}t||�}td�}|j|j	d�|j
|�|j|j	d�|j|ddfgdd	�|jD��dS)Nzproxy.example.com:3128)r�r��return responsezhttp://acme.example.com/zacme.example.comrcSsg|]}|dd��qS)rr�r#)r��tupr#r#r$r�Osz+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_proxyzproxy.example.com)r�zhttp://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_proxyQs



z HandlerTests.test_proxy_no_proxycCsfdtjd<t�}tjjtdd��}|j|�td�}|j	|j
d�|j|�|j	|j
d�tjd=dS)N�*r�zproxy.example.com)r�zhttp://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)Nzproxy.example.com:3128)r�r��return responsezhttps://www.example.com/zwww.example.comrcSsg|]}|dd��qS)rr�r#)r�r�r#r#r$r�ysz1HandlerTests.test_proxy_https.<locals>.<listcomp>)r�r�)rr)r
r�r�r�r�rrr�rzr�)rr�r�r�r�rVr#r#r$�test_proxy_httpsks


zHandlerTests.test_proxy_httpscCs�t�}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)Nzproxy.example.com:3128)r�zhttps://www.example.com/�Proxy-Authorization�FooBar�
User-Agent�Grailzwww.example.comzProxy-authorization)r�r�)r�r�)rr)r
r�r�r�r�rrPrr�rSr�rzrQr�r�r?r�rT)rr�r�Z
https_handlerrVr#r#r$�$test_proxy_https_proxy_authorization{s$




z1HandlerTests.test_proxy_https_proxy_authorization�darwinzonly relevant for OSXcCszddddddgd�}x"dD]}|jt||�d|�qWx"dD]}|jt||�d|�q>Wdgd�}|jtd|��dS)NF�foo.barz	*.bar.com�	127.0.0.1z10.10z10.0/16)Zexclude_simple�
exceptions�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_bypasscCsz|j||d��bt�}t�}tjj|�}dj|�d}td|�}|j|�|j|�|j	||d|||dd�WdQRXdS)N)r�rGz
z

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



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


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

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


z"HandlerTests.test_proxy_basic_authc
	Cs�Gdd�dt�}Gdd�dtjj�}Gdd�dtjj�}|�}t�}||�}||�}d}tdd	|�}	|j|�|j|�|j|	�|j||d
||	|dd�|j	|j
dd
gd�dS)Nc@seZdZdd�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$�recordszXHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.recordN)rCrrDr�r�r#r#r#r$�RecordingOpenerDirector�sr�c@seZdZdd�ZdS)zOHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandlerc_s&|jjd�tjjj|f|�|�dS)Nr�)r�r�r)r
�HTTPDigestAuthHandler�http_error_401)rr|r�r#r#r$r�sz^HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandler.http_error_401N)rCrrDr�r#r#r#r$�TestDigestAuthHandlersr�c@seZdZdd�ZdS)zNHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandlerc_s&|jjd�tjjj|f|�|�dS)Nr�)r�r�r)r
rr�)rr|r�r#r#r$r�sz]HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandler.http_error_401N)rCrrDr�r#r#r#r$�TestBasicAuthHandlersr�z
ACME Networksi�z&WWW-Authenticate: Basic realm="%s"

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



z0HandlerTests.test_basic_and_digest_auth_handlerscCsDt�}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_handlercCsDt�}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_handler0s

z0HandlerTests.test_unsupported_auth_basic_handlerc	CsHddl}	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��decoderTrRr�)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�T)�is_authenticated)r�r�)
r�rrrhrrNrr�rzr�)	rr�rYrr��pwd_manager�auth_prior_handler�is_authr�r#r#r$�test_basic_prior_auth_auto_send_s




z,HandlerTests.test_basic_prior_auth_auto_sendc
Cs�d\}}d}d}t�}t|�}|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�)rrrhrrRrr�r�rzrNr�r�)
rr�rYrr�rrrr�r�r#r#r$�.test_basic_prior_auth_send_after_first_successys.









z;HandlerTests.test_basic_prior_auth_send_after_first_successcCs�xzd
D]r\}}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
�rr�r
r
)rrr)�formatrr�rr)r
r�rryr�r0rN�fakesock�closed)rZtransferrx�headerr�rrVZrespr#r#r$�test_http_closed�s


zHandlerTests.test_http_closedc
Cs\tjd�}tjj�}td�}d|_|jtj	j
��|j||�WdQRX|j|j
jd�dS)z;Test the connection is cleaned up after an invalid responserJz
http://dummy/NzConnection not closed)rr�r)r
r�rryr'r�r�Z
BadStatusLiner�rNrr)rr�rrVr#r#r$�test_invalid_closed�s

z HandlerTests.test_invalid_closedN)/rCrrDr,rBrUr_rarkrnrortrurwrzr{r|r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�
skipUnlessrd�platformr�r�r�r�r�r�r�r�rrrrr#r#r#r$r�sP@\B
J
	#-2

%(rc@sNeZdZdd�Zdd�Zejejd�d�dd��Z	d	d
�Z
dd�Zd
d�ZdS)�	MiscTestscs"|jt�fdd�|jD���dS)Nc3s|]}|j�kVqdS)N)r�)r�r8)�
handler_classr#r$�	<genexpr>�sz/MiscTests.opener_has_handler.<locals>.<genexpr>)rN�anyr�)rr�rr#)rr$�opener_has_handler�szMiscTests.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@seZdZdS)z2MiscTests.test_build_opener.<locals>.MyHTTPHandlerN)rCrrDr#r#r#r$�
MyHTTPHandler�src@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�sr c@seZdZdd�ZdS)z/MiscTests.test_build_opener.<locals>.BarHandlercSsdS)Nr#)rr#r#r$�bar_open�sz8MiscTests.test_build_opener.<locals>.BarHandler.bar_openN)rCrrDr!r#r#r#r$�
BarHandler�sr"c@seZdZdS)z7MiscTests.test_build_opener.<locals>.MyOtherHTTPHandlerN)rCrrDr#r#r#r$�MyOtherHTTPHandler�sr#)r)r
r��BaseHandlerr�r)rrr r"r�r�r#r#r#r$�test_build_opener�s,

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/rfzus-ascii�1zContent-lengthZ
1234567890s
1234567890rn)rZtransient_internetr)r
r�rrrxrzrrT)rr�r
r#r#r$�test_issue16464�s
zMiscTests.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=rGzHTTP Error %s: %sz<HTTPError %s: %r>)r)rr�rNr�rr=rGr�r�r@r�)rr�rHr�r�rSrAZexpected_errmsgr#r#r$�test_HTTPError_interface�sz"MiscTests.test_HTTPError_interfacecCsRddddddddd!d#d%d'g}x |D]\}}|jt|�|�q"W|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:3128r3�%http://joe:password@proxy.example.comzfile:/ftp.example.com�NNNr))r)r3�NNNr*)r*r4�NNNr))r)r5�NNNr*)r*r6�NrXrYr))r+r7�NrXrYr*)r,r8�r�NNr))r-r9�r�NNr*)r.r:�r�rXrYr))r/r;�r�rXrYr*)r0r<�r3rXrYr))r1r=�r�rXrYr))r2r>)rrr'r()rZparse_proxy_test_casesZtcrr#r#r$�test_parse_proxys4zMiscTests.test_parse_proxyc
Cs<t�}|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_algorithm6sz$MiscTests.test_unsupported_algorithmN)
rCrrDrr%rrrZis_resource_enabledr'r(r?rAr#r#r#r$r�s,%rc@s�eZdZGdd�de�Zdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�ZdS) �RequestTestsc@seZdZdZdS)zRequestTests.PutRequest�PUTN)rCrrDr�r#r#r#r$�
PutRequestAsrDcCsLtd�|_tddddid�|_tddd�|_|jd�|_|jddd�|_dS)	Nzhttp://www.python.org/~jeremy/rxzX-Testr�)rG�HEAD)r�rD)rr+�post�headrD�put�
force_post)rr#r#r$�setUpDs
zRequestTests.setUpcCs^|jd|jj��|jd|jj��|jd|jj��|jd|jj��|jd|jj��dS)NrDrCrErC)rrFr�r+rGrHrI)rr#r#r$�test_methodNs
zRequestTests.test_methodcCsL|j|jj�|jd|jj��d|j_|j|jj�|jd|jj��dS)NrCrerD)rRr+rxrr�rN)rr#r#r$�	test_dataUs
zRequestTests.test_datacCsN|jd|jj�|jjdd�|jd|jjd�d|j_|jd|jj�dS)NzContent-length�*re)rQr+rRrUrrx)rr#r#r$�.test_setting_data_should_remove_content_length_s
z;RequestTests.test_setting_data_should_remove_content_lengthcCsT|jd|jj�d|j_|jjdd�|jd|jjd�|j`|jd|jj�dS)NzContent-lengthr`rp)rQr+rRrxrUr)rr#r#r$�/test_deleting_data_should_remove_content_lengthgs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_urloszRequestTests.test_get_full_urlcCs*|jd|jj�td�}|jd|j�dS)Nz	/~jeremy/zhttp://www.python.org/r&)rr+r�r)rrVr#r#r$�
test_selectorsszRequestTests.test_selectorcCs|jd|jj�dS)Nr�)rr+r@)rr#r#r$�
test_get_typexszRequestTests.test_get_typecCs|jd|jj�dS)Nzwww.python.org)rr+r�)rr#r#r$�
test_get_host{szRequestTests.test_get_hostcCstd�}|jd|j�dS)Nzhttp://www.%70ython.org/zwww.python.org)rrr�)rrVr#r#r$�test_get_host_unquote~sz"RequestTests.test_get_host_unquotecCsR|j|jj��|jjdd�|j|jj��|jd|jj�|jd|jj�dS)Nzwww.perl.orgr�zwww.python.org)rRr+Z	has_proxyrvrNrr�r�)rr#r#r$r��s
zRequestTests.test_proxycCstd�}|jd|j�dS)Nz<URL:http://www.python.org>zwww.python.org)rrr�)rrVr#r#r$�test_wrapped_url�szRequestTests.test_wrapped_urlcCsLtd�}|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�szRequestTests.test_url_fragmentcCs6dddg}x&|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�rx)rrArHrVr#r#r$�test_url_fullurl_get_full_url�s
z*RequestTests.test_url_fullurl_get_full_urlN)rCrrDrrDrJrKrLrNrOrPrQrRrSrTr�rUrVrWr#r#r#r$rB@s

rB�__main__)8rr�rrrr�r~rsrdr[rfZurllib.requestr)rrrrrr	r
Zurllib.parserr�Zhttp.clientr�ZTestCaserrErwr�r�r�r�r�r�r��IOBaser�r�r�r�r�r
r$r�r�r�r�r�r�rrrrBrC�mainr#r#r#r$�<module>sb$3,

-6"
	a