Ë
    ojy)  ã                   ó  — d Z ddl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mZmZmZmZ ddl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«      Z G d„ de«      Z G d„ de«      Zy)z$
Tests for L{twisted.web.resource}.
é    )ÚTestCase)ÚUnsupportedMethod)ÚHeaders)ÚIRequest)Ú	FORBIDDENÚ	NOT_FOUNDÚResourceÚ_UnsafeErrorPageÚ_UnsafeForbiddenResourceÚ_UnsafeNoResourceÚgetChildForRequest)ÚDummyRequestc            
       óp   — e Zd ZdZeZeZeZ	dd„Z
dd„Zdd„Zdd„Zded	ed
ededdf
d„Zdd„Zdd„Zdd„Zy)ÚErrorPageTestszb
    Tests for L{_UnafeErrorPage}, L{_UnsafeNoResource}, and
    L{_UnsafeForbiddenResource}.
    ÚreturnNc                 óÈ   — ddl m} | j                  || j                  «       | j	                  «       \  }| j                  |d   t        «       | j                  d|d   «       y)z©
        The public C{twisted.web.resource.ErrorPage} alias for the
        corresponding C{_Unsafe} class produces a deprecation warning when
        imported.
        r   )Ú	ErrorPageÚcategoryztwisted.web.pages.errorPageÚmessageN)Útwisted.web.resourcer   ÚassertIsÚ	errorPageÚflushWarningsÚassertEqualÚDeprecationWarningÚassertIn)Úselfr   Úwarnings      ú@/usr/lib/python3/dist-packages/twisted/web/test/test_resource.pyÚtest_deprecatedErrorPagez'ErrorPageTests.test_deprecatedErrorPage"   sS   € õ 	3à�‰�i §¡Ô0à×&Ñ&Ó(‰	ˆØ×Ñ˜ Ñ,Ô.@ÔAØ�‰Ð3°W¸YÑ5GÕHó    c                 óÈ   — ddl m} | j                  || j                  «       | j	                  «       \  }| j                  |d   t        «       | j                  d|d   «       y)zª
        The public C{twisted.web.resource.NoResource} alias for the
        corresponding C{_Unsafe} class produces a deprecation warning when
        imported.
        r   )Ú
NoResourcer   ztwisted.web.pages.notFoundr   N)r   r#   r   Ú
noResourcer   r   r   r   )r   r#   r   s      r   Útest_deprecatedNoResourcez(ErrorPageTests.test_deprecatedNoResource0   sS   € õ 	4à�‰�j $§/¡/Ô2à×&Ñ&Ó(‰	ˆØ×Ñ˜ Ñ,Ô.@ÔAØ�‰Ð2°G¸IÑ4FÕGr!   c                 óÈ   — ddl m} | j                  || j                  «       | j	                  «       \  }| j                  |d   t        «       | j                  d|d   «       y)z°
        The public C{twisted.web.resource.ForbiddenResource} alias for the
        corresponding C{_Unsafe} class produce a deprecation warning when
        imported.
        r   )ÚForbiddenResourcer   ztwisted.web.pages.forbiddenr   N)r   r'   r   ÚforbiddenResourcer   r   r   r   )r   r'   r   s      r   Ú test_deprecatedForbiddenResourcez/ErrorPageTests.test_deprecatedForbiddenResource>   sV   € õ 	;à�‰Ð'¨×)?Ñ)?Ô@à×&Ñ&Ó(‰	ˆØ×Ñ˜ Ñ,Ô.@ÔAØ�‰Ð3°W¸YÑ5GÕHr!   c                 ó~   — | j                  ddd«      }| j                  |j                  dt        «       «      |«       y)zj
        The C{getChild} method of L{ErrorPage} returns the L{ErrorPage} it is
        called on.
        éA  ÚfooÚbars   nameN)r   ÚassertIdenticalÚgetChildÚobject)r   Úpages     r   Útest_getChildzErrorPageTests.test_getChildL   s3   € ð
 �~‰~˜c 5¨%Ó0ˆØ×Ñ˜TŸ]™]¨7´F³HÓ=¸tÕDr!   r1   ÚcodeÚbriefÚdetailc                 ó  — t        dg«      }d}|||||fz  }| j                  |j                  |«      |j                  d«      «       | j                  |j                  |«       | j                  |j
                  t        ddgi«      «       y )Nr!   zg
<html>
  <head><title>%s - %s</title></head>
  <body>
    <h1>%s</h1>
    <p>%s</p>
  </body>
</html>
zutf-8s   content-types   text/html; charset=utf-8)r   r   ÚrenderÚencodeÚresponseCodeÚresponseHeadersr   )r   r1   r3   r4   r5   ÚrequestÚtemplateÚexpecteds           r   Ú_pageRenderingTestz!ErrorPageTests._pageRenderingTestT   s�   € ô  ˜uÓ%ˆðð 	ð ˜t U¨E°6Ð:Ñ:ˆØ×Ñ˜Ÿ™ WÓ-¨x¯©¸wÓ/GÔHØ×Ñ˜×-Ñ-¨tÔ4Ø×ÑØ×#Ñ#Ü�_Ð'BÐ&CÐDÓEõ	
r!   c                 ó^   — d}d}d}| j                  |||«      }| j                  ||||«       y)a  
        L{ErrorPage.render} returns a C{bytes} describing the error defined by
        the response code and message passed to L{ErrorPage.__init__}.  It also
        uses that response code to set the response code on the L{Request}
        passed in.
        r+   zbrief description textzmuch longer text might go hereN)r   r>   )r   r3   r4   r5   r1   s        r   Útest_errorPageRenderingz&ErrorPageTests.test_errorPageRenderingj   s9   € ð ˆØ(ˆØ1ˆØ�~‰~˜d E¨6Ó2ˆØ×Ñ  d¨E°6Õ:r!   c                 óZ   — d}| j                  |«      }| j                  |t        d|«       y)z@
        L{NoResource} sets the HTTP I{NOT FOUND} code.
        zlong messagezNo Such ResourceN)r$   r>   r   ©r   r5   r1   s      r   Útest_noResourceRenderingz'ErrorPageTests.test_noResourceRenderingw   s,   € ð  ˆØ�‰˜vÓ&ˆØ×Ñ ¤iÐ1CÀVÕLr!   c                 óZ   — d}| j                  |«      }| j                  |t        d|«       y)zG
        L{ForbiddenResource} sets the HTTP I{FORBIDDEN} code.
        zlonger messagezForbidden ResourceN)r(   r>   r   rB   s      r   Útest_forbiddenResourceRenderingz.ErrorPageTests.test_forbiddenResourceRendering   s.   € ð "ˆØ×%Ñ% fÓ-ˆØ×Ñ ¤iÐ1EÀvÕNr!   ©r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r#   r$   r'   r(   r    r%   r)   r2   r	   ÚintÚstrr>   r@   rC   rE   © r!   r   r   r      so   „ ñð
 €IØ€JØ)ÐóIóHóIóEð
Øð
Ø$'ð
Ø03ð
Ø=@ð
à	ó
ó,;óMôOr!   r   c                   ó$   — e Zd ZdZdededdfd„Zy)ÚDynamicChildzG
    A L{Resource} to be created on the fly by L{DynamicChildren}.
    Úpathr;   r   Nc                 óJ   — t        j                  | «       || _        || _        y ©N)r	   Ú__init__rP   r;   ©r   rP   r;   s      r   rS   zDynamicChild.__init__�   s   € Ü×Ñ˜$ÔØˆŒ	Øˆ�r!   )rG   rH   rI   rJ   Úbytesr   rS   rM   r!   r   rO   rO   ˆ   s!   „ ñð˜Uð ¨Xð ¸$ô r!   rO   c                   ó$   — e Zd ZdZdededefd„Zy)ÚDynamicChildrenz.
    A L{Resource} with dynamic children.
    rP   r;   r   c                 ó   — t        ||«      S rR   )rO   rT   s      r   r/   zDynamicChildren.getChild˜   s   € Ü˜D 'Ó*Ð*r!   N)rG   rH   rI   rJ   rU   r   rO   r/   rM   r!   r   rW   rW   “   s!   „ ñð+˜Uð +¨Xð +¸,ô +r!   rW   c                   ó0   — e Zd ZdZdeddfd„Zdedefd„Zy)ÚBytesReturnedRenderablezG
    A L{Resource} with minimal capabilities to render a response.
    Úresponser   Nc                 ó<   — t        j                  | «       || _        y)zo
        @param response: A C{bytes} object giving the value to return from
            C{render_GET}.
        N)r	   rS   Ú	_response)r   r[   s     r   rS   z BytesReturnedRenderable.__init__¡   s   € ô
 	×Ñ˜$ÔØ!ˆ�r!   r;   c                 ó   — | j                   S )z}
        Render a response to a I{GET} request by returning a short byte string
        to be written by the server.
        )r]   ©r   r;   s     r   Ú
render_GETz"BytesReturnedRenderable.render_GET©   s   € ð
 �~‰~Ðr!   )rG   rH   rI   rJ   rU   rS   r0   r`   rM   r!   r   rZ   rZ   œ   s-   „ ñð" ð "¨4ó "ð &ð ¨Uô r!   rZ   c                   ó0   — e Zd ZdZdeddfd„Zdeddfd„Zy)ÚImplicitAllowedMethodszn
    A L{Resource} which implicitly defines its allowed methods by defining
    renderers to handle them.
    r;   r   Nc                  ó   — y rR   rM   r_   s     r   r`   z!ImplicitAllowedMethods.render_GET·   ó   € Ør!   c                  ó   — y rR   rM   r_   s     r   Ú
render_PUTz!ImplicitAllowedMethods.render_PUTº   rd   r!   )rG   rH   rI   rJ   r0   r`   rf   rM   r!   r   rb   rb   ±   s-   „ ñð
 &ð ¨Tó ð &ð ¨Tô r!   rb   c                   ó@   — e Zd ZdZd	d„Zd	d„Zd	d„Zd	d„Zd	d„Zd	d„Z	y)
ÚResourceTestsz 
    Tests for L{Resource}.
    Nc           	      óÞ   — t        «       }t        «       }t        «       }|j                  d|«       |j                  d|«       | j                  ||j                  dt	        g «      «      «       y)z½
        L{Resource.putChild} adds a I{static} child to the resource.  That child
        is returned from any call to L{Resource.getChildWithDefault} for the
        child's path.
        ó   fooó   barN)r	   ÚputChildr.   ÚgetChildWithDefaultr   ©r   ÚresourceÚchildÚsiblings       r   Útest_staticChildrenz!ResourceTests.test_staticChildrenÃ   s^   € ô “:ˆÜ“
ˆÜ“*ˆØ×Ñ˜& %Ô(Ø×Ñ˜& 'Ô*Ø×ÑØ�8×/Ñ/°¼ÀRÓ8HÓIõ	
r!   c                 óò   — d}t        g «      }t        «       }|j                  ||«      }| j                  |t        «       | j                  |j                  |«       | j                  |j                  |«       y)zœ
        L{Resource.getChildWithDefault} delegates to L{Resource.getChild} when
        the requested path is not associated with any static child.
        rj   N)	r   rW   rm   ÚassertIsInstancerO   r   rP   r.   r;   )r   rP   r;   ro   rp   s        r   Útest_dynamicChildrenz"ResourceTests.test_dynamicChildrenÒ   se   € ð
 ˆÜ˜rÓ"ˆÜ"Ó$ˆØ×,Ñ,¨T°7Ó;ˆØ×Ñ˜e¤\Ô2Ø×Ñ˜Ÿ™ TÔ*Ø×Ñ˜UŸ]™]¨GÕ4r!   c                 óÈ   — t        «       }t        «       }t        «       }| j                  t        |j                  d|«       | j                  t        |j                  d|«       y)zv
        Test that passing the wrong type to putChild results in a warning,
        and a failure in Python 3
        r,   N)r	   ÚassertRaisesÚ	TypeErrorrl   rn   s       r   Útest_staticChildPathTypez&ResourceTests.test_staticChildPathTypeß   sM   € ô
 “:ˆÜ“
ˆÜ“*ˆØ×Ñœ) X×%6Ñ%6¸¸uÔEØ×Ñœ) X×%6Ñ%6¸¸gÕFr!   c                 ó„   — d}t        g «      }d|_        t        |«      }| j                  ||j	                  |«      «       y)z„
        When not otherwise overridden, L{Resource.render} treats a I{HEAD}
        request as if it were a I{GET} request.
        s   insert response hereó   HEADN)r   ÚmethodrZ   r   r7   )r   r=   r;   ro   s       r   Útest_defaultHEADzResourceTests.test_defaultHEADê   s=   € ð
 +ˆÜ˜rÓ"ˆØ ˆŒÜ*¨8Ó4ˆØ×Ñ˜ 8§?¡?°7Ó#;Õ<r!   c                 óð   — g d¢}t        «       }||_        t        g «      }d|_        | j	                  t
        |j                  |«      }| j                  t        |«      t        |j                  «      «       y)zò
        The L{UnsupportedMethod} raised by L{Resource.render} for an unsupported
        request method has a C{allowedMethods} attribute set to the value of the
        C{allowedMethods} attribute of the L{Resource}, if it has one.
        )ó   GETr{   ó   PUTó	   FICTIONALN)	r	   ÚallowedMethodsr   r|   rw   r   r7   r   Úset©r   r=   ro   r;   Úexcs        r   Útest_explicitAllowedMethodsz)ResourceTests.test_explicitAllowedMethodsõ   sa   € ò -ˆÜ“:ˆØ"*ˆÔÜ˜rÓ"ˆØ%ˆŒØ×ÑÔ 1°8·?±?ÀGÓLˆØ×Ñœ˜X›¬¨C×,>Ñ,>Ó(?Õ@r!   c                 óÐ   — h d£}t        «       }t        g «      }d|_        | j                  t        |j
                  |«      }| j                  |t        |j                  «      «       y)al  
        The L{UnsupportedMethod} raised by L{Resource.render} for an unsupported
        request method has a C{allowedMethods} attribute set to a list of the
        methods supported by the L{Resource}, as determined by the
        I{render_}-prefixed methods which it defines, if C{allowedMethods} is
        not explicitly defined by the L{Resource}.
        >   r   r€   r{   r�   N)	rb   r   r|   rw   r   r7   r   rƒ   r‚   r„   s        r   Útest_implicitAllowedMethodsz)ResourceTests.test_implicitAllowedMethods  sV   € ò -ˆÜ)Ó+ˆÜ˜rÓ"ˆØ%ˆŒØ×ÑÔ 1°8·?±?ÀGÓLˆØ×Ñ˜¤3 s×'9Ñ'9Ó#:Õ;r!   rF   )
rG   rH   rI   rJ   rr   ru   ry   r}   r†   rˆ   rM   r!   r   rh   rh   ¾   s'   „ ñó
ó5ó	Gó	=óAô<r!   rh   c                   ó(   — e Zd ZdZdd„Zdd„Zdd„Zy)ÚGetChildForRequestTestsz*
    Tests for L{getChildForRequest}.
    Nc                 ój   — t        g «      }t        «       }t        ||«      }| j                  ||«       y)zŠ
        L{getChildForRequest} returns whatever resource has been reached by the
        time the request's C{postpath} is empty.
        N)r   r	   r   r.   ©r   r;   ro   Úresults       r   Útest_exhaustedPostPathz.GetChildForRequestTests.test_exhaustedPostPath  s1   € ô
 ˜rÓ"ˆÜ“:ˆÜ# H¨gÓ6ˆØ×Ñ˜X vÕ.r!   c                 ó|   — t        ddg«      }t        «       }d|_        t        ||«      }| j	                  ||«       y)zƒ
        L{getChildForRequest} returns the first resource it encounters with a
        C{isLeaf} attribute set to C{True}.
        rj   rk   TN)r   r	   ÚisLeafr   r.   rŒ   s       r   Útest_leafResourcez)GetChildForRequestTests.test_leafResource"  s=   € ô
  ¨Ð/Ó0ˆÜ“:ˆØˆŒÜ# H¨gÓ6ˆØ×Ñ˜X vÕ.r!   c                 ó$  — t        ddg«      }t        «       }t        «       }d|_        |j                  d|«       | j	                  |t        ||«      «       | j                  |j                  dg«       | j                  |j                  dg«       y)zƒ
        As path segments from the request are traversed, they are taken from
        C{postpath} and put into C{prepath}.
        rj   rk   TN)	r   r	   r�   rl   r.   r   r   ÚprepathÚpostpath)r   r;   Úrootrp   s       r   Útest_postPathToPrePathz.GetChildForRequestTests.test_postPathToPrePath-  s|   € ô
  ¨Ð/Ó0ˆÜ‹zˆÜ“
ˆØˆŒØ�‰�f˜eÔ$Ø×Ñ˜UÔ$6°t¸WÓ$EÔFØ×Ñ˜Ÿ™¨6¨(Ô3Ø×Ñ˜×)Ñ)¨F¨8Õ4r!   rF   )rG   rH   rI   rJ   rŽ   r‘   r–   rM   r!   r   rŠ   rŠ     s   „ ñó/ó	/ô5r!   rŠ   N)rJ   Útwisted.trial.unittestr   Útwisted.web.errorr   Útwisted.web.http_headersr   Útwisted.web.iwebr   r   r   r   r	   r
   r   r   r'   r   r#   r   Útwisted.web.test.requesthelperr   r   rO   rW   rZ   rb   rh   rŠ   rM   r!   r   ú<module>rœ      sˆ   ðñõ ,Ý /Ý ,Ý %÷÷ ñ õ 8ômO�Xô mOô`�8ô ô+�hô +ô˜hô ô*
˜Xô 
ôR<�Hô R<ôj&5˜hõ &5r!   