o
    >h<                     @  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 )Foobar ).0lwr   r   ~/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/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.TCPP   FNc                 C  s,   |d u r
t ddd}|| _t | _g | _d S )NTCPz192.168.1.1i80  )r   _peerr   written	producersselfpeerr   r   r   __init__3   s
   
zDummyChannel.TCP.__init__c                 C     | j S N)r-   r1   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r1   datar   r   r   r;   =   s   
zDummyChannel.TCP.writec                 C  s   |D ]}|  | qd S r5   )r;   )r1   iovecr=   r   r   r   writeSequenceB   s   zDummyChannel.TCP.writeSequencec                 C  s   t dd| jS )Nr,   z10.0.0.1)r   portr6   r   r   r   getHostF      zDummyChannel.TCP.getHostc                 C  s   | j ||f d S r5   )r/   appendr1   producer	streamingr   r   r   registerProducerI   s   z!DummyChannel.TCP.registerProducerc                 C     d S r5   r   r6   r   r   r   unregisterProducerL      z#DummyChannel.TCP.unregisterProducerc                 C  
   d| _ d S )NT)disconnectedr6   r   r   r   loseConnectionO      
zDummyChannel.TCP.loseConnectionr5   )r'   r(   r)   r@   rL   r3   r7   r;   r?   rA   rG   rI   rM   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  rH   r5   r   r6   r   r   r   abortConnectionT      z DummyChannel.SSL.abortConnectionc                 C  rH   r5   r   r6   r   r   r   getTcpKeepAliveX   rP   z DummyChannel.SSL.getTcpKeepAlivec                 C  rH   r5   r   r6   r   r   r   getTcpNoDelay\   rP   zDummyChannel.SSL.getTcpNoDelayc                 C  rH   r5   r   r6   r   r   r   loseWriteConnection`   rP   z$DummyChannel.SSL.loseWriteConnectionc                 C  rH   r5   r   r1   enabledr   r   r   setTcpKeepAlived   rP   z DummyChannel.SSL.setTcpKeepAlivec                 C  rH   r5   r   rT   r   r   r   setTcpNoDelayh   rP   zDummyChannel.SSL.setTcpNoDelayc                 C  rH   r5   r   r6   r   r   r   getPeerCertificatel   rP   z#DummyChannel.SSL.getPeerCertificateN)
r'   r(   r)   rO   rQ   rR   rS   rV   rW   rX   r   r   r   r   SSLR   s    rY   Nc                 C  s   |  || _d S r5   )r,   	transportr0   r   r   r   r3   r      zDummyChannel.__init__c                 C  rH   r5   r   )r1   requestr   r   r   requestDoneu   rJ   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   : rb   Nr   )r   namevaluer   r   r   	<genexpr>   s   " z,DummyChannel.writeHeaders.<locals>.<genexpr>)r8   r   getAllRawHeadersextendrC   rZ   r?   )r1   versioncodereasonheadersresponse_lineheaderSequencer   r   r   writeHeadersx   s   

zDummyChannel.writeHeadersc                 C  
   | j  S r5   )rZ   r7   r6   r   r   r   r7      rN   zDummyChannel.getPeerc                 C  ro   r5   )rZ   rA   r6   r   r   r   rA      rN   zDummyChannel.getHostc                 C  s   | j || d S r5   )rZ   rG   rD   r   r   r   rG      s   zDummyChannel.registerProducerc                 C     | j   d S r5   )rZ   rI   r6   r   r   r   rI      rB   zDummyChannel.unregisterProducerc                 C     | j | d S r5   )rZ   r;   r<   r   r   r   r;      r[   zDummyChannel.writec                 C  rq   r5   )rZ   r?   )r1   r>   r   r   r   r?      r[   zDummyChannel.writeSequencec                 C  rp   r5   )rZ   rM   r6   r   r   r   rM      rB   zDummyChannel.loseConnectionc                 C  rH   r5   r   r6   r   r   r   
endRequest   rJ   zDummyChannel.endRequestc                 C  s   t | j| jS r5   )r8   rZ   rY   r6   r   r   r   isSecure   rB   zDummyChannel.isSecurec                 C  rH   r5   r   r6   r   r   r   rO      rP   zDummyChannel.abortConnectionc                 C  rH   r5   r   r6   r   r   r   rQ      rP   zDummyChannel.getTcpKeepAlivec                 C  rH   r5   r   r6   r   r   r   rR      rP   zDummyChannel.getTcpNoDelayc                 C  rH   r5   r   r6   r   r   r   rS      rP   z DummyChannel.loseWriteConnectionc                 C  rH   r5   r   r6   r   r   r   rV      rP   zDummyChannel.setTcpKeepAlivec                 C  rH   r5   r   r6   r   r   r   rW      rP   zDummyChannel.setTcpNoDelayc                 C  rH   r5   r   r6   r   r   r   rX      rP   zDummyChannel.getPeerCertificater5   )r'   r(   r)   r,   r	   r   rY   r   r   siter3   r]   rn   r7   rA   rG   rI   r;   r?   rM   rr   rs   rO   rQ   rR   rS   rV   rW   rX   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)goresumeProducing)r1   prodsr   r   r   rG      s   zDummyRequest.registerProducerc                 C  rK   )Nr   )r|   r6   r   r   r   rI      rN   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)rt   uidreactors   dummys   HTTP/1.0)rw   r.   finishedr   rx   r   r   r   protoSessionry   r   requestHeadersresponseHeadersresponseCoderz   _serverNameclientproto)r1   r   r   rv   r   r   r   r3      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}.
        )r   rf   lower)r1   rk   r^   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   getRawHeadersr   )r1   rc   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r1   rc   rd   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.)r   r.   r   )r1   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   r;   finish)r1   resourceresultr   r   r   r   -  s
   

zDummyRequest.renderc                 C  s"   t |ts	td| j| d S )Nzwrite() only accepts bytes)r8   r9   r:   r.   rC   r<   r   r   r   r;   @  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.
        )r   rz   rC   )r1   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.
        r{   N)r   rz   callback)r1   	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)rz   errback)r1   rj   r   r   r   r   r   processingFailed[  s   
zDummyRequest.processingFailedc                 C  s   |g| j |< d S r5   )ry   r   r   r   r   addArge  r[   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   responseMessage)r1   ri   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   )r1   whenr   r   r   setLastModifiedu     
zDummyRequest.setLastModifiedc                 C  r   )Nz3ETag cannot be set after data has been written: {}.r   r   )r1   tagr   r   r   setETag|  r   zDummyRequest.setETag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)r8   rv   r   r   hostr6   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)rv   r&   r6   r   r   r   r     s   
zDummyRequest.getClientAddressc                 C  r4   )z
        Get a dummy hostname associated to the HTTP request.

        @rtype: C{bytes}
        @returns: a dummy hostname
        )r   r6   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+   )r   r6   r   r   r   rA     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)	_forceSSLrs   r   r   )r1   r   r@   ssldefault
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   )r1   urlr   r   r   redirect  s   
zDummyRequest.redirect)NN)r   r   r   r   rv   ru   r   r   r5   )r   r   )r   ) r'   r(   r)   r*   urimethodrv   __annotations__rG   rI   r3   r   r   r   r   r   r;   r   r   r   r   r   r   r   r   r   r   r   r   rA   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}
        )offendingFunctionsr{   categoryr   z}twisted.web.test.requesthelper.DummyRequest.getClientIP was deprecated in Twisted 18.4.0; please use getClientAddress insteadN)r   r   flushWarningstest_getClientIPDeprecatedassertEquallengetDeprecationWarning)r1   r\   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::190  N)r   r   rv   r   r   )r1   r\   rv   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
   verifyObjectr   )r1   r\   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   rv   r   assertIs)r1   r\   rv   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   zope.interfacer	   r
   incrementalr   twisted.internet.addressr   r   twisted.internet.deferr   twisted.internet.interfacesr   r   twisted.internet.taskr   twisted.python.deprecater   twisted.trialr   twisted.web._responsesr   twisted.web.http_headersr   twisted.web.resourcer   twisted.web.serverr   r   r   textLinearWhitespaceComponentssanitizedTextbytesLinearWhitespaceComponentsr$   sanitizedBytesr&   r   r   SynchronousTestCaser   r   r   r   r   <module>   s>   
   