U
    
W[R?  ã                   @   s  d Z ddlmZmZ ddlZddlZddlZddlmZ ddl	m
Z
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.
é    )ÚdivisionÚabsolute_importN)Úunittest)ÚnativeStringÚ_PY3)Ú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   Z_codeToMessageÚassertEqual©ÚselfÚm© r   ú=/usr/lib/python3/dist-packages/twisted/web/test/test_error.pyÚtest_validCode   s    
z!CodeToMessageTests.test_validCodec                 C   s   t  d¡}|  |d ¡ d S )Ns   987r   r   r   r   r   Útest_invalidCode   s    
z#CodeToMessageTests.test_invalidCodec                 C   s   t  d¡}|  |d ¡ d S )Nó   InvalidCoder   r   r   r   r   Útest_nonintegerCode#   s    
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   s   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_noMessageInvalidStatus7   s    
z&ErrorTests.test_noMessageInvalidStatusc                 C   s   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_strI   s    zErrorTests.test_strN)r   r   r   r   r"   r#   r%   r(   r   r   r   r   r   )   s
   
		r   c                   @   s8   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   s    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   ZPageRedirectr   r   r    r   r   r   r"   \   s    z+PageRedirectTests.test_noMessageValidStatusc                 C   s   t  d¡}|  |jd¡ dS )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   Nr.   r    r   r   r   Ú#test_noMessageValidStatusNoLocationf   s    
z5PageRedirectTests.test_noMessageValidStatusNoLocationc                 C   s    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+   Nr.   r    r   r   r   Ú)test_noMessageInvalidStatusLocationExistsq   s    z;PageRedirectTests.test_noMessageInvalidStatusLocationExistsc                 C   s"   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 /fooNr.   r    r   r   r   Ú test_messageExistsLocationExistsz   s    z2PageRedirectTests.test_messageExistsLocationExistsc                 C   s   t  dd¡}|  |jd¡ dS )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$   Nr.   r    r   r   r   Útest_messageExistsNoLocationƒ   s    z.PageRedirectTests.test_messageExistsNoLocationN©	r   r   r   r   r"   r/   r0   r2   r3   r   r   r   r   r)   X   s   
		r)   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚInfiniteRedirectionTestszJ
    Tests for how L{InfiniteRedirection} attributes are initialized.
    c                 C   s    t jddd}|  |jd¡ dS )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+   r-   N©r   ZInfiniteRedirectionr   r   r    r   r   r   r"   ’   s    z2InfiniteRedirectionTests.test_noMessageValidStatusc                 C   s   t  d¡}|  |jd¡ dS )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   Nr6   r    r   r   r   r/      s    
z<InfiniteRedirectionTests.test_noMessageValidStatusNoLocationc                 C   s    t jddd}|  |jd¡ dS )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+   Nr6   r    r   r   r   r0   ©   s    zBInfiniteRedirectionTests.test_noMessageInvalidStatusLocationExistsc                 C   s"   t jdddd}|  |jd¡ dS )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+   r1   Nr6   r    r   r   r   r2   ³   s    ÿz9InfiniteRedirectionTests.test_messageExistsLocationExistsc                 C   s   t  dd¡}|  |jd¡ dS )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$   Nr6   r    r   r   r   r3   ½   s    z5InfiniteRedirectionTests.test_messageExistsNoLocationNr4   r   r   r   r   r5   Ž   s   

r5   c                   @   s   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   ZRedirectWithNoLocationr   r   Zurir    r   r   r   Útest_validMessageÍ   s
    ÿz-RedirectWithNoLocationTests.test_validMessageN)r   r   r   r   r8   r   r   r   r   r7   È   s   r7   c                   @   s    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ÚelementZ
renderName©r   Z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.
        r:   zB'MissingRenderMethod': %r had no render method named 'renderThing'N)r;   r   r<   r   Úreprr?   r   r   r   Ú	test_reprë   s    ÿþz"MissingRenderMethodTests.test_reprN©r   r   r   r   r@   rB   r   r   r   r   r9   Ú   s   r9   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú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)r;   r   ÚMissingTemplateLoaderr=   r>   r?   r   r   r   r@   þ   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)r;   r   rE   r   rA   r?   r   r   r   rB     s    
þz$MissingTemplateLoaderTests.test_reprNrC   r   r   r   r   rD   ù   s   
rD   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   sX   zt dƒ‚W nF tk
rR } z(t t ¡ d ¡}t |||¡ W Y ¢S d }~X Y nX d S )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)rP   N)rR   r   Z_rootsr    r   r   r   r@   %  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)rR   r   r'   rA   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.
        rU   rV   zœException while flattening:
  R\(a\)
  R\(b\)
  File "[^"]*", line [0-9]*, in makeFlattenerError
    raise RuntimeError\("oh noes"\)
RuntimeError: oh noes
$N)	rR   rT   Ú_formatRootÚ
assertTrueÚreÚmatchrA   ÚMÚSr    r   r   r   Ú"test_reprWithRootsAndWithTraceback8  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)rR   rX   rY   rZ   rA   r[   r\   r    r   r   r   Ú%test_reprWithoutRootsAndWithTracebackK  s    
 
üúz9FlattenerErrorTests.test_reprWithoutRootsAndWithTracebackc                 C   s<   t  tdƒg d¡}|  t dt|ƒtjtjB ¡t|ƒ¡ dS )r^   rG   Nz3Exception while flattening:
RuntimeError: oh noes
$)	r   rO   rI   rX   rY   rZ   rA   r[   r\   r    r   r   r   Ú(test_reprWithoutRootsAndWithoutTracebackZ  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.
        ZabcdN)rR   r   rW   r   rA   r    r   r   r   Ú!test_formatRootShortUnicodeStringg  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)rR   r   r   rW   rA   ©r   r!   Z
longStringr   r   r   Ú test_formatRootLongUnicodeStringp  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©rR   r   rW   rA   r    r   r   r   Útest_formatRootShortByteString{  s    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-Nrd   rb   r   r   r   Útest_formatRootLongByteString„  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)rR   r   rW   r   r    r   r   r   Útest_formatRootTagNoFilename  s    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
        rg   ztpl.pyé
   é   )ÚfilenameZ
lineNumberZcolumnNumberz-File "tpl.py", line 10, column 20, in "a-tag"N)rR   r   r   rW   )r   r!   Útr   r   r   Útest_formatRootTagWithFilename˜  s
    ÿz2FlattenerErrorTests.test_formatRootTagWithFilenamec                 C   sD   |   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.
        ÚreasonZ	abc123xyzz?Exception while flattening:
  'abc123xyz'
RuntimeError: reason
Zd0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789zcException while flattening:
  '01234567890123456789<...>01234567890123456789'
RuntimeError: reason
N)r   r'   r   rO   rI   )r   r   r   r   Útest_string£  s     ÿý  ÿýzFlattenerErrorTests.test_stringc                 C   s`   t rddinddi}|  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 unicode root, up to around 40
        characters from that string are included in the string representation
        of the exception.
        ÚuÚ rn   u	   abcâ˜ƒxyzzGException while flattening:
  %(u)s'abc\u2603xyz'
RuntimeError: reason
ux   01234567â˜ƒ901234567â˜ƒ901234567â˜ƒ901234567â˜ƒ901234567â˜ƒ901234567â˜ƒ901234567â˜ƒ901234567â˜ƒ901234567â˜ƒ901234567â˜ƒ9z|Exception while flattening:
  %(u)s'01234567\u2603901234567\u26039<...>01234567\u2603901234567\u26039'
RuntimeError: reason
N)r   r   r'   r   rO   rI   )r   rp   r   r   r   Útest_unicode¸  s*      ÿþý þüüz FlattenerErrorTests.test_unicodeN)r   r   r   r   rR   rT   r@   r(   r]   r_   r`   ra   rc   re   rf   rh   rm   ro   rr   r   r   r   r   rF     s    
				rF   c                   @   s   e Zd ZdZdd„ ZdS )ÚUnsupportedMethodTestsz)
    Tests for L{UnsupportedMethod}.
    c                 C   s6   t rdnd}t ddg¡}|  t|ƒdj|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.
        rV   rq   s   HEADs   PATCHz'Expected one of [{b}'HEAD', {b}'PATCH'])rV   N)r   r   ZUnsupportedMethodr   r'   Úformat)r   rV   r!   r   r   r   r(   ×  s     
ÿzUnsupportedMethodTests.test_strN)r   r   r   r   r(   r   r   r   r   rs   Ó  s   rs   )r   Z
__future__r   r   rY   rM   rK   Ztwisted.trialr   Ztwisted.python.compatr   r   Ztwisted.webr   Ztwisted.web.templater   ZTestCaser	   r   r)   r5   r7   r9   rD   rF   ZSynchronousTestCasers   r   r   r   r   Ú<module>   s$   /6: ?