+
    Dfj;                     b   R t ^ RIt^ RIHt ^ RIHt ^ RIHt ^ RIH	t	H
t
 ^ RIHtHt ^ RIHt ^ RIHt ^ R	IHt ^ R
IHtHtHt ^ RIHtHt ^ RIHtHtHtHtH t H!t!H"t"H#t#H$t$  ! R R]
4      t% ! R R]	4      t& ! R R]
4      t' ! R R]
4      t( ! R R]PR                  4      t* ! R R]	4      t+R# )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                   <   a  ] tR t^!t o RtR tR tR tR tRt	V t
R# )RedirectToTestsz
Tests for L{redirectTo}.
c                F   \        \        4       R4      pRVn        Rp\        W!4       V P	                  VP
                  \        4       V P	                  VP                  P                  R4      V.4       V P	                  VP                  P                  R4      R.4       R# )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   &  </usr/lib/python3/dist-packages/twisted/web/test/test_util.pytest_headersAndCode#RedirectToTests.test_headersAndCode&   s     ,.$/5	9&u-##11+>	
 	##11/B()	
    c                v    \        \        4       R4      pRVn        RpV P                  \        \
        W!4       R# )zG
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_redirectToUnicodeURL)RedirectToTests.test_redirectToUnicodeURL9   s0     ,.$/4	)ZDr*   c                \    \        R.4      p\        RV4      pRpV P                  W#4       R# )za
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_legitimateRedirect'RedirectToTests.test_legitimateRedirectB   s4     u%17;	 	(r*   c                \    \        R.4      p\        RV4      pRpV P                  W#4       R# )z]
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>
Nr1   r2   s   &   r'   test_maliciousRedirect&RedirectToTests.test_maliciousRedirectT   s7     u%NPW
	 	(r*    N)__name__
__module____qualname____firstlineno____doc__r(   r.   r5   r8   __static_attributes____classdictcell____classdict__s   @r'   r   r   !   s$     
&E)$) )r*   r   c                   H   a  ] tR t^it o RtV 3R lR ltR tR tR tRt	V t
R# )	ParentRedirectTestsz
Test L{ParentRedirect}.
c                &   < V ^8  d   QhRS[ RS[ /# )   requestPathreturn)bytes)formatrC   s   "r'   __annotate__ ParentRedirectTests.__annotate__n   s      % E r*   c                   \        \        4       R4      pRVn        VP                  R4      P	                  R4      Vn        \        4       pVP                  ! V4       VP                  P                  R4      pVf   Q hVw  pV# )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$   rH   r%   r   headerslocations   &&    r'   doLocationTest"ParentRedirectTests.doLocationTestn   sx     ,.$/%,,T288>!# ))77D"""
r*   c                    V P                  RV P                  R4      4       V P                  RV P                  R4      4       R# )zQ
At the URL root issue a redirect to the current URL, removing any query
string.
   http://10.0.0.1/rO   s   /?biff=baffNr   rV   r$   s   &r'   test_locationRoot%ParentRedirectTests.test_locationRoot   s<    
 	,d.A.A$.GH,d.A.A..QRr*   c                    V P                  RV P                  R4      4       V P                  RV P                  R4      4       R# )z\
A request for a resource one level down from the URL root produces
a redirect to the root.
rY   s   /foos   /foo?bar=sproiiingNrZ   r[   s   &r'   test_locationToRoot'ParentRedirectTests.test_locationToRoot   s?    
 	,d.A.A'.JK!4!45J!K	
r*   c                    V P                  RV P                  R4      4       V P                  RV P                  R4      4       V P                  RV P                  R4      4       R# )zX
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=bazNrZ   r[   s   &r'   test_locationUpOne&ParentRedirectTests.test_locationUpOne   s[    
 	0$2E2Eh2OP0$2E2Ek2RS#T%8%89L%M	
r*   r:   N)r;   r<   r=   r>   r?   rV   r\   r_   rb   r@   rA   rB   s   @r'   rE   rE   i   s)      (S
	
 	
r*   rE   c                   f   a  ] tR t^t o RtR tR tR tR tR t	R t
R tR	 tR
 tR tR tRtV tR# )FailureElementTestszb
Tests for L{FailureElement} and related helpers which can render a
L{Failure} as an HTML string.
c                    R pVP                   P                  ^,           V n         V! 4        R#   \         d8    \	        RR7      T n        T P
                  P                  R,          T n         R# i ; i)z?
Create a L{Failure} which can be used by the rendering tests.
c                      R p \        V 4      h)zThis is a problem)	Exception)messages    r'   lineNumberProbeAlsoBroken<FailureElementTests.setUp.<locals>.lineNumberProbeAlsoBroken   s    )GG$$r*   T)captureVarsN)__code__co_firstlinenobaseBaseExceptionr   failureframesframe)r$   rj   s   & r'   setUpFailureElementTests.setUp   s]    
	%
 .66EEI		1%' 	1"t4DL,,R0DJ	1s   0 >A21A2c                "   \        \        \        P                  ! \        P                  ! RR7      \        P                  ! RR7      4      4      ^2R4      p\        RV4      pRpVP                  V P                  VP                  R4      4       V# )z>
L{_SourceLineElement} renders a source line and line number.

lineNumberrS   
sourceLinez    print 'hello'Nu:   <div><span>50</span><span>    print 'hello'</span></div>zutf-8)	r   r   r   divspanr   addCallbackr   encode)r$   elementdr4   s   &   r'   test_sourceLineElement*FailureElementTests.test_sourceLineElement   s{     %,7,9WX 
 $(O 	 	
d&&(@Ar*   c                ^   \        \        \        P                  ! \        P                  ! RR7      \        P                  ! RR7      RR7      4      V P
                  4      p. R
Op\        RV4      pRp\        V4       F~  w  rVRpV^8:  d@   WGP                  RR.V^8H  ,          V P                  V,           RV,           4      ,          pKM  WGP                  RV P                  V,           RV,           4      ,          pK  	  VP                  R	4      pVP                  V P                  V4       V# )zk
L{_SourceFragmentElement} renders source lines at and around the line
number indicated by a frame object.
rx   ry   rz   sourceLines Nz?<div class="snippet{}Line"><span>{}</span><span>{}</span></div>	Highlightutf8)u#       message = "This is a problem"u       raise Exception(message)r   u           )r   r   r   r{   r|   rt   r   	enumeraterK   rp   r~   r}   r   )	r$   r   sourcer   stringToCheckForrx   rz   templatebytesToCheckFors	   &        r'   test_sourceFragmentElement.FailureElementTests.test_sourceFragmentElement   s   
 )II\2II\2( JJ	


 $(&/&7"JXHQ OO%jAo6II
*.;%   !OO		J.j%   '8 +11&9	d&&8r*   c           	        \        \        \        P                  ! RR7      4      V P                  4      p\        RV4      pVP                  V P                  R\        \        P                  R4      4      ,           R,           4       V# )z
The I{filename} renderer of L{_FrameElement} renders the filename
associated with the frame object used to initialize the
L{_FrameElement}.
filenamery   N   <span>c   </span>)r   r   r   r|   rt   r   r}   r   r   __file__rstripr$   r   r   s   &  r'   test_frameElementFilename-FailureElementTests.test_frameElementFilename   sg      	$)):*F GT$(	 hooc&:;;jH		
 r*   c                    \        \        \        P                  ! RR7      4      V P                  4      p\        RV4      pVP                  V P                  RV P                  ^,           3,          4       V# )z
The I{lineNumber} renderer of L{_FrameElement} renders the line number
associated with the frame object used to initialize the
L{_FrameElement}.
rx   ry   Ns   <span>%d</span>)	r   r   r   r|   rt   r   r}   r   rp   r   s   &  r'   test_frameElementLineNumber/FailureElementTests.test_frameElementLineNumber  sX      	$))<*H I4::V$(	d&&(:dii!m=M(MNr*   c                    \        \        \        P                  ! RR7      4      V P                  4      p\        RV4      pVP                  V P                  R4       V# )z
The I{function} renderer of L{_FrameElement} renders the line number
associated with the frame object used to initialize the
L{_FrameElement}.
functionry   Ns&   <span>lineNumberProbeAlsoBroken</span>)r   r   r   r|   rt   r   r}   r   r   s   &  r'   test_frameElementFunction-FailureElementTests.test_frameElementFunction  sH      	$)):*F GT$(	d&&(QRr*   c                ^   \        RV P                  4      pVP                  R4      p\        P                  ! 4       pV! RV4      pV P                  V\        4       V P                  VP                  V P                  4       V P                  V.VP                  P                  4       4       R# )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}.
Nr   )r   rt   lookupRenderMethodr   r{   assertIsInstancer   assertIdenticalr   loaderloadr$   r   renderertagresults   &    r'   test_frameElementSource+FailureElementTests.test_frameElementSource  s      djj1--h7hhj$$f&<=V\\4::6# 2 2 45r*   c                    \        RV P                  P                  R,          4      pVP                  R4      p\        P
                  ! 4       pV! RV4      pV P                  V\        4       V P                  V^ ,          \        4       V P                  V^ ,          P                  V P                  P                  ^ ,          4       V P                  V^,          \        4       V P                  V^,          P                  V P                  P                  ^,          4       V P                  V^ ,          P                  P                  4       V^,          P                  P                  4       4       V P                  ^\        V4      4       R# )z
The I{frames} renderer of L{_StackElement} renders each stack frame in
the list of frames used to initialize the L{_StackElement}.
N:NrG   Nrs   )r   rr   rs   r   r   r{   r   listr   r   rt   assertNotEqualr   r   r   lenr   s   &    r'   test_stackElement%FailureElementTests.test_stackElement)  s   
  dll&9&9"&=>--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                p   \        V P                  4      pVP                  R4      p\        P                  ! 4       pV! RV4      pV P                  V\        4       V P                  VP                  V P                  P                  4       V P                  V.VP                  P                  4       4       R# )zk
The I{traceback} renderer of L{FailureElement} renders the failure's
stack frames using L{_StackElement}.
	tracebackN)r   rr   r   r   r{   r   r   r   stackFramesrs   r   r   r   r   s   &    r'   test_failureElementTraceback0FailureElementTests.test_failureElementTraceback;  s    
 !.--k:hhj$$fm4V//1D1DE# 2 2 45r*   c           	         \        V P                  \        \        P                  ! RR7      4      4      p\        RV4      pRpVP                  V P                  RV,           R,           4       V# )zQ
The I{type} renderer of L{FailureElement} renders the failure's
exception type.
typery   Ns   builtins.Exceptionr   r   r   rr   r   r   r|   r   r}   r   )r$   r   r   excs   &   r'   test_failureElementType+FailureElementTests.test_failureElementTypeH  sU    
 !y&9Q/RS$(#	d&&	C*(DEr*   c           	         \        V P                  \        \        P                  ! RR7      4      4      p\        RV4      pVP                  V P                  R4       V# )zQ
The I{value} renderer of L{FailureElement} renders the value's exception
value.
valuery   Ns   <span>This is a problem</span>r   r   s   &  r'   test_failureElementValue,FailureElementTests.test_failureElementValueS  sH    
 !y'9R/ST$(	d&&(IJr*   )rp   rr   rt   N)r;   r<   r=   r>   r?   ru   r   r   r   r   r   r   r   r   r   r   r@   rA   rB   s   @r'   re   re      sG     
1$&(T 		6)$6	 r*   re   c                   0   a  ] tR tRt o RtR tR tRtV tR# )FormatFailureTestsi^  z}
Tests for L{twisted.web.util.formatFailure} which returns an HTML string
representing the L{Failure} instance passed to it.
c                L    V P                  \        \        \        4       4       R# )ze
If there is an error flattening the L{Failure} instance,
L{formatFailure} raises L{FlattenerError}.
N)r,   r	   r   objectr[   s   &r'   test_flattenerError&FormatFailureTests.test_flattenerErrord  s    
 	.-Br*   c                <    \        R4      h  \         d    \        \        4       4      p Mi ; iT P	                  T\
        4       T P                  \        ;QJ d    R T 4       F  '       d   K   RM	  RM! R T 4       4      4       T P                  RT4       R# )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   *   "   T F	  q^8  x  K  	  R# 5i)   Nr:   ).0chs   & r'   	<genexpr>7FormatFailureTests.test_returnsBytes.<locals>.<genexpr>w  s     6vHvs   FTs   &#160;N)	rh   rq   r   r   r   rJ   
assertTrueallassertIn)r$   r   s   & r'   test_returnsBytes$FormatFailureTests.test_returnsBytesk  st    	.J'' 	."79-F	. 	fe,6v66v667i(s    ..r:   N)	r;   r<   r=   r>   r?   r   r   r@   rA   rB   s   @r'   r   r   ^  s     
C) )r*   r   c                   ,   a  ] tR tRt o R tR tRtV tR# )
SDResourcei|  c                    Wn         R # Ndefault)r$   r   s   &&r'   __init__SDResource.__init__}  s    r*   c                    \         P                  ! V P                  4      p\        P                  ! V4      pVP
                  ! W4      # r   )r   succeedr   r   r   getChildWithDefault)r$   namer%   r   r   s   &&&  r'   r   SDResource.getChildWithDefault  s5    MM$,,'((+++D::r*   r   N)r;   r<   r=   r>   r   r   r@   rA   rB   s   @r'   r   r   |  s     ; ;r*   r   c                   6   a  ] tR tRt o RtR tR tR tRtV t	R# )DeferredResourceTestsi  z 
Tests for L{DeferredResource}.
c                    \         P                  ! 4       p^Vn        \        V4      p\	        . RO4      p\         P
                  ! W#4       V P                  VP                  RR.4       R# )   barbazN)foor   r   )r   ResourceisLeafr   r   getChildForRequestr   postpath)r$   rsr   s   &   r'   testDeferredResource*DeferredResourceTests.testDeferredResource  sQ    qM./##A)eU^4r*   c                    . p\        . 4      pVP                  Vn        \        P                  ! 4       p\        \        P                  ! V4      4      pVP                  V4       V P                  W.4       R# )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   appendrS   r   r   r   r   r   r   )r$   renderedr%   r   deferredResources   &    r'   test_render!DeferredResourceTests.test_render  s^     r"!""$+EMM&,AB(8,r*   c                ~   \        . 4      pVP                  4       p\        \        4       4      p\	        \
        P                  ! V4      4      pVP                  V4       V P                  V P                  V4      V4       ?\        P                  ! 4        V P                  \        4      pV P                  V. 4       R# )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   failrS   r   failureResultOfgccollectflushLoggedErrors)r$   r%   r   rr   r   errorss   &     r'   test_renderNoFailure*DeferredResourceTests.test_renderNoFailure  s     r"  ",.)+EJJw,?@(--a0':


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