o
    ¯bJ>  ã                   @   sò   d Z ddlZddl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 G dd„ dejƒZG d	d
„ d
ejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZdS )z
HTTP errors.
é    N)ÚnativeString)Úunittest)Úerror)ÚTagc                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚCodeToMessageTestsz<
    L{_codeToMessages} inverts L{_responses.RESPONSES}
    c                 C   s   t  d¡}|  |d¡ d S )Nó   302s   Found©r   Ú_codeToMessageÚassertEqual©ÚselfÚm© r   ú=/usr/lib/python3/dist-packages/twisted/web/test/test_error.pyÚtest_validCode   ó   
z!CodeToMessageTests.test_validCodec                 C   ó   t  d¡}|  |d ¡ d S )Ns   987r   r   r   r   r   Útest_invalidCode   r   z#CodeToMessageTests.test_invalidCodec                 C   r   )Nó   InvalidCoder   r   r   r   r   Útest_nonintegerCode    r   z&CodeToMessageTests.test_nonintegerCodeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r      s
    r   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )Ú
ErrorTestsz<
    Tests for how L{Error} attributes are initialized.
    c                 C   ó   t  d¡}|  |jd¡ dS )zâ
        If no C{message} argument is passed to the L{Error} constructor and the
        C{code} argument is a valid HTTP status code, C{code} is mapped to a
        descriptive string to which C{message} is assigned.
        ó   200ó   OKN©r   ÚErrorr
   Úmessage©r   Úer   r   r   Útest_noMessageValidStatus*   s   
z$ErrorTests.test_noMessageValidStatusc                 C   s   t  d¡}|  |jd¡ dS )zŸ
        If no C{message} argument is passed to the L{Error} constructor and
        C{code} isn't a valid HTTP status code, C{message} stays L{None}.
        r   Nr   r!   r   r   r   Útest_noMessageInvalidStatus3   s   
z&ErrorTests.test_noMessageInvalidStatusc                 C   ó   t  dd¡}|  |jd¡ dS )z’
        If a C{message} argument is passed to the L{Error} constructor, the
        C{message} isn't affected by the value of C{status}.
        r   ó   My own messageNr   r!   r   r   r   Útest_messageExists;   s   zErrorTests.test_messageExistsc                 C   s<   t  dd¡}|  t|ƒd¡ t  dd¡}|  t|ƒd¡ dS )zh
        C{str()} on an L{Error} returns the code and message it was
        instantiated with.
        r   r   z200 OKéÈ   N)r   r   r
   Ústrr!   r   r   r   Útest_strC   s   zErrorTests.test_strN)r   r   r   r   r#   r$   r'   r*   r   r   r   r   r   %   s    	r   c                   @   ó8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚPageRedirectTestszC
    Tests for how L{PageRedirect} attributes are initialized.
    c                 C   ó    t jddd�}|  |jd¡ dS )zé
        If no C{message} argument is passed to the L{PageRedirect} constructor
        and the C{code} argument is a valid HTTP status code, C{code} is mapped
        to a descriptive string to which C{message} is assigned.
        r   ó   /foo©Úlocationó
   OK to /fooN©r   ÚPageRedirectr
   r    r!   r   r   r   r#   V   ó   z+PageRedirectTests.test_noMessageValidStatusc                 C   r   )a;  
        If no C{message} argument is passed to the L{PageRedirect} constructor
        and C{location} is also empty and the C{code} argument is a valid HTTP
        status code, C{code} is mapped to a descriptive string to which
        C{message} is assigned without trying to include an empty location.
        r   r   Nr2   r!   r   r   r   Ú#test_noMessageValidStatusNoLocation_   s   
z5PageRedirectTests.test_noMessageValidStatusNoLocationc                 C   ó    t jddd�}|  |jd¡ dS )z¦
        If no C{message} argument is passed to the L{PageRedirect} constructor
        and C{code} isn't a valid HTTP status code, C{message} stays L{None}.
        r   r.   r/   Nr2   r!   r   r   r   Ú)test_noMessageInvalidStatusLocationExistsi   s   z;PageRedirectTests.test_noMessageInvalidStatusLocationExistsc                 C   ó"   t jdddd�}|  |jd¡ dS )z™
        If a C{message} argument is passed to the L{PageRedirect} constructor,
        the C{message} isn't affected by the value of C{status}.
        r   r&   r.   r/   ó   My own message to /fooNr2   r!   r   r   r   Ú test_messageExistsLocationExistsq   ó   z2PageRedirectTests.test_messageExistsLocationExistsc                 C   r%   )zº
        If a C{message} argument is passed to the L{PageRedirect} constructor
        and no location is provided, C{message} doesn't try to include the
        empty location.
        r   r&   Nr2   r!   r   r   r   Útest_messageExistsNoLocationy   ó   z.PageRedirectTests.test_messageExistsNoLocationN©	r   r   r   r   r#   r5   r7   r:   r<   r   r   r   r   r,   Q   s    	
r,   c                   @   r+   )ÚInfiniteRedirectionTestszJ
    Tests for how L{InfiniteRedirection} attributes are initialized.
    c                 C   r-   )zø
        If no C{message} argument is passed to the L{InfiniteRedirection}
        constructor and the C{code} argument is a valid HTTP status code,
        C{code} is mapped to a descriptive string to which C{message} is
        assigned.
        r   r.   r/   r1   N©r   ÚInfiniteRedirectionr
   r    r!   r   r   r   r#   ˆ   s   z2InfiniteRedirectionTests.test_noMessageValidStatusc                 C   r   )aJ  
        If no C{message} argument is passed to the L{InfiniteRedirection}
        constructor and C{location} is also empty and the C{code} argument is a
        valid HTTP status code, C{code} is mapped to a descriptive string to
        which C{message} is assigned without trying to include an empty
        location.
        r   r   Nr@   r!   r   r   r   r5   ’   s   
z<InfiniteRedirectionTests.test_noMessageValidStatusNoLocationc                 C   r6   )zµ
        If no C{message} argument is passed to the L{InfiniteRedirection}
        constructor and C{code} isn't a valid HTTP status code, C{message} stays
        L{None}.
        r   r.   r/   Nr@   r!   r   r   r   r7   �   r4   zBInfiniteRedirectionTests.test_noMessageInvalidStatusLocationExistsc                 C   r8   )z 
        If a C{message} argument is passed to the L{InfiniteRedirection}
        constructor, the C{message} isn't affected by the value of C{status}.
        r   r&   r.   r/   r9   Nr@   r!   r   r   r   r:   ¦   r;   z9InfiniteRedirectionTests.test_messageExistsLocationExistsc                 C   r%   )zÁ
        If a C{message} argument is passed to the L{InfiniteRedirection}
        constructor and no location is provided, C{message} doesn't try to
        include the empty location.
        r   r&   Nr@   r!   r   r   r   r<   ®   r=   z5InfiniteRedirectionTests.test_messageExistsNoLocationNr>   r   r   r   r   r?   ƒ   s    
	r?   c                   @   ó   e Zd ZdZdd„ ZdS )ÚRedirectWithNoLocationTestszq
    L{RedirectWithNoLocation} is a subclass of L{Error} which sets
    a custom message in the constructor.
    c                 C   s.   t  ddd¡}|  |jd¡ |  |jd¡ dS )z»
        When C{code}, C{message}, and C{uri} are passed to the
        L{RedirectWithNoLocation} constructor, the C{message} and C{uri}
        attributes are set, respectively.
        r   s   REDIRECTs   https://example.coms   REDIRECT to https://example.comN)r   ÚRedirectWithNoLocationr
   r    Úurir!   r   r   r   Útest_validMessage¾   s   z-RedirectWithNoLocationTests.test_validMessageN)r   r   r   r   rF   r   r   r   r   rC   ¸   s    rC   c                   @   ó    e Zd ZdZdd„ Zdd„ ZdS )ÚMissingRenderMethodTestsz\
    Tests for how L{MissingRenderMethod} exceptions are initialized and
    displayed.
    c                 C   s2   t ƒ }t |d¡}|  |j|¡ |  |jd¡ dS )zª
        Given C{element} and C{renderName} arguments, the
        L{MissingRenderMethod} constructor assigns the values to the
        corresponding attributes.
        ÚrenderThingN)Úobjectr   ÚMissingRenderMethodÚassertIsÚelementÚ
renderName©r   Úeltr"   r   r   r   Útest_constructorÏ   s   z)MissingRenderMethodTests.test_constructorc                 C   s*   t ƒ }t |d¡}|  t|ƒd| ¡ dS )z”
        A L{MissingRenderMethod} is represented using a custom string
        containing the element's representation and the method name.
        rI   zB'MissingRenderMethod': %r had no render method named 'renderThing'N)rJ   r   rK   r
   ÚreprrO   r   r   r   Ú	test_reprÚ   s   ÿþz"MissingRenderMethodTests.test_reprN©r   r   r   r   rQ   rS   r   r   r   r   rH   É   s    rH   c                   @   rG   )ÚMissingTemplateLoaderTestsz^
    Tests for how L{MissingTemplateLoader} exceptions are initialized and
    displayed.
    c                 C   s"   t ƒ }t |¡}|  |j|¡ dS )z’
        Given an C{element} argument, the L{MissingTemplateLoader} constructor
        assigns the value to the corresponding attribute.
        N)rJ   r   ÚMissingTemplateLoaderrL   rM   rO   r   r   r   rQ   î   s   
z+MissingTemplateLoaderTests.test_constructorc                 C   s(   t ƒ }t |¡}|  t|ƒd| ¡ dS )z–
        A L{MissingTemplateLoader} is represented using a custom string
        containing the element's representation and the method name.
        z)'MissingTemplateLoader': %r had no loaderN)rJ   r   rV   r
   rR   rO   r   r   r   rS   ÷   s   
z$MissingTemplateLoaderTests.test_reprNrT   r   r   r   r   rU   è   s    	rU   c                   @   sŒ   e Zd ZdZg fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd S )!ÚFlattenerErrorTestsz&
    Tests for L{FlattenerError}.
    c              
   C   sN   zt dƒ‚ ty& } zt t ¡ d ¡}t |||¡W  Y d }~S d }~ww )Núoh noesé   )ÚRuntimeErrorÚ	ExceptionÚ	tracebackÚ
extract_tbÚsysÚexc_infor   ÚFlattenerError)r   Úrootsr"   Útbr   r   r   ÚmakeFlattenerError  s   €þz&FlattenerErrorTests.makeFlattenerErrorc                 C   s   d| S )NzR(%s)r   )r   Úobjr   r   r   ÚfakeFormatRoot  s   z"FlattenerErrorTests.fakeFormatRootc                 C   s&   | j ddgd�}|  |jddg¡ dS )z«
        Given C{exception}, C{roots}, and C{traceback} arguments, the
        L{FlattenerError} constructor assigns the roots to the C{_roots}
        attribute.
        ÚaÚb)ra   N)rc   r
   Ú_rootsr!   r   r   r   rQ     s   z$FlattenerErrorTests.test_constructorc                 C   s    |   ¡ }|  t|ƒt|ƒ¡ dS )zd
        The string form of a L{FlattenerError} is identical to its
        representation.
        N)rc   r
   r)   rR   r!   r   r   r   r*     s   zFlattenerErrorTests.test_strc                 C   s@   |   ddg¡}| j|_|  t dt|ƒtjtjB ¡t|ƒ¡ dS )z×
        The representation of a L{FlattenerError} initialized with roots and a
        traceback contains a formatted representation of those roots (using
        C{_formatRoot}) and a formatted traceback.
        rf   rg   zœException while flattening:
  R\(a\)
  R\(b\)
  File "[^"]*", line [0-9]*, in makeFlattenerError
    raise RuntimeError\("oh noes"\)
RuntimeError: oh noes
$N)	rc   re   Ú_formatRootÚ
assertTrueÚreÚmatchrR   ÚMÚSr!   r   r   r   Ú"test_reprWithRootsAndWithTraceback!  s   
ø
õz6FlattenerErrorTests.test_reprWithRootsAndWithTracebackc                 C   s4   |   g ¡}|  t dt|ƒtjtjB ¡t|ƒ¡ dS )úŸ
        The representation of a L{FlattenerError} initialized without roots but
        with a traceback contains a formatted traceback but no roots.
        zŠException while flattening:
  File "[^"]*", line [0-9]*, in makeFlattenerError
    raise RuntimeError\("oh noes"\)
RuntimeError: oh noes
$N)rc   rj   rk   rl   rR   rm   rn   r!   r   r   r   Ú%test_reprWithoutRootsAndWithTraceback7  s   

ú÷z9FlattenerErrorTests.test_reprWithoutRootsAndWithTracebackc                 C   s<   t  tdƒg d¡}|  t dt|ƒtjtjB ¡t|ƒ¡ dS )rp   rX   Nz3Exception while flattening:
RuntimeError: oh noes
$)	r   r`   rZ   rj   rk   rl   rR   rm   rn   r!   r   r   r   Ú(test_reprWithoutRootsAndWithoutTracebackI  s   
ýúz<FlattenerErrorTests.test_reprWithoutRootsAndWithoutTracebackc                 C   s&   |   ¡ }|  | tdƒ¡tdƒ¡ dS )zk
        The C{_formatRoot} method formats a short unicode string using the
        built-in repr.
        ÚabcdN)rc   r
   ri   r   rR   r!   r   r   r   Ú!test_formatRootShortUnicodeStringX  s   z5FlattenerErrorTests.test_formatRootShortUnicodeStringc                 C   s*   |   ¡ }tdƒ}|  | |¡tdƒ¡ dS )z{
        The C{_formatRoot} method formats a long unicode string using the
        built-in repr with an ellipsis.
        zxabcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-z-abcde-abcde-abcde-ab<...>e-abcde-abcde-abcde-N)rc   r   r
   ri   rR   ©r   r"   Ú
longStringr   r   r   Ú test_formatRootLongUnicodeString`  s   þz4FlattenerErrorTests.test_formatRootLongUnicodeStringc                 C   s"   |   ¡ }|  | d¡tdƒ¡ dS )zh
        The C{_formatRoot} method formats a short byte string using the
        built-in repr.
        s   abcdN©rc   r
   ri   rR   r!   r   r   r   Útest_formatRootShortByteStringl  ó   z2FlattenerErrorTests.test_formatRootShortByteStringc                 C   s&   |   ¡ }d}|  | |¡tdƒ¡ dS )zx
        The C{_formatRoot} method formats a long byte string using the
        built-in repr with an ellipsis.
        sx   abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-abcde-s-   abcde-abcde-abcde-ab<...>e-abcde-abcde-abcde-Nrx   ru   r   r   r   Útest_formatRootLongByteStringt  s   þz1FlattenerErrorTests.test_formatRootLongByteStringc                 C   s"   |   ¡ }|  | tdƒ¡d¡ dS )zu
        The C{_formatRoot} method formats a C{Tag} with no filename information
        as 'Tag <tagName>'.
        úa-tagzTag <a-tag>N)rc   r
   ri   r   r!   r   r   r   Útest_formatRootTagNoFilename€  rz   z0FlattenerErrorTests.test_formatRootTagNoFilenamec                 C   s.   |   ¡ }tddddd�}|  | |¡d¡ dS )z”
        The C{_formatRoot} method formats a C{Tag} with filename information
        using the filename, line, column, and tag information
        r|   ztpl.pyé
   é   )ÚfilenameÚ
lineNumberÚcolumnNumberz-File "tpl.py", line 10, column 20, in "a-tag"N)rc   r   r
   ri   )r   r"   Útr   r   r   Útest_formatRootTagWithFilenameˆ  s
   
ÿz2FlattenerErrorTests.test_formatRootTagWithFilenamec                 C   óD   |   tt tdƒdgg ¡ƒd¡ |   tt tdƒdgg ¡ƒd¡ dS )zº
        If a L{FlattenerError} is created with a string root, up to around 40
        bytes from that string are included in the string representation of the
        exception.
        ÚreasonÚ	abc123xyzz?Exception while flattening:
  'abc123xyz'
RuntimeError: reason
Úd0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789zcException while flattening:
  '01234567890123456789<...>01234567890123456789'
RuntimeError: reason
N©r
   r)   r   r`   rZ   ©r   r   r   r   Útest_string“  s   þþzFlattenerErrorTests.test_stringc                 C   r…   )zÀ
        If a L{FlattenerError} is created with a unicode root, up to around 40
        characters from that string are included in the string representation
        of the exception.
        r†   u	   abcâ˜ƒxyzzBException while flattening:
  'abc\u2603xyz'
RuntimeError: reason
ux   01234567â˜ƒ901234567â˜ƒ901234567â˜ƒ901234567â˜ƒ901234567â˜ƒ901234567â˜ƒ901234567â˜ƒ901234567â˜ƒ901234567â˜ƒ901234567â˜ƒ9zwException while flattening:
  '01234567\u2603901234567\u26039<...>01234567\u2603901234567\u26039'
RuntimeError: reason
Nr‰   rŠ   r   r   r   Útest_unicode¥  s   ÿüÿÿúz FlattenerErrorTests.test_unicodeN)r   r   r   r   rc   re   rQ   r*   ro   rq   rr   rt   rw   ry   r{   r}   r„   r‹   rŒ   r   r   r   r   rW     s"    	rW   c                   @   rB   )ÚUnsupportedMethodTestsz)
    Tests for L{UnsupportedMethod}.
    c                 C   s"   t  ddg¡}|  t|ƒd¡ dS )z´
        The C{__str__} for L{UnsupportedMethod} makes it clear that what it
        shows is a list of the supported methods, not the method that was
        unsupported.
        s   HEADs   PATCHz#Expected one of [b'HEAD', b'PATCH']N)r   ÚUnsupportedMethodr
   r)   r!   r   r   r   r*   Æ  s
   þzUnsupportedMethodTests.test_strN)r   r   r   r   r*   r   r   r   r   r�   Á  s    r�   )r   rk   r^   r\   Útwisted.python.compatr   Útwisted.trialr   Útwisted.webr   Útwisted.web.templater   ÚTestCaser   r   r,   r?   rC   rH   rU   rW   ÚSynchronousTestCaser�   r   r   r   r   Ú<module>   s$   ,25 A