o
    >h;                     @   s*  d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZmZ ddlmZmZ ddlmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ G dd de
Z%G dd de	Z&G dd de
Z'G dd de
Z(G dd dej)Z*G dd de	Z+dS )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                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )RedirectToTestsz"
    Tests for L{redirectTo}.
    c                 C   s^   t t d}d|_d}t|| | |jt | |jd|g | |jddg dS )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   GETs   http://target.example.com/4321s   locations   content-types   text/html; charset=utf-8N)	r   r   methodr   assertEqualcoder
   responseHeadersgetRawHeadersselfrequest	targetURL r%   z/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/web/test/test_util.pytest_headersAndCode&   s   

z#RedirectToTests.test_headersAndCodec                 C   s*   t t d}d|_d}| tt|| dS )zW
        L{redirectTo} will raise TypeError if unicode object is passed in URL
        Tr   zhttp://target.example.com/4321N)r   r   r   assertRaises	TypeErrorr   r!   r%   r%   r&   test_redirectToUnicodeURL9   s   z)RedirectToTests.test_redirectToUnicodeURLc                 C   (   t dg}td|}d}| || dS )zq
        Legitimate URLs are fully interpolated in the `redirectTo` response body without transformation
            s   https://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r   r   r   r"   r#   htmlexpectedr%   r%   r&   test_legitimateRedirectB   s   


z'RedirectToTests.test_legitimateRedirectc                 C   r+   )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_maliciousRedirectT   s   

z&RedirectToTests.test_maliciousRedirectN)__name__
__module____qualname____doc__r'   r*   r1   r2   r%   r%   r%   r&   r   !   s    	r   c                   @   s:   e Zd ZdZdedefddZdd Zdd	 Zd
d ZdS )ParentRedirectTestsz!
    Test L{ParentRedirect}.
    requestPathreturnc                 C   sV   t t d}d|_|dd|_t }|| |j	d}|dus&J |\}|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      /s   LocationN)
r   r   r   lstripsplitprepathr   renderr   r    )r"   r8   r#   r   headerslocationr%   r%   r&   doLocationTestn   s   
z"ParentRedirectTests.doLocationTestc                 C   (   |  d| d |  d| d dS )zi
        At the URL root issue a redirect to the current URL, removing any query
        string.
           http://10.0.0.1/r:   s   /?biff=baffNr   rA   r"   r%   r%   r&   test_locationRoot   s   z%ParentRedirectTests.test_locationRootc                 C   rB   )zt
        A request for a resource one level down from the URL root produces
        a redirect to the root.
        rC   s   /foos   /foo?bar=sproiiingNrD   rE   r%   r%   r&   test_locationToRoot   s   
z'ParentRedirectTests.test_locationToRootc                 C   s:   |  d| d |  d| d |  d| d dS )zp
        Requests for resources directly under the path C{/foo/} produce
        redirects to C{/foo/}.
        s   http://10.0.0.1/foo/s   /foo/s   /foo/bars   /foo/bar?biz=bazNrD   rE   r%   r%   r&   test_locationUpOne   s
   
z&ParentRedirectTests.test_locationUpOneN)	r3   r4   r5   r6   bytesrA   rF   rG   rH   r%   r%   r%   r&   r7   i   s    
r7   c                   @   sh   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )FailureElementTestszn
    Tests for L{FailureElement} and related helpers which can render a
    L{Failure} as an HTML string.
    c                 C   sR   dd }|j jd | _z|  W dS  ty(   tdd| _| jjd | _Y dS w )zO
        Create a L{Failure} which can be used by the rendering tests.
        c                  S   s   d} t | )NzThis is a problem)	Exception)messager%   r%   r&   lineNumberProbeAlsoBroken   s   z<FailureElementTests.setUp.<locals>.lineNumberProbeAlsoBroken   T)captureVarsN)__code__co_firstlinenobaseBaseExceptionr   failureframesframe)r"   rM   r%   r%   r&   setUp   s   zFailureElementTests.setUpc                 C   sN   t tttjddtjdddd}td|}d}|| j|d |S )	zN
        L{_SourceLineElement} renders a source line and line number.
        
lineNumberr>   
sourceLine2   z    print 'hello'Nu:   <div><span>50</span><span>    print 'hello'</span></div>zutf-8)	r   r   r   divspanr   addCallbackr   encode)r"   elementdr0   r%   r%   r&   test_sourceLineElement   s   
z*FailureElementTests.test_sourceLineElementc           	      C   s   t ttjtjddtjdddd| j}g d}td|}d}t|D ]-\}}d}|d	krD||dd
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.
        rY   rZ   r[   sourceLines)u#       message = "This is a problem"u       raise Exception(message) Nre   z?<div class="snippet{}Line"><span>{}</span><span>{}</span></div>rN   	Highlightu           utf8)r   r   r   r]   r^   rW   r   	enumerateformatrS   r`   r_   r   )	r"   ra   sourcerb   stringToCheckForrY   r[   templatebytesToCheckForr%   r%   r&   test_sourceFragmentElement   s6   



z.FailureElementTests.test_sourceFragmentElementc                 C   sF   t ttjdd| j}td|}|| jdtt	
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}.
        filenamerZ   N   <span>c   </span>)r   r   r   r^   rW   r   r_   r   r   __file__rstripr"   ra   rb   r%   r%   r&   test_frameElementFilename   s   
z-FailureElementTests.test_frameElementFilenamec                 C   s@   t ttjdd| j}td|}|| jd| jd f  |S )z
        The I{lineNumber} renderer of L{_FrameElement} renders the line number
        associated with the frame object used to initialize the
        L{_FrameElement}.
        rY   rZ   Ns   <span>%d</span>rN   )	r   r   r   r^   rW   r   r_   r   rS   ru   r%   r%   r&   test_frameElementLineNumber  s   
z/FailureElementTests.test_frameElementLineNumberc                 C   s4   t ttjdd| j}td|}|| jd |S )z
        The I{function} renderer of L{_FrameElement} renders the line number
        associated with the frame object used to initialize the
        L{_FrameElement}.
        functionrZ   Ns&   <span>lineNumberProbeAlsoBroken</span>)r   r   r   r^   rW   r   r_   r   ru   r%   r%   r&   test_frameElementFunction  s   
z-FailureElementTests.test_frameElementFunctionc                 C   s\   t d| j}|d}t }|d|}| |t | |j| j | |g|j	
  dS )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}.
        Nrj   )r   rW   lookupRenderMethodr   r]   assertIsInstancer   assertIdenticalr   loaderloadr"   ra   renderertagresultr%   r%   r&   test_frameElementSource  s   

z+FailureElementTests.test_frameElementSourcec                 C   s   t d| jjdd }|d}t }|d|}| |t | |d t | 	|d j
| jjd  | |d t | 	|d j
| jjd  | |d j |d j  | dt| dS )z
        The I{frames} renderer of L{_StackElement} renders each stack frame in
        the list of frames used to initialize the L{_StackElement}.
        N   rV   r   rN   )r   rU   rV   rz   r   r]   r{   listr   r|   rW   assertNotEqualr}   r~   r   lenr   r%   r%   r&   test_stackElement)  s   

 z%FailureElementTests.test_stackElementc                 C   s\   t | j}|d}t }|d|}| |t | |j| jj	 | 
|g|j  dS )z
        The I{traceback} renderer of L{FailureElement} renders the failure's
        stack frames using L{_StackElement}.
        	tracebackN)r   rU   rz   r   r]   r{   r   r|   stackFramesrV   r   r}   r~   r   r%   r%   r&   test_failureElementTraceback;  s   


z0FailureElementTests.test_failureElementTracebackc                 C   s@   t | jttjdd}td|}d}|| jd| d  |S )zi
        The I{type} renderer of L{FailureElement} renders the failure's
        exception type.
        typerZ   Ns   builtins.Exceptionrp   rr   r   rU   r   r   r^   r   r_   r   )r"   ra   rb   excr%   r%   r&   test_failureElementTypeH  s
   
z+FailureElementTests.test_failureElementTypec                 C   s4   t | jttjdd}td|}|| jd |S )zi
        The I{value} renderer of L{FailureElement} renders the value's exception
        value.
        valuerZ   Ns   <span>This is a problem</span>r   ru   r%   r%   r&   test_failureElementValueS  s   
z,FailureElementTests.test_failureElementValueN)r3   r4   r5   r6   rX   rc   rn   rv   rw   ry   r   r   r   r   r   r%   r%   r%   r&   rJ      s    *rJ   c                   @   s    e Zd ZdZdd Zdd ZdS )FormatFailureTestsz
    Tests for L{twisted.web.util.formatFailure} which returns an HTML string
    representing the L{Failure} instance passed to it.
    c                 C   s   |  ttt  dS )z}
        If there is an error flattening the L{Failure} instance,
        L{formatFailure} raises L{FlattenerError}.
        N)r(   r	   r   objectrE   r%   r%   r&   test_flattenerErrord  s   z&FormatFailureTests.test_flattenerErrorc                 C   sZ   zt d ty   tt }Y nw | |t | tdd |D  | d| dS )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.
        zFake bugc                 s   s    | ]}|d k V  qdS )   Nr%   ).0chr%   r%   r&   	<genexpr>w  s    z7FormatFailureTests.test_returnsBytes.<locals>.<genexpr>s   &#160;N)	rK   rT   r   r   r{   rI   
assertTrueallassertIn)r"   r   r%   r%   r&   test_returnsBytesk  s   z$FormatFailureTests.test_returnsBytesN)r3   r4   r5   r6   r   r   r%   r%   r%   r&   r   ^  s    r   c                   @   s   e Zd Zdd Zdd ZdS )
SDResourcec                 C   s
   || _ d S N)default)r"   r   r%   r%   r&   __init__}  s   
zSDResource.__init__c                 C   s"   t | j}t|}|||S r   )r   succeedr   r   r   getChildWithDefault)r"   namer#   rb   r   r%   r%   r&   r     s   
zSDResource.getChildWithDefaultN)r3   r4   r5   r   r   r%   r%   r%   r&   r   |  s    r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	DeferredResourceTestsz(
    Tests for L{DeferredResource}.
    c                 C   sD   t  }d|_t|}tg d}t || | |jddg d S )NrN   )foobarbazr   r   )r   ResourceisLeafr   r   getChildForRequestr   postpath)r"   rsrb   r%   r%   r&   testDeferredResource  s   z*DeferredResourceTests.testDeferredResourcec                 C   sF   g }t g }|j|_t }tt|}|| | ||g dS )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   appendr>   r   r   r   r   r   r   )r"   renderedr#   r   deferredResourcer%   r%   r&   test_render  s   
z!DeferredResourceTests.test_renderc                 C   sh   t g }| }tt }tt|}|| | | 	|| ~t
  | t}| |g  dS )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   RuntimeErrorr   r   failr>   r   failureResultOfgccollectflushLoggedErrors)r"   r#   rb   rU   r   errorsr%   r%   r&   test_renderNoFailure  s   


z*DeferredResourceTests.test_renderNoFailureN)r3   r4   r5   r6   r   r   r   r%   r%   r%   r&   r     s
    r   ),r6   r   twisted.internetr   twisted.python.compatr   twisted.python.failurer   twisted.trial.unittestr   r   twisted.webr   r   twisted.web.errorr	   twisted.web.httpr
   twisted.web.serverr   twisted.web.templater   r   r   twisted.web.test.requesthelperr   r   twisted.web.utilr   r   r   r   r   r   r   r   r   r   r7   rJ   r   r   r   r   r%   r%   r%   r&   <module>   s(   ,H7 ?
