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   zException 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.
        zException 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