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_util.cpython-38.pyc
U

s�@gL6�@sFdZddlmZmZddlZddlmZddlmZm	Z	ddl
mZddlm
Z
mZmZddlmZmZdd	lmZdd
lmZddlmZddlmZmZmZdd
lmZmZddl m!Z!ddl m"Z"m#Z#ddl m$Z$m%Z%m&Z&ddl m'Z'm(Z(Gdd�de	�Z)Gdd�de	�Z*Gdd�de	�Z+Gdd�dej,�Z-Gdd�de�Z.dS)z 
Tests for L{twisted.web.util}.
�)�absolute_import�divisionN)�Failure)�SynchronousTestCase�TestCase)�defer)�_PY3�
intToBytes�
networkString)�resource�util)�FlattenerError)�FOUND)�Request)�	TagLoader�
flattenString�tags)�DummyChannel�DummyRequest)�DeferredResource)�_SourceFragmentElement�
_FrameElement)�
_StackElement�FailureElement�
formatFailure)�
redirectTo�_SourceLineElementc@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�RedirectToTestsz"
    Tests for L{redirectTo}.
    cCs^tt�d�}d|_d}t||�|�|jt�|�|j�d�|g�|�|j�d�dg�dS)z�
        L{redirectTo} will set the C{Location} and C{Content-Type} headers on
        its request, and set the response code to C{FOUND}, so the browser will
        be redirected.
        T�GETshttp://target.example.com/4321slocationscontent-typestext/html; charset=utf-8N)	rr�methodr�assertEqual�coderZresponseHeadersZ
getRawHeaders��self�requestZ	targetURL�r%�</usr/lib/python3/dist-packages/twisted/web/test/test_util.py�test_headersAndCode"s

�
�z#RedirectToTests.test_headersAndCodecCs*tt�d�}d|_d}|�tt||�dS)zW
        L{redirectTo} will raise TypeError if unicode object is passed in URL
        Trzhttp://target.example.com/4321N)rrr�assertRaises�	TypeErrorrr"r%r%r&�test_redirectToUnicodeURL4sz)RedirectToTests.test_redirectToUnicodeURLcCs(tdg�}td|�}d}|�||�dS)zq
        Legitimate URLs are fully interpolated in the `redirectTo` response body without transformation
        �shttps://twisted.org/s�
<html>
    <head>
        <meta http-equiv="refresh" content="0;URL=https://twisted.org/">
    </head>
    <body bgcolor="#FFFFFF" text="#000000">
    <a href="https://twisted.org/">click here</a>
    </body>
</html>
N�rrr �r#r$Zhtml�expectedr%r%r&�test_legitimateRedirect=s


z'RedirectToTests.test_legitimateRedirectcCs(tdg�}td|�}d}|�||�dS)zm
        Malicious URLs are HTML-escaped before interpolating them in the `redirectTo` response body
        r+s?https://twisted.org/"><script>alert(document.location)</script>sX
<html>
    <head>
        <meta http-equiv="refresh" content="0;URL=https://twisted.org/&quot;&gt;&lt;script&gt;alert(document.location)&lt;/script&gt;">
    </head>
    <body bgcolor="#FFFFFF" text="#000000">
    <a href="https://twisted.org/&quot;&gt;&lt;script&gt;alert(document.location)&lt;/script&gt;">click here</a>
    </body>
</html>
Nr,r-r%r%r&�test_maliciousRedirectOs
�
z&RedirectToTests.test_maliciousRedirectN)�__name__�
__module__�__qualname__�__doc__r'r*r/r0r%r%r%r&rs
	rc@sheZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�ZdS)�FailureElementTestszn
    Tests for L{FailureElement} and related helpers which can render a
    L{Failure} as an HTML string.
    cCsLdd�}|jjd|_z
|�Wn&tdd�|_|jjd|_YnXdS)zO
        Create a L{Failure} which can be used by the rendering tests.
        cSsd}t|��dS)NzThis is a problem)�	Exception)�messager%r%r&�lineNumberProbeAlsoBrokennsz<FailureElementTests.setUp.<locals>.lineNumberProbeAlsoBroken�T)ZcaptureVars���N)�__code__�co_firstlineno�baser�failure�frames�frame)r#r8r%r%r&�setUpjs
zFailureElementTests.setUpcCsNttt�tjdd�tjdd���dd�}td|�}d}|�|j|�d��|S)	zN
        L{_SourceLineElement} renders a source line and line number.
        �
lineNumber��render�
sourceLine�2z    print 'hello'Nu:<div><span>50</span><span>    print 'hello'</span></div>zutf-8)	rrr�div�spanr�addCallbackr �encode)r#�element�dr.r%r%r&�test_sourceLineElement{s 

��
��z*FailureElementTests.test_sourceLineElementcs�tttjtjdd�tjdd�dd���j�}dddg}td|�}trhd	��fd
d�t	|�D���
d�}nd	��fd
d�t	|�D��}|��j|�|S)z�
        L{_SourceFragmentElement} renders source lines at and around the line
        number indicated by a frame object.
        rBrCrEZsourceLinesu#    message = "This is a problem"u    raise Exception(message)zE# Figure out the line number from which the exception will be raised.N�cs4g|],\}}dddg|dk�j|d|f�qS)�?<div class="snippet%sLine"><span>%d</span><span>%s</span></div>rN�	Highlightr9�        )r=��.0rBrE�r#r%r&�
<listcomp>�s���zBFailureElementTests.test_sourceFragmentElement.<locals>.<listcomp>�utf8cs:g|]2\}}dddg|dk�j|d|�d�f�qS)rOrNrPr9rQrV)r=rJrRrTr%r&rU�s���)
rrrrGrHr@rr�join�	enumeraterJrIr )r#rK�sourcerLZstringToCheckForr%rTr&�test_sourceFragmentElement�s0

���
��
�	z.FailureElementTests.test_sourceFragmentElementcCsFtttjdd��|j�}td|�}|�|jdtt	�
d��d�|S)z�
        The I{filename} renderer of L{_FrameElement} renders the filename
        associated with the frame object used to initialize the
        L{_FrameElement}.
        �filenamerCN�<span>�c�</span>)rrrrHr@rrIr r
�__file__�rstrip�r#rKrLr%r%r&�test_frameElementFilename�s�
�z-FailureElementTests.test_frameElementFilenamecCsFtttjdd��|j�}td|�}|�|jdt|j	d�d�|S)z�
        The I{lineNumber} renderer of L{_FrameElement} renders the line number
        associated with the frame object used to initialize the
        L{_FrameElement}.
        rBrCNr\r9r^)
rrrrHr@rrIr r	r=rar%r%r&�test_frameElementLineNumber�s�
�z/FailureElementTests.test_frameElementLineNumbercCs4tttjdd��|j�}td|�}|�|jd�|S)z�
        The I{function} renderer of L{_FrameElement} renders the line number
        associated with the frame object used to initialize the
        L{_FrameElement}.
        ZfunctionrCNs&<span>lineNumberProbeAlsoBroken</span>)rrrrHr@rrIr rar%r%r&�test_frameElementFunction�s�
�z-FailureElementTests.test_frameElementFunctioncCs\td|j�}|�d�}t��}|d|�}|�|t�|�|j|j�|�|g|j	�
��dS)z�
        The I{source} renderer of L{_FrameElement} renders the source code near
        the source filename/line number associated with the frame object used to
        initialize the L{_FrameElement}.
        NrY)rr@�lookupRenderMethodrrG�assertIsInstancer�assertIdenticalr �loader�load�r#rKZrenderer�tag�resultr%r%r&�test_frameElementSource�s

z+FailureElementTests.test_frameElementSourcecCs�td|jjdd��}|�d�}t��}|d|�}|�|t�|�|dt�|�	|dj
|jjd�|�|dt�|�	|dj
|jjd�|�|dj�
�|dj�
��|�dt|��dS)z�
        The I{frames} renderer of L{_StackElement} renders each stack frame in
        the list of frames used to initialize the L{_StackElement}.
        N�r?rr9)rr>r?rerrGrf�listrrgr@ZassertNotEqualrhrir �lenrjr%r%r&�test_stackElement�s

 z%FailureElementTests.test_stackElementcCs\t|j�}|�d�}t��}|d|�}|�|t�|�|j|jj	�|�
|g|j���dS)z�
        The I{traceback} renderer of L{FailureElement} renders the failure's
        stack frames using L{_StackElement}.
        �	tracebackN)
rr>rerrGrfrrgZstackFramesr?r rhrirjr%r%r&�test_failureElementTracebacks


z0FailureElementTests.test_failureElementTracebackcCsJt|jttjdd���}td|�}tr,d}nd}|�|jd|d�|S)zi
        The I{type} renderer of L{FailureElement} renders the failure's
        exception type.
        �typerCNsbuiltins.Exceptionsexceptions.Exceptionr\r^)	rr>rrrHrrrIr )r#rKrL�excr%r%r&�test_failureElementTypes�

�z+FailureElementTests.test_failureElementTypecCs4t|jttjdd���}td|�}|�|jd�|S)zi
        The I{value} renderer of L{FailureElement} renders the value's exception
        value.
        �valuerCNs<span>This is a problem</span>)rr>rrrHrrIr rar%r%r&�test_failureElementValue+s�
�z,FailureElementTests.test_failureElementValueN)r1r2r3r4rArMrZrbrcrdrmrqrsrvrxr%r%r%r&r5es-r5c@s eZdZdZdd�Zdd�ZdS)�FormatFailureTestsz�
    Tests for L{twisted.web.util.formatFailure} which returns an HTML string
    representing the L{Failure} instance passed to it.
    cCs|�ttt��dS)z}
        If there is an error flattening the L{Failure} instance,
        L{formatFailure} raises L{FlattenerError}.
        N)r(r
r�objectrTr%r%r&�test_flattenerError>sz&FormatFailureTests.test_flattenerErrorcCsvztd��Wntt��}YnX|�|t�trN|�tdd�|D���n|�tdd�|D���|�d|�dS)z�
        The return value of L{formatFailure} is a C{str} instance (not a
        C{unicode} instance) with numeric character references for any non-ASCII
        characters meant to appear in the output.
        zFake bugcss|]}|dkVqdS��Nr%�rSZchr%r%r&�	<genexpr>Ssz7FormatFailureTests.test_returnsBytes.<locals>.<genexpr>css|]}t|�dkVqdSr|)�ordr~r%r%r&rUss&#160;N)	r6rrrf�bytesrZ
assertTrue�allZassertIn)r#rlr%r%r&�test_returnsBytesFsz$FormatFailureTests.test_returnsBytesN)r1r2r3r4r{r�r%r%r%r&ry9sryc@seZdZdd�Zdd�ZdS)�
SDResourcecCs
||_dS�N)�default)r#r�r%r%r&�__init__\szSDResource.__init__cCs"t�|j�}t�|�}|�||�Sr�)r�succeedr�rr�getChildWithDefault)r#�namer$rLrr%r%r&r�`s
zSDResource.getChildWithDefaultN)r1r2r3r�r�r%r%r%r&r�[sr�c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�DeferredResourceTestsz(
    Tests for L{DeferredResource}.
    cCsFt��}d|_t|�}tdddg�}t�||�|�|jddg�dS)Nr9ZfooZbarZbaz)r�ResourceZisLeafr�rZgetChildForRequestr Zpostpath)r#�r�srLr%r%r&�testDeferredResourcelsz*DeferredResourceTests.testDeferredResourcecCsFg}tg�}|j|_t��}tt�|��}|�|�|�||g�dS)z�
        L{DeferredResource} uses the request object's C{render} method to
        render the resource which is the result of the L{Deferred} being
        handled.
        N)	r�appendrDrr�rrr�r )r#Zrenderedr$rl�deferredResourcer%r%r&�test_renderus
z!DeferredResourceTests.test_rendercCshtg�}|��}tt��}tt�|��}|�|�|�|�	|�|�~t
��|�t�}|�|g�dS)z�
        If the L{Deferred} fails, L{DeferredResource} reports the failure via
        C{processingFailed}, and does not cause an unhandled error to be
        logged.
        N)
rZnotifyFinishr�RuntimeErrorrrZfailrDr ZfailureResultOf�gcZcollectZflushLoggedErrors)r#r$rLr>r��errorsr%r%r&�test_renderNoFailure�s


z*DeferredResourceTests.test_renderNoFailureN)r1r2r3r4r�r�r�r%r%r%r&r�gs	r�)/r4Z
__future__rrr�Ztwisted.python.failurerZtwisted.trial.unittestrrZtwisted.internetrZtwisted.python.compatrr	r
Ztwisted.webrrZtwisted.web.errorr
Ztwisted.web.httprZtwisted.web.serverrZtwisted.web.templaterrrZtwisted.web.test.requesthelperrrZtwisted.web.utilrrrrrrrrrr5ryr�r�r�r%r%r%r&�<module>s,HU"