o
    Óó·iç<  ã                   @  sN  d Z ddlmZ ddgZddlmZ ddlmZmZm	Z	 ddl
mZmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$m%Z%m&Z& dd„ dD ƒZ'dZ(dd„ e'D ƒZ)e( *d¡Z+eeƒG dd„ dƒƒZ,G dd„ dƒZ-G dd„ dƒZ.G dd„ dej/ƒZ0dS ) z2
Helpers related to HTTP requests, used by tests.
é    )ÚannotationsÚDummyChannelÚDummyRequest)ÚBytesIO)ÚDictÚListÚOptional)ÚimplementerÚverify)ÚVersion)ÚIPv4AddressÚIPv6Address)ÚDeferred)ÚIAddressÚISSLTransport)ÚClock)Ú
deprecated)Úunittest)ÚFOUND)ÚHeaders)ÚResource)ÚNOT_DONE_YETÚSessionÚSitec                 C  s   g | ]}d |› d�‘qS )ZFooÚbar© )Ú.0Zlwr   r   údC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/web/test/requesthelper.pyÚ
<listcomp>   s    r   )úÚ
z
zFoo barc                 C  s   g | ]}|  d ¡‘qS )Úascii)Úencode)r   Ú	componentr   r   r   r   !   s    
ÿr!   c                   @  s   e Zd ZdZdS )ÚNullAddressz/
    A null implementation of L{IAddress}.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r$   '   s    r$   c                   @  sÖ   e Zd ZG dd„ dƒZeeƒG dd„ deƒƒZeeƒ ƒZ	d,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 )-r   c                   @  sV   e Zd ZdZdZd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 )zDummyChannel.TCPéP   FNc                 C  s,   |d u r
t dddƒ}|| _tƒ | _g | _d S )NÚTCPz192.168.1.1i80  )r   Ú_peerr   ÚwrittenÚ	producers©ÚselfZpeerr   r   r   Ú__init__3   s
   
zDummyChannel.TCP.__init__c                 C  ó   | j S ©N)r+   ©r/   r   r   r   ÚgetPeer:   s   zDummyChannel.TCP.getPeerc                 C  s(   t |tƒstd|›�ƒ‚| j |¡ d S )Nz)Can only write bytes to a transport, not )Ú
isinstanceÚbytesÚ	TypeErrorr,   Úwrite©r/   Údatar   r   r   r8   =   s   
zDummyChannel.TCP.writec                 C  s   |D ]}|   |¡ qd S r2   )r8   )r/   Úiovecr:   r   r   r   ÚwriteSequenceB   s   ÿzDummyChannel.TCP.writeSequencec                 C  s   t dd| jƒS )Nr*   z10.0.0.1)r   Úportr3   r   r   r   ÚgetHostF   ó   zDummyChannel.TCP.getHostc                 C  s   | j  ||f¡ d S r2   )r-   Úappend©r/   ZproducerZ	streamingr   r   r   ÚregisterProducerI   s   z!DummyChannel.TCP.registerProducerc                 C  ó   d S r2   r   r3   r   r   r   ÚunregisterProducerL   ó   z#DummyChannel.TCP.unregisterProducerc                 C  ó
   d| _ d S )NT)Údisconnectedr3   r   r   r   ÚloseConnectionO   ó   
zDummyChannel.TCP.loseConnectionr2   )r%   r&   r'   r=   rG   r0   r4   r8   r<   r>   rB   rD   rH   r   r   r   r   r*   /   s    
r*   c                   @  sD   e 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S )zDummyChannel.SSLc                 C  rC   r2   r   r3   r   r   r   ÚabortConnectionT   ó   z DummyChannel.SSL.abortConnectionc                 C  rC   r2   r   r3   r   r   r   ÚgetTcpKeepAliveX   rK   z DummyChannel.SSL.getTcpKeepAlivec                 C  rC   r2   r   r3   r   r   r   ÚgetTcpNoDelay\   rK   zDummyChannel.SSL.getTcpNoDelayc                 C  rC   r2   r   r3   r   r   r   ÚloseWriteConnection`   rK   z$DummyChannel.SSL.loseWriteConnectionc                 C  rC   r2   r   ©r/   Úenabledr   r   r   ÚsetTcpKeepAlived   rK   z DummyChannel.SSL.setTcpKeepAlivec                 C  rC   r2   r   rO   r   r   r   ÚsetTcpNoDelayh   rK   zDummyChannel.SSL.setTcpNoDelayc                 C  rC   r2   r   r3   r   r   r   ÚgetPeerCertificatel   rK   z#DummyChannel.SSL.getPeerCertificateN)
r%   r&   r'   rJ   rL   rM   rN   rQ   rR   rS   r   r   r   r   ÚSSLR   s    rT   Nc                 C  s   |   |¡| _d S r2   )r*   Ú	transportr.   r   r   r   r0   r   ó   zDummyChannel.__init__c                 C  rC   r2   r   )r/   Úrequestr   r   r   ÚrequestDoneu   rE   zDummyChannel.requestDonec                 C  sh   t |tƒrdd„ | ¡ D ƒ}|d | d | d }|g}| dd„ |D ƒ¡ | d¡ | j |¡ d S )Nc                 S  s"   g | ]\}}|D ]}||f‘qqS r   r   )r   ÚkÚvaluesÚvr   r   r   r   z   s
    ÿÿz-DummyChannel.writeHeaders.<locals>.<listcomp>ó    ó   
c                 s  s$   � | ]\}}|d  | d V  qdS )s   : r]   Nr   )r   ÚnameÚvaluer   r   r   Ú	<genexpr>   s   €" z,DummyChannel.writeHeaders.<locals>.<genexpr>)r5   r   ÚgetAllRawHeadersÚextendr@   rU   r<   )r/   ÚversionÚcodeÚreasonÚheadersZresponse_lineZheaderSequencer   r   r   ÚwriteHeadersx   s   
ÿ
zDummyChannel.writeHeadersc                 C  ó
   | j  ¡ S r2   )rU   r4   r3   r   r   r   r4   ƒ   rI   zDummyChannel.getPeerc                 C  rh   r2   )rU   r>   r3   r   r   r   r>   †   rI   zDummyChannel.getHostc                 C  s   | j  ||¡ d S r2   )rU   rB   rA   r   r   r   rB   ‰   s   zDummyChannel.registerProducerc                 C  ó   | j  ¡  d S r2   )rU   rD   r3   r   r   r   rD   Œ   r?   zDummyChannel.unregisterProducerc                 C  ó   | j  |¡ d S r2   )rU   r8   r9   r   r   r   r8   �   rV   zDummyChannel.writec                 C  rj   r2   )rU   r<   )r/   r;   r   r   r   r<   ’   rV   zDummyChannel.writeSequencec                 C  ri   r2   )rU   rH   r3   r   r   r   rH   •   r?   zDummyChannel.loseConnectionc                 C  rC   r2   r   r3   r   r   r   Ú
endRequest˜   rE   zDummyChannel.endRequestc                 C  s   t | j| jƒS r2   )r5   rU   rT   r3   r   r   r   ÚisSecure›   r?   zDummyChannel.isSecurec                 C  rC   r2   r   r3   r   r   r   rJ   ž   rK   zDummyChannel.abortConnectionc                 C  rC   r2   r   r3   r   r   r   rL   ¢   rK   zDummyChannel.getTcpKeepAlivec                 C  rC   r2   r   r3   r   r   r   rM   ¦   rK   zDummyChannel.getTcpNoDelayc                 C  rC   r2   r   r3   r   r   r   rN   ª   rK   z DummyChannel.loseWriteConnectionc                 C  rC   r2   r   r3   r   r   r   rQ   ®   rK   zDummyChannel.setTcpKeepAlivec                 C  rC   r2   r   r3   r   r   r   rR   ²   rK   zDummyChannel.setTcpNoDelayc                 C  rC   r2   r   r3   r   r   r   rS   ¶   rK   zDummyChannel.getPeerCertificater2   )r%   r&   r'   r*   r	   r   rT   r   r   Úsiter0   rX   rg   r4   r>   rB   rD   r8   r<   rH   rk   rl   rJ   rL   rM   rN   rQ   rR   rS   r   r   r   r   r   .   s0    #

c                   @  s$  e Zd ZU dZdZdZdZded< ded< ded	< ded
< ded< ded< dd„ Zdd„ Z			dHdIdd„Z
dd„ Zdd„ Zdd „ ZdJd!d"„Zd#d$„ Zd%d&„ ZdKd(d)„Zd*d+„ Zd,d-„ Zd.d/„ ZdJd0d1„Zd2d3„ Zd4d5„ Zeed6d7d8d9ƒd:d;�d<d=„ ƒZd>d?„ Zd@dA„ ZdBdC„ ZdLdDdE„ZdFdG„ ZdS )Mr   a  
    Represents a dummy or fake request. See L{twisted.web.server.Request}.

    @ivar _finishedDeferreds: L{None} or a C{list} of L{Deferreds} which will
        be called back with L{None} when C{finish} is called or which will be
        errbacked if C{processingFailed} is called.

    @type requestheaders: C{Headers}
    @ivar requestheaders: A Headers instance that stores values for all request
        headers.

    @type responseHeaders: C{Headers}
    @ivar responseHeaders: A Headers instance that stores values for all
        response headers.

    @type responseCode: C{int}
    @ivar responseCode: The response code which was passed to
        C{setResponseCode}.

    @type written: C{list} of C{bytes}
    @ivar written: The bytes which have been written to the request.
    s   http://dummy/s   GETNúOptional[IAddress]ÚclientzList[bytes]Úsitepathr,   ÚprepathzDict[bytes, List[bytes]]ÚargszList[Deferred[None]]Ú_finishedDeferredsc                 C  s"   d| _ | j r| ¡  | j sdS dS )zû
        Call an L{IPullProducer}'s C{resumeProducing} method in a
        loop until it unregisters itself.

        @param prod: The producer.
        @type prod: L{IPullProducer}

        @param s: Whether or not the producer is streaming.
        é   N)ÚgoZresumeProducing)r/   ÚprodÚsr   r   r   rB   Ü   s   ÿzDummyRequest.registerProducerc                 C  rF   )Nr   )ru   r3   r   r   r   rD   ë   rI   zDummyRequest.unregisterProducerÚpostpathúlist[bytes]ÚsessionúOptional[Session]ÚreturnÚNonec                 C  sl   g | _ g | _d| _|| _g | _d | _|ptd dtƒ d�| _i | _	t
ƒ | _t
ƒ | _d | _g | _d| _d| _d S )Nr   ó   0)rm   ÚuidZreactors   dummys   HTTP/1.0)rp   r,   Úfinishedrx   rq   rz   r   r   ÚprotoSessionrr   r   ÚrequestHeadersÚresponseHeadersÚresponseCoders   Ú_serverNameZclientproto)r/   rx   rz   ro   r   r   r   r0   î   s   
zDummyRequest.__init__c                 C  s,   i }| j  ¡ D ]\}}|d || ¡ < q|S )ah  
        Return dictionary mapping the names of all received headers to the last
        value received for each.

        Since this method does not return all header information,
        C{self.requestHeaders.getAllRawHeaders()} may be preferred.

        NOTE: This function is a direct copy of
        C{twisted.web.http.Request.getAllRawHeaders}.
        éÿÿÿÿN)r‚   ra   Úlower)r/   rf   rY   r[   r   r   r   ÚgetAllHeaders  s   zDummyRequest.getAllHeadersc                 C  s   | j  | ¡ dg¡d S )aH  
        Retrieve the value of a request header.

        @type name: C{bytes}
        @param name: The name of the request header for which to retrieve the
            value.  Header names are compared case-insensitively.

        @rtype: C{bytes} or L{None}
        @return: The value of the specified request header.
        Nr   )r‚   ZgetRawHeadersr‡   )r/   r^   r   r   r   Ú	getHeader  s   zDummyRequest.getHeaderc                 C  s   | j  ||¡ dS )zATODO: make this assert on write() if the header is content-lengthN)rƒ   ÚaddRawHeader©r/   r^   r_   r   r   r   Ú	setHeader   s   zDummyRequest.setHeaderc                 C  s(   | j r| j S | jrJ dƒ‚| j| _ | j S )Nz8Session cannot be requested after data has been written.)rz   r,   r�   )r/   ZsessionInterfacer   r   r   Ú
getSession$  s   ÿþzDummyRequest.getSessionc                 C  s,   |  | ¡}|tu rdS |  |¡ |  ¡  dS )a   
        Render the given resource as a response to this request.

        This implementation only handles a few of the most common behaviors of
        resources.  It can handle a render method that returns a string or
        C{NOT_DONE_YET}.  It doesn't know anything about the semantics of
        request methods (eg HEAD) nor how to set any particular headers.
        Basically, it's largely broken, but sufficient for some tests at least.
        It should B{not} be expanded to do all the same stuff L{Request} does.
        Instead, L{DummyRequest} should be phased out and L{Request} (or some
        other real code factored in a different way) used.
        N)Úrenderr   r8   Úfinish)r/   ÚresourceÚresultr   r   r   rŽ   -  s
   

zDummyRequest.renderc                 C  s"   t |tƒs	tdƒ‚| j |¡ d S )Nzwrite() only accepts bytes)r5   r6   r7   r,   r@   r9   r   r   r   r8   @  s   
zDummyRequest.writeúDeferred[None]c                 C  s   t ƒ }| j |¡ |S )z¹
        Return a L{Deferred} which is called back with L{None} when the request
        is finished.  This will probably only work if you haven't called
        C{finish} yet.
        N)r   rs   r@   )r/   r€   r   r   r   ÚnotifyFinishE  s   zDummyRequest.notifyFinishc                 C  s>   | j d | _ | jdur| j}d| _|D ]	}| d¡ qdS dS )z}
        Record that the request is finished and callback and L{Deferred}s
        waiting for notification of this.
        rt   N)r€   rs   Úcallback)r/   Ú	observersÚobsr   r   r   r�   O  s   
üzDummyRequest.finishc                 C  s2   | j dur| j }d| _ |D ]	}| |¡ qdS dS )zK
        Errback and L{Deferreds} waiting for finish notification.
        N)rs   Zerrback)r/   re   r•   r–   r   r   r   ÚprocessingFailed[  s   
üzDummyRequest.processingFailedc                 C  s   |g| j |< d S r2   )rr   r‹   r   r   r   ÚaddArge  rV   zDummyRequest.addArgc                 C  s,   | j rJ d d | j ¡¡ƒ‚|| _|| _dS )z{
        Set the HTTP status response code, but takes care that this is called
        before any data is written.
        z;Response code cannot be set after data hasbeen written: {}.ú@@@@N)r,   ÚformatÚjoinr„   ZresponseMessage)r/   rd   Úmessager   r   r   ÚsetResponseCodeh  s   ÿ
ÿþ
zDummyRequest.setResponseCodec                 C  ó    | j rJ d d | j ¡¡ƒ‚d S )Nz<Last-Modified cannot be set after data has been written: {}.r™   ©r,   rš   r›   )r/   Úwhenr   r   r   ÚsetLastModifiedu  ó   ÿ
ÿþzDummyRequest.setLastModifiedc                 C  rž   )Nz3ETag cannot be set after data has been written: {}.r™   rŸ   )r/   Útagr   r   r   ÚsetETag|  r¢   zDummyRequest.setETagZTwistedé   é   r   ÚgetClientAddress)Úreplacementc                 C  s   t | jttfƒr| jjS dS )z|
        Return the IPv4 address of the client which made this request, if there
        is one, otherwise L{None}.
        N)r5   ro   r   r   Úhostr3   r   r   r   ÚgetClientIPƒ  s   zDummyRequest.getClientIPc                 C  s   | j du rtƒ S | j S )z•
        Return the L{IAddress} of the client that made this request.

        @return: an address.
        @rtype: an L{IAddress} provider.
        N)ro   r$   r3   r   r   r   r§   �  s   
zDummyRequest.getClientAddressc                 C  r1   )zƒ
        Get a dummy hostname associated to the HTTP request.

        @rtype: C{bytes}
        @returns: a dummy hostname
        N)r…   r3   r   r   r   ÚgetRequestHostname˜  s   zDummyRequest.getRequestHostnamec                 C  s   t dddƒS )zz
        Get a dummy transport's host.

        @rtype: C{IPv4Address}
        @returns: a dummy transport's host
        r*   ú	127.0.0.1r)   N)r   r3   r   r   r   r>   ¡  s   zDummyRequest.getHostc                 C  sD   || _ |  ¡ r
d}nd}||kr|}nd||f }| j d|¡ dS )a_  
        Change the host and port the request thinks it's using.

        @type host: C{bytes}
        @param host: The value to which to change the host header.

        @type ssl: C{bool}
        @param ssl: A flag which, if C{True}, indicates that the request is
            considered secure (if C{True}, L{isSecure} will return C{True}).
        i»  r)   s   %b:%ds   hostN)Z	_forceSSLrl   r‚   rŠ   )r/   r©   r=   ÚsslÚdefaultZ
hostHeaderr   r   r   ÚsetHostª  s   zDummyRequest.setHostc                 C  s   |   t¡ |  d|¡ dS )zu
        Utility function that does a redirect.

        The request should have finish() called after this.
        s   locationN)r�   r   rŒ   )r/   Úurlr   r   r   ÚredirectÀ  s   
zDummyRequest.redirect)NN)rx   ry   rz   r{   ro   rn   r|   r}   r2   )r|   r’   )r   ) r%   r&   r'   r(   ÚuriÚmethodro   Ú__annotations__rB   rD   r0   rˆ   r‰   rŒ   r�   rŽ   r8   r“   r�   r—   r˜   r�   r¡   r¤   r   r   rª   r§   r«   r>   r¯   r±   r   r   r   r   r   »   sF   
 ü
	




		
	c                   @  s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚDummyRequestTestsz$
    Tests for L{DummyRequest}.
    c                 C  s^   t g ƒ}| ¡  | j| jgd�}|  dt|ƒ¡ |\}|  | d¡t¡ |  | d¡d¡ dS )zp
        L{DummyRequest.getClientIP} is deprecated in favor of
        L{DummyRequest.getClientAddress}
        )ZoffendingFunctionsrt   Úcategoryrœ   z}twisted.web.test.requesthelper.DummyRequest.getClientIP was deprecated in Twisted 18.4.0; please use getClientAddress insteadN)r   rª   ZflushWarningsÚtest_getClientIPDeprecatedÚassertEqualÚlenÚgetÚDeprecationWarning)r/   rW   ÚwarningsÚwarningr   r   r   r·   Ï  s   ÿýz,DummyRequestTests.test_getClientIPDeprecatedc                 C  s.   t g ƒ}tdddƒ}||_|  d| ¡ ¡ dS )z�
        L{DummyRequest.getClientIP} supports IPv6 addresses, just like
        L{twisted.web.http.Request.getClientIP}.
        r*   z::1é90  N)r   r   ro   r¸   rª   )r/   rW   ro   r   r   r   Útest_getClientIPSupportsIPv6è  s   z.DummyRequestTests.test_getClientIPSupportsIPv6c                 C  s    t g ƒ}| ¡ }t t|¡ dS )zu
        L{DummyRequest.getClientAddress} returns an L{IAddress}
        provider no C{client} has been set.
        N)r   r§   r
   ZverifyObjectr   )r/   rW   Únullr   r   r   Ú"test_getClientAddressWithoutClientó  s   z4DummyRequestTests.test_getClientAddressWithoutClientc                 C  s2   t g ƒ}tdddƒ}||_| ¡ }|  ||¡ dS )zI
        L{DummyRequest.getClientAddress} returns the C{client}.
        r*   r¬   r¾   N)r   r   ro   r§   ZassertIs)r/   rW   ro   Úaddressr   r   r   Útest_getClientAddressü  s
   z'DummyRequestTests.test_getClientAddressN)r%   r&   r'   r(   r·   r¿   rÁ   rÃ   r   r   r   r   rµ   Ê  s    	rµ   N)1r(   Ú
__future__r   Ú__all__Úior   Útypingr   r   r   Zzope.interfacer	   r
   Úincrementalr   Ztwisted.internet.addressr   r   Ztwisted.internet.deferr   Ztwisted.internet.interfacesr   r   Ztwisted.internet.taskr   Ztwisted.python.deprecater   Ztwisted.trialr   Ztwisted.web._responsesr   Ztwisted.web.http_headersr   Ztwisted.web.resourcer   Ztwisted.web.serverr   r   r   ZtextLinearWhitespaceComponentsZsanitizedTextZbytesLinearWhitespaceComponentsr"   ZsanitizedBytesr$   r   r   ZSynchronousTestCaserµ   r   r   r   r   Ú<module>   s>   ÿ
   