HEX
Server: Apache
System: Linux bd12.noc223.com 4.18.0-553.121.1.lve.el8.x86_64 #1 SMP Thu Apr 30 16:40:41 UTC 2026 x86_64
User: handcraf (1693)
PHP: 8.1.28
Disabled: NONE
Upload Files
File: //proc/self/root/opt/alt/python312/lib64/python3.12/test/__pycache__/test_urllib2.cpython-312.pyc
�

�h�e�=��>�ddlZddlmZddlmZddlmZddlmZddlmZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlmZmZmZmZmZmZmZddlmZddlZddlZej:d	�
�Gd�dej<�ZGd
�dej<�Z Gd�d�Z!Gd�d�Z"Gd�de#�Z$Gd�de	jJ�Z&Gd�d�Z'Gd�d�Z(Gd�de	jR�Z*Gd�d�Z+Gd�d �Z,d!�Z-d"�Z.Gd#�d$ej^j`�Z1Gd%�d&ej^jd�Z3e4ejjd'�rGd(�d)ej^jl�Z7Gd*�d+ej^jd�Z8Gd,�d-�Z9Gd.�d/ej<�Z:d0�Z;Gd1�d2ej<�Z<Gd3�d4ej<�Z=Gd5�d6ej<�Z>e?d7k(rej��yy)8�N)�support)�	os_helper)�warnings_helper)�test_urllib)�mock)�Request�OpenerDirector�HTTPBasicAuthHandler�HTTPPasswordMgrWithPriorAuth�_parse_proxy�_proxy_bypass_macosx_sysconf�AbstractDigestAuthHandler)�urlparseT)�modulec�$�eZdZd�Zd�Zd�Zd�Zy)�TrivialTestsc��dD]�}i}td|z|�|d=|dk(rmtjdk(rZ|jd�|jd�}}|j	|j
d�|j	|j
d�|j
�D]7\}}|j	|j
d	|z|�d
|�d|j
����9��y)N)�request�response�parse�error�robotparserzfrom urllib.%s import *�__builtins__r�nt�url2pathname�pathname2url�
nturl2pathz	urllib.%sz is exposed in 'urllib.z' but defined in )�exec�os�name�pop�assertEqual�
__module__�items)�selfr�context�u�p�k�vs       �8/opt/alt/python312/lib64/python3.12/test/test_urllib2.py�test___all__zTrivialTests.test___all__%s���L�	/�F��G��*�V�3�W�=���'���"�r�w�w�$���{�{�>�2�G�K�K��4O�1��� � ����|�<�� � ����|�<��
�
��
/���1�� � ����{�V�/C������.�/�
/�	/�c�"�|jtjj�|j	t
tjjd�tjjtjj�jtjd�}tjdk(rd|z}nd|z}tjj
|�5}|j�ddd�y#1swYyxYw)Nz	bogus url�/rz
file:///%s�	file://%s)�
addCleanup�urllibr�
urlcleanup�assertRaises�
ValueError�urlopenr�path�abspath�__file__�replace�sepr �read)r%�fname�file_url�fs    r+�test_trivialzTrivialTests.test_trivial4s���	
������1�1�2����*�f�n�n�&<�&<�k�J��������� 7� 7�8�@�@�����M��
�7�7�d�?�#�e�+�H�"�U�*�H�
�^�^�
#�
#�H�
-�	��
�F�F�H�	�	�	�s�+D�Dc��dgd�fdddgfdgd�fdgd	�fg}|D]4\}}|jtjj|�|��6y)
Nza,b,c)�a�b�czpath"o,l"og"i"cal, examplezpath"o,l"og"i"cal�exampleza, b, "c", "d", "e,f", g, h)rBrCz"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")r"r2r�parse_http_list)r%�tests�string�lists    r+�test_parse_http_listz!TrivialTests.test_parse_http_listGsk��
�o�&�
)�,?��+K�L�
*�
8�
:�
/�
/�
1�2��"�	K�L�F�D����V�^�^�;�;�F�C�T�J�	Kr-c��tjjd�}|j|jt|��y)N�reason)r2r�URLError�assertInrN�str)r%�errs  r+�test_URLError_reasonstrz$TrivialTests.test_URLError_reasonstrRs,���l�l�#�#�H�-���
�
�c�j�j�#�c�(�+r-N)�__name__r#�__qualname__r,r@rLrS�r-r+rr#s��
/��&	K�,r-rc�$�eZdZd�Zd�Zd�Zd�Zy)�RequestHdrsTestsc��d}|jt|ddi��jdd�|jt|ddi��jdd�y)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)r"rr^)r%�urls  r+�test_request_headers_dictz*RequestHdrsTests.test_request_headers_dictYsi��#�������*5�v�)>�#�#*�7�;�8�9?�	A�	
�����*5�v�)>�#�#*�7�;�8�9?�	Ar-c��d}t|ddi��}|j|jd��|j|j	�dg�|jdd�|jt
|j	��ddg�|j|jd	��|j|jd	��|j|jd	d
�d
�|jd�|j|jd��|jdd�|j|jd��|jd�|j|jd��y
)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
        rZr[r\r])r[r\zFoo-Bar�baz)zFoo-barrbz	Not-there�defaultzUnredirected-spam�EggsN)r�
assertTrue�
has_headerr"�header_items�
add_header�sorted�assertFalse�assertIsNone�
get_header�
remove_header�add_unredirected_header)r%r_�reqs   r+�test_request_headers_methodsz-RequestHdrsTests.test_request_headers_methodsns>��#���c�K��#8�9��������{�3�4�����)�)�+�.C�-D�E����y�%�(������ 0� 0� 2�3�,�.C�D�	F��������4�5����#�.�.��5�6��������Y�?��K����+�&��������4�5��#�#�$7��@�������':�;�<����-�.�������(;�<�=r-c��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�|d	ddd�|j|d	d�d�|j|d	d�d�|j|d	d�d�|j|d	d�d�|j|d	d �d�|j|d	d!�d"�|d	d#d$d%�|j|d	d&�d"�|j|d	d#�d'�|j|d	d(�d'�|d)dd*d+�|d)dd,d-�|j|d)d�d.�|d/dd0d/�|j|d/d�d1�|j|d/d2�d"�|j|dd3�d"�|j|dd4�d5�|j|dd�d5�|j|dd
�d6�|j|dd
�d7�y)8Nz
Some Realm�http://example.com/�joe�passwordzhttp://example.com/ni�nizhttp://c.example.com:3128�3rDz
d.example.com�4�dze.example.com:3128�5�e�example.com)rsrtrZzhttp://example.com/spamzhttp://example.com/spam/spamzhttp://example.com/foo�foozhttp://example.com/bar�bar�ninizhttp://example.com/foo/bar�foobar�nibar)r|ru)r}r~zhttp://example.com/foo/zhttp://example.com/foo/bazzhttp://example.com/foobar�NNzhttp://example.com/baz/rb�nininizhttp://example.com/baz)rbr�zhttp://example.com/baz/barrC�firstr\�second�spam)r�r�rB�1)r�rBzhttp://a.example.com/z
c.example.comzc.example.com:3128)rvrD)rwrx)ryrz�r2r�HTTPPasswordMgr�add_password�find_user_passwordr"�r%�mgr�add�find_user_passs    r+�test_password_managerz&RequestHdrsTests.test_password_manager�s`���n�n�,�,�.�������/�/���L�/��
�C��L�1�4��>��L�5�s�C�@��L�/�3��4��L�.��S�9�	
�����m�D�,�	.������6M�N�,�	.������6J�K�,�	.������6K�L�,�	.������(A�C�,�	.�	
�����(F�H�,�	.�	�C�)�5�$�7��C�)�5�&�9��C�-�x��A������-E�F�&�	(������-E�F�(�	*������-F�G�&�	(������-I�J�&�	(������-I�J�&�	(������-H�I�%�	'�	�C�*�E�8�<������-E�F�%�	'������-F�G�*�	,������-I�J�*�	,�
	�C�&���8��C�&��&�9������-B�C�+�	-�
	�C�%�s�C�0������-B�C�#�	%�	
�����-D�E�%�	'�
	
�����o�F�%�	'������6J�K�#�	%�����<�)D�E��	�	
�����o�F�#�	%������6J�K�#�	%r-c���tjj�}|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�y#)$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�jrFzhttp://h.example.com�11r)rGzi.example.com:80�12�l�iz
j.example.com�13�mzg.example.com:100r�zg.example.com:80)r�r�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:80Nr�r�s    r+�"test_password_manager_default_portz3RequestHdrsTests.test_password_manager_default_port�s���n�n�,�,�.�������/�/���C�*�D�#�6��C�'��s�3��C�#�T�3�/��C��$��,������-@�A�%�	'������-?�@�$�	&������_�=�%�	'������-G�H�%�	'������-F�G�$�	&������-C�D�$�	&������_�=�{�K������-?�@�+�N������-F�G�$�	&������_�=�|�L������-?�@�+�N������-F�G�$�	&������_�=�{�K������-?�@�%�	'������-C�D�$�	&������-F�G�%�	'r-N)rTr#rUr`rpr�r�rVr-r+rXrXWs��A�*">�HT%�l''r-rXc�6�eZdZgZdej
fd�Zd�Zy)�
MockOpenerNc�0�|||c|_|_|_y�N)ro�data�timeout)r%ror�r�s    r+�openzMockOpener.opens��,/��w�)���$�)�T�\r-c�"�||c|_|_yr�)�proto�args)r%r�r�s   r+rzMockOpener.errors�� %�t���
�D�Ir-)rTr#rU�
addheaders�socket�_GLOBAL_DEFAULT_TIMEOUTr�rrVr-r+r�r�s���J�!�6�+I�+I�?�,r-r�c�"�eZdZdd�Zdd�Zd�Zy)�MockFileNc��yr�rV�r%�counts  r+r<z
MockFile.read���r-c��yr�rVr�s  r+�readlinezMockFile.readline r�r-c��yr�rV�r%s r+�closezMockFile.close#r�r-r�)rTr#rUr<r�r�rVr-r+r�r�s��
�
�
r-r�c��eZdZd�Zy)�MockHeadersc�4�t|j��Sr�)rK�values)r%r s  r+�
getheaderszMockHeaders.getheaders(s���D�K�K�M�"�"r-N)rTr#rUr�rVr-r+r�r�'s��#r-r�c� �eZdZdd�Zd�Zd�Zy)�MockResponseNc��tjj||�||||f\|_|_|_|_yr�)�io�StringIO�__init__�code�msgr^r_)r%r�r�r^r�r_s      r+r�zMockResponse.__init__-s5��
�����T�4�(�6:�C��#�6M�3��	�4�8�T�\�4�8r-c��|jSr�r]r�s r+�infozMockResponse.info1s���|�|�r-c��|jSr��r_r�s r+�geturlzMockResponse.geturl4����x�x�r-r�)rTr#rUr�r�r�rVr-r+r�r�,s��N��r-r�c��eZdZd�Zd�Zy)�
MockCookieJarc��||_yr�)�ach_req)r%rs  r+�add_cookie_headerzMockCookieJar.add_cookie_header9s	����r-c�"�||c|_|_yr�)�ec_req�ec_r)r%rrs   r+�extract_cookieszMockCookieJar.extract_cookies<s��!(�(����T�Yr-N)rTr#rUr�r�rVr-r+r�r�8s���3r-r�c��eZdZd�Zd�Zy)�
FakeMethodc�.�||_||_||_yr�)�	meth_name�handle�action)r%r�r�r�s    r+r�zFakeMethod.__init__As��"��������r-c�R�|j|j|jg|���Sr�)r�r�r�)r%r�s  r+�__call__zFakeMethod.__call__Fs"���t�{�{�4�>�>�4�;�;�>��>�>r-N)rTr#rUr�r�rVr-r+r�r�@s���
?r-r�c�$�eZdZd�Zd�Zd�Zd�Zy)�MockHTTPResponsec�J�||_||_||_||_d|_y)N��)�fpr��statusrNr�)r%r�r�r�rNs     r+r�zMockHTTPResponse.__init__Ks%����������������	r-c��y)N�rVr�s r+r<zMockHTTPResponse.readRs��r-c��iSr�rVr�s r+r�zMockHTTPResponse.infoUs���	r-c��|jSr�r�r�s r+r�zMockHTTPResponse.geturlXr�r-N)rTr#rUr�r<r�r�rVr-r+r�r�Js�����r-r�c�X�eZdZd�Zej
fd�Zd�Zdd�Zddd�d�Z	d	�Z
d
�Zy)�
MockHTTPClassc�X�d|_g|_d|_d|_d|_i|_y)NrF)�level�req_headersr��raise_on_endheaders�sock�_tunnel_headersr�s r+r�zMockHTTPClass.__init__]s/����
������	�#(�� ���	�!��r-c�"�||_||_|Sr�)�hostr�)r%r�r�s   r+r�zMockHTTPClass.__call__es����	�����r-c��||_yr�)r�)r%r�s  r+�set_debuglevelzMockHTTPClass.set_debugleveljs	����
r-Nc�h�||_||_|r||_y|jj�yr�)�_tunnel_host�_tunnel_portr��clear)r%r��portr^s    r+�
set_tunnelzMockHTTPClass.set_tunnelms/�� ��� ����#*�D� �� � �&�&�(r-F)�encode_chunkedc���||_||_|�#|xj|j�z
c_|jj	�|r||_||_|jr
t��yr�)	�method�selectorr�r$�sortr�r�r��OSError)r%r�r_�bodyr^r�s      r+rzMockHTTPClass.requestusf�������
�������
�
��/����������D�I�,����#�#��)�O�$r-c�.�tt�idd�S)Nr��OK)r�r�r�s r+�getresponsezMockHTTPClass.getresponse�s����
�B��T�:�:r-c��yr�rVr�s r+r�zMockHTTPClass.close�r�r-r�)rTr#rUr�r�r�r�r�r�rr�r�rVr-r+r�r�\s8��"�&,�%C�%C��
�)��$��;�
r-r�c�4�eZdZdZd�Zd�Zd�Zd�Zd�Zd�Z	y)	�MockHandler��c�&�|j|�yr�)�_define_methods)r%�methodss  r+r�zMockHandler.__init__�s�����W�%r-c��|D]H}t|�dk(r|\}}n|d}}t|||j�}t|j||��Jy)N�)�lenr�r��setattr�	__class__)r%r�specr r��meths      r+rzMockHandler._define_methods�sP���	0�D��4�y�A�~�#���f�#�T�f���d�F�D�K�K�8�D��D�N�N�D�$�/�
	0r-c���|jjj||||f�|�y|dk(r|S|dk(rtddid�}|S|dk(rt	d�S|jd�rT||j
d	�d
zd}	t|�}tddid�}|jjd|d||di�S|d
k(rtjjd��J�#t$rY�bwxYw)N�return self�return responser�r�r��return requestzhttp://blah/r� ��httpr�raiser\)�parent�calls�appendr�r�
startswith�rfind�intr5rr2rO)r%�fn_namer�r��kwds�resr�s       r+r�zMockHandler.handle�s
������� � �$���t�!<�=��>��
�}�
$��K�
�(�
(��s�D�"�b�1�C��J�
�'�
'��>�*�*�
�
�
�w�
'��&�,�,�s�+�A�-�.�/�D�
��4�y���s�D�"�b�1�C��;�;�$�$�V�T�!�W�c�4��R�H�H�
�w�
��,�,�'�'��/�/���
�
��
�s�C$�$	C0�/C0c��yr�rVr�s r+r�zMockHandler.close�r�r-c�4�||_g|j_yr�)rr)r%rs  r+�
add_parentzMockHandler.add_parent�s���������r-c�N�t|d�sy|j|jkS)N�
handler_orderT)�hasattrr)r%�others  r+�__lt__zMockHandler.__lt__�s&���u�o�.���!�!�E�$7�$7�7�7r-N)
rTr#rUrr�rr�r�rr"rVr-r+r�r��s'���M�&�0��.
��8r-r�c���g}d}|D]f}Gd�dt�}||�}|xj|z
c_|j|�|dz}|j|�|j	|��h|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��eZdZy)�6add_ordered_mock_handlers.<locals>.MockHandlerSubclassN�rTr#rUrVr-r+�MockHandlerSubclassr%����r-r'r)r�rrr�add_handler)�opener�	meth_spec�handlersr��methsr'rGs       r+�add_ordered_mock_handlersr.�sy��&�H�
�E��	��	�+�	�
 ��&��	���5� ��	���V����	����������1��	��Or-c�J�t�}|D]}|j|��|Sr�)r	r))�handler_instancesr*rGs   r+�build_test_openerr1�s,��
�
�F�
������1����Mr-c�&��eZdZd�fd�	Zd�Z�xZS)�MockHTTPHandlerc�L��tt|�|��t�|_y)N��
debuglevel)�superr3r�r��httpconn)r%r6rs  �r+r�zMockHTTPHandler.__init__�s���
�o�t�-��-�D�%���
r-c�:�|j|j|�Sr���do_openr8�r%ros  r+�	http_openzMockHTTPHandler.http_open�s���|�|�D�M�M�3�/�/r-r�)rTr#rUr�r=�
__classcell__�rs@r+r3r3�s
���(�0r-r3c��eZdZd�Zd�Zd�Zy)�MockHTTPHandlerRedirectc�@�||_||_|j�yr�)r�r^�reset)r%r�r^s   r+r�z MockHTTPHandlerRedirect.__init__�s����	�����
�
�r-c� �d|_g|_y)Nr)�_count�requestsr�s r+rCzMockHTTPHandlerRedirect.reset�s�������
r-c��ddl}ddl}|jj|j	|��|j
dk(r�|j
dz|_tjj|j}|j|j�}|jjd|t�|j||�S||_|jd�}t!dd|d|j#��S)Nrrr�

r�r�r�)�email�copyrFr�deepcopyrEr�client�	responsesr��message_from_stringr^rrr�ror��get_full_url)r%rorIrJr r�s      r+r=z!MockHTTPHandlerRedirect.http_opens�����
�
���T�]�]�3�/�0��;�;�!���+�+��/�D�K��;�;�(�(����3�D��+�+�D�L�L�9�C��;�;�$�$���X�Z����D�#�?�
?��D�H��+�+�J�7�C���T�3��C�4D�4D�4F�G�Gr-N�rTr#rUr�rCr=rVr-r+rArA�s���
�Hr-rA�HTTPSConnectionc�&��eZdZd�fd�	Zd�Z�xZS)�MockHTTPSHandlerc�N��tt|�|||�t�|_yr�)r7rSr�r�r8)r%r6r&�check_hostnamers    �r+r�zMockHTTPSHandler.__init__s ����"�D�2�:�w��W�)�O�D�Mr-c�:�|j|j|�Sr�r:r<s  r+�
https_openzMockHTTPSHandler.https_opens���<�<��
�
�s�3�3r-)NNN)rTr#rUr�rWr>r?s@r+rSrSs
���	,�	4r-rSc��eZdZd�Zd�Zd�Zy)�MockHTTPHandlerCheckAuthc� �||_d|_y�NF)r��has_auth_header)r%r�s  r+r�z!MockHTTPHandlerCheckAuth.__init__ s����	�$��r-c��d|_yr[)r\r�s r+rCzMockHTTPHandlerCheckAuth.reset$s
��$��r-c���|jd�rd|_tjj|j
}t
|j
|t�d|j��S)N�
AuthorizationTr�)	rfr\rrLrMr�r�r�rO)r%ror s   r+r=z"MockHTTPHandlerCheckAuth.http_open'sP���>�>�/�*�#'�D� ��{�{�$�$�T�Y�Y�/���D�I�I�t�X�Z��S�=M�=M�=O�P�Pr-NrPrVr-r+rYrYs��%�%�Qr-rYc��eZdZd�Zd�Zy)�MockPasswordManagerc�<�||_||_||_||_yr�)�realmr_�userrt)r%rc�urirdrts     r+r�z MockPasswordManager.add_password0s����
������	� ��
r-c�N�||_||_|j|jfSr�)�target_realm�
target_urlrdrt)r%rc�authuris   r+r�z&MockPasswordManager.find_user_password6s$��!���!����y�y�$�-�-�'�'r-N)rTr#rUr�r�rVr-r+rara/s��!�(r-rac�6�eZdZd�Zd�Zd�Zd�Zd�Zd�Zd�Z	y)	�OpenerDirectorTestsc�~�Gd�dt�}|jtt�j|��y)Nc��eZdZy)�<OpenerDirectorTests.test_add_non_handler.<locals>.NonHandlerNr&rVr-r+�
NonHandlerrn?r(r-ro)�objectr4�	TypeErrorr	r))r%ros  r+�test_add_non_handlerz(OpenerDirectorTests.test_add_non_handler>s/��	��	����)�(�*�6�6�
��	Fr-c���ddlm}t�}ddgdgg}t||�|j	t
jj��dD]"}|j||j|dz��$y)Nr)rO)r;r)�
proxy_openr)�redirect_requestr)�do�proxy�redirectz://example.com/)
�urllib.errorrOr	r.r)r2r�UnknownHandlerr4r�)r%rO�or+�schemes     r+�test_badly_named_methodsz,OpenerDirectorTests.test_badly_named_methodsDsw��	*����
'�)F�G�
0�1��	�	"�!�Y�/�	�
�
�f�n�n�3�3�5�6�/�	J�F����h�����7H�0H�I�	Jr-c�\�t�}gd�dgdgdgg}t||�}td�}|j|�}|j	||d�|ddf|ddfg}t||j�D]3\}}|\}	}
}}|j	|	|
f|�|j	||f��5y)N)r=�ftp_open�http_error_302r�r=rrrrrr=)r	r.rr�r"�zipr)
r%r{r+r,ro�rr�expected�got�handlerr r�rs
             r+�test_handledz OpenerDirectorTests.test_handledYs������7�
�L�
)�*�
)�*�	�	�-�Q�	�:���+�,��
�F�F�3�K��	
����H�Q�K�(��1�+�{�+�h�q�k�;�-G�H�� �����0�	+�M�H�c�(+�%�G�T�4�����g�t�_�h�7����T�C�6�*�	+r-c�v�t�}g}dgdfdgdffD]E\}}Gd�dt�}||�}||_|j|�|j	|��G|jd�|j
|jdd|d�|j
|jdd|d�y)	Nr�r�r=rc��eZdZy)�COpenerDirectorTests.test_handler_order.<locals>.MockHandlerSubclassNr&rVr-r+r'r�xs��r-r'rrr)r	r�rrr)r�r"r)r%r{r,r-rr'rGs       r+�test_handler_orderz&OpenerDirectorTests.test_handler_orderss��������'C�&D�c�%J�'2�m�Q�%7�%9�	� �E�=�
�k�
�$�E�*�A�+�A�O��O�O�A��
�M�M�!��	�	
���$�%���������A�����4���������A�����4r-c��t�}dgdgg}t||�}td�}|jtj
j|j|�|j|j|dd|fifg�y)N)r=rr�rrrr=)
r	r.rr4r2rrOr�r"r)r%r{r+r,ros     r+�
test_raisezOpenerDirectorTests.test_raise�s|�����
#�$�
)�*��	�-�Q�	�:���+�,�����&�,�,�/�/�����=�������H�Q�K��s�f�b�#I�"J�Kr-c��t�}dgddggd�dgg}t||�}td�}|j|�t	|j
�dk(sJ�|dd|ff|dd|tjd	d
iffg}t||j
�D]7\}}|\}}	}
|j||	f|dd�|j|
|d��9y)N)r=z	error 302)�http_error_400rr=))r�r�http_error_303�
http_errorr�rrrr�.r�)
r	r.rr�rrr�	ALWAYS_EQr�r")r%r{r+r,rorr�r�r��method_namer�s           r+�test_http_errorz#OpenerDirectorTests.test_http_error�s���
���
'�(�
(�+�6�
�� ��	�-�Q�	�:���+�,��	���s���1�7�7�|�q� � ��1�+�{�S�F�3��1�+�/���)�)�3��B�7�9�:��!�����0�	+�M�H�c�)1�&�G�[�$����g�{�3�S��!�W�=����T�3�q�6�*�	+r-c�f�t�}ddgddgg}t||�}td�}|j|�|ddf|ddf|ddf|ddfg}t	|j
�D]�\}\}}}	}
|dkrL|j
||f||�|j
t|	�d�|j|	dt��\|j
||f||�|j
t|	�d�|j|	dt�|	d���|j|	dt���y)	N)�http_requestr
)�
http_responserrrrr�rr�r)
r	r.rr��	enumeraterr"r�assertIsInstancer�)r%r{r+r,rorr�r�r r�rs           r+�test_processorsz#OpenerDirectorTests.test_processors�sV�����
/�
1�
3�
/�
1�
3��	�-�Q�	�:���+�,��	���s���a�[�.�)�H�Q�K��+H�
�a�[�/�*�X�a�[�/�,J�L��/8����.@�	A�*�A�*���t�T��1�u�� � �'�4��%��(�;�� � ��T��A�.��%�%�d�1�g�w�7�� � �'�4��%��(�;�� � ��T��A�.��%�%�d�1�g�w�7���7�&��)�)�$�q�'�<�@�	Ar-N)
rTr#rUrrr}r�r�r�r�r�rVr-r+rkrk<s)��F�J�*+�45�$L�+�.!Ar-rkc��	|jd�tj
j
|�}tjdk(r|jd�r|dd}|S#t$rtjd��wxYw)Nzutf-8zpath is not encodable to utf8rz///r)
�encode�UnicodeEncodeError�unittest�SkipTestr2rrrr r)r7�urlpaths  r+�sanepathname2urlr��sw��A����G���n�n�)�)�$�/�G�	�w�w�$��7�-�-�e�4��!�"�+���N��
�A���� ?�@�@�A�s�A�A<c�>�eZdZd�Zd�Zd�Zd�Zd�Zd�Zd�Z	d�Z
d	�Zd
�Zd�Z
ejeej$d�d
�d��Zejeej$d�d
�d��Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z d�Z!d�Z"d�Z#d�Z$d �Z%ejeej$d�d
�d!��Z&eje'jPd"k(d#�d$��Z)d%�Z*d&�Z+d'�Z,d(�Z-d)�Z.d*�Z/d+�Z0d,�Z1d-�Z2d.�Z3d/�Z4y0)1�HandlerTestsc���Gd�d��G�fd�dtjj�}ddl}d}||�}t	�|_dd|jd	d	d
ddgd
df	dd|jdd	d
ddgd
df	dd|jdd	d
ddgd
df	dd|jdd	d
ddgd
df	dddd	d	dddgd	df	dd|jd	d	dgddf	fD�]s\	}}}}}	}
}}}
t|�}d|_|j|�}|j|j|�|j|j|	�|j|jtj|��|j|j |�|j|j"|�|j|j$j&|�|j|j$j(|
�|j+�}|j|j-d�|
�|jt/|d�t1|����vy)Nc��eZdZd�Zd�Zd�Zy)�-HandlerTests.test_ftp.<locals>.MockFTPWrapperc��||_yr��r��r%r�s  r+r�z6HandlerTests.test_ftp.<locals>.MockFTPWrapper.__init__��	�� ��	r-c��||c|_|_tj|j�t|j�fSr�)�filename�filetyper�r�r�r)r%r�r�s   r+�retrfilez6HandlerTests.test_ftp.<locals>.MockFTPWrapper.retrfile�s2��/7��,��
�t�}��{�{�4�9�9�-�s�4�9�9�~�=�=r-c��yr�rVr�s r+r�z3HandlerTests.test_ftp.<locals>.MockFTPWrapper.close����r-N)rTr#rUr�r�r�rVr-r+�MockFTPWrapperr��s��
!�
>�
r-r�c�6��eZdZd�Zej
f�fd�	Zy)�-HandlerTests.test_ftp.<locals>.NullFTPHandlerc��||_yr�r�r�s  r+r�z6HandlerTests.test_ftp.<locals>.NullFTPHandler.__init__�r�r-c���||c|_|_||c|_|_||_�|j
�|_|jSr�)rd�passwdr�r��dirsr��
ftpwrapper)r%rdr�r�r�r�r�r�s       �r+�connect_ftpz9HandlerTests.test_ftp.<locals>.NullFTPHandler.connect_ftp�sD���)-�v�&��	�4�;�'+�T�$��	�4�9� ��	�"0����";������&r-N)rTr#rUr�r�r�r�)r�s�r+�NullFTPHandlerr��s���
!�%+�$B�$B�
'r-r�rzrheum rhaponicumz ftp://localhost/foo/bar/baz.html�	localhostr��Ir|r}zbaz.htmlz	text/htmlz'ftp://parrot@localhost/foo/bar/baz.html�parrotz*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)r2r�
FTPHandler�ftplibr�r�FTP_PORTrr�rr"rdr�r�r��
gethostbynamer�r�r�r�r�r��getrr)r%r�r�r�rGr_r�r�rdr��type_r�r��mimetyperor�r^r�s                 @r+�test_ftpzHandlerTests.test_ftp�s=���		�		�
	'�V�^�^�6�6�
	'�	�!���4� ���<���0�
�&�/�/�2�r�3��U�^�Z��
6�7�
�&�/�/�8�R���U�^�Z��
6�:�
�&�/�/�9�b�#��U�^�Z��
6�<�
�&�/�/�;��C��U�^�Z��
6�+�
�"�b�"�c��U�^�R��
'�.�
�&�/�/�2�r�3�
��D�
"�!O�!	H�J�C��t�T�6�5�$��(�(�#�,�C��C�K��
�
�3��A����Q�V�V�T�*����Q�X�X�v�.����Q�V�V�V�%9�%9�$�%?�@����Q�V�V�T�*����Q�V�V�T�*����Q�\�\�2�2�H�=����Q�\�\�2�2�E�:��f�f�h�G����W�[�[��8�(�C����S��)9�!:�;�S��Y�G�C!	Hr-c	��ddl}tjj�}t	�x}|_tj}ttjj|��}d}d|zd|zdtjd��|��g}	tjtj��}|r|j!d|�|���|D�]A}	t#|d�}
		|
j%|�|
j'�|j)t+|	��}	|j-�}|j/�}
|j1�}|j'�tj2|�}|j4j7|j8d	�
�}tj:|�|j=||�|j=|
dd�|j=|
d
d�|j=|
d|�|j=||	���Dd|zdddtjd��dtj>��d|��dtj>��d|��fD]�}		t#|d�}
	|
j%|�|
j'�|jAtjBjD|j(t+|	��tj:|���tjj�}t	�x}|_dD]y\}	}t+|	�}	|j)|�|jG|jH|�|j=|jJd�|j=|jJdk(|��{y#tj$rd}Y���wxYw#|
j'�wxYw#|j'�wxYw#tj:|�wxYw#|
j'�wxYw#tj:|�wxYw#tjBjD$r|jM|�Y��wxYw)Nrs
hello, world
zfile://localhost%sr0zfile://r�r��wbT)�usegmtr�z
text/plainr�r�z
Last-modifiedzfile://localhost:80%szfile:///file_does_not_exist.txtz)file://not-a-local-host.com//dir/file.txtz:80r/z'file://somerandomhost.ontheinternet.com))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)'�email.utilsr2r�FileHandlerr�rr�TESTFNr�rr7r8r�r��gethostname�gaierrorrr��writer��	file_openrr<r�r��stat�utils�
formatdate�st_mtime�remover"�getcwdr4rrO�assertIsro�typerj)r%rIrGr{r�r��towrite�urls�	localaddrr_r?r�r�r^�respurl�stats�modifiedr�ros                   r+�	test_filezHandlerTests.test_files�����N�N�&�&�(��!�|�#��A�H��!�!��"�2�7�7�?�?�6�#:�;��#�� �7�*��'�!�#�1�1�+�>��H���
	��,�,�V�-?�-?�-A�B�I���K�K��G�<�=��	+�C��V�T�"�A�
"���G�G�G�$��G�G�I��K�K����-����6�6�8�D��f�f�h�G��h�h�j�G��G�G�I������� �;�;�1�1�%�.�.��1�N���	�	�&�!����T�7�+����W�^�4�l�C����W�%5�6��=����W�_�5�x�@����W�c�*�/	+�4
$�g�-�-�7�#)�#7�#7��#D�#%�9�9�;��
8��Y�Y�[�&�
"�
�	"�C�

"����&����G�G�G�$��G�G�I��!�!�&�,�,�"7�"7�"#�+�+�w�s�|�=��	�	�&�!�'	"�*
�N�N�&�&�(��!�|�#��A�H��	5�H�C���#�,�C�
2����C� ��
�
�a�e�e�S�)�� � ����5�1����S�X�X��.��4�	5��y���	��I�	���G�G�I���G�G�I���	�	�&�!��*�G�G�I��
�	�	�&�!��*�<�<�(�(�
&�� � ��%�
&�s��'N�%N9�6*O#�!0O�AO#�,P�9O=�
AP�2P,�N6�5N6�9O�O#�O � O#�#O:�=P�P�P)�,.Q�Qc���tjj�}t�x}|_d}dD�]�\}}t||ddi�}d|_|jdd�t�}|j||�}|j|j|j|j|j|jdk(df|j�}	|	j |	j"|j%|j�|�|j%|j&d	�|j%|j(d
�|j%|j*|�|j%|j,d�|j%|j.gd��|j%|j0|����d
_|j5tj6j8|j|�tdd�}|j5t:|j<|�dg|_dD�]�}td|�}tAddid�}|j=|�}
|�9|jCd|jD�|jCd|jD�n>|j%|jDdd�|j%|jDdd�|j%|jDdd	�|j%|jDdd�|jdd�|jdd�|jdd�|jdd�|j=|�}
|j%|jDdd�|j%|jDdd�|j%|jDdd�|j%|jDdd����y)Nrr))�GETN)�POSTsblah�Foor}�Spam�eggsr�r�r{rr/))�
Connectionr�)r�r}�r�r�T�badpostr�)r-Nr�r�r��0z!application/x-www-form-urlencoded�Hostr|rb)#r2r�AbstractHTTPHandlerr�rrr�rnr�r;r<r�r�r�r�r�r��__contains__r"r�r�r�r�r�r�r�r4rrOrq�do_request_r�r��assertNotIn�unredirected_hdrs)r%rGr{r_r�r�rorr��hdrs�newreqs           r+�	test_httpzHandlerTests.test_httpws[���N�N�.�.�0��!�|�#��A�H�#��>�	.�L�F�D��#�t�e�U�^�4�C��C�K��'�'���7� �?�D��	�	�$��$�A�
�F�F�A�J�J�
�F�F�A�H�H�
�F�F�A�E�E�S�L�$�&��6�6�8�D��H�H�d�'�'����Q�X�X�Z��-����T�Y�Y�
�6����T�Z�Z��+����T�[�[�&�1����T�]�]�C�0����T�-�-�@�
A�
���T�Y�Y��-�-	.�2$(�� ����&�,�,�/�/����D�#�F��+�I�6�����)�Q�]�]�C�8�)�)����	C�D��/��6�C��S�$��B�/�A��]�]�3�'�F��|�� � �!1�3�3H�3H�I�� � ���1F�1F�G�� � ��!6�!6�7G�!H�#�N�� � ��!6�!6�~�!F�@�B�
���S�2�2�6�:�M�J����S�2�2�6�:�F�C�
�'�'�(8�%�@��'�'���>��'�'���6��'�'���6��]�]�3�'�F����S�2�2�3C�D�e�L����S�2�2�>�B�E�J����S�2�2�6�:�E�B����S�2�2�6�:�E�B�1	Cr-c�"�tjj�}t�x}|_tjdd��}|j}|j�|jtj|�t|d�5}td|i�}|j|�}|jd�}|j!|d�|j#|j%d��ddd�t|d�5}td|d	d
i�}|j|�}|j!t'|jd��d
�|j#|j%d��ddd�y#1swY��xYw#1swYyxYw)Nzw+bF)�mode�delete�rbrr�Transfer-encoding�chunkedr��Content-Length�)r2rr�r�r�tempfile�NamedTemporaryFiler r�r1r�unlinkr�rr�rlr"rjrfr)	r%rGr{�file_obj�	file_pathr?ror��tes	         r+�test_http_body_filez HandlerTests.test_http_body_file�sc��
�N�N�.�.�0��!�|�#��A�H��.�.�E�%�H���M�M�	���������	�	�9�-�
�)�T�
"�	B�a��/��B�7�C��]�]�3�'�F��"�"�#6�7�B����R��+����V�.�.�/?�@�A�	B��)�T�
"�	E�a��/��5E�r�4J�K�C��]�]�3�'�F����S��!2�!2�3C�!D�E�r�J����V�.�.�/B�C�D�		E�	E�	B�	B��	E�	E�s�A"E9�A+F�9F�Fc�@�tjj�}t�x}|_tj�}td|i�}|j|�}|j|jd�d�|j|jd��ddi}td||�}|j|�}|jt|jd��d�|j|jd��|j�y)Nrrr�r�r�r�r)r2rr�r�rr��BytesIOrr�r"rlrjrfrr�)r%rGr{rror�r^s       r+�test_http_body_fileobjz#HandlerTests.test_http_body_fileobj�s���
�N�N�.�.�0��!�|�#��A�H��:�:�<���+�X�r�:�����s�#������*�*�+>�?��K�����*�*�+;�<�=�#�R�(���+�X�w�?�����s�#������V�.�.�/?�@�A�2�F�����*�*�+>�?�@����r-c	�@�tjj�}t�x}|_t
jddg}iddifD]�}tj|tj��5}td|j|�}|j|�}|sC|j|jd�d�|j|jd�d	�n*|jt|jd��d�ddd���y#1swY��xYw)
Nz-c�passr�r)�stdoutrrr�r�r�)r2rr�r�r�sys�
executable�
subprocess�Popen�PIPErr
r�r"rlr)r%rGr{�cmdr^�procror�s        r+�test_http_body_pipez HandlerTests.test_http_body_pipe�s��
�N�N�.�.�0��!�|�#��A�H��~�~�t�W�-���,�b�1�1�
	)�G��!�!�#�j�o�o�>�	
)�$��3�T�[�[�'�J�����s�+����$�$�V�%6�%6�7G�%H�$�O��$�$�V�%6�%6�7J�%K�%.�0��$�$�S��):�):�;K�)L�%M�%'�)�	
)�	
)�
	)�	
)�	
)�s
�1BD�D	c��tjj�}t�x}|_d�}iddifD]�}td|�|�}|j
|�}|sC|j|jd�d�|j|jd�d��k|jt|jd��d���y)Nc3�K�d��y�w)NsonerVrVr-r+�
iterable_bodyz;HandlerTests.test_http_body_iterable.<locals>.iterable_bodys�����L�s�r��rrr�r�r�)
r2rr�r�rrr�r"rlr)r%rGr{rr^ror�s       r+�test_http_body_iterablez$HandlerTests.test_http_body_iterable�s���
�N�N�.�.�0��!�|�#��A�H�	��,�b�1�1�	O�G��/���'�J�C��]�]�3�'�F��� � ��!2�!2�3C�!D�d�K�� � ��!2�!2�3F�!G�!*�,�� � ��V�%6�%6�7G�%H�!I�2�N�	Or-c��tjj�}t�|_|jt
dd��}|j|jd�d�|j|jd��y)NrrrVr�r�r�)r2rr�r�rr�rr"rlrjrf)r%rGros   r+�test_http_body_empty_seqz%HandlerTests.test_http_body_empty_seqse���N�N�.�.�0���<����m�m�G�$9�2�>�?��������(;�<�i�H�������(8�9�:r-c�8�tjj�}t�x}|_tj
dgd��}iddifD]J}t
d||�}|j|�}|jt|jd��d��Ly)Nr�)rr��r��rrr�)r2rr�r�r�arrayrr�r"rrl)r%rGr{�iterable_arrayr^ror�s       r+�test_http_body_arrayz!HandlerTests.test_http_body_arrays���
�N�N�.�.�0��!�|�#��A�H����S��3���,�b�1�1�	J�G��/���I�C��]�]�3�'�F����S��!2�!2�3C�!D�E�b�I�	Jr-c�D�tjjtjj
dd�5t
�}t�}|j|�|jd�|j|jd�ddd�y#1swYyxYw)Nr6��http://www.example.com)r�patchrprrL�HTTPConnectionr	r3r)r�r"�_debuglevel�r%r{rGs   r+�#test_http_handler_global_debuglevelz0HandlerTests.test_http_handler_global_debuglevel(st��
�Z�Z�
�
�t�{�{�9�9�<��
K�	/�� �A��!�A�
�M�M�!��
�F�F�+�,����Q�]�]�A�.�	/�	/�	/���AB�Bc��t�}td��}|j|�|jd�|j	|j
d�y)N�r5r&)r	r3r)r�r"r)r*s   r+�"test_http_handler_local_debuglevelz/HandlerTests.test_http_handler_local_debuglevel0sA������q�)��	�
�
�a��	���'�(��������*r-rQz)HTTPSConnection required for HTTPS tests.c�D�tjjtjj
dd�5t
�}t�}|j|�|jd�|j|jd�ddd�y#1swYyxYw)Nr6��https://www.example.com)rr'rprrLrQr	rSr)r�r"r)r*s   r+�$test_https_handler_global_debuglevelz1HandlerTests.test_https_handler_global_debuglevel7st��
�Z�Z�
�
�t�{�{�:�:�L�!�
L�	/�� �A� �"�A�
�M�M�!��
�F�F�,�-����Q�]�]�A�.�	/�	/�	/�r,c��t�}td��}|j|�|jd�|j	|j
d�y)Nrr5r2)r	rSr)r�r"r)r*s   r+�#test_https_handler_local_debuglevelz0HandlerTests.test_https_handler_local_debuglevel@sA�������*��	�
�
�a��	���(�)��������*r-c�t�tjj�}t�|_d}gd�}|D]�}t||�}|j
|�}|j|jdd�|jdd�|j
|�}|j|jdd���y)Nr-)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.htmlr�r{zsomeproxy:3128)
r2rr�r�rrr�r"r��	set_proxy)r%rGr��ds_urls�ds_url�ds_req�	np_ds_req�p_ds_reqs        r+�test_http_doubleslashz"HandlerTests.test_http_doubleslashHs���
�N�N�.�.�0���<��������
	P�F��V�T�*�F��
�
�f�-�I����Y�8�8��@�-�P�
���-�t�4��}�}�V�,�H����X�7�7��?��O�
	Pr-c�b�gd�}td�}|D]�}||_t|�}|j|j	�|�|j|j
xsd|j
�|jt|j	��j|j���y)N)zhttp://example.com?foo=bar#bazz)http://example.com?foo=bar&spam=eggs#bashrZrZr�)r�full_urlrr"rO�fragment�query)r%r�r�r_�parseds     r+�test_full_url_setterz!HandlerTests.test_full_url_settercs���
��
�(�)���	M�C��A�J��c�]�F����Q�^�^�-�s�3�
���Q�Z�Z�-�2�v���?����X�a�n�n�&6�7�=�=�v�|�|�L�	Mr-c���td�}|`|j|j�|j|j�|j	|j
d�y)Nr&r�)rr?rkr@r"r�)r%r�s  r+�test_full_url_deleterz"HandlerTests.test_full_url_deleterzsJ���,�-��
�J����!�*�*�%����!�*�*�%�������R�(r-c��tjj�}t�|_d}t|�}|j
|�}|j|jd�|j|jd�d}t|�}|j
|�}|j|jd�|j|jd�y)Nzhttp://www.python.org?getspam�www.python.orgz	/?getspam�http://www.python.orgr�)
r2rr�r�rrr�r"r�r�)r%rG�	weird_urlror��url_without_paths      r+�test_fixpath_in_weirdurlsz&HandlerTests.test_fixpath_in_weirdurls�s���
�N�N�.�.�0���<���3�	��i� �����s�#��������&6�7�������+�6�2���&�'�����s�#��������&6�7�������"�-r-c�*�tjj�}t�x}|_d}t|�}t
ddid|�}|j||�}|j||�|jt|d��t
ddid|�}|j||�}|j||�|jt|d��t
dd	id|�}|j||�}|j||�|jt|d��t
d
did|�}|j|j||��|j|jd�|j|j||d
dif�y)
Nrrr�r�r�r����Accepted��zPartial contenti�zBad gatewayr)r2r�HTTPErrorProcessorr�rrr�r�r�rjr rkr"r�r�)r%rGr{r_ror��newrs       r+�test_errorszHandlerTests.test_errors�s^���N�N�-�-�/��!�|�#��A�H�#���c�l����d�B��C�0�����s�A�&���
�
�a��������G�,�-���j�"�b�#�6�����s�A�&���
�
�a��������G�,�-���/��R��=�����s�A�&���
�
�a��������G�,�-���m�R��S�9�����!�/�/�#�q�1�2�������&�)�������#�q�#�}�b�!A�Br-c�^�t�}tjj|�}t	�|_t
d�}tddid�}|j|�}|j|j|�|j|j|�|j|jd�|j|j�|j||�}|j|j |�|j|j"|�|j||�y)Nrrr�r�r�r{)r�r2r�HTTPCookieProcessorr�rrr�r�r�r�r"�origin_req_hostrj�unverifiabler�r�r�)r%�cjrGror�r�rQs       r+�test_cookieszHandlerTests.test_cookies�s���
�_���N�N�.�.�r�2���<����+�,����d�B��+������$���
�
�b�j�j�#�&��
�
�b�j�j�&�)�����,�,�m�<�����)�)�*����s�A�&���
�
�b�i�i��%��
�
�b�g�g�q�!��
�
�a��r-c��d}d}tjj�}t�x}|_dD�]�}dD�]�}t|d|z�}t
||�}tj|_	|jdd�|�$|jdtt|���|jd	d
�	||t�|dtd|i��|j)|j*j-�|�	|j)|j*j/�d�|j*j6D�	cgc]}	|	j9���}
}	|j;d|
�|j;d|
�|j)|j*j6dd�|j;d	|j*j6�|j;d	|j*j<�������t
|�}tj|_	|fd�}t
|d��}d}tj|_			|||d�|dz}�#tj j"$r'|j%|d
�|j'|�Y���wxYw#t0$r)|j3|j*j4�Y���wxYwcc}	w#tj j"$r7|j)|tjjj>�YnwxYwt
|d��}d}tj|_			|||d|z�|dz}�#tj j"$r7|j)|tjjj@�YywxYw)N�http://example.com/a.htmlzhttp://example.com/b.html)i-r�i/�3�4)Nz
blah
blah
z
http_error_%s�Nonsensezviking=withholdr�r�r��Blah�location)r[r\r�zcontent-lengthzcontent-typec
�T�|j|t�ddtd|i��y)Nr�r^r_)r�r�r�)rGror_s   r+rxz,HandlerTests.test_redirect.<locals>.redirect�s'��
���S�(�*�c�6�(�*�c�):�;�
=r-r{)rUrrrrzhttp://example.com/%d)!r2r�HTTPRedirectHandlerr�r�getattrrr�r�r�rhrQrrnr�r�r�	HTTPErrorrP�assertIsNotNoner"rorO�
get_method�AttributeErrorrjr�r^�lowerr�r��max_repeats�max_redirections)
r%�from_url�to_urlrGr{r�r�r�ro�xr^rxr�s
             r+�
test_redirectzHandlerTests.test_redirect�s@��.��,���N�N�.�.�0��!�|�#��A�H�,�	B�D�,�
B�� ��O�d�$:�;���h��-��$�<�<������z�+<�=��#��N�N�#3�S��T��^�D��+�+�F�F�;�/��3��
�D�&�&�
�F�';�<�>�� � ����!3�!3�!5�v�>�1��$�$�Q�U�U�%5�%5�%7��?�/0�e�e�m�m�<��1�7�7�9�<��<�� � �!1�7�;�� � ���9�� � ������z�!:�!2�4�� � �������7�� � �����)@�)@�A�=
B�	B�D�h����4�4���!'�	=��h�
�>�����4�4���	T����C�!6�7���	����I�|�|�-�-�/��M�M�$�
�3��(�(��.�/��&�1��$�$�Q�U�U�Z�Z�0�1��
=��6�|�|�%�%�	T����U�F�N�N�$F�$F�$R�$R�S�	T��
�h�
�>�����4�4���	R����C�!8�5�!@�A���	�����|�|�%�%�	R����U�#�^�^�?�?�P�P�
R�	R�sQ�;I�*J!�K�	K�AJ�J�!.K�K�AL/�.L/�M+�+AN?�>N?c
�2�d}gd�}gd�}d}tjj�}t�x}|_t|�}tj|_|D]V}|dz|z}	|jtjj|j|t�ddtd|	i���X|D]\}|dz|z}
|j|t�dd	td|
i��|j|j j#�|
��^y)
NrZ)r�httpsr�)�file�imap�ldapzexample.com/b.htmlz://r�zSecurity Loopholer_�That's fine)r2rrar�rrr�r�r�r4rrcr�r�r�r"rorO)r%rj�
valid_schemes�invalid_schemes�schemeless_urlrGr{ror|�invalid_url�	valid_urls           r+�test_invalid_redirectz"HandlerTests.test_invalid_redirects��.��.�
�0��-���N�N�.�.�0��!�|�#��A�H��h����4�4���%�	<�F� �5�.�>�9�K����f�l�l�4�4�a�6F�6F����S�*=���[� 9�:�
<�	<�$�	>�F�����7�I�
���S�(�*�c�=��Z��3�4�
6����Q�U�U�/�/�1�9�=�		>r-c
��d}d}tjj�}t�x}|_t|�}tj|_tjj||�}|j|t�ddtd|i��|j|jj!�|�y)NrZz/b.htmlr�rsr_)r2rrar�rrr�r�r�r�urljoinr�r�r�r"rorO)r%rj�relative_urlrGr{rorxs       r+�test_relative_redirectz#HandlerTests.test_relative_redirects���.�� ���N�N�.�.�0��!�|�#��A�H��h����4�4����L�L�(�(��,�?�	�	����h�j�#�}���Y�/�0�	2�������+�+�-�y�9r-c��ddlm}ddlm}|�}||dd�t	dd�}t
jj�}t
jj�}t
jj|�}t||||�}|jd�|j|jjd��y)	Nr)�	CookieJar)�interact_netscapezhttp://www.example.com/z	spam=eggsr�z%Location: http://www.cracker.com/

�Cookie)�http.cookiejarr�test.test_http_cookiejarr�rAr2r�HTTPDefaultErrorHandlerrarTr1r�rjrorf)	r%rr�rW�hh�hdeh�hrh�cpr{s	         r+�test_cookie_redirectz!HandlerTests.test_cookie_redirect+s���,�>�
�[���"�7��E�
$�S�*U�
V���~�~�5�5�7���n�n�0�0�2��
�^�^�
/�
/��
3���b�$��R�0��	���(�)�������*�*�8�4�5r-c�6�d}tdd|z�}tjj�}tjj	�}t|||�}|j
d�}|j|j�|j��y)Nz(http://www.example.com/index.html#OK

r�z
Location: r&)
rAr2rr�rar1r�r"r��strip)r%�redirected_urlr�r�r�r{r�s       r+�test_redirect_fragmentz#HandlerTests.test_redirect_fragment:sx��G��
$�S�,��*G�
H���~�~�5�5�7���n�n�0�0�2���b�$��,��
�V�V�,�
-���������n�&:�&:�&<�=r-c������jtjj�tj
j�d}tj|�tj
_�jttj
d��td�����fd�}|tj
j_tjjd�}�j|j�d�y)Ns(HTTP/1.1 302 Found
Location: ?query

r()z/pathz/path?queryc����j|t����j|||g|��i|��d|j_y)NsHTTP/1.1 200 OK

Hello!)r"�nextrr�fakedata)�connr�r_�pos�kw�
real_classr%r�s     ���r+rz3HandlerTests.test_redirect_no_path.<locals>.requestNsA������S�$�t�*�-��J���t�V�S�=�3�=�"�=�&F�D�N�N�#r-zhttp://python.org/pathzhttp://python.org/path?query)r1r2rr3rrLr(r�fakehttpr�iterr6r"r�)r%�	response1rr�r�r�s`   @@r+�test_redirect_no_pathz"HandlerTests.test_redirect_no_pathCs����	
������1�1�2��[�[�/�/�
�E�	�%0�%9�%9�)�%D����"��������.>�
�K��,�-��	G�
.5����"�"�*�
�^�^�
#�
#�$<�
=���������&D�Er-c���Gd�dtjj�}|�}tjj|�}d}|D]�\}}|j	t|��5t
jd|zdz�|_|jd�}d|zdz}|j}	|j|	j|�t|	��ddd���y#1swY��xYw)	Nc��eZdZd�Zy)�4HandlerTests.test_redirect_encoding.<locals>.Handlerc��|j|j|�}|jj|_t	j
d�|_|S)Ns)HTTP/1.1 200 OK
Content-Length: 3

123)r;�
connection�buf�last_bufrr�)r%ro�results   r+r=z>HandlerTests.test_redirect_encoding.<locals>.Handler.http_open[sE�����d�o�o�s�;�� $��� 3� 3��
�"-�"6�"6��#����
r-N)rTr#rUr=rVr-r+�Handlerr�Zs��

r-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

rrsGET � )
r2r�HTTPHandler�build_opener�subTest�reprrr�r�r�r�rer)
r%r�r�r*rIr_r�rr�rs
          r+�test_redirect_encodingz#HandlerTests.test_redirect_encodingWs���	�f�n�n�0�0�	��)�����,�,�W�5��
��#(�
	M��X�v����d�8�n�-�	
M�%0�%9�%9�"�$,�-�0��&��"�
"�;�;�'<�=��"�V�+�d�2��!�*�*������ 2� 2�8� <�d�7�m�L�	
M�	
M�
	M�	
M�	
M�s
�.A0C)�)C2	c
���d}t|��t|��fD]�}t�}tjj	|�}|j|�dgg}t
||�}td�}|j|jd�|j|�|j|j|�|j|ddfg|jD�cgc]}|dd	��	c}���ycc}w)
N�proxy.example.com:3128�r)�HTTP)r=rzhttp://acme.example.com/zacme.example.comrr=r)�dictr	r2r�ProxyHandlerr)r.rr"r�r�r)	r%r'rxr{�phr+r,ro�tups	         r+�
test_proxyzHandlerTests.test_proxyzs���$���1��t��|�+�	<�A�� �A����,�,�Q�/�B�
�M�M�"��1�2��I�1��I�>�H��4�5�C����S�X�X�'9�:�
�F�F�3�K����S�X�X�q�)����x��{�K�8�9�23�'�'�:�3�c�!�A�h�:�
<�	<��;s�C2c�$�dtjd<t�}tjjt
d���}|j|�td�}|j|jd�|j|�|j|jd�td�}|j|jd�|j|�|j|jd�tjd=y)	Nz
python.org�no_proxy�proxy.example.comr�zhttp://www.perl.org/�www.perl.orgrHrG�r�environr	r2rr�r�r)rr"r�r��r%r{r�ros    r+�test_proxy_no_proxyz HandlerTests.test_proxy_no_proxy�s���!-��
�
�:�����
�^�^�
(�
(��3F�)G�
H��	�
�
�b���,�-��������>�2�	���s��������#6�7��-�.��������#3�4�	���s��������#3�4��J�J�z�"r-c�|�dtjd<t�}tjjt
d���}|j|�td�}|j|jd�|j|�|j|jd�tjd=y)N�*r�r�r�rHrGr�r�s    r+�test_proxy_no_proxy_allz$HandlerTests.test_proxy_no_proxy_all�s���!$��
�
�:�����
�^�^�
(�
(��3F�)G�
H��	�
�
�b���-�.��������#3�4�	���s��������#3�4��J�J�z�"r-c	���t�}tjjt	d���}|j|�dgg}t
||�}td�}|j|jd�|j|�|j|jd�|j|ddfg|jD�cgc]}|dd��	c}�ycc}w)	Nr��ro)rWr�https://www.example.com/�www.example.comrrWr)r	r2rr�r�r)r.rr"r�r�r)r%r{r�r+r,ror�s       r+�test_proxy_httpszHandlerTests.test_proxy_https�s������
�^�^�
(�
(��4L�)M�
N��	�
�
�b��
.�/�
�	�-�Q�	�:���0�1��������#4�5�	���s��������#;�<����8�A�;��5�6�./�g�g�6�s�#�a��(�6�	8��6s�	C
c��t�}tjjt	d���}|j|�t
�}|j|�td�}|jdd�|jdd�|j|jd�|j|j�|j|�|jd	|jj �|j#d
|jj �|j%|j�|j|jd�|j|j'd�d�y)Nr�r�r��Proxy-Authorization�FooBar�
User-Agent�Grailr�)r�r�)r�r��Proxy-authorization)r	r2rr�r�r)rSrrhr"r�rkr�r�r�r8r�rPrdrl)r%r{r��
https_handlerros     r+�$test_proxy_https_proxy_authorizationz1HandlerTests.test_proxy_https_proxy_authorization�s#�����
�^�^�
(�
(��4L�)M�
N��	�
�
�b��(�*�
�	�
�
�m�$��0�1�����,�h�7����|�W�-�������#4�5����#�*�*�+�	���s��	
���:�&�/�/�;�;�	=��
�
�-�#�,�,�8�8�	:����S�-�-�.�������#;�<�������(=�>��Ir-�darwinzonly relevant for OSXc�|�dgd�d�}dD]!}|jt||�d|z��#dD]!}|jt||�d|z��#dgd�}|jtd	|��dd
dgd�}d}|jt||�d|z�d
}|jt||�d|z�y)NF)�foo.barz	*.bar.com�	127.0.0.1z10.10z10.0/16)�exclude_simple�
exceptions)r�zwww.bar.comr�z	10.10.0.1z10.0.0.1z expected bypass of %s to be True)zabc.foo.barzbar.comz	127.0.0.2z	10.11.0.1�notinbypassz!expected bypass of %s to be FalseT�testz10.0.0.0/40z172.19.10.0/24z172.19.10.5z10.0.1.5)rer
rj)r%�bypassr�s   r+�test_osx_proxy_bypassz"HandlerTests.test_osx_proxy_bypass�s���$�&�
��!�	G�D��O�O�8��v�F�>��E�
G�	G�
�	I�D����9�$��G�@�4�G�
I�	I�%)��;�����4�V�V�D�E�$�)�+;�=�
�������4�T�6�B�:�T�A�	C������5�d�F�C�;�d�B�	Dr-c�n�|j||��5t�}t�}tjj|�}dj
|�dz}td|�}|j|�|j|�|j||d|||dd�ddd�y#1swYyxYw)N)rcr^z
rH�r_�!http://acme.example.com/protected)
r�r	rar2rr
�joinrAr)�_test_basic_auth)r%r^rcr*�password_manager�auth_handlerr��http_handlers        r+�check_basic_authzHandlerTests.check_basic_auth�s���
�\�\��w�\�
7�	G�#�%�F�2�4��!�>�>�>�>�?O�P�L��;�;�w�'�*�4�D�2�3��=�L����|�,����|�,��!�!�&�,��"'��7G�"E�"E�
G�	G�	G�	G�s�BB+�+B4c
���d}d}d|�d�}d|�d�}d}d|�d�}d|�d�d|�d�d|�d	�|�d
|��|�d
|��|�d
|��|�d
|��|�d
|��fD]}d|��g}|j||��tjdtf�5d
|��g}|j||�ddd�||g||g||gfD]&}	|	D�
cgc]}
d|
����	}}
|j||��(y#1swY�>xYwcc}
w)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=)r�r�check_warnings�UserWarning)r%rc�realm2�basic�basic2�other_no_realm�digest�	realm_strr^�
challenges�	challenges           r+�test_basic_authzHandlerTests.test_basic_auth�s���$��%����w�a�(�� ����*��*��"�6�(�+>�?���E�7�!�$��E�7�!�$��E�7�"4�5��g�R��x� ��g�R��'�(���b���(��g�R��x� ��h�b��� �
�	2�I�,�I�;�7�8�G��!�!�'�5�1�!	2�&�
+�
+�-L�%0�-2�3�	2�7��w�?�@�G��!�!�'�5�1�	2��V��
�V��
�U�O�
�	2�J�)3�4�$�,�I�;�7�4�G�4��!�!�'�5�1�	2�	2�	2��4s�C�3C"�Cc
�t�t�}tjjt	d���}|j|�t
�}tjj|�}d}tdd|z�}|j|�|j|�|j||d|||dd�y)Nr�r��
ACME Networksi�z(Proxy-Authenticate: Basic realm="%s"

r�z&http://acme.example.com:3128/protected)
r	r2rr�r�r)ra�ProxyBasicAuthHandlerrAr�)r%r*r�r�r�rcr�s       r+�test_proxy_basic_authz"HandlerTests.test_proxy_basic_auth+s����!��
�^�^�
(�
(��3K�)L�
M�����2��.�0���~�~�;�;�<L�M����.��?�%�G�I�����<�(����<�(����f�l�4I�#�\�3C�F�6�	 r-c
���Gd�dt�}Gd�dtjj�}Gd�dtjj�}|�}t�}||�}||�}d}t
dd	|z�}	|j|�|j|�|j|	�|j||d
||	|dd�|j|jdd
gdz�y)Nc��eZdZd�Zd�Zy)�QHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirectorc�<�tj|�g|_yr�)r	r��recordedr�s r+r�zZHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.__init__Fs���'�'��-� "��
r-c�:�|jj|�yr�)r�r)r%r�s  r+�recordzXHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.recordJs���
�
�$�$�T�*r-N)rTr#rUr�r�rVr-r+�RecordingOpenerDirectorr�Es��
#�
+r-r�c��eZdZd�Zy)�OHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandlerc��|jjd�tjjj
|g|��i|��y)Nr�)rr�r2r�HTTPDigestAuthHandler�http_error_401�r%r�rs   r+r�z^HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandler.http_error_401NsA�����"�"�8�,����4�4�C�C�D�L�>B�L�FJ�Lr-N�rTr#rUr�rVr-r+�TestDigestAuthHandlerr�Ms��
Lr-r�c��eZdZd�Zy)�NHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandlerc��|jjd�tjjj
|g|��i|��y)Nr�)rr�r2rr
r�r�s   r+r�z]HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandler.http_error_401TsA�����"�"�7�+����3�3�B�B�4�K�=A�K�EI�Kr-Nr�rVr-r+�TestBasicAuthHandlerrSs��
Kr-rr�r��&WWW-Authenticate: Basic realm="%s"

r_r�r�r�r)r	r2rr�r
rarAr)r�r"r�)
r%r�r�rr*r��digest_handler�
basic_handlerrcr�s
          r+�#test_basic_and_digest_auth_handlersz0HandlerTests.test_basic_and_digest_auth_handlers<s���	+�n�	+�	L�F�N�N�$H�$H�	L�	K�6�>�>�#F�#F�	K�)�*��.�0��.�/?�@��,�-=�>�
���.��=��E�G�����=�)����>�*����<�(�	
���f�m�_�#�\�3C�A�A�	 �	
������8�W�*=�a�*?�@r-c���t�}tjjd�}t	dd�}|j|�|j|�|j
t|jd�y)Nr�zWWW-Authenticate: Kerberos

r&)	r	r2rr�rAr)r4r5r�)r%r*�digest_auth_handlerr�s    r+�$test_unsupported_auth_digest_handlerz1HandlerTests.test_unsupported_auth_digest_handlernsc���!��$�n�n�B�B�4�H��.��5�7�����.�/����<�(����*�f�k�k�3K�Lr-c���t�}tjjd�}t	dd�}|j|�|j|�|j
t|jd�y)Nr�zWWW-Authenticate: NTLM

r&)	r	r2rr
rAr)r4r5r�)r%r*�basic_auth_handlerr�s    r+�#test_unsupported_auth_basic_handlerz0HandlerTests.test_unsupported_auth_basic_handlerxsc���!��#�^�^�@�@��F��.��1�3�����-�.����<�(����*�f�k�k�3K�Lr-c	��ddl}	d\}
}|j|||
|�|j||j�|j||j�|j|
|j
�|j||j�|j|�|j|j|�|j|j|�|jt|j�d�|j|jdj|��t|
�d|��d�}d|	j|�j!�j#�z}
|j|jdj%|�|
�|j|jdj&||
�dx|_|_|j)�|j|�|jt|j�d�|j|jdj|��y)Nr��wile�coyoter�:�asciizBasic r)�base64r�r"rcr_rdrtr�rgrhrrFrjrf�bytes�encodebytesr��decoderlr�rC)r%r*r��auth_headerrcr�r��request_url�
protected_urlrrdrt�userpass�auth_hdr_values              r+r�zHandlerTests._test_basic_auth�s���	�)���h�	�!�!�%��d�H�E����� 0� 6� 6�7�����&6�&:�&:�;�����/�4�4�5�����#3�#<�#<�=����K� �	
���)�6�6��>����)�4�4�m�D�	
����\�2�2�3�Q�7�����.�.�q�1�<�<�[�I�J��D�(�3�W�=��"����x�(�.�.�0�7�7�9�:������.�.�q�1�<�<�[�I�'�	)�����.�.�q�1�C�C�K�P�'�	)�=A�@��� 0� 9��������K� �����\�2�2�3�Q�7�����.�.�q�1�<�<�[�I�Jr-c��d\}}d}td�}t�}t|�}|jd|||d��|j	|j|��|j	|j|dz��|j
|j|dz��t�}|j|�|j|�|j|�|j	|j�y)Nrr�r�T)�is_authenticatedz/nested�plain)rYrr
r�rerrjr	r)r�r\)r%rdrtrr��pwd_manager�auth_prior_handlerr*s        r+�test_basic_prior_auth_auto_sendz,HandlerTests.test_basic_prior_auth_auto_send�s���*���h�9��/��4��2�4��1�+�>���'�'��+�t�X��	(�	F�	
����4�4�[�A�B�����4�4�[�9�5L�M�N�����5�5�k�G�6K�L�M��!�����-�.����<�(����K� �	
����4�4�5r-c��d\}}d}d}t�}t|�}|j||||�|j|�}|j	|�t�}|j
|�tdddz�}	|j
|	�|j|�|j|�}|j|�td�}	|j	|	j�t�}|j
|�|j
|	�|j|�|j|	j�y)Nrr��ACMEr�rr�)rr
r�rrjr	r)rAr�rerYr\)
r%rdrtrrcr r!�is_authr*r�s
          r+�.test_basic_prior_auth_send_after_first_successz;HandlerTests.test_basic_prior_auth_send_after_first_success�s$��*���h�9����2�4��1�+�>���'�'��{�D�(�K��.�.�{�;������!��!�����-�.�.��=��D�F�����<�(����K� ��.�.�{�;������ �/��4������5�5�6��!�����-�.����<�(�	���K� �	
����4�4�5r-c��dD]�\}}dj|�}tj|j�|z�}tj
j
�}td�}d|_|j||�5}|j�ddd�|j|jjdj|����y#1swY�AxYw)z=Test the connection is cleaned up when the response is closed))zConnection: close�data)zTransfer-Encoding: chunkeds4
data
0

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

�
http://dummy/NzConnection not closed with {!r})�formatrr�r�r2rr�rr�r;r<re�fakesock�closed)r%�transferr��headerr�r�ro�resps        r+�test_http_closedzHandlerTests.test_http_closed�s���!
�
	D��X�t�
5�;�;�H�E�F��'�'��
�
��$�(>�?�D��n�n�8�8�:�G��/�*�C��C�K�����s�+�
�t��	�	��
��O�O�D�M�M�0�0�1�8�8��B�
D�
	D�
�
�s�C�C	c�z�tjd�}tjj	�}td�}d|_|jtjj�5|j||�ddd�|j|jjd�y#1swY�0xYw)z;Test the connection is cleaned up after an invalid responser-r)NzConnection not closed)rr�r2rr�rr�r4rrL�
BadStatusLiner;rer+r,)r%r�r�ros    r+�test_invalid_closedz HandlerTests.test_invalid_closed�s����#�#�C�(���.�.�4�4�6���o�&�����
�
�
�t�{�{�8�8�
9�	'��O�O�D�#�&�	'�����
�
�,�,�.E�F�	'�	'�s�/B1�1B:N)5rTr#rUr�r�r�rr
rrrr#r+r/r��
skipUnlessr rrLr3r5r=rCrErKrRrXrmryr}r�r�r�r�r�r�r�r�r�r�platformr�r�r�r�rr
r
r�r"r&r0r3rVr-r+r�r��s���>H�@Z5�x@C�DE�2�.)�,O�(;�J�/�+��X�������.?�@�Bm�n�/�o�/��X�������.?�@�Bm�n�+�o�+�P�6M�.)�.�&C�2�"HR�T>�,:�
6�>�F�(!M�F<�$
#�	#�8� �X�������.?�@�Bm�n�J�o�J�.�X�������1�3J�K�D�L�D�BG�+2�Z �"0A�dM�M�#K�J6�6&6�PD�"Gr-r�c�0�eZdZd�Zd�Zd�Zd�Zd�Zd�Zy)�	MiscTestsc�`��|jt�fd�|jD���y)Nc3�<�K�|]}|j�k(���y�wr�r?)�.0rG�
handler_classs  �r+�	<genexpr>z/MiscTests.opener_has_handler.<locals>.<genexpr>	s"�����6� !��K�K�=�8�6�s�)re�anyr,)r%r*r;s  `r+�opener_has_handlerzMiscTests.opener_has_handlers'�������6�%+�_�_�6�6�	7r-c�.�Gd�dtjj�}Gd�dtjj�}Gd�dtjj�}tjj}|||�}|j||�|j||�|||��}|j||�|j||�||�}|j||�|�}|j|tjj�|tjj�}|j|tjj�|tjj��}|j|tjj�Gd�dtjj�}|||�}|j||�|j||�y)	Nc��eZdZy)�2MiscTests.test_build_opener.<locals>.MyHTTPHandlerNr&rVr-r+�
MyHTTPHandlerrA
r(r-rBc��eZdZd�Zy)�/MiscTests.test_build_opener.<locals>.FooHandlerc��yr�rVr�s r+�foo_openz8MiscTests.test_build_opener.<locals>.FooHandler.foo_openr�r-N)rTr#rUrFrVr-r+�
FooHandlerrD���
r-rGc��eZdZd�Zy)�/MiscTests.test_build_opener.<locals>.BarHandlerc��yr�rVr�s r+�bar_openz8MiscTests.test_build_opener.<locals>.BarHandler.bar_openr�r-N)rTr#rUrLrVr-r+�
BarHandlerrJrHr-rMc��eZdZy)�7MiscTests.test_build_opener.<locals>.MyOtherHTTPHandlerNr&rVr-r+�MyOtherHTTPHandlerrO1r(r-rP)r2rr��BaseHandlerr�r>)r%rBrGrMr�r{rPs       r+�test_build_openerzMiscTests.test_build_openers���	�F�N�N�6�6�	�	����3�3�	�	����3�3�	��~�~�2�2����Z�0������:�.�����:�.�
��Z�\�2������:�.�����:�.�
��'������=�1�
�N������6�>�>�#=�#=�>�����3�3�4������6�>�>�#=�#=�>�����3�3�5�6������6�>�>�#=�#=�>�	����!;�!;�	�
��(:�;������=�1�����#5�6r-c��d}dx}x}}d}tjj|||||�}|jt	|d��|j|jd�|jt	|d��|j|jd�d|j�d|j��}|jt|�|�d|j�d|j�d	�}|jt|�|�y)
z�
        Issue 13211 reveals that HTTPError didn't implement the URLError
        interface even though HTTPError is a subclass of URLError.
        zsomething bad happenedNzContent-Length: 42rNr^zHTTP Error z: z<HTTPError �>)r2rrcrer r"rNr^r�r�rQr�)r%r�r_r�r�r�rR�expected_errmsgs        r+�test_HTTPError_interfacez"MiscTests.test_HTTPError_interface8s���
'������d�R�#���l�l�$�$�S�$��T�2�>�������X�.�/�������%=�>������Y�/�0�������&:�;�14���3�7�7�C������S��?�3�25�(�(�C�G�G�D������c��O�4r-c���tjjddddd�}|jt	|dd�d�|j|jj�t�y)Nr_i�zMETHOD NOT ALLOWED�	__notes__rV)	r2rrcr"rbr�r�r<r)r%rls  r+�
test_gh_98778zMiscTests.test_gh_98778JsR���L�L�"�"�5�#�/C�T�4�P�������K��4�b�9����a�d�d�i�i�k�5�1r-c��gd�}|D] \}}|jt|�|��"|jttd�fy)N)�r�)NNNr��r�)NNNr�r[r\)zjoe:password@proxy.example.com)Nrsrtr�)z#joe:password@proxy.example.com:3128)Nrsrtr�)zhttp://proxy.example.com/)rNNr�)zhttp://proxy.example.com:3128/)rNNr�)z&http://joe:password@proxy.example.com/�rrsrtr�)z*http://joe:password@proxy.example.com:3128)rrsrtr�)z1ftp://joe:password@proxy.example.com/rubbish:3128)r�rsrtr�)z%http://joe:password@proxy.example.comr])z&http://user/name:password@localhost:22)r�	user/namert�localhost:22)z&http://username:pass/word@localhost:22)r�username�	pass/wordr_)z'http://user/name:pass/word@localhost:22)rr^rar_zfile:/ftp.example.com)r"rr4r5)r%�parse_proxy_test_cases�tcr�s    r+�test_parse_proxyzMiscTests.test_parse_proxyOsM��$"
��N3�	9�L�B�����\�"�-�x�8�	9�	
���*�l�4K�L�Mr-c���t�}|jt�5}|jd�ddd�|j	tj�d�y#1swY�/xYw)N�invalidz5Unsupported digest authentication algorithm 'invalid')rr4r5�get_algorithm_implsr"rQ�	exception)r%r��excs   r+�test_unsupported_algorithmz$MiscTests.test_unsupported_algorithm|sZ��+�-��
�
�
�z�
*�	3�c��'�'�	�2�	3������
�
��C�	
�	3�	3�s�A � A)N)	rTr#rUr>rRrVrYrdrjrVr-r+r7r7s#��7�*7�X5�$2�
+N�Z
r-r7c�v�eZdZGd�de�Zd�Zd�Zd�Zd�Zd�Z	d�Z
d	�Zd
�Zd�Z
d�Zd
�Zd�Zd�Zd�Zy)�RequestTestsc��eZdZdZy)�RequestTests.PutRequest�PUTN)rTr#rUr�rVr-r+�
PutRequestrn�s���r-rpc���td�|_tddddi��|_tdd��|_|j	d�|_|j	dd��|_y)	N�http://www.python.org/~jeremy/r�zX-Testr�r]�HEAD)r�r�)rr��post�headrp�put�
force_postr�s r+�setUpzRequestTests.setUp�sf���;�<����<�"�%-�v�$6�8��	��<�V�L��	��?�?�#C�D����/�/�*J��*���r-c��|jd|jj��|jd|jj��|jd|jj��|jd|j
j��|jd|jj��y)Nr�r�rsro)r"rtrer�rurvrwr�s r+�test_methodzRequestTests.test_method�s����������!5�!5�!7�8�������� 3� 3� 5�6��������!5�!5�!7�8�������� 3� 3� 5�6��������!;�!;�!=�>r-c�b�|j|jj�|jd|jj	��d|j_|j|jj�|jd|jj	��y)Nr�r�r�)rjr�r�r"rerer�s r+�	test_datazRequestTests.test_data�sq����������'�������� 3� 3� 5�6�����
�������
�
�&��������!4�!4�!6�7r-c�H�|jd|jj�|jjdd�|j	d|jjd�d|j_|jd|jj�y)Nr��*r�)r�r�r�rnr"r�r�s r+�.test_setting_data_should_remove_content_lengthz;RequestTests.test_setting_data_should_remove_content_length�sx�����)�4�8�8�+E�+E�F����(�(�)9�2�>�����T�X�X�7�7�8H�I�J�����
����)�4�8�8�+E�+E�Fr-c�`�|jd|jj�d|j_|jj	dd�|jd|jjd�|j`|jd|jj�y)Nr�r|r)r�r�r�r�rnr"r�s r+�/test_deleting_data_should_remove_content_lengthz<RequestTests.test_deleting_data_should_remove_content_length�s������)�4�8�8�+E�+E�F�����
����(�(�)9�1�=�����D�H�H�6�6�7G�H�I��H�H�M����)�4�8�8�+E�+E�Fr-c�X�|jd|jj��y)Nrr)r"r�rOr�s r+�test_get_full_urlzRequestTests.test_get_full_url�s#�����9����.�.�0�	2r-c��|jd|jj�td�}|jd|j�y)Nz	/~jeremy/zhttp://www.python.org/r/)r"r�r�rr<s  r+�
test_selectorzRequestTests.test_selector�s;������d�h�h�&7�&7�8��.�/������c�l�l�+r-c�P�|jd|jj�y)Nr)r"r�r�r�s r+�
test_get_typezRequestTests.test_get_type�s�����������/r-c�P�|jd|jj�y)NrG)r"r�r�r�s r+�
test_get_hostzRequestTests.test_get_host�s�����)�4�8�8�=�=�9r-c�R�td�}|jd|j�y)Nzhttp://www.%70ython.org/rG�rr"r�r<s  r+�test_get_host_unquotez"RequestTests.test_get_host_unquote�s"���0�1�����)�3�8�8�4r-c�x�|j|jj��|jjdd�|j	|jj��|jd|jj�|jd|jj�y)Nr�rrG)rjr��	has_proxyr7rer"rUr�r�s r+r�zRequestTests.test_proxy�s}��������+�+�-�.������>�6�2�������*�*�,�-����)�4�8�8�+C�+C�D����������7r-c�R�td�}|jd|j�y)Nz<URL:http://www.python.org>rGr�r<s  r+�test_wrapped_urlzRequestTests.test_wrapped_url�s"���3�4�����)�3�8�8�4r-c��td�}|jd|j�td�}|jd|j�d}t|�}|j|j�|�y)N�-http://www.python.org/?qs=query#fragment=truez
/?qs=queryzhttp://www.python.org/#fun=truer/�.http://docs.python.org/library/urllib2.html#OK)rr"r�rO)r%ror_s   r+�test_url_fragmentzRequestTests.test_url_fragment�si���E�F������s�|�|�4��7�8������c�l�l�+�?���c�l������)�)�+�S�1r-c��gd�}|D]7}t|�}|j|j�|j��9y)N)zhttp://docs.python.orgr�r�)rr"rOr?)r%r�r_ros    r+�test_url_fullurl_get_full_urlz*RequestTests.test_url_fullurl_get_full_url�sA��A���	?�C��#�,�C����S�-�-�/����>�	?r-N)rTr#rUrrprxrzr|rr�r�r�r�r�r�r�r�r�r�rVr-r+rlrl�sV���W���?�8�G�G�2�,�
0�:�5�8�5�	2�?r-rl�__main__)Ar�r�r�test.supportrrrrrr�r�r!rrr�urllib.requestr2rr	r
rrr
r�urllib.parserry�http.clientr�requires_working_socket�TestCaserrXr�r�r�r�r�r�r�r��IOBaser�r�r�r.r1rr�r3rQrAr rL�HTTPSHandlerrSrYrarkr�r�r7rlrT�mainrVr-r+�<module>r�s�����"�(���	�	�
��
����7�7�7�"�������t�,�1,�8�$�$�1,�hx'�x�(�(�x'�v,�,�
�
�#�$�#�
	�2�;�;�	�3�3�?�?��r�y�y��$*
�*
�Z38�38�l�D�0�f�n�n�0�0�0�H�f�n�n�8�8�H�8�4�;�;�)�*�	4�6�>�>�6�6�	4�Q�v�~�~�9�9�Q�&
(�
(�NA�(�+�+�NA�b	�jG�8�$�$�jG�Z!}
��!�!�}
�@^?�8�$�$�^?�B�z���H�M�M�O�r-