
    /Jf5                     |   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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      ]/home/alex/cs2snipeproduction/venv/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_redirectToUnicodeURL:   sC     ,..$//4	)ZGDDDDDr*   N)__name__
__module____qualname____doc__r)   r.    r*   r(   r   r   "   sA         
 
 
&E E 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%   r6   r&   r   headerslocations         r(   doLocationTestz"ParentRedirectTests.doLocationTestI   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/r9   s   /?biff=baffNr    r@   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.
        rB   s   /foos   /foo?bar=sproiiingNrC   rD   s    r(   test_locationToRootz'ParentRedirectTests.test_locationToRoote   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=bazNrC   rD   s    r(   test_locationUpOnez&ParentRedirectTests.test_locationUpOneo   s    
 	0$2E2Eh2O2OPPP0$2E2Ek2R2RSSS#T%8%89L%M%M	
 	
 	
 	
 	
r*   N)	r/   r0   r1   r2   bytesr@   rE   rG   rI   r3   r*   r(   r5   r5   D   sp         % E    (S S S
 
 
	
 	
 	
 	
 	
r*   r5   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%   rQ   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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dexpecteds       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.
        r^   r_   r`   sourceLines)u#       message = "This is a problem"u       raise Exception(message) Nrl   z?<div class="snippet{}Line"><span>{}</span><span>{}</span></div>rR   	Highlightu           utf8)r   r   r   rb   rc   r[   r   	enumerateformatrW   re   rd   r    )	r%   rf   sourcerg   stringToCheckForr^   r`   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}.
        filenamer_   N   <span>c   </span>)r   r   r   rc   r[   r   rd   r    r   __file__rstripr%   rf   rg   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}.
        r^   r_   Ns   <span>%d</span>rR   )	r   r   r   rc   r[   r   rd   r    rW   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}.
        functionr_   Ns&   <span>lineNumberProbeAlsoBroken</span>)r   r   r   rc   r[   r   rd   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}.
        Nrq   )r   r[   lookupRenderMethodr   rb   assertIsInstancer   assertIdenticalr    loaderloadr%   rf   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   rZ   r   rR   )r   rY   rZ   r   r   rb   r   listr   r   r[   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   rY   r   r   rb   r   r   r   stackFramesrZ   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.
        typer_   Ns   builtins.Exceptionrx   rz   r   rY   r   r   rc   r   rd   r    )r%   rf   rg   excs       r(   test_failureElementTypez+FailureElementTests.test_failureElementType#  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.
        valuer_   Ns   <span>This is a problem</span>r   r}   s      r(   test_failureElementValuez,FailureElementTests.test_failureElementValue.  sV    
 !y'9R9R9R/S/STT$((	d&(IJJJr*   N)r/   r0   r1   r2   r\   ri   ru   r~   r   r   r   r   r   r   r   r3   r*   r(   rL   rL   {   s         
1 1 1$  &( ( (T   	 	 		 	 	6 6 6) ) )$6 6 6	 	 	    r*   rL   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   objectrD   s    r(   test_flattenerErrorz&FormatFailureTests.test_flattenerError?  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 )   Nr3   ).0chs     r(   	<genexpr>z7FormatFailureTests.test_returnsBytes.<locals>.<genexpr>R  s&      66BH666666r*   s   &#160;N)	rO   rX   r   r   r   rJ   
assertTrueallassertIn)r%   r   s     r(   test_returnsBytesz$FormatFailureTests.test_returnsBytesF  s    	.J''' 	. 	. 	."799--FFF	. 	fe,,,66v66666777i(((((s    %99N)r/   r0   r1   r2   r   r   r3   r*   r(   r   r   9  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__X  s    r*   c                 ~    t          j        | j                  }t          j        |          } |j        ||          S r   )r   succeedr   r	   r   getChildWithDefault)r%   namer&   rg   r   s        r(   r   zSDResource.getChildWithDefault[  s8    M$,''(+++x+D':::r*   N)r/   r0   r1   r   r   r3   r*   r(   r   r   W  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 )NrR   )foobarbazr   r   )r   ResourceisLeafr   r   getChildForRequestr    postpath)r%   rsrg   s       r(   testDeferredResourcez*DeferredResourceTests.testDeferredResourcef  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   appendr=   r   r   r   r   r   r    )r%   renderedr&   r   deferredResources        r(   test_renderz!DeferredResourceTests.test_rendern  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   failr=   r    failureResultOfgccollectflushLoggedErrors)r%   r&   rg   rY   r   errorss         r(   test_renderNoFailurez*DeferredResourceTests.test_renderNoFailure}  s     r""  "",..))+EJw,?,?@@(((--a00':::

''55$$$$$r*   N)r/   r0   r1   r2   r   r   r   r3   r*   r(   r   r   a  sK         5 5 5- - -% % % % %r*   r   ),r2   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   r5   rL   r   r   r   r   r3   r*   r(   <module>r      sx   
 
			 " " " " " " / / / / / / * * * * * * @ @ @ @ @ @ @ @ & & & & & & & & , , , , , , " " " " " " & & & & & & ? ? ? ? ? ? ? ? ? ? E E E E E E E E
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
E E E E Eh E E ED4
 4
 4
 4
 4
- 4
 4
 4
n{ { { { {( { { {|) ) ) ) ) ) ) )<; ; ; ; ;" ; ; ;+% +% +% +% +%/ +% +% +% +% +%r*   