Ϫf ;`dZddlZddlmZddlmZddlmZddlm Z m Z ddl m Z m Z ddlmZdd lmZdd lmZdd lmZmZmZdd lmZmZdd lmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$Gdde Z%Gdde Z&Gdde Z'Gdde Z(Gdde jRZ*Gdde Z+y)z Tests for L{twisted.web.util}. N)defer) networkString)Failure)SynchronousTestCaseTestCase)resourceutil)FlattenerError)FOUND)Request) TagLoader flattenStringtags) DummyChannel DummyRequest) DeferredResourceFailureElementParentRedirect _FrameElement_SourceFragmentElement_SourceLineElement _StackElement formatFailure redirectToc(eZdZdZdZdZdZdZy)RedirectToTestsz" Tests for L{redirectTo}. cFttd}d|_d}t|||j |j t |j |jjd|g|j |jjddgy)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. TGETshttp://target.example.com/4321slocations content-typestext/html; charset=utf-8N) r rmethodr assertEqualcoder responseHeaders getRawHeadersselfrequest targetURLs      # # 1 1/ B ( ) cvttd}d|_d}|jtt ||y)zW L{redirectTo} will raise TypeError if unicode object is passed in URL Trzhttp://target.example.com/4321N)r rr assertRaises TypeErrorrr$s r(test_redirectToUnicodeURLz)RedirectToTests.test_redirectToUnicodeURL9s2,.$/4  )ZGDr*c\tdg}td|}d}|j||y)zq Legitimate URLs are fully interpolated in the `redirectTo` response body without transformation r*shttps://twisted.org/s click here Nrrr r%r&htmlexpecteds r(test_legitimateRedirectz'RedirectToTests.test_legitimateRedirectBs6u%17;  x(r*c\tdg}td|}d}|j||y)zm Malicious URLs are HTML-escaped before interpolating them in the `redirectTo` response body r*s?https://twisted.org/">sX click here Nr0r1s r(test_maliciousRedirectz&RedirectToTests.test_maliciousRedirectTs9u% NPW   x(r*N)__name__ __module__ __qualname____doc__r)r.r4r6r*r(rr!s &E)$)r*rc2eZdZdZdedefdZdZdZdZy) ParentRedirectTestsz! Test L{ParentRedirect}. requestPathreturncttd}d|_|jdj d|_t }|j||jjd}|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/sLocation) r rrlstripsplitprepathrrenderr"r#)r%r>r&rheaderslocations r(doLocationTestz"ParentRedirectTests.doLocationTestnsz,.$/%,,T288>!# ))77 D""" r*c|jd|jd|jd|jdy)zi At the URL root issue a redirect to the current URL, removing any query string. http://10.0.0.1/rAs /?biff=baffNr rHr%s r(test_locationRootz%ParentRedirectTests.test_locationRoots< ,d.A.A$.GH ,d.A.A..QRr*c|jd|jd|jd|jdy)zt A request for a resource one level down from the URL root produces a redirect to the root. rJs/foos/foo?bar=sproiiingNrKrLs r(test_locationToRootz'ParentRedirectTests.test_locationToRoots? ,d.A.A'.JK  !4!45J!K r*c|jd|jd|jd|jd|jd|jdy)zp Requests for resources directly under the path C{/foo/} produce redirects to C{/foo/}. shttp://10.0.0.1/foo/s/foo/s/foo/bars/foo/bar?biz=bazNrKrLs r(test_locationUpOnez&ParentRedirectTests.test_locationUpOnes[ 0$2E2Eh2OP 0$2E2Ek2RS  #T%8%89L%M r*N) r7r8r9r:bytesrHrMrOrQr;r*r(r=r=is*%E(S   r*r=cReZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zy )FailureElementTestszn Tests for L{FailureElement} and related helpers which can render a L{Failure} as an HTML string. cd}|jjdz|_ |y#t$r2t d|_|j j d|_YywxYw)zO Create a L{Failure} which can be used by the rendering tests. cd}t|)NzThis is a problem) Exception)messages r(lineNumberProbeAlsoBrokenz.lineNumberProbeAlsoBrokens)GG$ $r*T) captureVarsN)__code__co_firstlinenobase BaseExceptionrfailureframesframe)r%rYs r(setUpzFailureElementTests.setUps]  % .66EEI  1 % ' 1"t4DL,,R0DJ 1s+8A&%A&c tttjtjdtjddd}t d|}d}|j |j|jd|S) zN L{_SourceLineElement} renders a source line and line number. lineNumberrE sourceLine2z print 'hello'Nu:
50    print 'hello'
zutf-8) rr rdivspanr addCallbackr encode)r%elementdr3s r(test_sourceLineElementz*FailureElementTests.test_sourceLineElements{% ,7,9WX      $ ( O  d&&(@Ar*c tttjtjdtjdd|j }gd}t d|}d}t|D]a\}}d}|d kr/||jdd g|d k(|j|zd |zz }<||jd|j|zd|zz }c|jd }|j|j||S) z L{_SourceFragmentElement} renders source lines at and around the line number indicated by a frame object. rfrgrh sourceLines)u#    message = "This is a problem"u    raise Exception(message)Nrsz?
{}{}
rZ Highlightu        utf8) rr rrjrkrcr enumerateformatr_rmrlr ) r%rnsourcerostringToCheckForrfrhtemplatebytesToCheckFors r(test_sourceFragmentElementz.FailureElementTests.test_sourceFragmentElements ) II\2II\2(  JJ    $ (&/&7  "J XHQ HOO%jAo6II *.;% !HOO J.j%  +11&9 d&&8r*c tttjd|j}t d|}|j |jdttjdzdz|S)z The I{filename} renderer of L{_FrameElement} renders the filename associated with the frame object used to initialize the L{_FrameElement}. filenamergNc) rr rrkrcrrlr r__file__rstripr%rnros r(test_frameElementFilenamez-FailureElementTests.test_frameElementFilenamesg  $)):*F GT $ (      hooc&:; ;j H  r*ctttjd|j}t d|}|j |jd|jdzfz|S)z The I{lineNumber} renderer of L{_FrameElement} renders the line number associated with the frame object used to initialize the L{_FrameElement}. rfrgNs%drZ) rr rrkrcrrlr r_rs r(test_frameElementLineNumberz/FailureElementTests.test_frameElementLineNumbersX  $))<*H I4::V $ ( d&&(:dii!m=M(MNr*ctttjd|j}t d|}|j |jd|S)z The I{function} renderer of L{_FrameElement} renders the line number associated with the frame object used to initialize the L{_FrameElement}. functionrgNs&lineNumberProbeAlsoBroken)rr rrkrcrrlr rs r(test_frameElementFunctionz-FailureElementTests.test_frameElementFunctionsH  $)):*F GT $ ( d&&(QRr*cZtd|j}|jd}tj}|d|}|j |t |j|j|j|j|g|jjy)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) rrclookupRenderMethodrrjassertIsInstancerassertIdenticalr loaderloadr%rnrenderertagresults r(test_frameElementSourcez+FailureElementTests.test_frameElementSources  djj1--h7hhj$$ f&<= V\\4::6 # 2 2 45r*ctd|jjdd}|jd}t j }|d|}|j |t|j |dt|j|dj|jjd|j |dt|j|dj|jjd|j|djj|djj|jdt|y)z The I{frames} renderer of L{_StackElement} renders each stack frame in the list of frames used to initialize the L{_StackElement}. NrbrrZ)rrarbrrrjrlistrrrcassertNotEqualrrr lenrs r(test_stackElementz%FailureElementTests.test_stackElement)s  dll&9&9"1&=>--h7hhj$$ fd+ fQi7 VAY__dll.A.A!.DE fQi7 VAY__dll.A.A!.DE F1I,,113VAY5E5E5J5J5LM CK(r*clt|j}|jd}tj}|d|}|j |t |j|j|jj|j|g|jjy)z The I{traceback} renderer of L{FailureElement} renders the failure's stack frames using L{_StackElement}. tracebackN) rrarrrjrrr stackFramesrbr rrrs r(test_failureElementTracebackz0FailureElementTests.test_failureElementTraceback;s !.--k:hhj$$ fm4 V//1D1DE # 2 2 45r*ct|jttjd}t d|}d}|j |jd|zdz|S)zi The I{type} renderer of L{FailureElement} renders the failure's exception type. typergNsbuiltins.Exceptionrrrrar rrkrrlr )r%rnroexcs r(test_failureElementTypez+FailureElementTests.test_failureElementTypeHsU !y&9Q/RS $ (# d&& C*(DEr*ct|jttjd}t d|}|j |jd|S)zi The I{value} renderer of L{FailureElement} renders the value's exception value. valuergNsThis is a problemrrs r(test_failureElementValuez,FailureElementTests.test_failureElementValueSsH !y'9R/ST $ ( d&&(IJr*N)r7r8r9r:rdrpr|rrrrrrrrr;r*r(rTrTs? 1$&(T   6)$ 6 r*rTceZdZdZdZdZy)FormatFailureTestsz Tests for L{twisted.web.util.formatFailure} which returns an HTML string representing the L{Failure} instance passed to it. cJ|jttty)z} If there is an error flattening the L{Failure} instance, L{formatFailure} raises L{FlattenerError}. N)r,r robjectrLs r(test_flattenerErrorz&FormatFailureTests.test_flattenerErrords .-Br*c td#t$rtt}YnwxYw|j |t |j td|D|jd|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. zFake bugc3&K|] }|dk yw)Nr;).0chs r( z7FormatFailureTests.test_returnsBytes..ws6BH6ss N) rWr`rrrrR assertTrueallassertIn)r%rs r(test_returnsBytesz$FormatFailureTests.test_returnsBytesksd  .J' ' ."79-F . fe, 6v667 i(s ,,N)r7r8r9r:rrr;r*r(rr^s C)r*rceZdZdZdZy) SDResourcec||_yN)default)r%rs r(__init__zSDResource.__init__}s  r*ctj|j}tj|}|j ||Sr)rsucceedrr rgetChildWithDefault)r%namer&rors r(rzSDResource.getChildWithDefaults9 MM$,, '((++x++D'::r*N)r7r8r9rrr;r*r(rr|s ;r*rc"eZdZdZdZdZdZy)DeferredResourceTestsz( Tests for L{DeferredResource}. ctj}d|_t|}t gd}tj |||j |jddgy)NrZ)foobarbazrr)rResourceisLeafrrgetChildForRequestr postpath)r%rsros r(testDeferredResourcez*DeferredResourceTests.testDeferredResourcesS     qM . /##Aq) eU^4r*cg}tg}|j|_tj}t t j|}|j||j||gy)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) rappendrErrrrrr )r%renderedr&rdeferredResources r( test_renderz!DeferredResourceTests.test_renders` r"!""$+EMM&,AB( F8,r*cxtg}|j}tt}t t j |}|j||j|j||~tj|jt}|j|gy)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 RuntimeErrorrrfailrEr failureResultOfgccollectflushLoggedErrors)r%r&rorarerrorss r(test_renderNoFailurez*DeferredResourceTests.test_renderNoFailures r"  ",.)+EJJw,?@( --a0':  '' 5 $r*N)r7r8r9r:rrrr;r*r(rrs5 -%r*r),r:rtwisted.internetrtwisted.python.compatrtwisted.python.failurertwisted.trial.unittestrr twisted.webrr twisted.web.errorr twisted.web.httpr twisted.web.serverr twisted.web.templater rrtwisted.web.test.requesthelperrrtwisted.web.utilrrrrrrrrrrr=rTrrrrr;r*r(rs "/*@&,"&??E   E)hE)P4 -4 n{({|))<;"";+%/+%r*