HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/twisted/web/test/__pycache__/test_util.cpython-310.pyc
o

�b�5�@s*dZddlZddlmZddlmZddlmZddlm	Z	m
Z
ddlmZm
Z
ddlmZdd	lmZdd
lmZddlmZmZmZddlmZmZdd
lmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$Gdd�de
�Z%Gdd�de	�Z&Gdd�de
�Z'Gdd�de
�Z(Gdd�dej)�Z*Gdd�de	�Z+dS)z 
Tests for L{twisted.web.util}.
�N)�defer)�
networkString)�Failure)�SynchronousTestCase�TestCase)�resource�util)�FlattenerError)�FOUND)�Request)�	TagLoader�
flattenString�tags)�DummyChannel�DummyRequest)	�DeferredResource�FailureElement�ParentRedirect�
_FrameElement�_SourceFragmentElement�_SourceLineElement�
_StackElement�
formatFailure�
redirectToc@� eZdZdZdd�Zdd�ZdS)�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�coder
�responseHeaders�
getRawHeaders��self�request�	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_redirectToUnicodeURL:sz)RedirectToTests.test_redirectToUnicodeURLN)�__name__�
__module__�__qualname__�__doc__r(r+r&r&r&r'r"src@s6eZdZdZdefdd�Zdd�Zdd�Zd	d
�ZdS)�ParentRedirectTestsz!
    Test L{ParentRedirect}.
    �requestPathcCsFtt�d�}d|_|�d��d�|_t�}|�|�|j�	d�\}|S)z�
        Render a response to a request with path *requestPath*

        @param requestPath: A slash-separated path like C{b'/foo/bar'}.

        @returns: The value of the I{Location} header.
        Tr�/sLocation)
rrr�lstrip�split�prepathr�renderr r!)r#r1r$r�locationr&r&r'�doLocationTestIs
z"ParentRedirectTests.doLocationTestcC�(|�d|�d��|�d|�d��dS)zi
        At the URL root issue a redirect to the current URL, removing any query
        string.
        �http://10.0.0.1/r2s/?biff=baffN�rr8�r#r&r&r'�test_locationRoot[sz%ParentRedirectTests.test_locationRootcCr9)zt
        A request for a resource one level down from the URL root produces
        a redirect to the root.
        r:s/foos/foo?bar=sproiiingNr;r<r&r&r'�test_locationToRootcs
�z'ParentRedirectTests.test_locationToRootcCs:|�d|�d��|�d|�d��|�d|�d��dS)zp
        Requests for resources directly under the path C{/foo/} produce
        redirects to C{/foo/}.
        shttp://10.0.0.1/foo/s/foo/s/foo/bars/foo/bar?biz=bazNr;r<r&r&r'�test_locationUpOnems

�z&ParentRedirectTests.test_locationUpOneN)	r,r-r.r/�bytesr8r=r>r?r&r&r&r'r0Ds
r0c@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.
    cCsRdd�}|jjd|_z|�WdSty(tdd�|_|jjd|_YdSw)zO
        Create a L{Failure} which can be used by the rendering tests.
        cSsd}t|��)NzThis is a problem)�	Exception)�messager&r&r'�lineNumberProbeAlsoBroken�sz<FailureElementTests.setUp.<locals>.lineNumberProbeAlsoBroken�T)�captureVars���N)�__code__�co_firstlineno�base�
BaseExceptionr�failure�frames�frame)r#rDr&r&r'�setUps�zFailureElementTests.setUpcCsNttt�tjdd�tjdd���dd�}td|�}d}|�|j|�d��|S)	zN
        L{_SourceLineElement} renders a source line and line number.
        �
lineNumber�r6�
sourceLine�2z    print 'hello'Nu:<div><span>50</span><span>    print 'hello'</span></div>zutf-8)	rrr�div�spanr
�addCallbackr�encode)r#�element�d�expectedr&r&r'�test_sourceLineElement�s��
�z*FailureElementTests.test_sourceLineElementc	Cs�tttjtjdd�tjdd�dd��|j�}gd�}td|�}d}t|�D]-\}}d}|d	krD||�dd
g|d	k|j	|d|�7}q%||�d|j	|d|�7}q%|�
d�}|�|j|�|S)
z�
        L{_SourceFragmentElement} renders source lines at and around the line
        number indicated by a frame object.
        rPrQrR�sourceLines)u#    message = "This is a problem"u    raise Exception(message)�Nr]z?<div class="snippet{}Line"><span>{}</span><span>{}</span></div>rE�	Highlightu        �utf8)
rrrrTrUrNr
�	enumerate�formatrJrWrVr)	r#rX�sourcerY�stringToCheckForrPrR�template�bytesToCheckForr&r&r'�test_sourceFragmentElement�s6

���
��
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}.
        �filenamerQN�<span>�c�</span>)rrrrUrNr
rVrr�__file__�rstrip�r#rXrYr&r&r'�test_frameElementFilename�s
�z-FailureElementTests.test_frameElementFilenamecCs@tttjdd��|j�}td|�}|�|jd|jdf�|S)z�
        The I{lineNumber} renderer of L{_FrameElement} renders the line number
        associated with the frame object used to initialize the
        L{_FrameElement}.
        rPrQNs<span>%d</span>rE)	rrrrUrNr
rVrrJrmr&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}.
        �functionrQNs&<span>lineNumberProbeAlsoBroken</span>)rrrrUrNr
rVrrmr&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}.
        Nrb)rrN�lookupRenderMethodrrT�assertIsInstancer�assertIdenticalr�loader�load�r#rX�renderer�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�rMrrE)rrLrMrrrrTrs�listrrtrN�assertNotEqualrurvr�lenrwr&r&r'�test_stackElements

 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)
rrLrrrrTrsrrt�stackFramesrMrrurvrwr&r&r'�test_failureElementTracebacks


z0FailureElementTests.test_failureElementTracebackcCs@t|jttjdd���}td|�}d}|�|jd|d�|S)zi
        The I{type} renderer of L{FailureElement} renders the failure's
        exception type.
        �typerQNsbuiltins.Exceptionrhrj�rrLrrrUr
rVr)r#rXrY�excr&r&r'�test_failureElementType!s

z+FailureElementTests.test_failureElementTypecCs4t|jttjdd���}td|�}|�|jd�|S)zi
        The I{value} renderer of L{FailureElement} renders the value's exception
        value.
        �valuerQNs<span>This is a problem</span>r�rmr&r&r'�test_failureElementValue,s
z,FailureElementTests.test_failureElementValueN)r,r-r.r/rOr[rfrnrorqr{r�r�r�r�r&r&r&r'rAys*
rAc@r)�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�objectr<r&r&r'�test_flattenerError=sz&FormatFailureTests.test_flattenerErrorcCsZztd��tytt��}Ynw|�|t�|�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&)�.0�chr&r&r'�	<genexpr>Ps�z7FormatFailureTests.test_returnsBytes.<locals>.<genexpr>s&#160;N)	rBrKrrrsr@�
assertTrue�all�assertIn)r#rzr&r&r'�test_returnsBytesDs�z$FormatFailureTests.test_returnsBytesN)r,r-r.r/r�r�r&r&r&r'r�7sr�c@seZdZdd�Zdd�ZdS)�
SDResourcecCs
||_dS�N)�default)r#r�r&r&r'�__init__Vs
zSDResource.__init__cCs"t�|j�}t�|�}|�||�Sr�)r�succeedr�rr�getChildWithDefault)r#�namer$rYrr&r&r'r�Ys
zSDResource.getChildWithDefaultN)r,r-r.r�r�r&r&r&r'r�Usr�c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�DeferredResourceTestsz(
    Tests for L{DeferredResource}.
    cCsDt��}d|_t|�}tgd��}t�||�|�|jddg�dS)NrE)�foo�bar�bazr�r�)r�Resource�isLeafr�r�getChildForRequestr�postpath)r#�r�srYr&r&r'�testDeferredResourceds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�appendr6rr�rrr�r)r#�renderedr$rz�deferredResourcer&r&r'�test_renderls
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)
r�notifyFinishr�RuntimeErrorrr�failr6r�failureResultOf�gc�collect�flushLoggedErrors)r#r$rYrLr��errorsr&r&r'�test_renderNoFailure{s


z*DeferredResourceTests.test_renderNoFailureN)r,r-r.r/r�r�r�r&r&r&r'r�_s
r�),r/r��twisted.internetr�twisted.python.compatr�twisted.python.failurer�twisted.trial.unittestrr�twisted.webrr�twisted.web.errorr	�twisted.web.httpr
�twisted.web.serverr�twisted.web.templaterr
r�twisted.web.test.requesthelperrr�twisted.web.utilrrrrrrrrrrr0rAr�r�r�r�r&r&r&r'�<module>s(,
"5?