
    Vh<                       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  cg c]  } d|  d
 c} Z'dZ(e'D cg c]  }|jS                  d       c}Z*e(jS                  d      Z+ ee       G d d             Z, G d d      Z- G d d      Z. G d dej^                        Z0yc c} w c c}w )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Site)
z
FoobarzFoo barasciic                      e Zd ZdZy)NullAddressz/
    A null implementation of L{IAddress}.
    N)__name__
__module____qualname____doc__     N/home/dcms/DCMS/lib/python3.12/site-packages/twisted/web/test/requesthelper.pyr!   r!   '   s    r'   r!   c                      e Zd Z G d d      Z ee       G d de             Z e e             Z	ddZ
d Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)r   c                  F    e Zd ZdZdZddZd Zd Zd Zd Z	d	 Z
d
 Zd Zy)DummyChannel.TCPP   FNc                \    |t        ddd      }|| _        t               | _        g | _        y )NTCPz192.168.1.1i80  )r   _peerr   written	producersselfpeers     r(   __init__zDummyChannel.TCP.__init__3   s,    |"5-?DJ"9DLDNr'   c                    | j                   S N)r/   r3   s    r(   getPeerzDummyChannel.TCP.getPeer:   s    ::r'   c                v    t        |t              st        d|      | j                  j	                  |       y )Nz)Can only write bytes to a transport, not )
isinstancebytes	TypeErrorr0   writer3   datas     r(   r>   zDummyChannel.TCP.write=   s1    dE*"KD8 TUULLt$r'   c                4    |D ]  }| j                  |        y r7   )r>   )r3   iovecr@   s      r(   writeSequencezDummyChannel.TCP.writeSequenceB   s     !

4 !r'   c                0    t        dd| j                        S )Nr.   z10.0.0.1)r   portr8   s    r(   getHostzDummyChannel.TCP.getHostF   s    uj$))<<r'   c                >    | j                   j                  ||f       y r7   )r1   appendr3   producer	streamings      r(   registerProducerz!DummyChannel.TCP.registerProducerI   s    NN!!8Y"78r'   c                     y r7   r&   r8   s    r(   unregisterProducerz#DummyChannel.TCP.unregisterProducerL   s    r'   c                    d| _         y )NT)disconnectedr8   s    r(   loseConnectionzDummyChannel.TCP.loseConnectionO   s
     $Dr'   r7   )r"   r#   r$   rE   rP   r5   r9   r>   rC   rF   rL   rN   rQ   r&   r'   r(   r.   r+   /   s4    	 		%
	!	=	9		%r'   r.   c                  6    e Zd Zd Zd Zd Zd Zd Zd Zd Z	y)	DummyChannel.SSLc                     y r7   r&   r8   s    r(   abortConnectionz DummyChannel.SSL.abortConnectionT       r'   c                     y r7   r&   r8   s    r(   getTcpKeepAlivez DummyChannel.SSL.getTcpKeepAliveX   rV   r'   c                     y r7   r&   r8   s    r(   getTcpNoDelayzDummyChannel.SSL.getTcpNoDelay\   rV   r'   c                     y r7   r&   r8   s    r(   loseWriteConnectionz$DummyChannel.SSL.loseWriteConnection`   rV   r'   c                     y r7   r&   r3   enableds     r(   setTcpKeepAlivez DummyChannel.SSL.setTcpKeepAlived   rV   r'   c                     y r7   r&   r^   s     r(   setTcpNoDelayzDummyChannel.SSL.setTcpNoDelayh   rV   r'   c                     y r7   r&   r8   s    r(   getPeerCertificatez#DummyChannel.SSL.getPeerCertificatel   rV   r'   N)
r"   r#   r$   rU   rX   rZ   r\   r`   rb   rd   r&   r'   r(   SSLrS   R   s%    							r'   re   Nc                0    | j                  |      | _        y r7   )r.   	transportr2   s     r(   r5   zDummyChannel.__init__r   s    $r'   c                     y r7   r&   )r3   requests     r(   requestDonezDummyChannel.requestDoneu       r'   c                <   t        |t              r-|j                         D cg c]  \  }}|D ]  }||f  }}}}|dz   |z   dz   |z   dz   }|g}	|	j                  d |D               |	j	                  d       | j
                  j                  |	       y c c}}}w )N       
c              3  8   K   | ]  \  }}|d z   |z   dz     yw)s   : rn   Nr&   ).0namevalues      r(   	<genexpr>z,DummyChannel.writeHeaders.<locals>.<genexpr>   s"     XudUlU2W<Xs   )r;   r   getAllRawHeadersextendrH   rg   rC   )
r3   versioncodereasonheaderskvaluesvresponse_lineheaderSequences
             r(   writeHeaderszDummyChannel.writeHeadersx   s    gw'*1*B*B*D &AvfIJAG   $-4v=G'XPWXXg&$$^4s   Bc                6    | j                   j                         S r7   )rg   r9   r8   s    r(   r9   zDummyChannel.getPeer       ~~%%''r'   c                6    | j                   j                         S r7   )rg   rF   r8   s    r(   rF   zDummyChannel.getHost   r   r'   c                <    | j                   j                  ||       y r7   )rg   rL   rI   s      r(   rL   zDummyChannel.registerProducer   s    '')<r'   c                8    | j                   j                          y r7   )rg   rN   r8   s    r(   rN   zDummyChannel.unregisterProducer   s    ))+r'   c                :    | j                   j                  |       y r7   )rg   r>   r?   s     r(   r>   zDummyChannel.write   s    T"r'   c                :    | j                   j                  |       y r7   )rg   rC   )r3   rB   s     r(   rC   zDummyChannel.writeSequence   s    $$U+r'   c                8    | j                   j                          y r7   )rg   rQ   r8   s    r(   rQ   zDummyChannel.loseConnection   s    %%'r'   c                     y r7   r&   r8   s    r(   
endRequestzDummyChannel.endRequest   rk   r'   c                B    t        | j                  | j                        S r7   )r;   rg   re   r8   s    r(   isSecurezDummyChannel.isSecure   s    $..$((33r'   c                     y r7   r&   r8   s    r(   rU   zDummyChannel.abortConnection       r'   c                     y r7   r&   r8   s    r(   rX   zDummyChannel.getTcpKeepAlive   r   r'   c                     y r7   r&   r8   s    r(   rZ   zDummyChannel.getTcpNoDelay   r   r'   c                     y r7   r&   r8   s    r(   r\   z DummyChannel.loseWriteConnection   r   r'   c                     y r7   r&   r8   s    r(   r`   zDummyChannel.setTcpKeepAlive   r   r'   c                     y r7   r&   r8   s    r(   rb   zDummyChannel.setTcpNoDelay   r   r'   c                     y r7   r&   r8   s    r(   rd   zDummyChannel.getPeerCertificate   r   r'   r7   )r"   r#   r$   r.   r
   r   re   r   r   siter5   rj   r   r9   rF   rL   rN   r>   rC   rQ   r   r   rU   rX   rZ   r\   r`   rb   rd   r&   r'   r(   r   r   .   s    !% !%F c   : 
D(	5((=,#,(4r'   c                  (   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 Zd Z		 	 d+	 	 	 	 	 	 	 d,dZ
d Zd Zd Zd-dZd Zd Zd.dZd Zd Zd Zd-dZd Zd Z e edd d!d"      d#$      d%        Zd& Zd' Zd( Zd/d)Zd* Zy)0r   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]sitepathr0   prepathzDict[bytes, List[bytes]]argszList[Deferred[None]]_finishedDeferredsc                f    d| _         | j                   r|j                          | j                   ryy)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)r3   prodss      r(   rL   zDummyRequest.registerProducer   s'     gg  " ggr'   c                    d| _         y )Nr   )r   r8   s    r(   rN   zDummyRequest.unregisterProducer   s	    r'   c                   g | _         g | _        d| _        || _        g | _        d | _        |xs t        d dt                     | _        i | _	        t               | _        t               | _        d | _        g | _        d| _        d| _        y )Nr      0)r   uidreactors   dummys   HTTP/1.0)r   r0   finishedpostpathr   sessionr   r   protoSessionr   r   requestHeadersresponseHeadersresponseCoder   _serverNameclientproto)r3   r   r   r   s       r(   r5   zDummyRequest.__init__   s      #TwDdEG'T	%i&y "$#&r'   c                z    i }| j                   j                         D ]  \  }}|d   ||j                         <    |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   rt   lower)r3   ry   rz   r|   s       r(   getAllHeaderszDummyRequest.getAllHeaders  sD     ''88: 	'DAq!"2GAGGI	'r'   c                ^    | j                   j                  |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   )r3   rq   s     r(   	getHeaderzDummyRequest.getHeader  s*     ""00vFqIIr'   c                <    | j                   j                  ||       y)zATODO: make this assert on write() if the header is content-lengthN)r   addRawHeaderr3   rq   rr   s      r(   	setHeaderzDummyRequest.setHeader   s    ))$6r'   c                    | j                   r| j                   S | j                  rJ d       | j                  | _         | j                   S )Nz8Session cannot be requested after data has been written.)r   r0   r   )r3   sessionInterfaces     r(   
getSessionzDummyRequest.getSession$  sC    <<<<	FE	F((||r'   c                z    |j                  |       }|t        u ry| j                  |       | j                          y)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)r3   resourceresults      r(   r   zDummyRequest.render-  s2     &\!

6r'   c                p    t        |t              st        d      | j                  j	                  |       y )Nzwrite() only accepts bytes)r;   r<   r=   r0   rH   r?   s     r(   r>   zDummyRequest.write@  s*    $&899D!r'   c                P    t               }| j                  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   r   rH   )r3   r   s     r(   notifyFinishzDummyRequest.notifyFinishE  s$     $,:&&x0r'   c                    | j                   dz   | _         | j                  ,| j                  }d| _        |D ]  }|j                  d        yy)z}
        Record that the request is finished and callback and L{Deferred}s
        waiting for notification of this.
        r   N)r   r   callback)r3   	observersobss      r(   r   zDummyRequest.finishO  sS    
 )"".//I&*D#  #T"# /r'   c                t    | j                   ,| j                   }d| _         |D ]  }|j                  |        yy)zK
        Errback and L{Deferreds} waiting for finish notification.
        N)r   errback)r3   rx   r   r   s       r(   processingFailedzDummyRequest.processingFailed[  sC     "".//I&*D#  $F#$ /r'   c                $    |g| j                   |<   y r7   )r   r   s      r(   addArgzDummyRequest.addArge  s     '		$r'   c                    | j                   r/J dj                  dj                  | j                                      || _        || _        y)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)r0   formatjoinr   responseMessage)r3   rw   messages      r(   setResponseCodezDummyRequest.setResponseCodeh  sH     	
KRRKK%
	
 !&r'   c                z    | j                   r/J dj                  dj                  | j                                      y )Nz<Last-Modified cannot be set after data has been written: {}.r   r0   r   r   )r3   whens     r(   setLastModifiedzDummyRequest.setLastModifiedu  s6    	
LSSKK%
	
r'   c                z    | j                   r/J dj                  dj                  | j                                      y )Nz3ETag cannot be set after data has been written: {}.r   r   )r3   tags     r(   setETagzDummyRequest.setETag|  s6    	
CJJKK%
	
r'   Twisted      r   getClientAddress)replacementc                p    t        | j                  t        t        f      r| j                  j                  S y)z|
        Return the IPv4 address of the client which made this request, if there
        is one, otherwise L{None}.
        N)r;   r   r   r   hostr8   s    r(   getClientIPzDummyRequest.getClientIP  s*     dkkK#=>;;###r'   c                F    | j                   
t               S | j                   S )z
        Return the L{IAddress} of the client that made this request.

        @return: an address.
        @rtype: an L{IAddress} provider.
        )r   r!   r8   s    r(   r   zDummyRequest.getClientAddress  s      ;;= {{r'   c                    | j                   S )z
        Get a dummy hostname associated to the HTTP request.

        @rtype: C{bytes}
        @returns: a dummy hostname
        )r   r8   s    r(   getRequestHostnamezDummyRequest.getRequestHostname  s     r'   c                    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   r8   s    r(   rF   zDummyRequest.getHost  s     5+r22r'   c                    || _         | j                         rd}nd}||k(  r|}nd||fz  }| j                  j                  d|       y)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)	_forceSSLr   r   r   )r3   r   rE   ssldefault
hostHeaders         r(   setHostzDummyRequest.setHost  sN     ==?GG7?J!T4L0J((*=r'   c                R    | j                  t               | j                  d|       y)zu
        Utility function that does a redirect.

        The request should have finish() called after this.
        s   locationN)r   r   r   )r3   urls     r(   redirectzDummyRequest.redirect  s      	U#{C(r'   )NN)r   zlist[bytes]r   zOptional[Session]r   r   returnNoner7   )r   zDeferred[None])r   ) r"   r#   r$   r%   urimethodr   __annotations__rL   rN   r5   r   r   r   r   r   r>   r   r   r   r   r   r   r   r   r   r   r   r   rF   r   r   r&   r'   r(   r   r      s    . CF!%F%
"",,# &*%)	'' #' #	'
 
'* J7&"

#$"'

 	2q!,:LM N	 3>,)r'   c                  (    e Zd ZdZd Zd Zd Zd Zy)DummyRequestTestsz$
    Tests for L{DummyRequest}.
    c                >   t        g       }|j                          | j                  | j                  g      }| j	                  dt        |             |\  }| j	                  |j                  d      t               | j	                  |j                  d      d       y)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)r3   ri   warningswarnings       r(   r   z,DummyRequestTests.test_getClientIPDeprecated  s     r"%% $ ? ?@ & 
 	CM*	Z02DEKK	"6	
r'   c                    t        g       }t        ddd      }||_        | j                  d|j	                                y)z
        L{DummyRequest.getClientIP} supports IPv6 addresses, just like
        L{twisted.web.http.Request.getClientIP}.
        r.   z::190  N)r   r   r   r   r   )r3   ri   r   s      r(   test_getClientIPSupportsIPv6z.DummyRequestTests.test_getClientIPSupportsIPv6  s<    
 r"UE51 3 3 56r'   c                n    t        g       }|j                         }t        j                  t        |       y)zu
        L{DummyRequest.getClientAddress} returns an L{IAddress}
        provider no C{client} has been set.
        N)r   r   r   verifyObjectr   )r3   ri   nulls      r(   "test_getClientAddressWithoutClientz4DummyRequestTests.test_getClientAddressWithoutClient  s,    
 r"'')Hd+r'   c                    t        g       }t        ddd      }||_        |j                         }| j	                  ||       y)zI
        L{DummyRequest.getClientAddress} returns the C{client}.
        r.   r   r  N)r   r   r   r   assertIs)r3   ri   r   addresss       r(   test_getClientAddressz'DummyRequestTests.test_getClientAddress  s?     r"UK7**,gv&r'   N)r"   r#   r$   r%   r   r  r  r  r&   r'   r(   r   r     s    
2	7,'r'   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encodebytesLinearWhitespaceComponentssanitizedBytesr!   r   r   SynchronousTestCaser   )lw	components   00r(   <module>r(     s    #>
*  ' ' .  = + ? ' / " ( , ) : ::N!OBCt3-!O /M#"+IW#  %%g. X  J JZL) L)^:'44 :'Y "P#s   3C4	C9