U
    n5{i}A  ã                   @  sþ   d Z ddlm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.
é    )ÚannotationsN)ÚnativeString)Úunittest)Úerror)ÚTagc                   @  s:   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zd
S )ÚCodeToMessageTestsz<
    L{_codeToMessages} inverts L{_responses.RESPONSES}
    ÚNone©Úreturnc                 C  s   t  d¡}|  |d¡ d S )Nó   302s   Found©r   Z_codeToMessageÚassertEqual©ÚselfÚm© r   ú?/tmp/pip-unpacked-wheel-mnhg7r0q/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 )Ns   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                   @  sV   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„Zddœdd„ZdS )Ú
ErrorTestsz<
    Tests for how L{Error} attributes are initialized.
    r   r	   c                 C  s,   t  d¡}|  |jd¡ |  t|ƒ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{message} is set to the
        HTTP reason phrase for C{code}.
        ó   200ó   OKú200 OKN©r   ÚErrorr   ÚmessageÚstr©r   Úer   r   r   Útest_noMessageValidStatus+   s    
z$ErrorTests.test_noMessageValidStatusc                 C  s,   t  d¡}|  |jd¡ |  t|ƒd¡ dS )zŸ
        If no C{message} argument is passed to the L{Error} constructor and
        C{code} isn't a known HTTP status code, C{message} stays L{None}.
        ó   999NÚ999r   r"   r   r   r   Útest_noMessageForStatus5   s    
z"ErrorTests.test_noMessageForStatusc              	   C  s$   |   t¡� t d¡ W 5 Q R X dS )z†
        If C{code} isn't plausibly an HTTP status code (i.e., composed of
        digits) it is rejected with L{ValueError}.
        s   InvalidStatusN)ÚassertRaisesÚ
ValueErrorr   r   ©r   r   r   r   Útest_invalidStatus>   s    zErrorTests.test_invalidStatusc                 C  s.   t  dd¡}|  |jd¡ |  t|ƒ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 messagez200 My own messageNr   r"   r   r   r   Útest_messageExistsF   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   r   éÈ   N)r   r   r   r!   r"   r   r   r   Útest_strO   s    zErrorTests.test_strN)	r   r   r   r   r$   r'   r+   r-   r/   r   r   r   r   r   &   s   
		r   c                   @  sV   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„Zddœdd„ZdS )ÚPageRedirectTestszC
    Tests for how L{PageRedirect} attributes are initialized.
    r   r	   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$   b   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   Nr5   r"   r   r   r   Ú#test_noMessageValidStatusNoLocationk   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%   r1   r2   Nr5   r"   r   r   r   Ú)test_noMessageInvalidStatusLocationExistsu   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,   r1   r2   ó   My own message to /fooNr5   r"   r   r   r   Ú test_messageExistsLocationExists}   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,   Nr5   r"   r   r   r   Útest_messageExistsNoLocation…   s    z.PageRedirectTests.test_messageExistsNoLocationN©	r   r   r   r   r$   r6   r7   r9   r:   r   r   r   r   r0   ]   s   	
r0   c                   @  sV   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„Zddœdd„ZdS )ÚInfiniteRedirectionTestszJ
    Tests for how L{InfiniteRedirection} attributes are initialized.
    r   r	   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   r1   r2   r4   N©r   Ú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   Nr=   r"   r   r   r   r6   ž   s    
z<InfiniteRedirectionTests.test_noMessageValidStatusNoLocationc                 C  s0   t jddd�}|  |jd¡ |  t|ƒ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%   r1   r2   Nr&   )r   r>   r   r    r!   r"   r   r   r   r7   ©   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,   r1   r2   r8   Nr=   r"   r   r   r   r9   ³   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,   Nr=   r"   r   r   r   r:   »   s    z5InfiniteRedirectionTests.test_messageExistsNoLocationNr;   r   r   r   r   r<   �   s   

r<   c                   @  s   e Zd ZdZddœdd„ZdS )ÚRedirectWithNoLocationTestszq
    L{RedirectWithNoLocation} is a subclass of L{Error} which sets
    a custom message in the constructor.
    r   r	   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    Úurir"   r   r   r   Útest_validMessageË   s    z-RedirectWithNoLocationTests.test_validMessageN)r   r   r   r   rA   r   r   r   r   r?   Å   s   r?   c                   @  s,   e Zd ZdZddœdd„Zddœdd„ZdS )	ÚMissingRenderMethodTestsz\
    Tests for how L{MissingRenderMethod} exceptions are initialized and
    displayed.
    r   r	   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   Ú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.
        rC   zB'MissingRenderMethod': %r had no render method named 'renderThing'N)rD   r   rE   r   ÚreprrH   r   r   r   Ú	test_reprç   s    ÿþz"MissingRenderMethodTests.test_reprN©r   r   r   r   rJ   rL   r   r   r   r   rB   Ö   s   rB   c                   @  s,   e Zd ZdZddœdd„Zddœdd„ZdS )	ÚMissingTemplateLoaderTestsz^
    Tests for how L{MissingTemplateLoader} exceptions are initialized and
    displayed.
    r   r	   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)rD   r   ÚMissingTemplateLoaderrF   rG   rH   r   r   r   rJ   û   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)rD   r   rO   r   rK   rH   r   r   r   rL     s    
z$MissingTemplateLoaderTests.test_reprNrM   r   r   r   r   rN   õ   s   	rN   c                   @  sê   e Zd ZdZg fdddœdd„Zddd	œd
d„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Z	ddœdd„Z
ddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœd d!„Zddœd"d#„Zddœd$d%„Zddœd&d'„Zd(S ))ÚFlattenerErrorTestsz&
    Tests for L{FlattenerError}.
    zlist[object]zerror.FlattenerError)Úrootsr
   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   rQ   r#   Útbr   r   r   ÚmakeFlattenerError  s
    z&FlattenerErrorTests.makeFlattenerErrorrD   r!   )Úobjr
   c                 C  s   d| S )NzR(%s)r   )r   r]   r   r   r   ÚfakeFormatRoot  s    z"FlattenerErrorTests.fakeFormatRootr   r	   c                 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)rQ   N)r\   r   Z_rootsr"   r   r   r   rJ     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)r\   r   r!   rK   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.
        r_   r`   zœException while flattening:
  R\(a\)
  R\(b\)
  File "[^"]*", line [0-9]*, in makeFlattenerError
    raise RuntimeError\("oh noes"\)
RuntimeError: oh noes
$N)	r\   r^   Ú_formatRootÚ
assertTrueÚreÚmatchrK   Ú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)r\   rb   rc   rd   rK   re   rf   r"   r   r   r   Ú%test_reprWithoutRootsAndWithTracebackD  s    

ú÷z9FlattenerErrorTests.test_reprWithoutRootsAndWithTracebackc                 C  s<   t  tdƒg d¡}|  t dt|ƒtjtjB ¡t|ƒ¡ dS )rh   rR   Nz3Exception while flattening:
RuntimeError: oh noes
$)	r   rZ   rT   rb   rc   rd   rK   re   rf   r"   r   r   r   Ú(test_reprWithoutRootsAndWithoutTracebackV  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)r\   r   ra   r   rK   r"   r   r   r   Ú!test_formatRootShortUnicodeStringe  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)r\   r   r   ra   rK   ©r   r#   Z
longStringr   r   r   Ú test_formatRootLongUnicodeStringm  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©r\   r   ra   rK   r"   r   r   r   Útest_formatRootShortByteStringy  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-Nrn   rl   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)r\   r   ra   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
        rq   ztpl.pyé
   é   )ÚfilenameZ
lineNumberZcolumnNumberz-File "tpl.py", line 10, column 20, in "a-tag"N)r\   r   r   ra   )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   rZ   rT   r*   r   r   r   Útest_string   s    þþzFlattenerErrorTests.test_stringc                 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 unicode root, up to around 40
        characters from that string are included in the string representation
        of the exception.
        rx   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
Nry   r*   r   r   r   Útest_unicode²  s     ÿü  ÿÿúz FlattenerErrorTests.test_unicodeN)r   r   r   r   r\   r^   rJ   r/   rg   ri   rj   rk   rm   ro   rp   rr   rw   rz   r{   r   r   r   r   rP     s    	rP   c                   @  s   e Zd ZdZddœdd„ZdS )ÚUnsupportedMethodTestsz)
    Tests for L{UnsupportedMethod}.
    r   r	   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   Z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   Ú
__future__r   rc   rX   rV   Ztwisted.python.compatr   Ztwisted.trialr   Ztwisted.webr   Ztwisted.web.templater   ZTestCaser   r   r0   r<   r?   rB   rN   rP   ZSynchronousTestCaser|   r   r   r   r   Ú<module>   s$   726 A