o
    ¯büZ  ã                   @   s\   d Z ddlmZ ddlmZ ddlmZmZmZ dd„ Z	G dd„ deƒZ
G d	d
„ d
eƒZdS )z(
Tests for L{twisted.web.http_headers}.
é    )ÚTestCase)ÚHeaders)ÚbytesLinearWhitespaceComponentsÚsanitizedBytesÚtextLinearWhitespaceComponentsc                 C   sš   |D ]H}g }|  t||giƒ¡ tƒ }| ||¡ |  |¡ tƒ }| ||g¡ |  |¡ |D ]}|  t| ¡ ƒ||gfg¡ |  | |¡|g¡ q/qdS )a  
    Assert that the components are sanitized to the expected value as
    both a header name and value, across all of L{Header}'s setters
    and getters.

    @param testCase: A test case.

    @param components: A sequence of values that contain linear
        whitespace to use as header names and values; see
        C{textLinearWhitespaceComponents} and
        C{bytesLinearWhitespaceComponents}

    @param expected: The expected sanitized form of the component for
        both headers names and their values.
    N)Úappendr   ÚaddRawHeaderÚsetRawHeadersÚassertEqualÚlistÚgetAllRawHeadersÚgetRawHeaders)ÚtestCaseÚ
componentsÚexpectedÚ	componentÚheadersÚaddedÚ	setHeaderÚheader© r   úD/usr/lib/python3/dist-packages/twisted/web/test/test_http_headers.pyÚassertSanitized   s    

ÿüôr   c                   @   sØ   e Zd Z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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d0d1„ Zd2d3„ Zd4S )5ÚBytesHeadersTestszE
    Tests for L{Headers}, using L{bytes} arguments for methods.
    c                 C   ó   t | ttƒ dS ©zf
        Linear whitespace in header names or values is replaced with a
        single space.
        N)r   r   r   ©Úselfr   r   r   Útest_sanitizeLinearWhitespace:   ó   z/BytesHeadersTests.test_sanitizeLinearWhitespacec                 C   s&   t ddgiƒ}|  | d¡dg¡ dS )zx
        The header values passed to L{Headers.__init__} can be retrieved via
        L{Headers.getRawHeaders}.
        ó   Fooó   baró   fooN©r   r
   r   ©r   Úhr   r   r   Útest_initializerA   s   z"BytesHeadersTests.test_initializerc                 C   sP   ddg}t ƒ }| d|¡ |  | d¡¡ |  | d¡¡ |  | d¡|¡ dS )zŠ
        L{Headers.setRawHeaders} sets the header values for the given
        header name to the sequence of byte string values.
        ó   value1ó   value2ó   testó   TestN©r   r	   Ú
assertTrueÚ	hasHeaderr
   r   )r   ÚrawValuer%   r   r   r   Útest_setRawHeadersI   s   z$BytesHeadersTests.test_setRawHeadersc                 C   ó    t ƒ }|  t|jdddi¡ dS )zN
        L{Headers.setRawHeaders} requires values to be of type list.
        ó   keyr    r!   N©r   ÚassertRaisesÚ	TypeErrorr	   r$   r   r   r   Ú)test_rawHeadersTypeCheckingValuesIterableU   ó   z;BytesHeadersTests.test_rawHeadersTypeCheckingValuesIterablec                 C   s0   t ƒ }|  t|jddg¡}|  |jd d¡ dS )zf
        L{Headers.setRawHeaders} requires C{name} to be a L{bytes} or
        L{str} string.
        Nr"   r   úBHeader name is an instance of <class 'NoneType'>, not bytes or str©r   r3   r4   r	   r
   Úargs©r   r%   Úer   r   r   Útest_rawHeadersTypeCheckingName\   s   þz1BytesHeadersTests.test_rawHeadersTypeCheckingNamec                 C   s2   t ƒ }|  t|jdddg¡}|  |jd d¡ dS )zn
        L{Headers.setRawHeaders} requires values to a L{list} of L{bytes} or
        L{str} strings.
        r1   r!   Nr   zQHeader value at position 1 is an instance of <class 'NoneType'>, not bytes or strr8   r:   r   r   r   Ú*test_rawHeadersTypeCheckingValuesAreStringh   s   þz<BytesHeadersTests.test_rawHeadersTypeCheckingValuesAreStringc                 C   sL   t ƒ }| dd¡ |  | d¡dg¡ | dd¡ |  | d¡ddg¡ dS )úN
        L{Headers.addRawHeader} adds a new value for a given header.
        r)   ó   lemuró   pandaN©r   r   r
   r   r$   r   r   r   Útest_addRawHeaderu   s
   z#BytesHeadersTests.test_addRawHeaderc                 C   s.   t ƒ }|  t|jdd¡}|  |jd d¡ dS )ze
        L{Headers.addRawHeader} requires C{name} to be a L{bytes} or L{str}
        string.
        Nr"   r   r7   ©r   r3   r4   r   r
   r9   r:   r   r   r   Útest_addRawHeaderTypeCheckName   ó   þz0BytesHeadersTests.test_addRawHeaderTypeCheckNamec                 C   s.   t ƒ }|  t|jdd¡}|  |jd d¡ dS )zc
        L{Headers.addRawHeader} requires value to be a L{bytes} or L{str}
        string.
        r1   Nr   zCHeader value is an instance of <class 'NoneType'>, not bytes or strrC   r:   r   r   r   Útest_addRawHeaderTypeCheckValue‹   rE   z1BytesHeadersTests.test_addRawHeaderTypeCheckValuec                 C   ó   |   tƒ  d¡¡ dS )úz
        L{Headers.getRawHeaders} returns L{None} if the header is not found and
        no default is specified.
        r)   N©ÚassertIsNoner   r   r   r   r   r   Útest_getRawHeadersNoDefault—   ó   z-BytesHeadersTests.test_getRawHeadersNoDefaultc                 C   s$   t ƒ }tƒ }|  | d|¡|¡ dS )úo
        L{Headers.getRawHeaders} returns the specified default value when no
        header is found.
        r)   N)r   ÚobjectÚassertIdenticalr   ©r   r%   Údefaultr   r   r   Útest_getRawHeadersDefaultValuež   s   z0BytesHeadersTests.test_getRawHeadersDefaultValuec                 C   sJ   t ƒ }dg}| d|¡ |  | d|¡d t¡ |  | d|¡dg¡ dS )zÏ
        If the object passed as the value list to L{Headers.setRawHeaders}
        is later passed as a default to L{Headers.getRawHeaders}, the
        result nevertheless contains encoded values.
        Úvaluer1   r   ó   valueN)r   r	   ÚassertIsInstancer   Úbytesr
   rP   r   r   r   Ú*test_getRawHeadersWithDefaultMatchingValue§   ó
   z<BytesHeadersTests.test_getRawHeadersWithDefaultMatchingValuec                 C   s@   t ƒ }| ddg¡ |  | d¡dg¡ |  | d¡dg¡ dS )úm
        L{Headers.getRawHeaders} returns the values which have been set for a
        given header.
        r)   r?   r*   N©r   r	   r
   r   r$   r   r   r   Útest_getRawHeaders³   s   z$BytesHeadersTests.test_getRawHeadersc                 C   s8   t ƒ }| ddg¡ |  | d¡¡ |  | d¡¡ dS )úi
        Check that L{Headers.hasHeader} returns C{True} when the given header
        is found.
        r)   r?   r*   N©r   r	   r,   r-   r$   r   r   r   Útest_hasHeaderTrue½   s   z$BytesHeadersTests.test_hasHeaderTruec                 C   rG   )úc
        L{Headers.hasHeader} returns C{False} when the given header is not
        found.
        r)   N©ÚassertFalser   r-   r   r   r   r   Útest_hasHeaderFalseÇ   rL   z%BytesHeadersTests.test_hasHeaderFalsec                 C   sz   t ƒ }| ddg¡ |  | d¡¡ | d¡ |  | d¡¡ | ddg¡ |  | d¡¡ | d¡ |  | d¡¡ dS )úN
        Check that L{Headers.removeHeader} removes the given header.
        r"   r?   r!   r@   s   BarN©r   r	   r,   r-   ÚremoveHeaderra   r$   r   r   r   Útest_removeHeaderÎ   s   

z#BytesHeadersTests.test_removeHeaderc                 C   ó(   t ƒ }| d¡ |  t| ¡ ƒg ¡ dS )úk
        L{Headers.removeHeader} is a no-operation when the specified header is
        not found.
        r)   N©r   re   r
   r   r   r$   r   r   r   Útest_removeHeaderDoesntExistÞ   ó   
z.BytesHeadersTests.test_removeHeaderDoesntExistc                 C   s¬   t ƒ }|  | d¡d¡ |  | d¡d¡ |  | d¡d¡ |  | d¡d¡ |  | d	¡d
¡ |  | d¡d¡ |  | d¡d¡ |  | d¡d¡ |  | d¡d¡ dS )zr
        L{Headers._canonicalNameCaps} returns the canonical capitalization for
        the given header.
        r)   r*   s
   test-stuffs
   Test-Stuffs   content-md5ó   Content-MD5s   dnts   DNTs   etags   ETags   p3ps   P3Ps   tes   TEó   www-authenticateó   WWW-Authenticates   x-xss-protections   X-XSS-ProtectionN)r   r
   Ú_canonicalNameCapsr$   r   r   r   Útest_canonicalNameCapsç   s   z(BytesHeadersTests.test_canonicalNameCapsc                 C   sH   t ƒ }| ddg¡ | ddg¡ dd„ | ¡ D ƒ}|  |ddh¡ d	S )
úÇ
        L{Headers.getAllRawHeaders} returns an iterable of (k, v) pairs, where
        C{k} is the canonicalized representation of the header name, and C{v}
        is a sequence of values.
        r)   ó   lemursrm   ó   basic aksljdlk=c                 S   ó   h | ]
\}}|t |ƒf’qS r   ©Útuple©Ú.0ÚkÚvr   r   r   Ú	<setcomp>  ó    z:BytesHeadersTests.test_getAllRawHeaders.<locals>.<setcomp>©rn   )rs   )r*   ©rr   N©r   r	   r   r
   ©r   r%   Ú
allHeadersr   r   r   Útest_getAllRawHeaders÷   s   þz'BytesHeadersTests.test_getAllRawHeadersc                 C   sf   t ƒ }| ddg¡ t ƒ }| ddg¡ t ƒ }| dddg¡ |  ||¡ |  ||¡ |  ||¡ dS )ú
        A L{Headers} instance compares equal to itself and to another
        L{Headers} instance with the same values.
        r"   r@   r?   N©r   r	   r
   ÚassertNotEqual)r   ÚfirstÚsecondÚthirdr   r   r   Útest_headersComparison  s   z(BytesHeadersTests.test_headersComparisonc                 C   ó0   t ƒ }|  |d¡ |  |tƒ ¡ |  |d¡ dS )úf
        An instance of L{Headers} does not compare equal to other unrelated
        objects.
        r   r"   N©r   r…   rN   r$   r   r   r   Útest_otherComparison  ó   z&BytesHeadersTests.test_otherComparisonc              
   C   ó@   d}d}d}|   tt|||giƒƒd|›d|›d|›d¡ dS )	zy
        The L{repr} of a L{Headers} instance shows the names and values of all
        the headers it contains.
        r"   r!   ó   bazú	Headers({ú: [ú, ú]})N©r
   Úreprr   ©r   ÚfooÚbarÚbazr   r   r   Ú	test_repr!  s   þzBytesHeadersTests.test_reprc              
   C   r   )	z¡
        The L{repr} of a L{Headers} instance shows the names and values of all
        the headers it contains, not attempting to decode any raw bytes.
        r"   s   barás   bazár‘   r’   r“   r”   Nr•   r—   r   r   r   Útest_reprWithRawBytes.  s   þz'BytesHeadersTests.test_reprWithRawBytesc              
   C   sP   d}d}d}G dd„ dt ƒ}|  t||||giƒƒd|›d|›d|›d	¡ d
S )ú’
        The L{repr} of an instance of a subclass of L{Headers} uses the name
        of the subclass instead of the string C{"Headers"}.
        r"   r!   r   c                   @   ó   e Zd ZdS )z9BytesHeadersTests.test_subclassRepr.<locals>.FunnyHeadersN©Ú__name__Ú
__module__Ú__qualname__r   r   r   r   ÚFunnyHeadersH  ó    r£   zFunnyHeaders({r’   r“   r”   N)r   r
   r–   )r   r˜   r™   rš   r£   r   r   r   Útest_subclassRepr?  s   þz#BytesHeadersTests.test_subclassReprc                 C   sv   t ƒ }| ddg¡ | ¡ }|  | d¡dg¡ | dd¡ |  | d¡dg¡ | dd¡ |  | d¡ddg¡ dS )ú¶
        L{Headers.copy} creates a new independent copy of an existing
        L{Headers} instance, allowing future modifications without impacts
        between the copies.
        r)   r"   r!   r   N©r   r	   Úcopyr
   r   r   ©r   r%   Úir   r   r   Ú	test_copyP  s   zBytesHeadersTests.test_copyN)r    r¡   r¢   Ú__doc__r   r&   r/   r5   r<   r=   rB   rD   rF   rK   rR   rW   r[   r^   rb   rf   rj   rp   r‚   r‰   r   r›   rœ   r¥   r«   r   r   r   r   r   5   s6    
	

	
r   c                   @   sÀ   e Zd Z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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 )/ÚUnicodeHeadersTestszC
    Tests for L{Headers}, using L{str} arguments for methods.
    c                 C   r   r   )r   r   r   r   r   r   r   r   e  r   z1UnicodeHeadersTests.test_sanitizeLinearWhitespacec                 C   s:   t ddgiƒ}|  | d¡dg¡ |  | d¡dg¡ dS )aC  
        The header values passed to L{Headers.__init__} can be retrieved via
        L{Headers.getRawHeaders}. If a L{bytes} argument is given, it returns
        L{bytes} values, and if a L{str} argument is given, it returns
        L{str} values. Both are the same header value, just encoded or
        decoded.
        ÚFoor™   r"   r!   r˜   Nr#   r$   r   r   r   r&   l  s   z$UnicodeHeadersTests.test_initializerc                 C   sŠ   ddg}ddg}t ƒ }| d|¡ |  | d¡¡ |  | d¡¡ |  | d¡¡ |  | d¡¡ |  | d¡|¡ |  | d¡|¡ d	S )
zˆ
        L{Headers.setRawHeaders} sets the header values for the given
        header name to the sequence of strings, encoded.
        Úvalue1Úvalue2r'   r(   Útestr)   r*   ÚTestNr+   )r   r.   ÚrawEncodedValuer%   r   r   r   r/   x  s   z&UnicodeHeadersTests.test_setRawHeadersc                 C   sx   t ƒ }|  t¡ | ddg¡ W d  ƒ n1 sw   Y  |  t¡ | d¡ W d  ƒ dS 1 s5w   Y  dS )zÉ
        Passing L{str} to any function that takes a header name will encode
        said header name as ISO-8859-1, and if it cannot be encoded, it will
        raise a L{UnicodeDecodeError}.
        õ   â˜ƒÚvalN)r   r3   ÚUnicodeEncodeErrorr	   r-   r$   r   r   r   Útest_nameNotEncodableˆ  s   ÿ"ÿz)UnicodeHeadersTests.test_nameNotEncodablec                 C   sL   t ƒ }| ddg¡ |  | d¡¡ |  | d¡dg¡ |  | d¡¡ dS )z}
        Passing L{str} to any function that takes a header name will encode
        said header name as ISO-8859-1.
        õ   Ã¡r"   ó   áNr+   r$   r   r   r   Útest_nameEncoding—  s
   z%UnicodeHeadersTests.test_nameEncodingc                 C   s@   t ƒ }| dddg¡ |  | d¡¡ |  | d¡ddg¡ dS )z|
        Passing L{str} to L{Headers.setRawHeaders} will encode the name as
        ISO-8859-1 and values as UTF-8.
        r¸   r´   r"   r¹   s   â˜ƒNr+   r$   r   r   r   Útest_rawHeadersValueEncoding¨  s   z0UnicodeHeadersTests.test_rawHeadersValueEncodingc                 C   r0   )zQ
        L{Headers.setRawHeaders} requires values to be of type sequence
        Úkeyr®   r™   Nr2   r$   r   r   r   Útest_rawHeadersTypeChecking²  r6   z/UnicodeHeadersTests.test_rawHeadersTypeCheckingc                 C   sb   t ƒ }| dd¡ |  | d¡dg¡ | dd¡ |  | d¡ddg¡ |  | d¡ddg¡ dS )r>   r±   ÚlemurÚpandar)   r?   r@   NrA   r$   r   r   r   rB   ¹  s   z%UnicodeHeadersTests.test_addRawHeaderc                 C   rG   )rH   r±   NrI   r   r   r   r   rK   Ä  rL   z/UnicodeHeadersTests.test_getRawHeadersNoDefaultc                 C   sh   t ƒ }tƒ }|  | d|¡|¡ |  | dd¡d¡ |  | ddg¡dg¡ |  | ddg¡dg¡ dS )rM   r±   Nr´   )r   rN   rO   r   r
   rP   r   r   r   rR   Ë  s   þz2UnicodeHeadersTests.test_getRawHeadersDefaultValuec                 C   sJ   t ƒ }dg}| d|¡ |  | d|¡d t¡ |  | d|¡dg¡ dS )zÏ
        If the object passed as the value list to L{Headers.setRawHeaders}
        is later passed as a default to L{Headers.getRawHeaders}, the
        result nevertheless contains decoded values.
        rT   r1   r¼   r   rS   N)r   r	   rU   r   Ústrr
   rP   r   r   r   rW   Ú  rX   z>UnicodeHeadersTests.test_getRawHeadersWithDefaultMatchingValuec                 C   sh   t ƒ }| ddg¡ |  | d¡dg¡ |  | d¡dg¡ |  | d¡dg¡ |  | d¡dg¡ dS )rY   õ   testÃ¡r¾   õ   TestÃ¡ó   testár?   ó   TestáNrZ   r$   r   r   r   r[   æ  s   z&UnicodeHeadersTests.test_getRawHeadersc                 C   sX   t ƒ }| ddg¡ |  | d¡¡ |  | d¡¡ |  | d¡¡ |  | d¡¡ dS )r\   rÁ   r¾   rÂ   rÃ   rÄ   Nr]   r$   r   r   r   r^   ò  s   z&UnicodeHeadersTests.test_hasHeaderTruec                 C   rG   )r_   rÁ   Nr`   r   r   r   r   rb   þ  rL   z'UnicodeHeadersTests.test_hasHeaderFalsec                 C   sš   t ƒ }| ddg¡ |  | d¡¡ | d¡ |  | d¡¡ |  | d¡¡ | ddg¡ |  | d¡¡ | d¡ |  | d¡¡ |  | d¡¡ dS )	rc   r˜   r¾   r"   r™   r¿   ÚBarr!   Nrd   r$   r   r   r   rf     s   

z%UnicodeHeadersTests.test_removeHeaderc                 C   rg   )rh   r±   Nri   r$   r   r   r   rj     rk   z0UnicodeHeadersTests.test_removeHeaderDoesntExistc                 C   sV   t ƒ }| ddg¡ | ddg¡ | ddg¡ dd„ | ¡ D ƒ}|  |h d	£¡ d
S )rq   rÁ   Úlemurszwww-authenticatezbasic aksljdlk=zcontent-md5Úkjdfdfgdfgnsdc                 S   rt   r   ru   rw   r   r   r   r{   +  r|   z<UnicodeHeadersTests.test_getAllRawHeaders.<locals>.<setcomp>>   ©rÄ   r~   ©rl   )s   kjdfdfgdfgnsdr}   Nr   r€   r   r   r   r‚      s   þz)UnicodeHeadersTests.test_getAllRawHeadersc                 C   sÈ   t ƒ }| ddg¡ t ƒ }| ddg¡ t ƒ }| dddg¡ |  ||¡ |  ||¡ |  ||¡ t ƒ }| ddg¡ t ƒ }| ddg¡ t ƒ }| dddg¡ |  ||¡ |  ||¡ |  ||¡ dS )rƒ   õ   fooÃ¡r¿   r¾   s   fooár@   r?   Nr„   )r   r†   r‡   rˆ   Ú
firstBytesÚsecondBytesÚ
thirdBytesr   r   r   r‰   6  s$   z*UnicodeHeadersTests.test_headersComparisonc                 C   rŠ   )r‹   r   r˜   NrŒ   r$   r   r   r   r   R  rŽ   z(UnicodeHeadersTests.test_otherComparisonc              
   C   sT   d}d}d}d}d}d| }d| }|   tt|||giƒƒd ||| d¡¡¡ d	S )
z×
        The L{repr} of a L{Headers} instance shows the names and values of all
        the headers it contains. This shows only reprs of bytes values, as
        undecodable headers may cause an exception.
        rÊ   õ   barâ˜ƒrš   z	'foo\xe1'z'bar\xe2\x98\x83'ÚbzHeaders({{{}: [{}, {!r}]}})Úutf8N)r
   r–   r   ÚformatÚencode)r   r˜   r™   rš   Ú
fooEncodedÚ
barEncodedr   r   r   r›   \  s   ÿþzUnicodeHeadersTests.test_reprc              	   C   sT   d}d}d}d}d}G dd„ dt ƒ}|  t||||giƒƒd||| d	¡f ¡ d
S )r   rÊ   rÎ   rš   z
b'foo\xe1'zb'bar\xe2\x98\x83'c                   @   rž   )z;UnicodeHeadersTests.test_subclassRepr.<locals>.FunnyHeadersNrŸ   r   r   r   r   r£   {  r¤   r£   zFunnyHeaders({%s: [%s, %r]})rÐ   N)r   r
   r–   rÒ   )r   r˜   r™   rš   rÓ   rÔ   r£   r   r   r   r¥   p  s   ÿþz%UnicodeHeadersTests.test_subclassReprc                 C   s´   t ƒ }| ddg¡ | ¡ }|  | d¡dg¡ |  | d¡dg¡ | dd¡ |  | d¡dg¡ |  | d¡dg¡ | dd¡ |  | d¡ddg¡ |  | d¡ddg¡ dS )	r¦   rÁ   u   fooâ˜ƒrÃ   s   fooâ˜ƒr™   r   r!   Nr§   r©   r   r   r   r«   „  s   zUnicodeHeadersTests.test_copyN)r    r¡   r¢   r¬   r   r&   r/   r·   rº   r»   r½   rB   rK   rR   rW   r[   r^   rb   rf   rj   r‚   r‰   r   r›   r¥   r«   r   r   r   r   r­   `  s0    
	
r­   N)r¬   Útwisted.trial.unittestr   Útwisted.web.http_headersr   Útwisted.web.test.requesthelperr   r   r   r   r   r­   r   r   r   r   Ú<module>   s   #  -