
    tf;                     |   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e
          Z% G d de	          Z& G d de
          Z' G d de
          Z( G d dej)                  Z* G 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                   *    e Zd ZdZd Zd Zd Zd ZdS )RedirectToTestsz"
    Tests for L{redirectTo}.
    c                 l   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	targetURLs      Y/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/web/test/test_util.pytest_headersAndCodez#RedirectToTests.test_headersAndCode&   s     ,..$//5	9g&&&u---#11+>>	
 	
 	
 	#11/BB()	
 	
 	
 	
 	
    c                     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$   s      r(   test_redirectToUnicodeURLz)RedirectToTests.test_redirectToUnicodeURL9   sC     ,..$//4	)ZGDDDDDr*   c                 v    t          dg          }t          d|          }d}|                     ||           dS )zq
        Legitimate URLs are fully interpolated in the `redirectTo` response body without transformation
        r*   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expecteds       r(   test_legitimateRedirectz'RedirectToTests.test_legitimateRedirectB   sH     u%%17;;	 	x(((((r*   c                 v    t          dg          }t          d|          }d}|                     ||           dS )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>
Nr0   r1   s       r(   test_maliciousRedirectz&RedirectToTests.test_maliciousRedirectT   sM     u%%NPW
 
	 	x(((((r*   N)__name__
__module____qualname____doc__r)   r.   r4   r6    r*   r(   r   r   !   s]         
 
 
&E E E) ) )$) ) ) ) )r*   r   c                   4    e Zd ZdZdedefdZd Zd Zd ZdS )	ParentRedirectTestsz!
    Test L{ParentRedirect}.
    requestPathreturnc                 &   t          t                      d          }d|_        |                    d                              d          |_        t                      } |j        |           |j        	                    d          }|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   Location)
r   r   r   lstripsplitprepathr   renderr"   r#   )r%   r>   r&   r   headerslocations         r(   doLocationTestz"ParentRedirectTests.doLocationTestn   s     ,..$//%,,T2288>>!##   )77DD"""
r*   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/rA   s   /?biff=baffNr    rH   r%   s    r(   test_locationRootz%ParentRedirectTests.test_locationRoot   sT    
 	,d.A.A$.G.GHHH,d.A.A..Q.QRRRRRr*   c                     |                      d|                     d                     |                      d|                     d                     dS )zt
        A request for a resource one level down from the URL root produces
        a redirect to the root.
        rJ   s   /foos   /foo?bar=sproiiingNrK   rL   s    r(   test_locationToRootz'ParentRedirectTests.test_locationToRoot   s_    
 	,d.A.A'.J.JKKK!4!45J!K!K	
 	
 	
 	
 	
r*   c                     |                      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=bazNrK   rL   s    r(   test_locationUpOnez&ParentRedirectTests.test_locationUpOne   s    
 	0$2E2Eh2O2OPPP0$2E2Ek2R2RSSS#T%8%89L%M%M	
 	
 	
 	
 	
r*   N)	r7   r8   r9   r:   bytesrH   rM   rO   rQ   r;   r*   r(   r=   r=   i   sp         % E    (S S S
 
 
	
 	
 	
 	
 	
r*   r=   c                   T    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd ZdS )FailureElementTestszn
    Tests for L{FailureElement} and related helpers which can render a
    L{Failure} as an HTML string.
    c                     d }|j         j        dz   | _        	  |             dS # t          $ r0 t	          d          | _        | j        j        d         | _        Y dS w xY w)zO
        Create a L{Failure} which can be used by the rendering tests.
        c                  $    d} t          |           )NzThis is a problem)	Exception)messages    r(   lineNumberProbeAlsoBrokenz<FailureElementTests.setUp.<locals>.lineNumberProbeAlsoBroken   s    )GG$$$r*      T)captureVarsN)__code__co_firstlinenobaseBaseExceptionr   failureframesframe)r%   rY   s     r(   setUpzFailureElementTests.setUp   s    
	% 	% 	%
 .6EI		1%%''''' 	1 	1 	1"t444DL,R0DJJJJ	1s   
% 6AAc           
      4   t          t          t          j        t          j        d          t          j        d                              dd          }t          d|          }d}|                    | j        |                    d                     |S )	zN
        L{_SourceLineElement} renders a source line and line number.
        
lineNumberrE   
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dr3   s       r(   test_sourceLineElementz*FailureElementTests.test_sourceLineElement   s     %,777,9W9W9WXX  
 
 $((O 	 	
d&(@(@AAAr*   c           
      4   t          t          t          j        t          j        d          t          j        d          d                    | j                  }g d}t          d|          }d}t          |          D ]d\  }}d}|d	k    r2||                    dd
g|d	k             | j	        |z   d|z             z  }?||                    d| j	        |z   d|z             z  }e|
                    d          }|                    | j        |           |S )z
        L{_SourceFragmentElement} renders source lines at and around the line
        number indicated by a frame object.
        rf   rg   rh   sourceLines)u#       message = "This is a problem"u       raise Exception(message) Nrs   z?<div class="snippet{}Line"><span>{}</span><span>{}</span></div>rZ   	Highlightu           utf8)r   r   r   rj   rk   rc   r   	enumerateformatr_   rm   rl   r    )	r%   rn   sourcero   stringToCheckForrf   rh   templatebytesToCheckFors	            r(   test_sourceFragmentElementz.FailureElementTests.test_sourceFragmentElement   sT   
 )I\222I\222(    J	
 	

 
 

 $((&/&7&7 	 	"J
XHQ HOO%jAo6I
*.;% %    !HOO	J.j% %    +11&99	d&888r*   c           	         t          t          t          j        d                    | j                  }t          d|          }|                    | j        dt          t          
                    d                    z   dz              |S )z
        The I{filename} renderer of L{_FrameElement} renders the filename
        associated with the frame object used to initialize the
        L{_FrameElement}.
        filenamerg   N   <span>c   </span>)r   r   r   rk   rc   r   rl   r    r   __file__rstripr%   rn   ro   s      r(   test_frameElementFilenamez-FailureElementTests.test_frameElementFilename   s      	$):*F*F*F G GTT$((	 hooc&:&:;;;jH		
 	
 	
 r*   c                     t          t          t          j        d                    | j                  }t          d|          }|                    | j        d| j        dz   fz             |S )z
        The I{lineNumber} renderer of L{_FrameElement} renders the line number
        associated with the frame object used to initialize the
        L{_FrameElement}.
        rf   rg   Ns   <span>%d</span>rZ   )	r   r   r   rk   rc   r   rl   r    r_   r   s      r(   test_frameElementLineNumberz/FailureElementTests.test_frameElementLineNumber  sd      	$)<*H*H*H I I4:VV$((	d&(:di!m=M(MNNNr*   c                     t          t          t          j        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}.
        functionrg   Ns&   <span>lineNumberProbeAlsoBroken</span>)r   r   r   rk   rc   r   rl   r    r   s      r(   test_frameElementFunctionz-FailureElementTests.test_frameElementFunction  sV      	$):*F*F*F G GTT$((	d&(QRRRr*   c                 j   t          d| j                  }|                    d          }t          j                    } |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}.
        Nrx   )r   rc   lookupRenderMethodr   rj   assertIsInstancer   assertIdenticalr    loaderloadr%   rn   renderertagresults        r(   test_frameElementSourcez+FailureElementTests.test_frameElementSource  s      dj11--h77hjj$$$f&<===V\4:666# 2 2 4 455555r*   c                    t          d| j        j        dd                   }|                    d          }t	          j                    } |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   rb   r   rZ   )r   ra   rb   r   r   rj   r   listr   r   rc   assertNotEqualr   r   r    lenr   s        r(   test_stackElementz%FailureElementTests.test_stackElement)  sD   
  dl&9"1"&=>>--h77hjj$$$fd+++fQi777VAY_dl.A!.DEEEfQi777VAY_dl.A!.DEEEF1I,1133VAY5E5J5J5L5LMMMCKK(((((r*   c                 r   t          | j                  }|                    d          }t          j                    } |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   ra   r   r   rj   r   r   r   stackFramesrb   r    r   r   r   s        r(   test_failureElementTracebackz0FailureElementTests.test_failureElementTraceback;  s    
 !..--k::hjj$$$fm444V/1DEEE# 2 2 4 455555r*   c                     t          | j        t          t          j        d                              }t          d|          }d}|                    | j        d|z   dz              |S )zi
        The I{type} renderer of L{FailureElement} renders the failure's
        exception type.
        typerg   Ns   builtins.Exceptionr   r   r   ra   r   r   rk   r   rl   r    )r%   rn   ro   excs       r(   test_failureElementTypez+FailureElementTests.test_failureElementTypeH  sc    
 !y&9Q9Q9Q/R/RSS$((#	d&	C*(DEEEr*   c                     t          | j        t          t          j        d                              }t          d|          }|                    | j        d           |S )zi
        The I{value} renderer of L{FailureElement} renders the value's exception
        value.
        valuerg   Ns   <span>This is a problem</span>r   r   s      r(   test_failureElementValuez,FailureElementTests.test_failureElementValueS  sV    
 !y'9R9R9R/S/STT$((	d&(IJJJr*   N)r7   r8   r9   r:   rd   rp   r|   r   r   r   r   r   r   r   r   r;   r*   r(   rT   rT      s         
1 1 1$  &( ( (T   	 	 		 	 	6 6 6) ) )$6 6 6	 	 	    r*   rT   c                       e Zd ZdZd Z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                 `    |                      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   objectrL   s    r(   test_flattenerErrorz&FormatFailureTests.test_flattenerErrord  s&    
 	.-BBBBBr*   c                 6   	 t          d          # t          $ r t          t                                }Y nw xY w|                     |t
                     |                     t          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              3   "   K   | ]
}|d k     V  dS )   Nr;   ).0chs     r(   	<genexpr>z7FormatFailureTests.test_returnsBytes.<locals>.<genexpr>w  s&      66BH666666r*   s   &#160;N)	rW   r`   r   r   r   rR   
assertTrueallassertIn)r%   r   s     r(   test_returnsBytesz$FormatFailureTests.test_returnsBytesk  s    	.J''' 	. 	. 	."799--FFF	. 	fe,,,66v66666777i(((((s    %99N)r7   r8   r9   r:   r   r   r;   r*   r(   r   r   ^  s?         
C C C) ) ) ) )r*   r   c                       e Zd Zd Zd ZdS )
SDResourcec                     || _         d S N)default)r%   r   s     r(   __init__zSDResource.__init__}  s    r*   c                 ~    t          j        | j                  }t          j        |          } |j        ||          S r   )r   succeedr   r	   r   getChildWithDefault)r%   namer&   ro   r   s        r(   r   zSDResource.getChildWithDefault  s8    M$,''(+++x+D':::r*   N)r7   r8   r9   r   r   r;   r*   r(   r   r   |  s2          ; ; ; ; ;r*   r   c                   $    e Zd ZdZd Zd Zd ZdS )DeferredResourceTestsz(
    Tests for L{DeferredResource}.
    c                     t          j                    }d|_        t          |          }t	          g d          }t          j        ||           |                     |j        ddg           d S )NrZ   )foobarbazr   r   )r   ResourceisLeafr   r   getChildForRequestr    postpath)r%   rsro   s       r(   testDeferredResourcez*DeferredResourceTests.testDeferredResource  sk    qMM...//#Aq)))eU^44444r*   c                     g }t          g           }|j        |_        t          j                    }t          t          j        |                    }|                    |           |                     ||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   appendrE   r   r   r   r   r   r    )r%   renderedr&   r   deferredResources        r(   test_renderz!DeferredResourceTests.test_render  sw     r""!"$$+EM&,A,ABB(((F8,,,,,r*   c                    t          g           }|                                }t          t                                }t	          t          j        |                    }|                    |           |                     | 	                    |          |           ~t          j                     |                     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   failrE   r    failureResultOfgccollectflushLoggedErrors)r%   r&   ro   ra   r   errorss         r(   test_renderNoFailurez*DeferredResourceTests.test_renderNoFailure  s     r""  "",..))+EJw,?,?@@(((--a00':::

''55$$$$$r*   N)r7   r8   r9   r:   r   r   r   r;   r*   r(   r   r     sK         5 5 5- - -% % % % %r*   r   ),r:   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   r=   rT   r   r   r   r   r;   r*   r(   <module>r      sx    
			 " " " " " " / / / / / / * * * * * * @ @ @ @ @ @ @ @ & & & & & & & & , , , , , , " " " " " " & & & & & & ? ? ? ? ? ? ? ? ? ? E E E E E E E E
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
E) E) E) E) E)h E) E) E)P4
 4
 4
 4
 4
- 4
 4
 4
n{ { { { {( { { {|) ) ) ) ) ) ) )<; ; ; ; ;" ; ; ;+% +% +% +% +%/ +% +% +% +% +%r*   