HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/lib/python3/dist-packages/twisted/web/test/__pycache__/test_webclient.cpython-38.pyc
U

s�@g���@s�dZddlmZmZddlZddlZddlmZzddlmZm	Z	Wn$e
k
rhddlmZm	Z	YnXddlm
Z
mZmZddlmZmZddlmZmZmZmZdd	lmZdd
lmZddlmZmZmZm Z ddl!m"Z"dd
l#m$Z$ddl%m&Z&ddl'm(Z(m)Z)m*Z*zddlm+Z+WndZ+YnXddl,m-Z-ddl.m/Z/m0Z0m1Z1m2Z2m3Z3ddl4m5Z5m6Z6e$e-j7��8d�Z9e9�:�j;Z<Gdd�dej=�Z>Gdd�dej=�Z?Gdd�dej=�Z@Gdd�dej=�ZAGdd�dej=�ZBGdd �d ej=�ZCGd!d"�d"ej=�ZDGd#d$�d$ej=�ZEGd%d&�d&ej=�ZFGd'd(�d(ej=�ZGGd)d*�d*ej=�ZHGd+d,�d,e�ZIGd-d.�d.ej=�ZJGd/d0�d0ejK�ZLGd1d2�d2ejK�ZMGd3d4�d4ejK�ZNGd5d6�d6eN�ZOGd7d8�d8ejK�ZPGd9d:�d:ejK�ZQGd;d<�d<ejK�ZRe+dk�s�eSe+d=��seOePfD]ZTd>eT_U�qe �Ved��s6eOePfD]ZTd?eT_U�q(Gd@dA�dA�ZWGdBdC�dCeWejK�ZXGdDdE�dEeWejK�ZYGdFdG�dGeWejK�ZZGdHdI�dIejK�Z[GdJdK�dKe5ej\�Z]GdLdM�dMe6ej\�Z^GdNdO�dOe5ej\�Z_GdPdQ�dQe6ej\�Z`dRdS�ZaGdTdU�dUe5ej\�ZbGdVdW�dWe6ej\�ZcGdXdY�dYeb�ZdGdZd[�d[ec�ZeGd\d]�d]e5ej\�ZfGd^d_�d_e6ej\�ZgGd`da�dae6ej\�ZhGdbdc�dce5ej\�ZiGddde�dee6ej\�ZjGdfdg�dge6ej\�ZkdS)hzG
Tests for the old L{twisted.web.client} APIs, C{getPage} and friends.
�)�division�absolute_importN)�ENOSPC)�urlparse�urljoin)�
networkString�nativeString�
intToBytes)�unittest�util)�server�client�error�resource)�Data)�Redirect)�address�reactor�defer�
interfaces)�
ClientFactory)�FilePath)�WrappingFactory)�StringTransport�waitUntilAllDisconnected�EventLoggingObserver)�ssl)�test)�globalLogPublisher�FilteringLogObserver�LogLevelFilterPredicate�LogLevel�Logger)�MethodInjectionTestsMixin�URIInjectionTestsMixinz
server.pemc@s8eZdZdZdZdZdd�Zdd�Zdd	�Zd
d�Z	dS)�ExtendedRedirectz�
    Redirection resource.

    The HTTP status code is set according to the C{code} query parameter.

    @type lastMethod: C{bytes}
    @ivar lastMethod: Last handled HTTP request method
    TNcCstj�|�||_dS�N)r�Resource�__init__�url)�selfr)�r+�A/usr/lib/python3/dist-packages/twisted/web/test/test_webclient.pyr(@szExtendedRedirect.__init__cCs@|jr|j|_dS|j|_t|jdd�}|�|j||�SdS)NsOK Thnx!scoder)�
lastMethod�method�int�args�
redirectTor))r*�request�coder+r+r,�renderEszExtendedRedirect.rendercCs|Sr&r+)r*�namer2r+r+r,�getChildOszExtendedRedirect.getChildcCs|�|�|�d|�dS)NslocationsOK Bye!)�setResponseCode�	setHeader)r*r)r2r3r+r+r,r1Ss
zExtendedRedirect.redirectTo)
�__name__�
__module__�__qualname__�__doc__ZisLeafr-r(r4r6r1r+r+r+r,r%3s
r%c@s"eZdZdZddd�Zdd�ZdS)	�ForeverTakingResourceza
    L{ForeverTakingResource} is a resource which never finishes responding
    to requests.
    FcCstj�|�||_dSr&)rr'r(�_write)r*�writer+r+r,r(_szForeverTakingResource.__init__cCs|jr|�d�tjS�Ns
some bytes)r>r?r�NOT_DONE_YET�r*r2r+r+r,r4cs
zForeverTakingResource.renderN)F�r9r:r;r<r(r4r+r+r+r,r=Zs
r=c@s eZdZdZdd�Zdd�ZdS)�ForeverTakingNoReadingResourcez�
    L{ForeverTakingNoReadingResource} is a resource that never finishes
    responding and that removes itself from the read loop.
    cCstj�|�dSr&)rr'r(�r*r+r+r,r(nsz'ForeverTakingNoReadingResource.__init__cCs|j��tjSr&)�	transportZpauseProducingrrArBr+r+r,r4qs
z%ForeverTakingNoReadingResource.renderNrCr+r+r+r,rDisrDc@seZdZdd�ZdS)�CookieMirrorResourcecCsJg}tt|j����D]\}}|�t|�t|�f�q|��tt|��Sr&)	�sorted�listZreceived_cookies�items�appendr�sortr�repr)r*r2�l�k�vr+r+r,r4xs
zCookieMirrorResource.renderN�r9r:r;r4r+r+r+r,rGwsrGc@seZdZdd�ZdS)�RawCookieMirrorResourcecCs&|�d�}|dkrdSttt|���S)Nscookie�None)Z	getHeaderrrMr)r*r2�headerr+r+r,r4�s
zRawCookieMirrorResource.renderNrQr+r+r+r,rRsrRc@seZdZdd�ZdS)�
ErrorResourcecCs&|�d�|j�d�r"|�dd�dS)Ni�s
showlength�content-length�0�)r7r0�getr8rBr+r+r,r4�s
zErrorResource.renderNrQr+r+r+r,rU�srUc@seZdZdd�ZdS)�NoLengthResourcecCsdS)N�nolengthr+rBr+r+r,r4�szNoLengthResource.renderNrQr+r+r+r,rZ�srZc@seZdZdZdd�ZdS)�HostHeaderResourcezg
    A testing resource which renders itself as the value of the host header
    from the request.
    cCs|j�d�dS)N�hostr)�requestHeaders�
getRawHeadersrBr+r+r,r4�szHostHeaderResource.renderN�r9r:r;r<r4r+r+r+r,r\�sr\c@seZdZdZdd�ZdS)�PayloadResourcez�
    A testing resource which renders itself as the contents of the request body
    as long as the request body is 100 bytes long, otherwise which renders
    itself as C{"ERROR"}.
    cCs:|j��}|j�d�d}t|�dks2t|�dkr6dS|S)NrVr�dsERROR)Zcontent�readr^r_�lenr/)r*r2�dataZ
contentLengthr+r+r,r4�s

zPayloadResource.renderNr`r+r+r+r,ra�srac@seZdZdd�Zdd�ZdS)�
DelayResourcecCs
||_dSr&)�seconds)r*rgr+r+r,r(�szDelayResource.__init__cs �fdd�}t�|j|�tjS)Ncs��d����dSr@)r?Zfinishr+�r2r+r,�response�s
z&DelayResource.render.<locals>.response)rZ	callLaterrgrrA)r*r2rir+rhr,r4�szDelayResource.renderN)r9r:r;r(r4r+r+r+r,rf�srfc@seZdZdd�ZdS)�BrokenDownloadResourcecCs|�dd�|�d�dS)NrV�5�abcrX)r8r?rBr+r+r,r4�s
zBrokenDownloadResource.renderNrQr+r+r+r,rj�srjc@s eZdZdZdd�Zdd�ZdS)�CountingRedirectzl
    A L{Redirect} resource that keeps track of the number of times the
    resource has been accessed.
    cOstj|f|�|�d|_dS�Nr)rr(�count)r*�a�kwr+r+r,r(�szCountingRedirect.__init__cCs|jd7_t�||�S�N�)rorr4rBr+r+r,r4�szCountingRedirect.renderNrCr+r+r+r,rm�srmc@s eZdZdZdd�Zdd�ZdS)�CountingResourcezR
    A resource that keeps track of the number of times it has been accessed.
    cCstj�|�d|_dSrn)rr'r(rorEr+r+r,r(�szCountingResource.__init__cCs|jd7_dS)NrssSuccess)rorBr+r+r,r4�szCountingResource.renderNrCr+r+r+r,rt�srtc@s eZdZdZdd�Zdd�ZdS)�URLJoinTestsz'
    Tests for L{client._urljoin}.
    cCs@|�t�dd�d�|�t�dd�d�|�t�dd�d�dS)z�
        L{client._urljoin} does not include a fragment identifier in the
        resulting URL if neither the base nor the new path include a fragment
        identifier.
        �http://foo.com/bar�/quuxshttp://foo.com/quuxshttp://foo.com/bar#s/quux#N��assertEqualr
Z_urljoinrEr+r+r,�test_noFragments�s
�
�
�zURLJoinTests.test_noFragmentscCs@|�t�dd�d�|�t�dd�d�|�t�dd�d�dS)a
        L{client._urljoin} preserves the fragment identifier from either the
        new path or the base URL respectively, as specified in the HTTP 1.1 bis
        draft.

        @see: U{https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-22#section-7.1.2}
        shttp://foo.com/bar#fragrwshttp://foo.com/quux#fragrvs/quux#frag2shttp://foo.com/quux#frag2NrxrEr+r+r,�test_preserveFragments�s
�
�
�z#URLJoinTests.test_preserveFragmentsN)r9r:r;r<rzr{r+r+r+r,ru�sruc@s&eZdZdZejed�gZdd�ZdS)�HTTPPageGetterTestszq
    Tests for L{HTTPPagerGetter}, the HTTP client protocol implementation
    used to implement L{getPage}.
    ��categorycCsdtjddddiddddd	d
d�d�}t�}t��}||_|�|�|��}d
D]}|�||�qNdS)a,
        When a connection is made, L{HTTPPagerGetter} sends the headers from
        its factory's C{headers} dict.  If I{Host} or I{Content-Length} is
        present in this dict, the values are not sent, since they are sent with
        special values before the C{headers} dict is processed.  If
        I{User-Agent} is present in the dict, it overrides the value of the
        C{agent} attribute of the factory.  If I{Cookie} is present in the
        dict, its value is added to the values from the factory's C{cookies}
        attribute.
        shttp://foo/barsfoobar�baz�quuxs	some datasexample.netsfoobles	blah blahs12981svalue)�Hosts
User-AgentsCookiesContent-LengthsUseful)�agent�cookies�postdata�headers)sHost: example.net
sUser-Agent: foobar
sContent-Length: 9
sUseful: value
sconnection: close
sCookie: blah blah; baz=quux
N)r
�HTTPClientFactoryr�HTTPPageGetter�factory�makeConnection�value�assertIn)r*r�rFZprotocol�resultZexpectedHeaderr+r+r,�test_earlyHeaderss&��
z%HTTPPageGetterTests.test_earlyHeadersN)r9r:r;r<r�suppress�DeprecationWarningr�r+r+r+r,r|sr|c@sheZdZejed�gZe�Zdd�Zdd�Z	dd�Z
dd	�Zd
d�Zdd
�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Z d4d5�Z!d6d7�Z"d8d9�Z#d:d;�Z$d<d=�Z%d>d?�Z&d@dA�Z'dBdC�Z(dDdE�Z)dFdG�Z*dHdI�Z+dJdK�Z,dLdM�Z-dNdO�Z.dPdQ�Z/dRdS�Z0dTS)U�WebClientTestsr}cCstjd|dd�S�Nr�	127.0.0.1��	interface�r�	listenTCP�r*�siter+r+r,�_listen:szWebClientTests._listencCs�d|_d|_t��}|�dtdd��|�dtd��td�|_|�d|j�|�d	t	��|�d
t	dd��|�d
t
��|�dt��|�dt��|�dt
��|�dt��|�dt��|�dt��|�dtd��|�dtd��t�|_|�d|j�|�dtd��tdd�}dd�|_|�d|�td �|_|�d!|j�tj|dd"�|_t|j�|_|�|j�|_|j��j|_dS)#Nrsfile�
0123456789z	text/htmlsredirect�/files/infiniteRedirectsinfiniteRedirectswaitswrite-then-waitT)r?s
never-readserrorr[r]spayloadsbroken�cookiemirrorsdelay1rssdelay2�safterFoundGetCountersafterFoundGetRedirects/afterFoundGetCounters"miscased-head GET response contentzmajor/minorcSsdS)N�miscased-head contentr+rhr+r+r,�<lambda>VrXz&WebClientTests.setUp.<locals>.<lambda>s
miscased-heads/extendedRedirectsextendedRedirect�Ztimeout) r��cleanupServerConnectionsrr'�putChildrrrm�infiniteRedirectResourcer=rDrUrZr\rarjrGrfrt�afterFoundGetCounterZrender_Headr%�extendedRedirectr�Siter�r�wrapperr��port�getHost�portno)r*�rZmiscasedHeadr+r+r,�setUp=s<



zWebClientTests.setUpcCs�|jr|j��d|_t|jj���}ttt|�|j	��D]&}|�
�}|jjd|d�|j
��q:|j��}t�ttt|jj����|g�S)NzClosing {proto})�proto)r�ZcloseCachedConnectionsrIr�Z	protocols�keys�range�minrdr��pop�_log�inforFZabortConnectionr��
stopListeningr�DeferredListrr)r*Zconnections�nr��dr+r+r,�tearDown`s

��zWebClientTests.tearDowncCsd|j}tt|t|���S)Nzhttp://127.0.0.1:%d/)r�rrr)r*�path�hostr+r+r,�getURLvs
zWebClientTests.getURLcCs"d}tj|�d�|d��|j|�S)Nsd0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789Zpayload)r��r
�getPager��addCallbackry)r*�sr+r+r,�testPayloadzs
�zWebClientTests.testPayloadcs4t���d��}��|tj�}|��fdd��|S)z�
        If the connection is closed before the number of bytes indicated by
        I{Content-Length} have been received, the L{Deferred} returned by
        L{getPage} fails with L{PartialDownloadError}.
        �brokencs��|jd�S�Nrl)ryri��excrEr+r,r��rXz;WebClientTests.test_getPageBrokenDownload.<locals>.<lambda>)r
r�r��
assertFailure�PartialDownloadErrorr��r*r�r+rEr,�test_getPageBrokenDownload�sz)WebClientTests.test_getPageBrokenDownloadcs`t�����t���d��j�}��|tj�}�fdd�}|�|���fdd�}|�|�|S)z�
        If the connection is closed before the number of bytes indicated by
        I{Content-Length} have been received, the L{Deferred} returned by
        L{downloadPage} fails with L{PartialDownloadError}.
        r�cs ��|jd���|jd�|S)z}
            The HTTP status code from the server is propagated through the
            C{PartialDownloadError}.
            �200�OK)ry�status�message)rirEr+r,�
checkResponse�szEWebClientTests.test_downloadPageBrokenDownload.<locals>.checkResponsecs�����d�dSr�)ry�
getContent��ignored�r�r*r+r,�cbFailed�sz@WebClientTests.test_downloadPageBrokenDownload.<locals>.cbFailed)	r�mktempr
�downloadPager�r�r�r�r�)r*r�r�r�r+r�r,�test_downloadPageBrokenDownload�s

z.WebClientTests.test_downloadPageBrokenDownloadcs�ttd��G�fdd�d�}t��t�ttjd�g��t�����	�fdd��t
���d�|��}��
|t
j�}���fdd	�}|�|�|S)
z�
        If there is an exception closing the file being written to after the
        connection is prematurely closed, that exception is logged.
        zNo file left on devicecs eZdZdd�Z�fdd�ZdS)zFWebClientTests.test_downloadPageLogsFileCloseError.<locals>.BrokenFilecSsdSr&r+)r*�bytesr+r+r,r?�szLWebClientTests.test_downloadPageLogsFileCloseError.<locals>.BrokenFile.writecs��dSr&r+rEr�r+r,�close�szLWebClientTests.test_downloadPageLogsFileCloseError.<locals>.BrokenFile.closeN�r9r:r;r?r�r+r�r+r,�
BrokenFile�sr�)ZdefaultLogLevelcs
t���Sr&)rZremoveObserverr+)�filteredr+r,r��rXzDWebClientTests.test_downloadPageLogsFileCloseError.<locals>.<lambda>r�csZ��dt����d}|d}��|jt���|jj�j���t��t��d�dS)NrsrZlog_failure)ZassertEqualsrd�assertIsInstancer��IOErrorr0ryZflushLoggedErrors)r�Zevent�f)r��logObserverr*r+r,r��s�zDWebClientTests.test_downloadPageLogsFileCloseError.<locals>.cbFailed)r�rrrr r!ZcriticalrZaddObserver�
addCleanupr
r�r�r�r�r�)r*r�r�r�r+)r�r�r�r*r,�#test_downloadPageLogsFileCloseError�s
�

z2WebClientTests.test_downloadPageLogsFileCloseErrorcCsLt�t�|�d���|jdt|j��tj|�d�ddid��|jd�g�S)Nr�s
127.0.0.1:r�swww.example.com)r�)	r�
gatherResultsr
r�r�r�ryr	r�rEr+r+r,�testHostHeader�s����zWebClientTests.testHostHeadercCs"t�|�d��}|�|jd�|S)z�
        L{client.getPage} returns a L{Deferred} which is called back with
        the body of the response if the default method B{GET} is used.
        �filer�r�r�r+r+r,�test_getPage�szWebClientTests.test_getPagecCs&tj|�d�dd�}|�|jd�|S)z�
        L{client.getPage} returns a L{Deferred} which is called back with
        the empty string if the method is I{HEAD} and there is a successful
        response code.
        r�sHEAD�r.rXr�r�r+r+r,�test_getPageHEAD�szWebClientTests.test_getPageHEADcCs&tj|�d�dd�}|�|jd�|S)z�
        If the request method is a different casing of I{HEAD} (ie, not all
        capitalized) then it is not a I{HEAD} request and the response body
        is returned.
        z
miscased-headsHeadr�r�r�r�r+r+r,�test_getPageNotQuiteHEAD�sz'WebClientTests.test_getPageNotQuiteHEADcCs2tj|�d�dd�}|�|jtd|jf��|S)z�
        When a non-zero timeout is passed to L{getPage} and the page is
        retrieved before the timeout period elapses, the L{Deferred} is
        called back with the contents of the page.
        r�rbr�z127.0.0.1:%s)r
r�r�r�ryrr�r�r+r+r,�test_timeoutNotTriggering�s
�z(WebClientTests.test_timeoutNotTriggeringcCs$d|_|�tj|�d�dd�tj�S)z�
        When a non-zero timeout is passed to L{getPage} and that many
        seconds elapse before the server responds to the request. the
        L{Deferred} is errbacked with a L{error.TimeoutError}.
        rs�waitg���ư>r�)r�r�r
r�r�r�TimeoutErrorrEr+r+r,�test_timeoutTriggerings
�z%WebClientTests.test_timeoutTriggeringcCsfg}d|��dfd|��dfg}|D]6\}}}t�|�|�|�}|�|j||�|�|�q$t�|�S)Nr�r�Znolengthr[)	r�r
r�r�r��_cbDownloadPageTestrKrr�)r*Z	downloadsZdownloadDatar)r5rer�r+r+r,�testDownloadPages�zWebClientTests.testDownloadPagec	Cs.t|d��}|��}W5QRX|�||�dS)N�rb)�openrcry)r*r�rer5r�r�r+r+r,r�sz"WebClientTests._cbDownloadPageTestcCs.Gdd�d�}|�}|�t�|�d�|�t�S)Nc@seZdZdd�Zdd�ZdS)z8WebClientTests.testDownloadPageError1.<locals>.errorfilecSstd��dS)Nzbadness happened during write�r��r*rer+r+r,r?&sz>WebClientTests.testDownloadPageError1.<locals>.errorfile.writecSsdSr&r+rEr+r+r,r�(sz>WebClientTests.testDownloadPageError1.<locals>.errorfile.closeNr�r+r+r+r,�	errorfile%sr�r��r�r
r�r�r��r*r�Zefr+r+r,�testDownloadPageError1$s�z%WebClientTests.testDownloadPageError1cCs.Gdd�d�}|�}|�t�|�d�|�t�S)Nc@seZdZdd�Zdd�ZdS)z8WebClientTests.testDownloadPageError2.<locals>.errorfilecSsdSr&r+r�r+r+r,r?1sz>WebClientTests.testDownloadPageError2.<locals>.errorfile.writecSstd��dS)Nzbadness happened during closer�rEr+r+r,r�3sz>WebClientTests.testDownloadPageError2.<locals>.errorfile.closeNr�r+r+r+r,r�0sr�r�r�r�r+r+r,�testDownloadPageError2/s�z%WebClientTests.testDownloadPageError2cCsDtdd���t�dd�|�t�|�d�d�t�}|�	|j
�|S)N�
unwritable�wbrr�)r�r��os�chmodr�r
r�r�r�ZaddBoth�_cleanupDownloadPageError3r�r+r+r,�testDownloadPageError3:s�z%WebClientTests.testDownloadPageError3cCst�dd�t�d�|S)Nr�i�)r�r��unlink)r*r�r+r+r,r�Es
z)WebClientTests._cleanupDownloadPageError3csVg}dD]>\}}||�}��|tj�}|�|f�fdd�	�|�|�qtj|dd�S)N))Z
nosuchfiles404)r�401)zerror?showlength=1r�cs��|jd|�Srn)ryr0)r�r3rEr+r,r�PrXz.WebClientTests._downloadTest.<locals>.<lambda>T)ZfireOnOneErrback)r�r�Errorr�rKrr�)r*r.Zdlr)r3r�r+rEr,�
_downloadTestJszWebClientTests._downloadTestcs���fdd��S)Ncst���|��Sr&)r
r�r��r)rEr+r,r�UrXz0WebClientTests.testServerError.<locals>.<lambda>�rrEr+rEr,�testServerErrorTszWebClientTests.testServerErrorcs���fdd��S)Ncst���|�|�d�d�S)N�?r)r
r�r��splitrrEr+r,r�XrXz8WebClientTests.testDownloadServerError.<locals>.<lambda>rrEr+rEr,�testDownloadServerErrorWsz&WebClientTests.testDownloadServerErrorcCsF|�d�}tj�|�}t�|�}t�t|j�|j	|�|j
�|j|�S�Nr�)
r�r
�URI�	fromBytesr�rZ
connectTCPrr�r��deferredr��_cbFactoryInfo�r*r)�urir�r+r+r,�testFactoryInfoZs


zWebClientTests.testFactoryInfocCsH|�|jd�|�|j�d��|�|jd�|�|jddd�dS)Nr�sHTTP/r�rVr�10)ryr�Z
assertTrue�version�
startswithr��response_headers)r*Z
ignoredResultr�r+r+r,raszWebClientTests._cbFactoryInfocCs"t�|�d��}|�|jd�|S)zy
        By default, L{client.getPage} follows redirects and returns the content
        of the target resource.
        �redirectr�r�r�r+r+r,�test_followRedirecthsz"WebClientTests.test_followRedirectcCs.|�tj|�d�dd�tj�}|�|j�|S)z�
        If C{followRedirect} is passed a false value, L{client.getPage} does not
        follow redirects and returns a L{Deferred} which fails with
        L{error.PageRedirect} when it encounters one.
        rF��followRedirect)r�r
r�r�r�PageRedirectr��_cbCheckLocationr�r+r+r,�test_noFollowRedirectrs�z$WebClientTests.test_noFollowRedirectcCs|�|jd�dS)Nr�)ry�location)r*r�r+r+r,rszWebClientTests._cbCheckLocationcsD��fdd�}tj��d�tjdd�����jtj�}|�|�|S)��
        When more than C{redirectLimit} HTTP redirects are encountered, the
        page request fails with L{InfiniteRedirection}.
        cs"���jd����jjd�dS)N�
�ryZ_redirectCountr�ro�rp�r�r*r+r,�checkRedirectCount�szCWebClientTests.test_infiniteRedirection.<locals>.checkRedirectCount�infiniteRedirectr)�
redirectLimit)	r
�_makeGetterFactoryr�r�r�r
r�InfiniteRedirectionr��r*r r�r+rr,�test_infiniteRedirection�s�
z'WebClientTests.test_infiniteRedirectioncsHtj|�d�dd��tj|�d�dd�}|�|tj���fdd��}|S)z�
        C{client.HTTPPagerGetter} instances each obey the C{followRedirect}
        value passed to the L{client.getPage} call which created them.
        rTrFcs�Sr&r+)Zdummy�Zd1r+r,r��rXz<WebClientTests.test_isolatedFollowRedirect.<locals>.<lambda>)r
r�r�r�rrr�)r*Zd2r�r+r'r,�test_isolatedFollowRedirect�s
�z*WebClientTests.test_isolatedFollowRedirectcsT��d�}tj|ddd�}��|jd��fdd�}tj|dddd�}|�|�|S)	z�
        Enabling unsafe redirection behaviour overwrites the method of
        redirected C{POST} requests with C{GET}.
        �extendedRedirect?code=302T�POST)rr.z*By default, afterFoundGet must be disabledcs���jjdd�dS�N�GETz6With afterFoundGet, the HTTP method must change to GET�ryr�r-�ZpagerEr+r,�gotPage�s
�z2WebClientTests.test_afterFoundGet.<locals>.gotPage�r�
afterFoundGetr.)r�r
r�ZassertFalser1r�r�)r*r)r�r/r�r+rEr,�test_afterFoundGet�s
��
z!WebClientTests.test_afterFoundGetcs8��d�}�fdd�}tj|ddddd�}|�|�|S)z�
        Passing C{True} for C{afterFoundGet} to L{client.downloadPage} invokes
        the same kind of redirect handling as passing that argument to
        L{client.getPage} invokes.
        r)cs���jjdd�dSr+r-r.rEr+r,r/�s
�z:WebClientTests.test_downloadAfterFoundGet.<locals>.gotPageZdownloadTempTr*r0)r�r
r�r�)r*r)r/r�r+rEr,�test_downloadAfterFoundGet�s
�
z)WebClientTests.test_downloadAfterFoundGetcs6�fdd�}��d�}tj|dddd�}|�|�|S)z�
        When C{afterFoundGet} is C{True}, L{client.getPage} only issues one
        request to the server when following the redirect.  This is a regression
        test, see #4760.
        cs���jjd�dSrr)ryr�rorrEr+r,r �szLWebClientTests.test_afterFoundGetMakesOneRequest.<locals>.checkRedirectCountZafterFoundGetRedirectTr*r0)r�r
r�r�)r*r r)r�r+rEr,�!test_afterFoundGetMakesOneRequest�s
�
z0WebClientTests.test_afterFoundGetMakesOneRequestcCs\d|_tj|�d�|��dd�}tj|�d�|��dd�}t�|�|tj�|�|tj�g�S)a4
        If the timeout indicated by the C{timeout} parameter to
        L{client.HTTPDownloader.__init__} elapses without the complete response
        being received, the L{defer.Deferred} returned by
        L{client.downloadPage} fires with a L{Failure} wrapping a
        L{defer.TimeoutError}.
        r�r�g{�G�z�?r�zwrite-then-wait)	r�r
r�r�r�rr�r�r�)r*�first�secondr+r+r,�test_downloadTimeout�s���z#WebClientTests.test_downloadTimeoutcCs.d|_tj|�d�|��dd�}|�|tj�S)ax
        If the timeout indicated by the C{timeout} parameter to
        L{client.HTTPDownloader.__init__} elapses without the complete response
        being received, the L{defer.Deferred} returned by
        L{client.downloadPage} fires with a L{Failure} wrapping a
        L{defer.TimeoutError}, even if the remote peer isn't reading data from
        the socket.
        rsz
never-readg�������?r�)r�r
r�r�r�r�rr�r�r+r+r,�'test_downloadTimeoutsWorkWithoutReading�s	�z6WebClientTests.test_downloadTimeoutsWorkWithoutReadingcs>�fdd��tj��d�tj���d���j���fdd��S)z�
        After L{client.HTTPDownloader.deferred} fires, the
        L{client.HTTPDownloader} instance's C{status} and C{response_headers}
        attributes are populated with the values from the response.
        csJ��|jd���|jddd���|jddd�t�|j�dS)Nr�scontent-typers	text/htmlrVr)ryr�rr�r�ZfileName)r�rEr+r,�checkHeaderssz9WebClientTests.test_downloadHeaders.<locals>.checkHeadersr�)�
fileOrNamecs���Sr&r+��_)r9r�r+r,r�rXz5WebClientTests.test_downloadHeaders.<locals>.<lambda>)r
r#r��HTTPDownloaderr�r
r�rEr+)r9r�r*r,�test_downloadHeaders	s�z#WebClientTests.test_downloadHeaderscsF����tj��d�tj�ddid�}��fdd�}|j�|�|jS)z�
        The C{cookies} dict passed to the L{client.HTTPDownloader}
        initializer is used to populate the I{Cookie} header included in the
        request sent to the server.
        �cookiemirror�foo�bar)r:r�cs��t����d�dS)Ns[('foo', 'bar')])ryrr�r���outputr*r+r,�
cbFinished's
�z7WebClientTests.test_downloadCookies.<locals>.cbFinished)r�r
r#r�r=r
r�)r*r�rDr+rBr,�test_downloadCookiess�z#WebClientTests.test_downloadCookiescsJ��fdd�}tj��d�tj���dd�����jtj�}|�	|�|S)rcs"���jd����jjd�dS)N�rrrr+r,r 4szEWebClientTests.test_downloadRedirectLimit.<locals>.checkRedirectCountr!rF)r:r")
r
r#r�r=r�r�r
rr$r�r%r+rr,�test_downloadRedirectLimit/s�
z)WebClientTests.test_downloadRedirectLimitcCs<d}t�|�}|�|ddddf|j|j|j|j|jf�dS)zv
        L{client.HTTPClientFactory.setURL} alters the scheme, host, port and
        path for absolute URLs.
        �http://example.com�http�example.com�P�/N)r
r�ryr)�schemer�r�r�)r*r)r�r+r+r,�test_setURLBs
�zWebClientTests.test_setURLcCsFt�d�}d}|�|�|�|ddddf|j|j|j|j|jf�dS)zu
        L{client.HTTPClientFactory.setURL} removes the fragment identifier from
        the path component.
        rHs#https://foo.com:8443/bar;123?a#fragshttpssfoo.comi� s
/bar;123?aN�	r
r��setURLryr)rMr�r�r��r*r�r)r+r+r,�test_setURLRemovesFragmentNs

�z)WebClientTests.test_setURLRemovesFragmentcCsFt�d�}d}|�|�|�|ddddf|j|j|j|j|jf�dS)zW
        L{client.HTTPClientFactory.setURL} alters the path in a relative URL.
        rHs/hellorIrJrKNrOrQr+r+r,�test_setURLRelativePath[s

�z&WebClientTests.test_setURLRelativePathN)1r9r:r;rr�r�r"r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrrrrr&r(r2r3r4r7r8r>rErGrNrRrSr+r+r+r,r�5sV#(






r�c@s$eZdZdd�Zdd�Zdd�ZdS)�WebClientSSLTestscCstjd|t�tt�dd�S)Nrr��ZcontextFactoryr�)r�	listenSSLr�DefaultOpenSSLContextFactory�
serverPEMPathr�r+r+r,r�is��zWebClientSSLTests._listencCstd|j|f�S�Nzhttps://127.0.0.1:%d/%s�rr��r*r�r+r+r,r�pszWebClientSSLTests.getURLcCsL|�d�}tj�|�}t�|�}t�t|j�|j	|t
���|j�
|j|�Sr)r�r
rr	r�rZ
connectSSLrr�r�rZClientContextFactoryr
r�rrr+r+r,rss

�z!WebClientSSLTests.testFactoryInfoN)r9r:r;r�r�rr+r+r+r,rThsrTc@sBeZdZejed�gZdd�Zdd�Zdd�Zdd	�Z	d
d�Z
dS)
�,WebClientRedirectBetweenSSLandPlainTextTestsr}cCstd|j|f�SrY)r�	tlsPortnor[r+r+r,�getHTTPS�sz5WebClientRedirectBetweenSSLandPlainTextTests.getHTTPScCstd|j|f�S�Nzhttp://127.0.0.1:%d/%s)r�plainPortnor[r+r+r,�getHTTP�sz4WebClientRedirectBetweenSSLandPlainTextTests.getHTTPcCs�tdd�}tdd�}tj|dd�}tj|dd�}tjd|t�tt�dd�|_tj	d|dd�|_
|j
��j|_
|j��j|_|�d	t|�d
���|�dt|�d���|�d
t|�d���|�dtdd��dS)Nsnot me�
text/plains
me neitherr�rr�rUr�soneZtwostwoZthreesthreeZfoursfour�	FOUND IT!)rrr�rrVrrWrX�tlsPortr��	plainPortr�r�r`r]r�rr^ra)r*Z	plainRootZtlsRootZ	plainSiteZtlsSiter+r+r,r��s(

��z2WebClientRedirectBetweenSSLandPlainTextTests.setUpcCs&tttj|jj|jjg��}t�|�Sr&)rI�maprZ
maybeDeferredrer�rdr�)r*Zdsr+r+r,r��s��z5WebClientRedirectBetweenSSLandPlainTextTests.tearDowncCst�|�d���|jd�S)NZonerc�r
r�rar�ryrEr+r+r,�testHoppingAround�s�z>WebClientRedirectBetweenSSLandPlainTextTests.testHoppingAroundN)r9r:r;rr�r�r^rar�r�rhr+r+r+r,r\~sr\c@sbeZdZejed�gZdd�Zdd�Zdd�Zdd	�Z	d
d�Z
dd
�Zdd�Zdd�Z
dd�ZdS)�CookieTestsr}cCstjd|dd�Sr�r�r�r+r+r,r��szCookieTests._listencCsRtdd�}|�dt��|�dt��tj|dd�}|�|�|_|j��j|_	dS)NsEl toro!rbr�srawcookiemirrorr�)
rr�rGrRrr�r�r�r�r�)r*�rootr�r+r+r,r��s
zCookieTests.setUpcCs
|j��Sr&)r�r�rEr+r+r,r��szCookieTests.tearDowncCstd|j|f�Sr_rZr[r+r+r,ra�szCookieTests.getHTTPcCst�|�d���|jd�S)Nr?s[]rgrEr+r+r,�
testNoCookies�s�zCookieTests.testNoCookiescCs(ddd�}tj|�d�|d��|jd�S)NrAr��r@rr?�r�s![('baz', 'quux'), ('foo', 'bar')]rg�r*r�r+r+r,�testSomeCookies�s

�zCookieTests.testSomeCookiescCst�|�d���|jd�S)N�rawcookiemirrorrSrgrEr+r+r,�testRawNoCookies�s�zCookieTests.testRawNoCookiescCs(ddd�}tj|�d�|d��|jd�S)NrAr�rlrprm)s'foo=bar; baz=quux's'baz=quux; foo=bar')r
r�rar�r�rnr+r+r,�testRawSomeCookies�s

�zCookieTests.testRawSomeCookiescCsft�d�}|�d�}t�}|�|�dD]}|�|d�q(|�|��d�|�|jdddd	��dS)
N�http://foo.example.com/�127.42.42.42)
s200 OksSquash: yess
Hands: stolensUSet-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMTs4Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/s%Set-Cookie: SHIPPING=FEDEX; path=/foosSet-Cookie: HttpOnly;SecurerXsbodys	more body�
sIGET / HTTP/1.0
Host: foo.example.com
User-Agent: Twisted PageGetter

s
WILE_E_COYOTEsROCKET_LAUNCHER_0001sFEDEX)sCUSTOMERsPART_NUMBERsSHIPPING)	r
r��
buildProtocolrr�ZdataReceivedryr�r�)r*r�r�rF�liner+r+r,�testCookieHeaderParsing�s



���z#CookieTests.testCookieHeaderParsingN)r9r:r;rr�r�r�r�r�rarkrorqrrrxr+r+r+r,ri�sric@sVeZdZdZejed�gZdd�Zdd�Zdd�Z	d	d
�Z
dd�Zd
d�Zdd�Z
dS)�HostHeaderTestsz\
    Test that L{HTTPClientFactory} includes the port in the host header
    if needed.
    r}c	Cs\|�d�D]L}z2|�dd�\}}|����dkr>|��WSWq
tk
rTYq
Xq
dS)zq
        Retrieve the value of the I{Host} header from the serialized
        request given by C{bytes}.
        ru�:rsr]N)r�strip�lower�
ValueError)r*r�rwr5r�r+r+r,�_getHost�szHostHeaderTests._getHostcCs<t�d�}|�d�}|�t��|�|�|j���d�dS)zq
        No port should be included in the host header when connecting to the
        default HTTP port.
        rss127.42.42.42�foo.example.comN�	r
r�rvr�rryr~rFr��r*r�r�r+r+r,�test_HTTPDefaultPorts

�z$HostHeaderTests.test_HTTPDefaultPortcCs<t�d�}|�d�}|�t��|�|�|j���d�dS)z�
        No port should be included in the host header when connecting to the
        default HTTP port even if it is in the URL.
        shttp://foo.example.com:80/rtrNr�r�r+r+r,�test_HTTPPort80s

�zHostHeaderTests.test_HTTPPort80cCs<t�d�}|�d�}|�t��|�|�|j���d�dS)zx
        The port should be included in the host header when connecting to the
        a non default HTTP port.
        �http://foo.example.com:8080/rt�foo.example.com:8080Nr�r�r+r+r,�test_HTTPNotPort80$s

�z"HostHeaderTests.test_HTTPNotPort80cCs<t�d�}|�d�}|�t��|�|�|j���d�dS)zr
        No port should be included in the host header when connecting to the
        default HTTPS port.
        shttps://foo.example.com/rtrNr�r�r+r+r,�test_HTTPSDefaultPort0s

�z%HostHeaderTests.test_HTTPSDefaultPortcCs<t�d�}|�d�}|�t��|�|�|j���d�dS)z�
        No port should be included in the host header when connecting to the
        default HTTPS port even if it is in the URL.
        shttps://foo.example.com:443/rtrNr�r�r+r+r,�test_HTTPSPort443<s

�z!HostHeaderTests.test_HTTPSPort443cCs<t�d�}|�d�}|�t��|�|�|j���d�dS)zy
        The port should be included in the host header when connecting to the
        a non default HTTPS port.
        r�rtr�Nr�r�r+r+r,�test_HTTPSNotPort443Hs

�z$HostHeaderTests.test_HTTPSNotPort443N)r9r:r;r<rr�r�r~r�r�r�r�r�r�r+r+r+r,ry�sryrWzOpenSSL not presentzReactor doesn't support SSLc@s�eZdZdZdd�Zd$dd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#S)%�URITestsa
    Abstract tests for L{twisted.web.client.URI}.

    Subclass this and L{unittest.TestCase}. Then provide a value for
    C{host} and C{uriHost}.

    @ivar host: A host specification for use in tests, must be L{bytes}.

    @ivar uriHost: The host specification in URI form, must be a L{bytes}. In
        most cases this is identical with C{host}. IPv6 address literals are an
        exception, according to RFC 3986 section 3.2.2, as they need to be
        enclosed in brackets. In this case this variable is different.
    cCsB|�|jt�|�|jt�|�|t�|�d|�|�d|j�S)a�
        Replace the string "HOST" in C{template} with this test's host.

        Byte strings Python between (and including) versions 3.0 and 3.4
        cannot be formatted using C{%} or C{format} so this does a simple
        replace.

        @type template: L{bytes}
        @param template: A string containing "HOST".

        @rtype: L{bytes}
        @return: A string where "HOST" has been replaced by C{self.host}.
        sHOST)r�r�r��uriHostr��replace)r*�templater+r+r,�
makeURIStringms
zURITests.makeURIStringrXc

Cs@|�||||||||	f|j|j|j|j|j|j|j|jf�dS)aU
        Assert that all of a L{client.URI}'s components match the expected
        values.

        @param uri: U{client.URI} instance whose attributes will be checked
            for equality.

        @type scheme: L{bytes}
        @param scheme: URI scheme specifier.

        @type netloc: L{bytes}
        @param netloc: Network location component.

        @type host: L{bytes}
        @param host: Host name.

        @type port: L{int}
        @param port: Port number.

        @type path: L{bytes}
        @param path: Hierarchical path.

        @type params: L{bytes}
        @param params: Parameters for last path segment, defaults to C{b''}.

        @type query: L{bytes}
        @param query: Query string, defaults to C{b''}.

        @type fragment: L{bytes}
        @param fragment: Fragment identifier, defaults to C{b''}.
        N)	ryrM�netlocr�r�r��params�query�fragment)
r*r
rMr�r�r�r�r�r�r�r+r+r,�assertURIEquals�s!��zURITests.assertURIEqualscCsdtj�|�d��}|�d|j�tj�|�d��}|�d|j�tj�|�d��}|�d|j�dS)z�
        L{client.URI.fromBytes} by default assumes port 80 for the I{http}
        scheme and 443 for the I{https} scheme.
        �http://HOSTrKshttp://HOST:�https://HOSTi�N�r
rr	r�ryr��r*r
r+r+r,�test_parseDefaultPort�szURITests.test_parseDefaultPortcCsLtjj|�d�dd�}|�d|j�tjj|�d�dd�}|�d|j�dS)z�
        L{client.URI.fromBytes} accepts a C{defaultPort} parameter that
        overrides the normal default port logic.
        r��)ZdefaultPortr�Nr�r�r+r+r,�test_parseCustomDefaultPort�s��z$URITests.test_parseCustomDefaultPortcCsjtj�|�d��}|�d|j�|�|j|j�|�|jd|j�tj�|�d��}|�|j|j�dS)zj
        Parsing a I{URI} splits the network location component into I{host} and
        I{port}.
        shttp://HOST:5144r�s:5144shttp://HOST N)	r
rr	r�ryr�r�r�r�r�r+r+r,�test_netlocHostPort�s�zURITests.test_netlocHostPortcCsD|�d�}tj�|�}|j|d|j|jddd�|�||���dS)z/
        Parse the path from a I{URI}.
        shttp://HOST/foo/barrIrK�/foo/bar�rMr�r�r�r�N�	r�r
rr	r�r�r�ry�toBytes�r*r
Zparsedr+r+r,�	test_path�s
�zURITests.test_pathcCsD|�d�}tj�|�}|j|d|j|jddd�|�||���dS)zL
        The path of a I{URI} that has no path is the empty string.
        r�rIrKrXr�Nr�r�r+r+r,�test_noPath�s
�zURITests.test_noPathcCs0|�d�}|jtj�|�d|j|jddd�dS)zE
        The path of a I{URI} with an empty path is C{b'/'}.
        �http://HOST/rIrKrLr�N)r�r�r
rr	r�r�r�r+r+r,�test_emptyPath�s

�zURITests.test_emptyPathc	CsF|�d�}tj�|�}|j|d|j|jdddd�|�||���dS)z8
        Parse I{URI} parameters from a I{URI}.
        shttp://HOST/foo/bar;paramrIrKr��param)rMr�r�r�r�r�Nr�r�r+r+r,�
test_params
�zURITests.test_paramc
CsH|�d�}tj�|�}|j|d|j|jddddd�|�||���dS)	z7
        Parse the query string from a I{URI}.
        s!http://HOST/foo/bar;param?a=1&b=2rIrKr�r��a=1&b=2)rMr�r�r�r�r�r�Nr�r�r+r+r,�
test_querys
�	zURITests.test_querycCsJ|�d�}tj�|�}|j|d|j|jdddddd�	|�||���d	S)
z>
        Parse the fragment identifier from a I{URI}.
        s&http://HOST/foo/bar;param?a=1&b=2#fragrIrKr�r�r�sfrag)rMr�r�r�r�r�r�r�Nr�r�r+r+r,�
test_fragment$s
�
zURITests.test_fragmentcCs$tj�|�d��}|�d|j�dS)zn
        L{client.URI.originForm} produces an absolute I{URI} path including
        the I{URI} path.
        shttp://HOST/foos/fooN�r
rr	r�ryZ
originFormr�r+r+r,�test_originForm7s�zURITests.test_originFormcCs$tj�|�d��}|�d|j�dS)z�
        L{client.URI.originForm} produces an absolute I{URI} path including
        the I{URI} path, parameters and query string but excludes the fragment
        identifier.
        shttp://HOST/foo;param?a=1#frags/foo;param?a=1Nr�r�r+r+r,�test_originFormComplexAs�zURITests.test_originFormComplexcCs$tj�|�d��}|�d|j�dS)zp
        L{client.URI.originForm} produces a path of C{b'/'} when the I{URI}
        specifies no path.
        r�rLNr�r�r+r+r,�test_originFormNoPathLszURITests.test_originFormNoPathcCs$tj�|�d��}|�d|j�dS)zv
        L{client.URI.originForm} produces a path of C{b'/'} when the I{URI}
        specifies an empty path.
        r�rLNr�r�r+r+r,�test_originFormEmptyPathUs�z!URITests.test_originFormEmptyPathcCsV|�d�}|�d�}t|�tj�|�}|�|jt�|�|j	t�|�|j
t�dS)z�
        L{client.URI.fromBytes} parses the scheme, host, and path elements
        into L{bytes}, even when passed an URL which has previously been passed
        to L{urlparse} as a L{unicode} string.
        shttp://HOST/path�asciiN)r��decoderr
rr	r�rMr�r�r�)r*Z	goodInputZbadInputr
r+r+r,� test_externalUnicodeInterference_s

z)URITests.test_externalUnicodeInterferenceN)rXrXrX)r9r:r;r<r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r+r+r+r,r�^s(�
'

	
r�c@seZdZdZdZZdS)�URITestsForHostnamez>
    Tests for L{twisted.web.client.URI} with host names.
    rJN�r9r:r;r<r�r�r+r+r+r,r�osr�c@seZdZdZdZZdS)�URITestsForIPv4zG
    Tests for L{twisted.web.client.URI} with IPv4 host addresses.
    s192.168.1.67Nr�r+r+r+r,r�xsr�c@s eZdZdZdZdZdd�ZdS)�URITestsForIPv6z�
    Tests for L{twisted.web.client.URI} with IPv6 host addresses.

    IPv6 addresses must always be surrounded by square braces in URIs. No
    attempt is made to test without.
    sfe80::20c:29ff:fea4:c60s[fe80::20c:29ff:fea4:c60]cCs<tj�d�}|�|jd�|�|jd�|�|��d�dS)z�
        Brackets around IPv6 addresses are stripped in the host field. The host
        field is then exported with brackets in the output of
        L{client.URI.toBytes}.
        shttp://[::1]:80/index.htmls::1s[::1]:80N)r
rr	ryr�r�r�r�r+r+r,�"test_hostBracketIPv6AddressLiteral�sz2URITestsForIPv6.test_hostBracketIPv6AddressLiteralN)r9r:r;r<r�r�r�r+r+r+r,r��sr�c@sHeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dS)�DeprecationTestszB
    Tests that L{client.getPage} and friends are deprecated.
    cCs�tjdt�tdd��dd�}|��j}|�|j�t	d|f�}t
�|�}|�|j
g�}|�t|�d�|�|ddt�|�|dd	d
�|�dd��S)
z2
        L{client.getPage} is deprecated.
        rrXrbr�r��http://127.0.0.1:%drsr~r�z�twisted.web.client.getPage was deprecated in Twisted 16.7.0; please use https://pypi.org/project/treq/ or twisted.web.client.Agent insteadcSsdSr&r+r;r+r+r,r��rXz9DeprecationTests.test_getPageDeprecated.<locals>.<lambda>)rr�rr�rr�r�r�r�rr
r��
flushWarnings�test_getPageDeprecatedryrdr��
addErrback)r*r�r�r)r��warningInfor+r+r,r��s"�


�z'DeprecationTests.test_getPageDeprecatedcCs�tjdt�tdd��dd�}|��j}|�|j�t	d|f�}t
|���}t�
||j�}|�|jg�}|�t|�d�|�|ddt�|�|dd	d
�|�dd��S)
z7
        L{client.downloadPage} is deprecated.
        rrXrbr�r�r�rsr~r�z�twisted.web.client.downloadPage was deprecated in Twisted 16.7.0; please use https://pypi.org/project/treq/ or twisted.web.client.Agent insteadcSsdSr&r+r;r+r+r,r��rXz>DeprecationTests.test_downloadPageDeprecated.<locals>.<lambda>)rr�rr�rr�r�r�r�rrr�r
r�r�r��test_downloadPageDeprecatedryrdr�r�)r*r�r�r)r�r�r�r+r+r,r��s$�

�z,DeprecationTests.test_downloadPageDeprecatedcCsTtt|�|��}|�t|�d�|�|ddt�|�|ddd�|��dS)z�
        Assert that accessing the given class was deprecated.

        @param klass: The class being deprecated.
        @type klass: L{str}
        rsrr~r�z�twisted.web.client.{} was deprecated in Twisted 16.7.0: please use https://pypi.org/project/treq/ or twisted.web.client.Agent insteadN)�getattrr
r�ryrdr��format)r*�klassr�r+r+r,�_testDeprecatedClass�s

��z%DeprecationTests._testDeprecatedClasscCs|�d�dS)z9
        L{client.HTTPPageGetter} is deprecated.
        r�N�r�rEr+r+r,�test_httpPageGetterDeprecated�sz.DeprecationTests.test_httpPageGetterDeprecatedcCs|�d�dS)z=
        L{client.HTTPPageDownloader} is deprecated.
        �HTTPPageDownloaderNr�rEr+r+r,�!test_httpPageDownloaderDeprecated�sz2DeprecationTests.test_httpPageDownloaderDeprecatedcCs|�d�dS)z<
        L{client.HTTPClientFactory} is deprecated.
        r�Nr�rEr+r+r,� test_httpClientFactoryDeprecated�sz1DeprecationTests.test_httpClientFactoryDeprecatedcCs|�d�dS)z9
        L{client.HTTPDownloader} is deprecated.
        r=Nr�rEr+r+r,�test_httpDownloaderDeprecated�sz.DeprecationTests.test_httpDownloaderDeprecatedN)r9r:r;r<r�r�r�r�r�r�r�r+r+r+r,r��sr�c@seZdZdZdd�ZdS)�GetPageMethodInjectionTests�@
    Test L{client.getPage} against HTTP method injections.
    cCsd}tj||d�dS)�v
        Attempt a request with the provided method.

        @param method: see L{MethodInjectionTestsMixin}
        �http://twisted.invalidr�N�r
r��r*r.r
r+r+r,�!attemptRequestWithMaliciousMethodsz=GetPageMethodInjectionTests.attemptRequestWithMaliciousMethodN�r9r:r;r<r�r+r+r+r,r��sr�c@seZdZdZdd�ZdS)�GetPageURIInjectionTestsz8
    Test L{client.getPage} against URI injections.
    cCst�|�dS)�m
        Attempt a request with the provided URI.

        @param uri: see L{URIInjectionTestsMixin}
        Nr�r�r+r+r,�attemptRequestWithMaliciousURIsz7GetPageURIInjectionTests.attemptRequestWithMaliciousURIN�r9r:r;r<r�r+r+r+r,r�sr�c@seZdZdZdd�ZdS)� DownloadPageMethodInjectionTestsr�cCsd}tj|t��|d�dS)r�r�)r�r.N�r
r��io�BytesIOr�r+r+r,r�*szBDownloadPageMethodInjectionTests.attemptRequestWithMaliciousMethodNr�r+r+r+r,r�"sr�c@seZdZdZdd�ZdS)�DownloadPageURIInjectionTestsz=
    Test L{client.downloadPage} against URI injections.
    cCstj|t��d�dS)r�)r�Nr�r�r+r+r,r�=sz<DownloadPageURIInjectionTests.attemptRequestWithMaliciousURINr�r+r+r+r,r�5sr�cCs>t�|�}||_||_||_d|_d|_i|_d|_i|_	|S)aV
    Make a L{ClientFactory} that can be used with
    L{client.HTTPPageGetter} and its subclasses.

    @param protocolClass: The protocol class
    @type protocolClass: A subclass of L{client.HTTPPageGetter}

    @param method: the HTTP method

    @param host: the host

    @param path: The URI path

    @return: A L{ClientFactory}.
    rIrs
User/Agent)
rZforProtocolr.r�r�rMr�r�r�r�)�
protocolClassr.r�r�r�r+r+r,�makeHTTPPageGetterFactoryGs
r�c@seZdZdZejZdd�ZdS)�"HTTPPageGetterMethodInjectionTestszG
    Test L{client.HTTPPageGetter} against HTTP method injections.
    cCs:t�}t|j|ddd�}|�t�ddd��}|�|�dS)�r
        Attempt a request with the provided method.

        @param method: L{MethodInjectionTestsMixin}
        stwisted.invalidrL�r.r�r��TCPr�rN�rr�r�rvrZIPv4Addressr�)r*r.rFr��getterr+r+r,r�ps��zDHTTPPageGetterMethodInjectionTests.attemptRequestWithMaliciousMethodN)r9r:r;r<r
r�r�r�r+r+r+r,r�gsr�c@seZdZdZejZdd�ZdS)�HTTPPageGetterURIInjectionTestszD
    Test L{client.HTTPPageGetter} against HTTP URI injections.
    cCs:t�}t|jd||d�}|�t�ddd��}|�|�dS)�i
        Attempt a request with the provided URI.

        @param uri: L{URIInjectionTestsMixin}
        r,r�r�r�rNr�)r*r
rFr�r�r+r+r,r��s��z>HTTPPageGetterURIInjectionTests.attemptRequestWithMaliciousURIN)r9r:r;r<r
r�r�r�r+r+r+r,r��sr�c@seZdZdZejZdS)�&HTTPPageDownloaderMethodInjectionTestszK
    Test L{client.HTTPPageDownloader} against HTTP method injections.
    N�r9r:r;r<r
r�r�r+r+r+r,r��sr�c@seZdZdZejZdS)�#HTTPPageDownloaderURIInjectionTestszH
    Test L{client.HTTPPageDownloader} against HTTP URI injections.
    Nr�r+r+r+r,r��sr�c@seZdZdZdd�ZdS)�%HTTPClientFactoryMethodInjectionTestszK
    Tests L{client.HTTPClientFactory} against HTTP method injections.
    cCst�d|�dS)r��https://twisted.invalidN�r
r��r*r.r+r+r,r��szGHTTPClientFactoryMethodInjectionTests.attemptRequestWithMaliciousMethodNr�r+r+r+r,r��sr�c@seZdZdZdd�ZdS)�"HTTPClientFactoryURIInjectionTestszH
    Tests L{client.HTTPClientFactory} against HTTP URI injections.
    cCst�|�dS�r�Nr�r�r+r+r,r��szAHTTPClientFactoryURIInjectionTests.attemptRequestWithMaliciousURINr�r+r+r+r,r��sr�c@seZdZdZdd�ZdS)�(HTTPClientFactorySetURLURIInjectionTestszO
    Tests L{client.HTTPClientFactory.setURL} against HTTP URI injections.
    cCst�d��|�dS�r�r�N)r
r�rPr�r+r+r,r��szGHTTPClientFactorySetURLURIInjectionTests.attemptRequestWithMaliciousURINr�r+r+r+r,r��sr�c@seZdZdZdd�ZdS)�"HTTPDownloaderMethodInjectionTestszH
    Tests L{client.HTTPDownloader} against HTTP method injections.
    cCstjdt��|d�dS)r�r�r�N�r
r=r�r�r�r+r+r,r��s
�zDHTTPDownloaderMethodInjectionTests.attemptRequestWithMaliciousMethodNr�r+r+r+r,r��sr�c@seZdZdZdd�ZdS)�HTTPDownloaderURIInjectionTestszE
    Tests L{client.HTTPDownloader} against HTTP URI injections.
    cCst�|t���dSr�r�r�r+r+r,r�sz>HTTPDownloaderURIInjectionTests.attemptRequestWithMaliciousURINr�r+r+r+r,r�sr�c@seZdZdZdd�ZdS)�%HTTPDownloaderSetURLURIInjectionTestszL
    Tests L{client.HTTPDownloader.setURL} against HTTP URI injections.
    cCst�dt���}|�|�dSr�)r
r=r�r�rP)r*r
Z
downloaderr+r+r,r�s
�zDHTTPDownloaderSetURLURIInjectionTests.attemptRequestWithMaliciousURINr�r+r+r+r,r�sr�)lr<Z
__future__rrr�r��errnorrr�ImportErrorZurllib.parseZtwisted.python.compatrrr	Z
twisted.trialr
rZtwisted.webrr
rrZtwisted.web.staticrZtwisted.web.utilrZtwisted.internetrrrrZtwisted.internet.protocolrZtwisted.python.filepathrZtwisted.protocols.policiesrZtwisted.test.proto_helpersrrrrZtwistedrZtwisted.loggerrrr r!r"Z!twisted.web.test.injectionhelpersr#r$�__file__ZsiblingZ	serverPEMZasBytesModer�rXr'r%r=rDrGrRrUrZr\rarfrjrmrtZTestCaserur|r�rTr\riry�hasattrZcase�skipZIReactorSSLr�r�r�r�r�ZSynchronousTestCaser�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r+r+r+r,�<module>s�

'

).7,L^

		b
�
�
�
� 
�
�
�
�

�
�
�
�
�
�