
    h<                       S r SSKJr  SS/rSSKJr  SSKJrJrJ	r	  SSK
JrJr  SSKJr  SS	KJrJr  SS
KJr  SSKJrJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJ r   SSK!J"r"  SSK#J$r$J%r%J&r&  S V s/ s H	  n SU  S3PM     sn r'Sr(\' Vs/ s H  oRS                  S5      PM     snr*\(RS                  S5      r+\" \5       " S S5      5       r, " S S5      r- " S S5      r. " S S\R^                  5      r0gs  sn f s  snf )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                      \ rS rSrSrSrg)NullAddress'   z'
A null implementation of L{IAddress}.
 N)__name__
__module____qualname____firstlineno____doc____static_attributes__r#       e/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/web/test/requesthelper.pyr!   r!   '   s    r*   r!   c                      \ rS rSr " S S5      r\" \5       " S S\5      5       r\" \	" 5       5      r
SS jrS rS	 rS
 rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rSrg)r   .   c                  P    \ rS rSrSrSrSS jrS rS rS r	S	 r
S
 rS rS rSrg)DummyChannel.TCP/   P   FNc                \    Uc  [        SSS5      nXl        [        5       U l        / U l        g )NTCPz192.168.1.1i80  )r   _peerr   written	producersselfpeers     r+   __init__DummyChannel.TCP.__init__3   s*    |"5-?J"9DLDNr*   c                    U R                   $ N)r4   r8   s    r+   getPeerDummyChannel.TCP.getPeer:   s    ::r*   c                    [        U[        5      (       d  [        SU< 35      eU R                  R	                  U5        g )Nz)Can only write bytes to a transport, not )
isinstancebytes	TypeErrorr5   writer8   datas     r+   rE   DummyChannel.TCP.write=   s4    dE**"KD8 TUULLt$r*   c                8    U H  nU R                  U5        M     g r=   )rE   )r8   iovecrG   s      r+   writeSequenceDummyChannel.TCP.writeSequenceB   s    

4  r*   c                0    [        SSU R                  5      $ )Nr3   z10.0.0.1)r   portr>   s    r+   getHostDummyChannel.TCP.getHostF   s    uj$))<<r*   c                <    U R                   R                  X45        g r=   )r6   appendr8   producer	streamings      r+   registerProducer!DummyChannel.TCP.registerProducerI   s    NN!!8"78r*   c                    g r=   r#   r>   s    r+   unregisterProducer#DummyChannel.TCP.unregisterProducerL   s    r*   c                    SU l         g )NT)disconnectedr>   s    r+   loseConnectionDummyChannel.TCP.loseConnectionO   s
     $Dr*   )r4   r\   r6   r5   r=   )r$   r%   r&   r'   rN   r\   r:   r?   rE   rK   rO   rV   rY   r]   r)   r#   r*   r+   r3   r/   /   s4    	 		%
	!	=	9		%r*   r3   c                  >    \ rS rSrS rS rS rS rS rS r	S r
S	rg
)DummyChannel.SSLR   c                    g r=   r#   r>   s    r+   abortConnection DummyChannel.SSL.abortConnectionT       r*   c                    g r=   r#   r>   s    r+   getTcpKeepAlive DummyChannel.SSL.getTcpKeepAliveX   re   r*   c                    g r=   r#   r>   s    r+   getTcpNoDelayDummyChannel.SSL.getTcpNoDelay\   re   r*   c                    g r=   r#   r>   s    r+   loseWriteConnection$DummyChannel.SSL.loseWriteConnection`   re   r*   c                    g r=   r#   r8   enableds     r+   setTcpKeepAlive DummyChannel.SSL.setTcpKeepAlived   re   r*   c                    g r=   r#   rp   s     r+   setTcpNoDelayDummyChannel.SSL.setTcpNoDelayh   re   r*   c                    g r=   r#   r>   s    r+   getPeerCertificate#DummyChannel.SSL.getPeerCertificatel   re   r*   r#   N)r$   r%   r&   r'   rc   rg   rj   rm   rr   ru   rx   r)   r#   r*   r+   SSLr`   R   s%    							r*   rz   Nc                0    U R                  U5      U l        g r=   )r3   	transportr7   s     r+   r:   DummyChannel.__init__r   s    $r*   c                    g r=   r#   )r8   requests     r+   requestDoneDummyChannel.requestDoneu       r*   c                N   [        U[        5      (       a1  UR                  5        VVVs/ s H  u  pVU  H  ouU4PM     M     nnnnUS-   U-   S-   U-   S-   nU/n	U	R                  S U 5       5        U	R	                  S5        U R
                  R                  U	5        g s  snnnf )N       
c              3  <   #    U  H  u  pUS -   U-   S-   v   M     g7f)s   : r   Nr#   ).0namevalues      r+   	<genexpr>,DummyChannel.writeHeaders.<locals>.<genexpr>   s"     XPWdUlU2W<PWs   )rB   r   getAllRawHeadersextendrR   r|   rK   )
r8   versioncodereasonheaderskvaluesvresponse_lineheaderSequences
             r+   writeHeadersDummyChannel.writeHeadersx   s    gw''*1*B*B*D*D;AfAf*D    $-4v=G'XPWXXg&$$^4s   B c                6    U R                   R                  5       $ r=   )r|   r?   r>   s    r+   r?   DummyChannel.getPeer       ~~%%''r*   c                6    U R                   R                  5       $ r=   )r|   rO   r>   s    r+   rO   DummyChannel.getHost   r   r*   c                :    U R                   R                  X5        g r=   )r|   rV   rS   s      r+   rV   DummyChannel.registerProducer   s    ''<r*   c                8    U R                   R                  5         g r=   )r|   rY   r>   s    r+   rY   DummyChannel.unregisterProducer   s    ))+r*   c                :    U R                   R                  U5        g r=   )r|   rE   rF   s     r+   rE   DummyChannel.write   s    T"r*   c                :    U R                   R                  U5        g r=   )r|   rK   )r8   rJ   s     r+   rK   DummyChannel.writeSequence   s    $$U+r*   c                8    U R                   R                  5         g r=   )r|   r]   r>   s    r+   r]   DummyChannel.loseConnection   s    %%'r*   c                    g r=   r#   r>   s    r+   
endRequestDummyChannel.endRequest   r   r*   c                B    [        U R                  U R                  5      $ r=   )rB   r|   rz   r>   s    r+   isSecureDummyChannel.isSecure   s    $..$((33r*   c                    g r=   r#   r>   s    r+   rc   DummyChannel.abortConnection       r*   c                    g r=   r#   r>   s    r+   rg   DummyChannel.getTcpKeepAlive   r   r*   c                    g r=   r#   r>   s    r+   rj   DummyChannel.getTcpNoDelay   r   r*   c                    g r=   r#   r>   s    r+   rm    DummyChannel.loseWriteConnection   r   r*   c                    g r=   r#   r>   s    r+   rr   DummyChannel.setTcpKeepAlive   r   r*   c                    g r=   r#   r>   s    r+   ru   DummyChannel.setTcpNoDelay   r   r*   c                    g r=   r#   r>   s    r+   rx   DummyChannel.getPeerCertificate   r   r*   )r|   r=   )r$   r%   r&   r'   r3   r
   r   rz   r   r   siter:   r   r   r?   rO   rV   rY   rE   rK   r]   r   r   rc   rg   rj   rm   rr   ru   rx   r)   r#   r*   r+   r   r   .   s    !% !%F c   : 
D(	5((=,#,(4r*   c                  6   \ rS rSr% SrSrSrSrS\S'   S\S	'   S\S
'   S\S'   S\S'   S\S'   S r	S r
  S-       S.S jjrS rS rS rS/S jrS rS rS0S jrS rS rS rS/S jrS rS r\" \" S S!S"S#5      S$S%9S& 5       rS' rS( rS) rS1S* jrS+ r S,r!g)2r      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]sitepathr5   prepathzDict[bytes, List[bytes]]argszList[Deferred[None]]_finishedDeferredsc                |    SU l         U R                   (       a$  UR                  5         U R                   (       a  M#  gg)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)r8   prodss      r+   rV   DummyRequest.registerProducer   s)     gg  " gggr*   c                    SU l         g )Nr   )r   r>   s    r+   rY   DummyRequest.unregisterProducer   s	    r*   c                   / U l         / U l        SU l        Xl        / U l        S U l        U=(       d    [        S S[        5       S9U l        0 U l	        [        5       U l        [        5       U l        S U l        / U l        SU l        SU l        g )Nr      0)r   uidreactors   dummys   HTTP/1.0)r   r5   finishedpostpathr   sessionr   r   protoSessionr   r   requestHeadersresponseHeadersresponseCoder   _serverNameclientproto)r8   r   r   r   s       r+   r:   DummyRequest.__init__   s      #TwDdEG'T	%i&y "$#&r*   c                z    0 nU R                   R                  5        H  u  p#US   XR                  5       '   M     U$ )a0  
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   r   lower)r8   r   r   r   s       r+   getAllHeadersDummyRequest.getAllHeaders  s;     ''88:DA!"2GGGI ;r*   c                ^    U R                   R                  UR                  5       S/5      S   $ )a  
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   )r8   r   s     r+   	getHeaderDummyRequest.getHeader  s*     ""00vFqIIr*   c                :    U R                   R                  X5        g)zATODO: make this assert on write() if the header is content-lengthN)r   addRawHeaderr8   r   r   s      r+   	setHeaderDummyRequest.setHeader   s    ))$6r*   c                    U R                   (       a  U R                   $ U R                  (       a   S5       eU R                  U l         U R                   $ )Nz8Session cannot be requested after data has been written.)r   r5   r   )r8   sessionInterfaces     r+   
getSessionDummyRequest.getSession$  sC    <<<<	FE	F((||r*   c                |    UR                  U 5      nU[        L a  gU R                  U5        U R                  5         g)aP  
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   rE   finish)r8   resourceresults      r+   r   DummyRequest.render-  s2     &\!

6r*   c                z    [        U[        5      (       d  [        S5      eU R                  R	                  U5        g )Nzwrite() only accepts bytes)rB   rC   rD   r5   rR   rF   s     r+   rE   DummyRequest.write@  s-    $&&899D!r*   c                P    [        5       nU R                  R                  U5        U$ )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   rR   )r8   r   s     r+   notifyFinishDummyRequest.notifyFinishE  s$     $,:&&x0r*   c                    U R                   S-   U l         U R                  b.  U R                  nSU l        U H  nUR                  S5        M     gg)ze
Record that the request is finished and callback and L{Deferred}s
waiting for notification of this.
r   N)r   r   callback)r8   	observersobss      r+   r   DummyRequest.finishO  sN    
 )"".//I&*D# T" ! /r*   c                z    U R                   b.  U R                   nSU l         U H  nUR                  U5        M     gg)z;
Errback and L{Deferreds} waiting for finish notification.
N)r   errback)r8   r   r   r   s       r+   processingFailedDummyRequest.processingFailed[  s>     "".//I&*D# F# ! /r*   c                $    U/U R                   U'   g r=   )r   r   s      r+   addArgDummyRequest.addArge  s     '		$r*   c                    U R                   (       a/   SR                  SR                  U R                   5      5      5       eXl        X l        g)zc
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)r5   formatjoinr   responseMessage)r8   r   messages      r+   setResponseCodeDummyRequest.setResponseCodeh  sD     	
KRRKK%
	
 !&r*   c                    U R                   (       a/   SR                  SR                  U R                   5      5      5       eg )Nz<Last-Modified cannot be set after data has been written: {}.r  r5   r  r	  )r8   whens     r+   setLastModifiedDummyRequest.setLastModifiedu  s6    	
LSSKK%
	
r*   c                    U R                   (       a/   SR                  SR                  U R                   5      5      5       eg )Nz3ETag cannot be set after data has been written: {}.r  r  )r8   tags     r+   setETagDummyRequest.setETag|  s6    	
CJJKK%
	
r*   Twisted      r   getClientAddress)replacementc                z    [        U R                  [        [        45      (       a  U R                  R                  $ g)zd
Return the IPv4 address of the client which made this request, if there
is one, otherwise L{None}.
N)rB   r   r   r   hostr>   s    r+   getClientIPDummyRequest.getClientIP  s-     dkkK#=>>;;###r*   c                H    U R                   c
  [        5       $ U R                   $ )zu
Return the L{IAddress} of the client that made this request.

@return: an address.
@rtype: an L{IAddress} provider.
)r   r!   r>   s    r+   r  DummyRequest.getClientAddress  s      ;;= {{r*   c                    U R                   $ )zc
Get a dummy hostname associated to the HTTP request.

@rtype: C{bytes}
@returns: a dummy hostname
)r   r>   s    r+   getRequestHostnameDummyRequest.getRequestHostname  s     r*   c                    [        SSS5      $ )zZ
Get a dummy transport's host.

@rtype: C{IPv4Address}
@returns: a dummy transport's host
r3   	127.0.0.1r1   )r   r>   s    r+   rO   DummyRequest.getHost  s     5+r22r*   c                    X0l         U R                  5       (       a  SnOSnX$:X  a  UnOSX4-  nU R                  R                  SU5        g)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  r1   s   %b:%ds   hostN)	_forceSSLr   r   r   )r8   r  rN   ssldefault
hostHeaders         r+   setHostDummyRequest.setHost  sJ     ==??GG?J!TL0J((*=r*   c                R    U R                  [        5        U R                  SU5        g)z]
Utility function that does a redirect.

The request should have finish() called after this.
s   locationN)r  r   r   )r8   urls     r+   redirectDummyRequest.redirect  s      	U#{C(r*   )r   r)  r   r   r   r   r   r   r   r   r   r   r   r
  r   r   r5   )NN)r   zlist[bytes]r   zOptional[Session]r   r   returnNoner=   )r3  zDeferred[None])r   )"r$   r%   r&   r'   r(   urimethodr   __annotations__rV   rY   r:   r   r   r   r   r   rE   r   r   r  r  r  r  r  r   r   r  r  r#  rO   r-  r1  r)   r#   r*   r+   r   r      s    . CF!%F%
"",,# &*%)	'' #' #	'
 
'* J7&"

#$"'

 	2q!,:LM N	 3>,)r*   c                  0    \ rS rSrSrS rS rS rS rSr	g)	DummyRequestTestsi  z
Tests for L{DummyRequest}.
c                8   [        / 5      nUR                  5         U R                  U R                  /S9nU R	                  S[        U5      5        Uu  nU R	                  UR                  S5      [        5        U R	                  UR                  S5      S5        g)zX
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)r8   r   warningswarnings       r+   r>  ,DummyRequestTests.test_getClientIPDeprecated  s     r"%% $ ? ?@ & 
 	CM*	Z02DEKK	"6	
r*   c                    [        / 5      n[        SSS5      nX!l        U R                  SUR	                  5       5        g)zi
L{DummyRequest.getClientIP} supports IPv6 addresses, just like
L{twisted.web.http.Request.getClientIP}.
r3   z::190  N)r   r   r   r?  r  )r8   r   r   s      r+   test_getClientIPSupportsIPv6.DummyRequestTests.test_getClientIPSupportsIPv6  s:    
 r"UE51 3 3 56r*   c                p    [        / 5      nUR                  5       n[        R                  " [        U5        g)z]
L{DummyRequest.getClientAddress} returns an L{IAddress}
provider no C{client} has been set.
N)r   r  r   verifyObjectr   )r8   r   nulls      r+   "test_getClientAddressWithoutClient4DummyRequestTests.test_getClientAddressWithoutClient  s,    
 r"'')Hd+r*   c                    [        / 5      n[        SSS5      nX!l        UR                  5       nU R	                  X25        g)z9
L{DummyRequest.getClientAddress} returns the C{client}.
r3   r&  rG  N)r   r   r   r  assertIs)r8   r   r   addresss       r+   test_getClientAddress'DummyRequestTests.test_getClientAddress  s;     r"UK7**,g&r*   r#   N)
r$   r%   r&   r'   r(   r>  rH  rM  rR  r)   r#   r*   r+   r9  r9    s    
2	7,'r*   r9  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   SynchronousTestCaser9  )lw	components   00r+   <module>rl     s    #>
*  ' ' .  = + ? ' / " ( , ) : ::N!O:NBCt3-:N!O /M#/M)W/M#  %%g. X  J JZL) L)^:'44 :'Y "P#s   3C9C>