
    Vh;                     `   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jR                        Z* G d de	      Z+y)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y)RedirectToTestsz"
    Tests for L{redirectTo}.
    c                 F   t        t               d      }d|_        d}t        ||       | j	                  |j
                  t               | j	                  |j                  j                  d      |g       | j	                  |j                  j                  d      dg       y)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      J/home/dcms/DCMS/lib/python3.12/site-packages/twisted/web/test/test_util.pytest_headersAndCodez#RedirectToTests.test_headersAndCode&   s     ,.$/5	9g&u-##11+>	
 	##11/B()	
    c                 v    t        t               d      }d|_        d}| j                  t        t
        ||       y)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   s2     ,.$/4	)ZGDr*   c                 \    t        dg      }t        d|      }d}| j                  ||       y)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   s6     u%17;	 	x(r*   c                 \    t        dg      }t        d|      }d}| j                  ||       y)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   s9     u%NPW
	 	x(r*   N)__name__
__module____qualname____doc__r)   r.   r4   r6    r*   r(   r   r   !   s    
&E)$)r*   r   c                   2    e Zd ZdZdedefdZd Zd Zd Zy)	ParentRedirectTestsz!
    Test L{ParentRedirect}.
    requestPathreturnc                    t        t               d      }d|_        |j                  d      j	                  d      |_        t               } |j                  |       |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   sz     ,.$/%,,T288>!# ))77D"""
r*   c                     | j                  d| j                  d             | j                  d| j                  d             y)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   s<    
 	,d.A.A$.GH,d.A.A..QRr*   c                     | j                  d| j                  d             | j                  d| j                  d             y)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K!4!45J!K	
r*   c                     | j                  d| j                  d             | j                  d| j                  d             | j                  d| j                  d             y)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$2E2Eh2OP0$2E2Ek2RS#T%8%89L%M	
r*   N)	r7   r8   r9   r:   bytesrH   rM   rO   rQ   r;   r*   r(   r=   r=   i   s*    % E (S
	
r*   r=   c                   R    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y)FailureElementTestszn
    Tests for L{FailureElement} and related helpers which can render a
    L{Failure} as an HTML string.
    c                     d }|j                   j                  dz   | _        	  |        y# t        $ r2 t	        d      | _        | j
                  j                  d   | _        Y y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6EEI		1%' 	1"t4DL,,R0DJ	1s   + 8A&%A&c           
         t        t        t        j                  t        j                  d      t        j                  d                  dd      }t        d|      }d}|j                  | j                  |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,9WX 
 $(O 	 	
d&&(@Ar*   c           
         t        t        t        j                  t        j                  d      t        j                  d      d            | j
                        }g d}t        d|      }d}t        |      D ]a  \  }}d}|d	k  r/||j                  dd
g|d	k(     | j                  |z   d|z         z  }<||j                  d| j                  |z   d|z         z  }c |j                  d      }|j                  | 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   s   
 )II\2II\2( JJ	


 $(&/&7 	"J
XHQ HOO%jAo6II
*.;%   !HOO		J.j%  	 +11&9	d&&8r*   c           	          t        t        t        j                  d            | j                        }t        d|      }|j                  | j                  dt        t        j                  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   sg      	$)):*F GT$(	 hooc&:;;jH		
 r*   c                     t        t        t        j                  d            | j                        }t        d|      }|j                  | 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  sX      	$))<*H I4::V$(	d&&(:dii!m=M(MNr*   c                     t        t        t        j                  d            | j                        }t        d|      }|j                  | 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  sH      	$)):*F GT$(	d&&(QRr*   c                 Z   t        d| j                        }|j                  d      }t        j                         } |d|      }| j                  |t               | j                  |j                  | j                         | j                  |g|j                  j                                y)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      djj1--h7hhj$$f&<=V\\4::6# 2 2 45r*   c                    t        d| j                  j                  dd       }|j                  d      }t	        j
                         } |d|      }| j                  |t               | j                  |d   t               | j                  |d   j                  | j                  j                  d          | j                  |d   t               | j                  |d   j                  | j                  j                  d          | j                  |d   j                  j                         |d   j                  j                                | j                  dt        |             y)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)  s   
  dll&9&9"1&=>--h7hhj$$fd+fQi7VAY__dll.A.A!.DEfQi7VAY__dll.A.A!.DEF1I,,113VAY5E5E5J5J5LMCK(r*   c                 l   t        | j                        }|j                  d      }t        j                         } |d|      }| j                  |t               | j                  |j                  | j                  j                         | j                  |g|j                  j                                y)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:hhj$$fm4V//1D1DE# 2 2 45r*   c                     t        | j                  t        t        j                  d                  }t        d|      }d}|j                  | 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  sU    
 !y&9Q/RS$(#	d&&	C*(DEr*   c                     t        | j                  t        t        j                  d                  }t        d|      }|j                  | 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  sH    
 !y'9R/ST$(	d&&(I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$&(T 		6)$6	r*   rT   c                       e Zd ZdZd Zd Zy)FormatFailureTestsz
    Tests for L{twisted.web.util.formatFailure} which returns an HTML string
    representing the L{Failure} instance passed to it.
    c                 J    | j                  t        t        t                      y)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    
 	.-Br*   c                     	 t        d      # t        $ r t        t                     }Y nw xY w| j	                  |t
               | j                  t        d |D                     | j                  d|       y)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    yw)   Nr;   ).0chs     r(   	<genexpr>z7FormatFailureTests.test_returnsBytes.<locals>.<genexpr>w  s     6BH6s   s   &#160;N)	rW   r`   r   r   r   rR   
assertTrueallassertIn)r%   r   s     r(   test_returnsBytesz$FormatFailureTests.test_returnsBytesk  sd    	.J'' 	."79-F	. 	fe,6v667i(s    ,,N)r7   r8   r9   r:   r   r   r;   r*   r(   r   r   ^  s    
C)r*   r   c                       e Zd Zd Zd Zy)
SDResourcec                     || _         y 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  s9    MM$,,'((++x++D'::r*   N)r7   r8   r9   r   r   r;   r*   r(   r   r   |  s    ;r*   r   c                   "    e Zd ZdZd Zd Zd Zy)DeferredResourceTestsz(
    Tests for L{DeferredResource}.
    c                     t        j                         }d|_        t        |      }t	        g d      }t        j
                  ||       | j                  |j                  ddg       y )NrZ   )foobarbazr   r   )r   ResourceisLeafr   r   getChildForRequestr    postpath)r%   rsro   s       r(   testDeferredResourcez*DeferredResourceTests.testDeferredResource  sS    qM./##Aq)eU^4r*   c                     g }t        g       }|j                  |_        t        j                         }t        t        j                  |            }|j                  |       | j                  ||g       y)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  s`     r"!""$+EMM&,AB(F8,r*   c                 x   t        g       }|j                         }t        t                     }t	        t        j                  |            }|j                  |       | j                  | j                  |      |       ~t        j                          | j                  t              }| j                  |g        y)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"  ",.)+EJJw,?@(--a0':


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