HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib/python3/dist-packages/twisted/web/test/__pycache__/test_static.cpython-38.pyc
U


W[	�@s�dZddlZddlZddlZddlZddlZddlZddlZddlm	Z
ddlmZddl
mZmZddlmZddlmZddlmZmZdd	lmZmZdd
lmZddlmZmZmZm Z ddl!m"Z"dd
l#m$Z$ddl%m&Z&ddl'm(Z(Gdd�de�Z)Gdd�de�Z*Gdd�de�Z+Gdd�de�Z,Gdd�de�Z-Gdd�de�Z.Gdd�de�Z/Gdd�de�Z0Gd d!�d!e�Z1Gd"d#�d#e�Z2Gd$d%�d%e�Z3dS)&z"
Tests for L{twisted.web.static}.
�N)�BytesIO)�verifyObject)�abstract�
interfaces)�platform)�FilePath)�compat�log)�
intToBytes�
networkString)�TestCase)�static�http�script�resource)�UnsupportedMethod)�DummyRequest��_render)�FOUNDc@s eZdZdZdd�Zdd�ZdS)�StaticDataTestsz
    Tests for L{Data}.
    csBt�dd�}tdg��d�_t|��}��fdd�}|�|�|S)zV
        L{Data.render} returns an empty response body for a I{HEAD} request.
        �fooZbar��HEADcs��d��j�d�dS�N���assertEqual�join�written��ignored��request�self��>/usr/lib/python3/dist-packages/twisted/web/test/test_static.py�
cbRendered-sz4StaticDataTests.test_headRequest.<locals>.cbRendered)r
�Datar�methodr�addCallback)r$�data�dr'r%r"r&�test_headRequest%s


z StaticDataTests.test_headRequestcCs0t�dd�}tdg�}d|_|�t|j|�dS)zv
        L{Data.render} raises L{UnsupportedMethod} in response to a non-I{GET},
        non-I{HEAD} request.
        rsbarr�POSTN)r
r(rr)�assertRaisesr�render)r$r+r#r%r%r&�test_invalidMethod3s
z"StaticDataTests.test_invalidMethodN)�__name__�
__module__�__qualname__�__doc__r-r1r%r%r%r&r!src@seZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
e��rjde
_dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Ze����d#kr�d$e��fe_d%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Z d3d4�Z!d5d6�Z"d7d8�Z#d9d:�Z$d;S)<�StaticFileTestsz2
    Tests for the basic behavior of L{File}.
    cCs
t||�S�Nr)r$rr#r%r%r&rCszStaticFileTests._renderc	CsNtjdd��(}tj|��dd�}|�|jdg�W5QRX|�t|�d�dS)z�
        Passing C{1} as the value to L{File}'s C{ignoredExts} argument
        issues a warning and sets the ignored extensions to the
        wildcard C{"*"}.
        T��record���ignoredExts�*N��warnings�catch_warningsr
�File�mktemprr<�len�r$ZcaughtWarnings�filer%r%r&�test_ignoredExtTrueGsz#StaticFileTests.test_ignoredExtTruec	CsLtjdd��&}tj|��dd�}|�|jg�W5QRX|�t|�d�dS)z�
        Passing C{1} as the value to L{File}'s C{ignoredExts} argument
        issues a warning and sets the ignored extensions to the empty
        list.
        Tr8rr;r:Nr>rDr%r%r&�test_ignoredExtFalseTsz$StaticFileTests.test_ignoredExtFalsec	CsNtjdd��(}tj|��dd�}|�|jdg�W5QRX|�t|�d�dS)z�
        Passing C{1} as the value to L{File}'s C{allowExt} argument
        issues a warning and sets the ignored extensions to the
        wildcard C{*}.
        Tr8r;r=r:Nr>rDr%r%r&�
test_allowExtaszStaticFileTests.test_allowExtcCsFtdg�}d|_t|���}|�d�t�|j�}|�t	|j
|�dS)zv
        L{File.render} raises L{UnsupportedMethod} in response to a non-I{GET},
        non-I{HEAD} request.
        rr.rN)rr)rrB�
setContentr
rA�pathr/rr0)r$r#rJrEr%r%r&r1ns

z"StaticFileTests.test_invalidMethodcs^t����}|��t�|j�}tdg��t�|��}��	|��}��fdd�}|�
|�|S)z�
        If a request is made which encounters a L{File} before a final segment
        which does not correspond to any file in the path the L{File} was
        created with, a not found response is sent.
        �foobarcs���jd�dS�N��r�responseCoder r"r%r&r'�sz1StaticFileTests.test_notFound.<locals>.cbRendered�rrB�makedirsr
rArJrr�getChildForRequestrr*�r$�baserE�childr,r'r%r"r&�
test_notFound{s

zStaticFileTests.test_notFoundcCsXt|���}|��t�|j�}tdg�}t�||�}|�	|tj
�|�|j|j�dS)z�
        The C{''} child of a L{File} which corresponds to a directory in the
        filesystem is a L{DirectoryLister}.
        rN)rrBrQr
rArJrrrR�assertIsInstance�DirectoryListerr�r$rTrEr#rUr%r%r&�test_emptyChild�s
zStaticFileTests.test_emptyChildcCs�t|�����}|��|�d��d���t�|j	�}t
dg�}t�||�}|�
|tj�t�|j	�}|�|j	|�|�|�}|�
|t�dS)z�
        The C{u''} child of a L{File} which corresponds to a directory
        whose path is text is a L{DirectoryLister} that renders to a
        binary listing.

        @see: U{https://twistedmatrix.com/trac/ticket/9438}
        z	text-file�wrN)rrBZ
asTextModerQrU�open�closer
rArJrrrRrWrXrZnativeStringrr0�bytes)r$ZtextBaseZtextFiler#rUZ
nativePathZresponser%r%r&�test_emptyChildUnicodeParent�s

z,StaticFileTests.test_emptyChildUnicodeParentcs^t����}|��t�|j�}tdg��t�|��}��	|��}��fdd�}|�
|�|S)z�
        If a request is made which encounters a L{File} before a final segment
        which cannot be looked up in the filesystem due to security
        considerations, a not found response is sent.
        s..cs���jd�dSrLrNr r"r%r&r'�szBStaticFileTests.test_securityViolationNotFound.<locals>.cbRenderedrPrSr%r"r&�test_securityViolationNotFound�s

z.StaticFileTests.test_securityViolationNotFoundcslt����}|�d���|jd�|�d�t�|j�}tdg����	|��}��fdd�}|�
|�|S)z�
        If the file in the filesystem which would satisfy a request cannot be
        read, L{File.render} sets the HTTP response code to I{FORBIDDEN}.
        ri�rcs���jd�dS)Ni�rNr r"r%r&r'�sz:StaticFileTests.test_forbiddenResource.<locals>.cbRendered)rrBrIZ
addCleanup�chmodr
rArJrrr*)r$rTrEr,r'r%r"r&�test_forbiddenResource�s



z&StaticFileTests.test_forbiddenResourcez(Cannot remove read permission on Windowscsv���}t|t�r|�d�}t|�}|��t�|j�}t	dg��t
�|��}��|��}��fdd�}|�
|�|S)z�
        A request whose path cannot be decoded as UTF-8 receives a not
        found response, and the failure is logged.
        �ascii��cs(���jd���t��t��d�dS)NrMr:)rrOrCZflushLoggedErrors�UnicodeDecodeErrorr r"r%r&r'�s�z8StaticFileTests.test_undecodablePath.<locals>.cbRendered)rB�
isinstancer^�decoderrQr
rArJrrrRrr*)r$rJrTrErUr,r'r%r"r&�test_undecodablePath�s



z$StaticFileTests.test_undecodablePathcCs|�t�d�jtj�dS)zN
        L{File.forbidden} defaults to L{resource.ForbiddenResource}.
        �.N)rWr
rA�	forbiddenrZForbiddenResource�r$r%r%r&�test_forbiddenResource_default�s
�z.StaticFileTests.test_forbiddenResource_defaultcs�t|���}|�d�d�dd�}G�fdd�dtj��G�fdd�dtj�}||j�}||_t	dg�}|�
|�}|��|�d	S)
z�
        The resource rendered for forbidden requests is stored as a class
        member so that users can customize it.
        rscustom-forbidden-responsecSsttjd��dS)Nr)�IOError�errnoZEACCESr%r%r%r&�failingOpenForReadingszOStaticFileTests.test_forbiddenResource_customize.<locals>.failingOpenForReadingcseZdZ�fdd�ZdS)zQStaticFileTests.test_forbiddenResource_customize.<locals>.CustomForbiddenResourcecs�Sr7r%�r$r#��markerResponser%r&r0szXStaticFileTests.test_forbiddenResource_customize.<locals>.CustomForbiddenResource.renderN�r2r3r4r0r%rqr%r&�CustomForbiddenResourcesrtcseZdZ��ZdS)zJStaticFileTests.test_forbiddenResource_customize.<locals>.CustomStaticFileN)r2r3r4rjr%)rtr%r&�CustomStaticFilesruN)rrBrIr�Resourcer
rArJ�openForReadingrr0r)r$rTroru�fileResourcer#�resultr%)rtrrr&� test_forbiddenResource_customizes



z0StaticFileTests.test_forbiddenResource_customizecsvt����}|��|�d��d�t�|j�}dg|_t	dg��t
�|��}��|��}��fdd�}|�
|�|S)a
        If a request is made which encounters a L{File} before a final empty
        segment, a file in the L{File} instance's C{indexNames} list which
        exists in the path the L{File} was created with is served as the
        response to the request.
        �foo.bar�bazrcs0��d��j�d����j�d�dd�dS�Nrr|�content-lengthr�3�rrr�responseHeaders�
getRawHeadersr r"r%r&r'/s
�z3StaticFileTests.test_indexNames.<locals>.cbRendered)rrBrQrUrIr
rArJZ
indexNamesrrrRrr*rSr%r"r&�test_indexNamess

zStaticFileTests.test_indexNamescsnt����}|��|�d��d�t�|j�}tdg��t	�
|��}��|��}��fdd�}|�|�|S)z�
        If a request is made which encounters a L{File} before a final segment
        which names a file in the path the L{File} was created with, that file
        is served as the response to the request.
        r{r|�foo.barcs0��d��j�d����j�d�dd�dSr}r�r r"r%r&r'Gs
�z3StaticFileTests.test_staticFile.<locals>.cbRendered�
rrBrQrUrIr
rArJrrrRrr*rSr%r"r&�test_staticFile8s

zStaticFileTests.test_staticFilecs~d}d�t����}|��|�|����t�|j�}t|�	d�g��t
�|��}��|��}���fdd�}|�
|�|S)zx
        A request for a existing unicode file path encoded as UTF-8
        returns the contents of that file.
        uῆscontent�utf-8cs<��d��j������j�d�dttt�����dS)Nrr~r)rrrr�r�r�strrCr ��contentr#r$r%r&r'as
�zBStaticFileTests.test_staticFileUnicodeFileName.<locals>.cbRendered)rrBrQrUrIr
rArJr�encoderrRrr*)r$�namerTrErUr,r'r%r�r&�test_staticFileUnicodeFileNamePs
z.StaticFileTests.test_staticFileUnicodeFileName)r�Zmcbsz>Cannot write unicode filenames with file system encoding of %scCs6t�|���}tdg�}|�d|�}|�||j�dS)z�
        A L{static.File} created for a directory which does not exist should
        return childNotFound from L{static.File.getChild}.
        r�N)r
rArBr�getChildr�
childNotFound)r$�
staticFiler#rUr%r%r&�test_staticFileDeletedGetChildns
z.StaticFileTests.test_staticFileDeletedGetChildcs\t�����}tdg��tdg����|�����|j��}����fdd�}|�|�|S)z{
        A L{static.File} created for a file which does not exist should render
        its C{childNotFound} page.
        r�cs���fdd�}��|��S)Ncs ��d��j�d��j��dSrrr )r#�request2r$r%r&r'�s
�zUStaticFileTests.test_staticFileDeletedRender.<locals>.cbRendered2.<locals>.cbRendered)r*)r!r'�r,r#r�r$r%r&�cbRendered2�s
zAStaticFileTests.test_staticFileDeletedRender.<locals>.cbRendered2)r
rArBrrr�r*)r$r�Zd2r�r%r�r&�test_staticFileDeletedRenderys


z,StaticFileTests.test_staticFileDeletedRendercs�t|���}|�d�d�G�fdd�dtj��G�fdd�dtj�}||j�}tdg�}|�	d|�}|�
|�}|��|�dS)	zs
        The resource rendered for child not found requests can be customize
        using a class member.
        rscustom-child-not-found-responsecseZdZ�fdd�ZdS)zYStaticFileTests.test_getChildChildNotFound_customize.<locals>.CustomChildNotFoundResourcecs�Sr7r%rprqr%r&r0�sz`StaticFileTests.test_getChildChildNotFound_customize.<locals>.CustomChildNotFoundResource.renderNrsr%rqr%r&�CustomChildNotFoundResource�sr�cseZdZ��ZdS)zNStaticFileTests.test_getChildChildNotFound_customize.<locals>.CustomStaticFileN)r2r3r4r�r%)r�r%r&ru�srusno-child.txtN)rrBrIrrvr
rArJrr�r0r)r$rTrurxr#rUryr%)r�rrr&�$test_getChildChildNotFound_customize�s



z4StaticFileTests.test_getChildChildNotFound_customizecsXt����}|�d�t�|j�}tdg��d�_t|��}��fdd�}|�	|�|S)zd
        L{static.File.render} returns an empty response body for I{HEAD}
        requests.
        rrrcs��d��j�d�dSrrr r"r%r&r'�sz4StaticFileTests.test_headRequest.<locals>.cbRendered)
rrBrIr
rArJrr)rr*)r$rJrEr,r'r%r"r&r-�s



z StaticFileTests.test_headRequestcszt����}|��|�d��d�t�|j�}dtj	i|_
tdg��t�
|��}��|��}��fdd�}|�|�|S)a!
        If a request is made which encounters a L{File} before a final segment
        which names a file with an extension which is in the L{File}'s
        C{processors} mapping, the processor associated with that extension is
        used to serve the response to the request.
        r{sTfrom twisted.web.static import Data
resource = Data(b'dynamic world', 'text/plain')
�.barr�cs0��d��j�d����j�d�dd�dS)Nrs
dynamic worldr~rs13r�r r"r%r&r'�s
�z3StaticFileTests.test_processors.<locals>.cbRendered)rrBrQrUrIr
rArJrZResourceScriptZ
processorsrrrRrr*rSr%r"r&�test_processors�s
�

zStaticFileTests.test_processorscCsbt�d�}|�|jg�|�d�|�d�|�|jddg�tjddd�}|�|jddg�dS)z�
        The list of ignored extensions can be set by passing a value to
        L{File.__init__} or by calling L{File.ignoreExt} later.
        riz.foor�)r��.bazr;r�N)r
rArr<Z	ignoreExt)r$rEr%r%r&�test_ignoreExt�s


zStaticFileTests.test_ignoreExtcs�t����}|��|�d��d�|�d��d�tj|jdd�}tdg��t	�
|��}��|��}��fdd	�}|�|�|S)
a
        A request for the I{base} child of a L{File} succeeds with a resource
        for the I{base<extension>} file in the path the L{File} was created
        with if such a file exists and the L{File} has been configured to
        ignore the I{<extension>} extension.
        r{r|zfoo.quuxrK)r�r;rcs��d��j�d�dS)Nrr|rr r"r%r&r'�szAStaticFileTests.test_ignoredExtensionsIgnored.<locals>.cbRenderedr�rSr%r"r&�test_ignoredExtensionsIgnored�s

z-StaticFileTests.test_ignoredExtensionsIgnoredcCs�t|���}|��|�d���t�|j�}tdg�}d|_t	�
||�}|�|�||��|�
|jt�|�
|j�d�dg�dS)z�
        A request for a path which is a directory but does not have a trailing
        slash will be redirected to a URL which does have a slash by L{File}.
        Zfoldersfoldershttp://dummy/folder#baz?foo=barslocations http://dummy/folder/#baz?foo=barN)rrBrQrUr
rArJr�urirrR�successResultOfrrrOrr�r�rYr%r%r&�+test_directoryWithoutTrailingSlashRedirects�s
�z;StaticFileTests.test_directoryWithoutTrailingSlashRedirectscs<t��t|���}|��t�|j�}�fdd�|_�|fS)z�
        Create a L{File} that when opened for reading, returns a L{StringIO}.

        @return: 2-tuple of the opened "file" and the L{File}.
        @rtype: L{tuple}
        cs�Sr7r%r%��fakeFiler%r&�<lambda>rz;StaticFileTests._makeFilePathWithStringIO.<locals>.<lambda>)�StringIOrrBZtouchr
rArJr\)r$rJrEr%r�r&�_makeFilePathWithStringIO
sz)StaticFileTests._makeFilePathWithStringIOcCsP|��\}}tdg�}d|_|�t||��|�d�|j�d�|�|j	�dS)zm
        A HEAD request opens the file, gets the size, and then closes it after
        the request.
        rrrN)
r�rr)r�rrrr�
assertTrue�closed�r$r�rEr#r%r%r&�test_HEADClosesFiles
z#StaticFileTests.test_HEADClosesFilecCsZ|��\}}tdg�}d|_dd�|_|�t||��|�d�|j�d�|�	|j
�dS)zQ
        A GET request that is cached closes the file after the request.
        rsGETcSstjSr7)rZCACHED)�_r%r%r&r�2rz>StaticFileTests.test_cachedRequestClosesFile.<locals>.<lambda>rN)r�rr)ZsetLastModifiedr�rrrrr�r�r�r%r%r&�test_cachedRequestClosesFile*s

z,StaticFileTests.test_cachedRequestClosesFileN)%r2r3r4r5rrFrGrHr1rVrZr_r`rbrZ	isWindows�skiprhrlrzr�r�r��sys�getfilesystemencoding�lowerr�r�r�r-r�r�r�r�r�r�r�r%r%r%r&r6?sF



��
r6c@s�eZdZdZd)dd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�ZdS)*�StaticMakeProducerTestsz)
    Tests for L{File.makeProducer}.
    NcCs4t|���}|�|�t�|���}||_||_|S)z�
        Make a L{static.File} resource that has C{content} for its content.

        @param content: The L{bytes} to use as the contents of the resource.
        @param type: Optional value for the content type of the resource.
        )rrBrIr
rAZ_asBytesPath�encoding�type)r$r�r�r�ZfileNamerr%r%r&�makeResourceWithContent?s
z/StaticMakeProducerTests.makeResourceWithContentcCs:i}|j��D]&\}}|���d�r|d||��<q|S)z�
        Extract the content-* headers from the L{DummyRequest} C{request}.

        This returns the subset of C{request.outgoingHeaders} of headers that
        start with 'content-'.
        scontent-r)r�ZgetAllRawHeadersr��
startswith)r$r#�contentHeaders�k�vr%r%r&r�Ns
z&StaticMakeProducerTests.contentHeadersc	CsD|�d�}tg�}|��� }|�||�}|�|tj�W5QRXdS)zp
        makeProducer when no Range header is set returns an instance of
        NoRangeStaticProducer.
        rN)r�rrw�makeProducerrWr
�NoRangeStaticProducer)r$rr#rE�producerr%r%r&�,test_noRangeHeaderGivesNoRangeStaticProducer\s


zDStaticMakeProducerTests.test_noRangeHeaderGivesNoRangeStaticProducerc	CsF|�d�}tg�}|���"}|�||�|�tj|j�W5QRXdS)zp
        makeProducer when no Range header is set sets the responseCode on the
        request to 'OK'.
        rN)r�rrwr�rr�OKrO)r$rr#rEr%r%r&�test_noRangeHeaderSets200OKhs


z3StaticMakeProducerTests.test_noRangeHeaderSets200OKc	Csrd}d}d}|jd|||d�}tg�}|���8}|�||�|�t|�t|�t|�d�|�|��W5QRXdS)zo
        makeProducer when no Range header is set sets the Content-* headers
        for the response.
        �{�
text/plain�gzip�a�r�r�)�content-typer~�content-encodingN)r�rrwr�rrr
r�)r$Zlength�contentType�contentEncodingrr#rEr%r%r&�$test_noRangeHeaderSetsContentHeadersts"�
��z<StaticMakeProducerTests.test_noRangeHeaderSetsContentHeadersc	CsRtg�}|j�dd�|�d�}|��� }|�||�}|�|tj�W5QRXdS)z�
        makeProducer when the Range header requests a single byte range
        returns an instance of SingleRangeStaticProducer.
        �range�	bytes=1-3�abcdefN�	r�requestHeaders�addRawHeaderr�rwr�rWr
�SingleRangeStaticProducer�r$r#rrEr�r%r%r&�.test_singleRangeGivesSingleRangeStaticProducer�s

zFStaticMakeProducerTests.test_singleRangeGivesSingleRangeStaticProducerc	CsTtg�}|j�dd�|�d�}|���"}|�||�|�tj|j	�W5QRXdS)z�
        makeProducer when the Range header requests a single, satisfiable byte
        range sets the response code on the request to 'Partial Content'.
        r�r�r�N�
rr�r�r�rwr�rr�PARTIAL_CONTENTrO�r$r#rrEr%r%r&�%test_singleRangeSets206PartialContent�s

�z=StaticMakeProducerTests.test_singleRangeSets206PartialContentc	Csvtg�}|j�dd�d}d}|jd||d�}|���6}|�||�|�t|�t|�ddd	�|�|��W5QRXd
S)��
        makeProducer when the Range header requests a single, satisfiable byte
        range sets the Content-* headers appropriately.
        r�r�r�r�r�r�sbytes 1-3/6r)r�r��
content-ranger~N)	rr�r�r�rwr�rrr�)r$r#r�r�rrEr%r%r&�"test_singleRangeSetsContentHeaders�s
��z:StaticMakeProducerTests.test_singleRangeSetsContentHeadersc	CsRtg�}|j�dd�|�d�}|��� }|�||�}|�|tj�W5QRXdS)z�
        makeProducer still returns an instance of L{SingleRangeStaticProducer}
        when the Range header requests a single unsatisfiable byte range.
        r��
bytes=4-10�abcNr�r�r%r%r&�=test_singleUnsatisfiableRangeReturnsSingleRangeStaticProducer�s

zUStaticMakeProducerTests.test_singleUnsatisfiableRangeReturnsSingleRangeStaticProducerc	CsTtg�}|j�dd�|�d�}|���"}|�||�|�tj|j	�W5QRXdS)z�
        makeProducer sets the response code of the request to of 'Requested
        Range Not Satisfiable' when the Range header requests a single
        unsatisfiable byte range.
        r�r�r�N�
rr�r�r�rwr�rr�REQUESTED_RANGE_NOT_SATISFIABLErOr�r%r%r&�?test_singleUnsatisfiableRangeSets416ReqestedRangeNotSatisfiable�s

�zWStaticMakeProducerTests.test_singleUnsatisfiableRangeSets416ReqestedRangeNotSatisfiablec	Csftg�}|j�dd�d}|jd|d�}|���,}|�||�|�dddd	�|�|��W5QRXd
S)z�
        makeProducer when the Range header requests a single, unsatisfiable
        byte range sets the Content-* headers appropriately.
        r�r�r�r��r��
text/plain�0�	bytes */3�r�r~r�N�rr�r�r�rwr�rr��r$r#r�rrEr%r%r&�/test_singleUnsatisfiableRangeSetsContentHeaders�s
��zGStaticMakeProducerTests.test_singleUnsatisfiableRangeSetsContentHeadersc	Csftg�}|j�dd�d}|jd|d�}|���,}|�||�|�dddd	�|�|��W5QRXd
S)z�
        makeProducer when the Range header requests a single byte range that
        partly overlaps the resource sets the Content-* headers appropriately.
        r�s
bytes=2-10r�r�r�r��1sbytes 2-2/3r�Nr�r�r%r%r&�6test_singlePartiallyOverlappingRangeSetsContentHeaders�s
��zNStaticMakeProducerTests.test_singlePartiallyOverlappingRangeSetsContentHeadersc	CsRtg�}|j�dd�|�d�}|��� }|�||�}|�|tj�W5QRXdS)z�
        makeProducer when the Range header requests a single byte range
        returns an instance of MultipleRangeStaticProducer.
        r��
bytes=1-3,5-6r�N�	rr�r�r�rwr�rWr
�MultipleRangeStaticProducerr�r%r%r&�2test_multipleRangeGivesMultipleRangeStaticProducer�s

zJStaticMakeProducerTests.test_multipleRangeGivesMultipleRangeStaticProducerc	CsTtg�}|j�dd�|�d�}|���"}|�||�|�tj|j	�W5QRXdS)z�
        makeProducer when the Range header requests a multiple satisfiable
        byte ranges sets the response code on the request to 'Partial
        Content'.
        r�r�r�Nr�r�r%r%r&�'test_multipleRangeSets206PartialContents

�z?StaticMakeProducerTests.test_multipleRangeSets206PartialContentc	Cs�tg�}|j�dd�|jddd�}|����}|�||�}|�|�}|�tddg�t|�	���d}|j
D]\}}}	|t|�7}qj|�t|�|d�|�
d|�|d}
|�d	t�d
|
��|�d|�W5QRXd	S)r�r�r�sabcdefghijklr�)r�r~r��Ns(multipart/byteranges; boundary="[^"]*"\Zr�)rr�r�r�rwr�r�r�set�keysZ	rangeInforCr
�assertInZassertNotIdentical�re�matchZassertNotIn)r$r#rrEr�r�ZexpectedLength�boundary�offset�sizer�r%r%r&�#test_mutipleRangeSetsContentHeaderss6�



���z;StaticMakeProducerTests.test_mutipleRangeSetsContentHeadersc	CsRtg�}|j�dd�|�d�}|��� }|�||�}|�|tj�W5QRXdS)z�
        makeProducer still returns an instance of L{SingleRangeStaticProducer}
        when the Range header requests multiple ranges, none of which are
        satisfiable.
        r��bytes=10-12,15-20r�Nr�r�r%r%r&�Btest_multipleUnsatisfiableRangesReturnsMultipleRangeStaticProducer2s

zZStaticMakeProducerTests.test_multipleUnsatisfiableRangesReturnsMultipleRangeStaticProducerc	CsTtg�}|j�dd�|�d�}|���"}|�||�|�tj|j	�W5QRXdS)z�
        makeProducer sets the response code of the request to of 'Requested
        Range Not Satisfiable' when the Range header requests multiple ranges,
        none of which are satisfiable.
        r�r�r�Nr�r�r%r%r&�Btest_multipleUnsatisfiableRangesSets416ReqestedRangeNotSatisfiable@s

�zZStaticMakeProducerTests.test_multipleUnsatisfiableRangesSets416ReqestedRangeNotSatisfiablec	Csttg�}|j�dd�d}|j�dd�|jd|d�}|���,}|�||�|�ddd	d
�|�|��W5QRXdS)z�
        makeProducer when the Range header requests multiple ranges, none of
        which are satisfiable, sets the Content-* headers appropriately.
        r�r�r�r�r�r�r�r�r�)r~r�r�Nr�r�r%r%r&�1test_multipleUnsatisfiableRangeSetsContentHeadersOs
��zIStaticMakeProducerTests.test_multipleUnsatisfiableRangeSetsContentHeadersc	CsTtg�}|j�dd�|�d�}|���"}|�||�|�tj|j	�W5QRXdS)z�
        makeProducer when the Range header requests multiple ranges, at least
        one of which matches, sets the response code to 'Partial Content'.
        r�sbytes=1-3,100-200r�Nr�r�r%r%r&� test_oneSatisfiableRangeIsEnoughbs

�z8StaticMakeProducerTests.test_oneSatisfiableRangeIsEnough)NN)r2r3r4r5r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r%r%r%r&r�9s(



"r�c@s eZdZdZdd�Zdd�ZdS)�StaticProducerTestsz3
    Tests for the abstract L{StaticProducer}.
    cCs*t�}t�d|�}|��|�|j�dS)zu
        L{StaticProducer.stopProducing} closes the file object the producer is
        producing data from.
        N)r�r
�StaticProducer�
stopProducingr�r��r$Z
fileObjectr�r%r%r&�test_stopProducingClosesFilewsz0StaticProducerTests.test_stopProducingClosesFilecCs0t�}t�tg�|�}|��|�d|j�dS)z�
        L{StaticProducer.stopProducing} sets the request instance variable to
        None, which indicates to subclasses' resumeProducing methods that no
        more data should be produced.
        N)r�r
r�rr��assertIdenticalr#r�r%r%r&�#test_stopProducingSetsRequestToNone�sz7StaticProducerTests.test_stopProducingSetsRequestToNoneN)r2r3r4r5r�rr%r%r%r&r�rsr�c@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�NoRangeStaticProducerTestsz-
    Tests for L{NoRangeStaticProducer}.
    cCsttjt�dd��dS)zG
        L{NoRangeStaticProducer} implements L{IPullProducer}.
        N)rr�
IPullProducerr
r�rkr%r%r&�test_implementsIPullProducer�s
�z7NoRangeStaticProducerTests.test_implementsIPullProducercCs<tg�}d}t�|t|��}|��|�|d�|j��dS)zs
        L{NoRangeStaticProducer.resumeProducing} writes content from the
        resource to the request.
        r�rN)rr
r�r��startrrr�r$r#r�r�r%r%r&�#test_resumeProducingProducesContent�s�z>NoRangeStaticProducerTests.test_resumeProducingProducesContentcCsttg�}tjj}dd|d}t�|t|��}|��|d|�||d|�|d|d�g}|�||j	�dS)z�
        L{NoRangeStaticProducer.start} writes at most
        C{abstract.FileDescriptor.bufferSize} bytes of content from the
        resource to the request at once.
        r��r:rN)
rr�FileDescriptor�
bufferSizer
r�r�rrr�r$r#r
r�r��expectedr%r%r&�!test_resumeProducingBuffersOutput�s�
�z<NoRangeStaticProducerTests.test_resumeProducingBuffersOutputcCsJtg�}|��}g}|�|j�t�|td��}|��|�dg|�dS)z�
        L{NoRangeStaticProducer.resumeProducing} calls finish() on the request
        after it is done producing content.
        r�N)	r�notifyFinishr*�appendr
r�r�rr�r$r#ZfinishDeferredZcallbackListr�r%r%r&�test_finishCalledWhenDone�s�z4NoRangeStaticProducerTests.test_finishCalledWhenDoneN�r2r3r4r5rrr
rr%r%r%r&r�s
	rc@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�SingleRangeStaticProducerTestsz1
    Tests for L{SingleRangeStaticProducer}.
    cCsttjt�dddd��dS)zK
        L{SingleRangeStaticProducer} implements L{IPullProducer}.
        N)rrrr
r�rkr%r%r&r�s�z;SingleRangeStaticProducerTests.test_implementsIPullProducercCsHtg�}d}t�|t|�dd�}|��|�|dd�d�|j��dS)z�
        L{SingleRangeStaticProducer.resumeProducing} writes the given amount
        of content, starting at the given offset, from the resource to the
        request.
        r�r:��rN)rr
r�r�rrrrrr%r%r&r�s�zBSingleRangeStaticProducerTests.test_resumeProducingProducesContentcCsntg�}tjj}d|}t�|t|�d|d�}|��|d|d�||d|d�g}|�||j	�dS)z�
        L{SingleRangeStaticProducer.start} writes at most
        C{abstract.FileDescriptor.bufferSize} bytes of content from the
        resource to the request at once.
        r�r:�
�N)
rrr	r
r
r�r�rrrrr%r%r&r
�s��z@SingleRangeStaticProducerTests.test_resumeProducingBuffersOutputcCsNtg�}|��}g}|�|j�t�|td�dd�}|��|�dg|�dS)z�
        L{SingleRangeStaticProducer.resumeProducing} calls finish() on the
        request after it is done producing content.
        r�r:N)	rrr*rr
r�r�rrrr%r%r&rs�z8SingleRangeStaticProducerTests.test_finishCalledWhenDoneNrr%r%r%r&r�s
	rc@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)� MultipleRangeStaticProducerTestsz3
    Tests for L{MultipleRangeStaticProducer}.
    cCsttjt�ddd��dS)zM
        L{MultipleRangeStaticProducer} implements L{IPullProducer}.
        N)rrrr
r�rkr%r%r&rs�z=MultipleRangeStaticProducerTests.test_implementsIPullProducercCsBtg�}d}t�|t|�ddg�}|��|�dd�|j��dS)z�
        L{MultipleRangeStaticProducer.resumeProducing} writes the requested
        chunks of content from the resource to the request, with the supplied
        boundaries in between each chunk.
        r�)r�r:r)�2r�r:s1bcd2frN)rr
r�r�rrrrrr%r%r&r's�zDMultipleRangeStaticProducerTests.test_resumeProducingProducesContentcCsttg�}d}t�|t|�dddg�}d|_|��d|dd�d	|d
d�|dd�d
g}|�||j�dS)a�
        L{MultipleRangeStaticProducer.start} writes about
        C{abstract.FileDescriptor.bufferSize} bytes of content from the
        resource to the request at once.

        To be specific about the 'about' above: it can write slightly more,
        for example in the case where the first boundary plus the first chunk
        is less than C{bufferSize} but first boundary plus the first chunk
        plus the second boundary is more, but this is unimportant as in
        practice the boundaries are fairly small.  On the other side, it is
        important for performance to bundle up several small chunks into one
        call to request.write.
        s01234567890123456789)r�rr)�br�r)�crrrr�rrrr�r�rN)rr
r�r�r
rrr)r$r#r�r�rr%r%r&r
7s��zBMultipleRangeStaticProducerTests.test_resumeProducingBuffersOutputcCsNtg�}|��}g}|�|j�t�|td�dg�}|��|�dg|�dS)z�
        L{MultipleRangeStaticProducer.resumeProducing} calls finish() on the
        request after it is done producing content.
        r�)rr:rN)	rrr*rr
r�r�rrrr%r%r&rUs�z:MultipleRangeStaticProducerTests.test_finishCalledWhenDoneNrr%r%r%r&rs
	rc@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,S)-�
RangeTestsa�
    Tests for I{Range-Header} support in L{twisted.web.static.File}.

    @type file: L{file}
    @ivar file: Temporary (binary) file containing the content to be served.

    @type resource: L{static.File}
    @ivar resource: A leaf web resource using C{file} as content.

    @type request: L{DummyRequest}
    @ivar request: A fake request, requesting C{resource}.

    @type catcher: L{list}
    @ivar catcher: List which gathers all log information.
    cCspt|���}d|_|�|j�|��|_t�|jj�|_	d|j	_
tdg�|_|jj|j_
g|_t�|jj�dS)a
        Create a temporary file with a fixed payload of 64 bytes.  Create a
        resource for that file and create a request which will be for that
        resource.  Each test can set a different range header to test different
        aspects of the implementation.
        s@�u�E�7���a0y0S�﬷��M6k{�	мkY�I��oZ�b�k�&�%݂q/AY�r:rN)rrB�payloadrIr\rEr
rAr�rZisLeafrr#r��catcherr	ZaddObserverr)r$rJr%r%r&�setUpws
zRangeTests.setUpcCs|j��t�|jj�dS)zB
        Clean up the resource file and the log observer.
        N)rEr]r	ZremoveObserverrrrkr%r%r&�tearDown�s
zRangeTests.tearDowncCs8|j��}|�|dd|�|�|jgd|f�dS)zU
        Asserts that a given log message occurred with an expected message.
        �messagerzAn additional log occurred: %rN)r�popr)r$rZlogItemr%r%r&�
_assertLogged�s
�zRangeTests._assertLoggedcCsn|jj}|�t|d�|�t|d�|�t|d�|�t|d�|�t|d�|�t|d�|�t|d�dS)	zw
        L{File._parseRangeHeader} raises L{ValueError} when passed
        syntactically invalid byte ranges.
        sbytessunknown=1-2sbytes=3sbytes=-s
bytes=foo-s
bytes=-foos	bytes=5-4N)r�_parseRangeHeaderr/�
ValueError)r$�fr%r%r&�test_invalidRanges�szRangeTests.test_invalidRangescCs|�|j�d�dg�dS)z�
        A single bytes range without an explicit stop position is parsed into a
        two-tuple giving the start position and L{None}.
        sbytes=0-)rNN�rrr%rkr%r%r&�test_rangeMissingStop�s
�z RangeTests.test_rangeMissingStopcCs|�|j�d�dg�dS)z�
        A single bytes range without an explicit start position is parsed into
        a two-tuple of L{None} and the end position.
        sbytes=-3)NrNr)rkr%r%r&�test_rangeMissingStart�s
�z!RangeTests.test_rangeMissingStartcCs|�|j�d�dg�dS)z�
        A single bytes range with explicit start and stop positions is parsed
        into a two-tuple of those positions.
        s	bytes=2-5)rr�Nr)rkr%r%r&�
test_range�s
�zRangeTests.test_rangecCs�|�|j�d�dg�|�|j�d�dg�|�|j�d�dg�|�|j�d�dg�|�|j�d�dg�|�|j�d�dg�dS)	z�
        A single bytes range with whitespace in allowed places is parsed in
        the same way as it would be without the whitespace.
        s bytes=1-2 �r:rsbytes =1-2 s
bytes= 1-2s
bytes=1 -2s
bytes=1- 2s
bytes=1-2 Nr)rkr%r%r&�test_rangeWithSpace�s0
�
�
�
�
�
�zRangeTests.test_rangeWithSpacecCs|�|j�d�dg�dS)z�
        If there are multiple byte ranges but only one is non-null, the
        non-null range is parsed and its start and stop returned.
        sbytes=1-2,
, ,	r-Nr)rkr%r%r&�test_nullRangeElements�s
�z!RangeTests.test_nullRangeElementscCs|�|j�d�ddg�dS)zd
        If multiple byte ranges are specified their starts and stops are
        returned.
        s
bytes=1-2,3-4r-)rrNr)rkr%r%r&�test_multipleRanges�s
�zRangeTests.test_multipleRangescCs<|jj�dd�|j�|j�|�td�|jj��d�dS)zp
        A correct response to a range request is as long as the length of the
        requested range.
        r�s
bytes=0-43r�,N)	r#r�r�rr0rrCrrrkr%r%r&�test_bodyLengthszRangeTests.test_bodyLengthcCs�d}|jj�d|�|j�|j�d|��f}|�|�|�d�|jj	�|j
�|�|jjtj
�|�|jj�d�dtt|j
���dS)a&
        An incorrect range request (RFC 2616 defines a correct range request as
        a Bytes-Unit followed by a '=' character followed by a specific range.
        Only 'bytes' is defined) results in the range header value being logged
        and a normal 200 response being sent.
        sfoobar=0-43r�z"Ignoring malformed Range header %rrr~rN)r#r�r�rr0rgr$rrrrrOrr�r�r�r
rC)r$�rangerr%r%r&�test_invalidRangeRequests
�z#RangeTests.test_invalidRangeRequestcCs�d|}|�|�}|�d|d�|�d|d�g}|dd�D]�}|�dd�\}}}	}
}|d	|	}|�d|�|�d|
�t�d
|tjtjB��d�}
t�d|tjtjB���\}}}|�|
|||f|d��qB|S)
z�
        Parse C{body} as a multipart MIME response separated by C{boundary}.

        Note that this with fail the calling test on certain syntactic
        problems.
        s
--rrs--
���r:s
r�
s^content-type: (.*)$s1^content-range: bytes ([0-9]+)-([0-9]+)/([0-9]+)$)�contentType�contentRange�body)	�splitrr��search�I�M�group�groupsr)r$�bodyr��sep�partsZparsed_parts�partZbeforeZheader1Zheader2ZblankZpartBodyZheadersZpartContentTypeValuer�endr�r%r%r&�parseMultipartBodys:

��
���zRangeTests.parseMultipartBodycCs2dddg}d�dd�|D��}|jj�dd|�|j�|j�|�|jjtj	�t
�d	|jj�
d
�d��d�}|�d
�|jj�|�}|�t|�t|��t||�D]�\}\}}|�t|jj�|d�|d\}}	}
|�t|�|�|�t|	�|�|�t|
�|j���|�|j||d�|d�q�dS)zo
        The response to a request for multiple bytes ranges is a MIME-ish
        multipart response.
        �rr)��)�(�2�,cSs g|]\}}td||f��qS�z%s-%s�r��.0�s�er%r%r&�
<listcomp>Bsz8RangeTests.test_multipleRangeRequest.<locals>.<listcomp>r��bytes=�'^multipart/byteranges; boundary="(.*)"$r�rr:rr7r8r9N)rr#r�r�rr0rrOrr�r�r�r�r�r>rErrC�ziprr��int�getFileSizer�r$Z	startEndsZrangeHeaderValuer�rBrCrPrQrrDr�r%r%r&�test_multipleRangeRequest<s0

����z$RangeTests.test_multipleRangeRequestcCsNddt|j�dfg}d�dd�|D��}|jj�dd|�|j�|j�|�|jj	t
j�t�
d	|jj�d
�d��d�}|�d
�|jj�|�}|�t|�t|��t||�D]�\}\}}|�t|jj�|d�|d\}}	}
|�t|�|�|�t|	�t||j��d��|�t|
�|j���|�|j||d�|d�q�dS)z�
        The response to a request for multiple bytes ranges is a MIME-ish
        multipart response, even when one of the ranged falls off the end of
        the resource.
        rFrIrrKcSs g|]\}}td||f��qSrLrMrNr%r%r&rR]szORangeTests.test_multipleRangeRequestWithRangeOverlappingEnd.<locals>.<listcomp>r�rSrTr�rr:rr7r8r9N)rCrrr#r�r�rr0rrOrr�r�r�r�r�r>rErrUrr�rV�minrWrXr%r%r&�0test_multipleRangeRequestWithRangeOverlappingEndVs0
���� z;RangeTests.test_multipleRangeRequestWithRangeOverlappingEndcCs�|jj�dd�|j�|j�|�d�|jj�|jdd��|�t	d�|jj��d�|�|jj
tj�|�|jj
�d�dd	�|�|jj
�d
�dd�dS)z�
        If the end byte position is omitted, then it is treated as if the
        length of the resource was specified by the end byte position.
        r�s	bytes=23-r�N�)r�rsbytes 23-63/64r~s41�r#r�r�rr0rrrrrCrOrr�r�r�rkr%r%r&�test_implicitEndqs ��zRangeTests.test_implicitEndcCs�|jj�dd�|j�|j�|�d�|jj�|jdd��|�t	d�|jj��d�|�|jj
tj�|�|jj
�d�dd	�|�|jj
�d
�dd�dS)z�
        If the start byte position is omitted but the end byte position is
        supplied, then the range is treated as requesting the last -N bytes of
        the resource, where N is the end byte position.
        r�s	bytes=-17ri��N�r�rsbytes 47-63/64r~s17r^rkr%r%r&�test_implicitStart�s ��zRangeTests.test_implicitStartcCs�|jj�dd�|j�|j�d�|jj�}|�||jdd��|�|jj	t
j�|�|jj�
d�dd�|�tt|��|jj�
d	�d�d
S)z�
        A correct response to a bytes range header request from A to B starts
        with the A'th byte and ends with (including) the B'th byte. The first
        byte of a page is numbered with 0.
        r�s
bytes=3-43rrr1r�rs
bytes 3-43/64r~N�r#r�r�rr0rrrrrOrr�r�r�r
rC�r$rr%r%r&�test_explicitRange�s�
�zRangeTests.test_explicitRangecCs�|jj�dd�|j�|j�d�|jj�}|�||jdd��|�|jj	t
j�|�|jj�
d�dd�|�tt|��|jj�
d	�d�dS)
a
        A correct response to a bytes range header request from A to B when B
        is past the end of the resource starts with the A'th byte and ends
        with the last byte of the resource. The first byte of a page is
        numbered with 0.
        r�sbytes=40-100rrINr�rsbytes 40-63/64r~rbrcr%r%r&� test_explicitRangeOverlappingEnd�s�
�z+RangeTests.test_explicitRangeOverlappingEndcCsp|jj�dd�|j�|j�|�|jjtj�|�d�	|jj
�|j�|�|jj�
d�dtt|j���dS)z�
        If a range is syntactically invalid due to the start being greater than
        the end, the range header is ignored (the request is responded to as if
        it were not present).
        r�sbytes=20-13rr~rN)r#r�r�rr0rrOrr�rrrr�r�r
rCrkr%r%r&�+test_statusCodeRequestedRangeNotSatisfiable�s�z6RangeTests.test_statusCodeRequestedRangeNotSatisfiablecCs�|jj�dd�|j�|j�|�|jjtj�|�d�	|jj
�d�|�|jj�d�dd�|�|jj�d�dt
dt|j�f��d	S)
a
        If a range is unsatisfiable due to the start not being less than the
        length of the resource, the response is 416 (Requested range not
        satisfiable) and no data is written to the response body (RFC 2616,
        section 14.35.1).
        r�sbytes=67-108rr~rr�r�z
bytes */%dN)r#r�r�rr0rrOrr�rrr�r�rrCrrkr%r%r&�test_invalidStartBytePos�s���z#RangeTests.test_invalidStartBytePosN)r2r3r4r5r r!r$r(r*r+r,r.r/r0r2r4rErYr[r_rardrerfrgr%r%r%r&rgs,
				

rc@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Ze��srde_dd�Zdd�Zdd�ZdS) �DirectoryListerTestsz.
    Tests for L{static.DirectoryLister}.
    cCstdg�}||_|Sr)rr�)r$r�r#r%r%r&�_request�s
zDirectoryListerTests._requestcCsLt|���}|��t�|j�}|�|�d��}|�d|�|�d|�dS)zm
        L{static.DirectoryLister} prints the request uri as header of the
        rendered content.
        rs"<h1>Directory listing for foo</h1>s(<title>Directory listing for foo</title>N�	rrBrQr
rXrJr0rir��r$rJ�listerr+r%r%r&�test_renderHeader�sz&DirectoryListerTests.test_renderHeadercCsLt|���}|��t�|j�}|�|�d��}|�d|�|�d|�dS)zW
        L{static.DirectoryLister} unquote the request uri before printing it.
        s	foo%20bars&<h1>Directory listing for foo bar</h1>s,<title>Directory listing for foo bar</title>Nrjrkr%r%r&�test_renderUnquoteHeader�sz-DirectoryListerTests.test_renderUnquoteHeadercCsLt|���}|��t�|j�}|�|�d��}|�d|�|�d|�dS)zl
        L{static.DirectoryLister} escape "&", "<" and ">" after unquoting the
        request uri.
        s	foo%26bars*<h1>Directory listing for foo&amp;bar</h1>s0<title>Directory listing for foo&amp;bar</title>Nrjrkr%r%r&�test_escapeHeadersz&DirectoryListerTests.test_escapeHeadercCsht|���}|��|�d��d�|�d��dd�t�|j�}|�|�	d��}d}|�
||�dS)	ze
        L{static.DirectoryLister} is able to list all the files inside a
        directory.
        �file1scontent1�file2scontent2i�rs�<tr class="odd">
    <td><a href="file1">file1</a></td>
    <td>8B</td>
    <td>[text/html]</td>
    <td></td>
</tr>
<tr class="even">
    <td><a href="file2">file2</a></td>
    <td>7K</td>
    <td>[text/html]</td>
    <td></td>
</tr>N)rrBrQrUrIr
rXrJr0rir��r$rJrlr+r@r%r%r&�test_renderFilessz%DirectoryListerTests.test_renderFilescCs`t|���}|��|�d���|�d���t�|j�}|�|�d��}d}|�	||�dS)zk
        L{static.DirectoryLister} is able to list all the directories inside
        a directory.
        �dir1zdir2 & 3rs�<tr class="odd">
    <td><a href="dir1/">dir1/</a></td>
    <td></td>
    <td>[Directory]</td>
    <td></td>
</tr>
<tr class="even">
    <td><a href="dir2%20%26%203/">dir2 &amp; 3/</a></td>
    <td></td>
    <td>[Directory]</td>
    <td></td>
</tr>N�
rrBrQrUr
rXrJr0rir�rrr%r%r&�test_renderDirectories0sz+DirectoryListerTests.test_renderDirectoriescCsvt|���}|��|�d���|�d���|�d���tj|jddgd�}|�|�d��}d}|�	||�dS)z�
        L{static.DirectoryLister} takes an optional C{dirs} argument that
        filter out the list of directories and files printed.
        rtZdir2Zdir3)�dirsrs�<tr class="odd">
    <td><a href="dir1/">dir1/</a></td>
    <td></td>
    <td>[Directory]</td>
    <td></td>
</tr>
<tr class="even">
    <td><a href="dir3/">dir3/</a></td>
    <td></td>
    <td>[Directory]</td>
    <td></td>
</tr>Nrurrr%r%r&�test_renderFilteredKsz(DirectoryListerTests.test_renderFilteredcCs�t�d�}dd�td�D�}|�|�}|�t|�d�|�|d�d��|�|d�d��|�|d	�d��|�|d
�d��|�|d�d��dS)zu
        L{static.DirectoryLister} gives an alternate class for each odd and
        even rows in the table.
        NcSsg|]}dddddd��qS)r)�href�textr�r�r�r%)rO�ir%r%r&rRls��z8DirectoryListerTests.test_oddAndEven.<locals>.<listcomp>r�rz<tr class="odd">r:z<tr class="even">rrr)r
rXr3Z_buildTableContentrrCr�r�)r$rl�elementsr�r%r%r&�test_oddAndEvenfs
�
z$DirectoryListerTests.test_oddAndEvencCsPt|���}|��t�|j�}|�d�}|�|�|�|j	�
d�dd�dS)z�
        L{static.DirectoryLister} produces a MIME-type that indicates that it is
        HTML, and includes its charset (UTF-8).
        rr�rstext/html; charset=utf-8N)rrBrQr
rXrJrir0rr�r�)r$rJrlZreqr%r%r&�test_contentTypexs

�z%DirectoryListerTests.test_contentTypecCs�t|���}|��|�d��d�|�d��d�|�d��d�|�d��d�t�|j�}|��d	d
ddd
�}t	j
|j|d�}|�|�\}}|�|g�|�|dddddd�dddddd�dddddd�dddddd�g�dS)zl
        L{static.DirectoryLister} is able to detect mimetype and encoding of
        listed files.
        z	file1.txt�file1zfile2.pyspythonz
file3.conf.gzsconf compressedzfile4.diff.bz2sdiff compressedr�ztext/pythonztext/configurationz	text/diff)z.txtz.pyz.confz.diff)�contentTypesrZ5Bz[text/plain])r�ryr�rzr�Z6Bz
[text/python]z[gzip]Z15Bz[text/configuration]z[bzip2]z[text/diff]N)
rrBrQrUrI�os�listdirrJ�sortr
rX�_getFilesAndDirectoriesr)r$rJ�	directoryr�rlrw�filesr%r%r&�test_mimeTypeAndEncodings�sT������z.DirectoryListerTests.test_mimeTypeAndEncodingscCs�t|���}|��|�d�}|�d�|�|�d��|��t�|j	�}t
�|j	�}|��|�
|�\}}|�|g�|�|g�dS)z�
        If on the file in the listing points to a broken symlink, it should not
        be returned by L{static.DirectoryLister._getFilesAndDirectories}.
        rprrqN)rrBrQrUrIZlinkTo�remover
rXrJr�r�r�r�r)r$rJrprlr�rwr�r%r%r&�test_brokenSymlink�s

z'DirectoryListerTests.test_brokenSymlinkzNo symlink supportcs\t����}|��t�|j�}��d��t�|��}t	|��}��fdd�}|�
|�|S)zu
        Any child resource of L{static.DirectoryLister} renders an HTTP
        I{NOT FOUND} response code.
        rcs���jtj�dSr7)rrOrZ	NOT_FOUNDr r"r%r&r'�sz>DirectoryListerTests.test_childrenNotFound.<locals>.cbRendered)rrBrQr
rXrJrirrRrr*)r$rJrlrUryr'r%r"r&�test_childrenNotFound�s


z*DirectoryListerTests.test_childrenNotFoundcCsLt|���}t�|j�}|�t|�d|jf�|�t|�d|jf�dS)zR
        L{static.DirectoryLister.__repr__} gives the path of the lister.
        z<DirectoryLister of %r>N)rrBr
rXrJr�reprr�)r$rJrlr%r%r&�	test_repr�s

�

�zDirectoryListerTests.test_reprcCsp|�t�d�d�|�t�d�d�|�t�d�d�|�t�d�d�|�t�d	�d
�|�t�d�d�d
S)zi
        L{static.formatFileSize} format an amount of bytes into a more readable
        format.
        rZ0Br�Z123Bi�Z4Ki�͇Z8Mi�X�IZ1GlP�c}Z1149GN)rr
ZformatFileSizerkr%r%r&�test_formatFileSize�sz(DirectoryListerTests.test_formatFileSizeN)r2r3r4r5rirmrnrorsrvrxr}r~r�r�rZ_supportsSymlinksr�r�r�r�r%r%r%r&rh�s"
/rhc@s>eZdZdZe�Zdd�Zdd�Zdd�Zdd	�Z	d
d�Z
dS)
�LoadMimeTypesTestsz�
    Tests for the MIME type loading routine.

    @cvar UNSET: A sentinel to signify that C{self.paths} has not been set by
        the mock init.
    cCs|j|_dSr7)�UNSET�pathsrkr%r%r&r �szLoadMimeTypesTests.setUpcCs
||_dS)z�
        A mock L{mimetypes.init} that records the value of the passed C{paths}
        argument.

        @param paths: The paths that will be recorded.
        N)r��r$r�r%r%r&�	_fakeInitszLoadMimeTypesTests._fakeInitcCs tj|jd�|�|jd�dS)zE
        By default, L{None} is passed to C{mimetypes.init}.
        ��initN�r
�
loadMimeTypesr�rr�rkr%r%r&�test_defaultArgumentIsNone
sz-LoadMimeTypesTests.test_defaultArgumentIsNonecCs,dddg}tj||jd�|�|j|�dS)zI
        Passed MIME type files are passed to C{mimetypes.init}.
        �x�y�zr�Nr�r�r%r%r&�test_extraLocationsWorks
z*LoadMimeTypesTests.test_extraLocationsWorkcCsdttdd�r0t�tj�}|�|jdjtj	�n0t�
tj�\}}}}||�d�}|�|tj	�dS)z:
        By default, C{mimetypes.init} is called.
        �	signatureNr�)�getattr�inspectr�r
r�ZassertIsZ
parameters�default�	mimetypesr�Z
getargspec�index)r$r��argsr��defaultsZdefaultInitr%r%r&�test_usesGlobalInitFunctions�z.LoadMimeTypesTests.test_usesGlobalInitFunctionN)r2r3r4r5�objectr�r r�r�r�r�r%r%r%r&r��s
	r�c@seZdZdd�ZdS)�StaticDeprecationTestscCsPddlm}|tdg��|�|jg�}|�t|�d�|�|ddd�dS)z?
        L{twisted.web.static.addSlash} is deprecated.
        r)�addSlashrr:r"z<twisted.web.static.addSlash was deprecated in Twisted 16.0.0N)Ztwisted.web.staticr�rZ
flushWarnings�test_addSlashDeprecatedrrC)r$r�r?r%r%r&r�1s�z.StaticDeprecationTests.test_addSlashDeprecatedN)r2r3r4r�r%r%r%r&r�/sr�)4r5rnr�r�r�r�r�r?�iorr�Zzope.interface.verifyrZtwisted.internetrrZtwisted.python.runtimerZtwisted.python.filepathrZtwisted.pythonrr	Ztwisted.python.compatr
rZtwisted.trial.unittestrZtwisted.webr
rrrZtwisted.web.serverrZtwisted.web.test.requesthelperrZtwisted.web.test._utilrZtwisted.web._responsesrrr6r�r�rrrrrhr�r�r%r%r%r&�<module>sP};EEN}9