+
    DfjLN                         R t ^ RIHtHt ^ RIHt ^ RIHtHtH	t	H
t
Ht ^ RIHt ^ RIHt ^ RIHt  ! R R]4      t ! R	 R
4      t ! R R]4      t ! R R]4      t ! R R]4      t ! R R4      t ! R R]4      tR# )z 
Test for L{twisted.web.proxy}.
)MemoryReactor StringTransportWithDisconnection)TestCase)ProxyClientProxyClientFactoryProxyRequestReverseProxyRequestReverseProxyResource)Resource)SiteDummyRequestc                   H   a  ] tR t^t o RtR tR tR tR tR t	R t
RtV tR	# )
ReverseProxyResourceTestsz$
Tests for L{ReverseProxyResource}.
c                h   \        4       p\        4       p\        RRRV4      pVP                  RV4       \	        V4      p\        4       pVP                  R4      pVP                  V4       V P                  VP                  R4       VP                  RV,           R,           4       VP                  w  w  rrpV P                  V	R4       V P                  V
R4       V P                  V\        4       V P                  VP                  V4       V P                  VP                   R,          R	4       R# )
z
Check that a request pointing at C{uri} produce a new proxy connection,
with the path of this request pointing at C{expectedURI}.
	127.0.0.1     /paths   indexNs   GET s     HTTP/1.1
Accept: text/html

   hosts   127.0.0.1:1234)r
   r   r	   putChildr   r   buildProtocolmakeConnection
addCleanupconnectionLostdataReceived
tcpClientsassertEqualassertIsInstancer   restheaders)selfuriexpectedURIrootreactorresourcesite	transportchannelhostportfactory_timeout
_bind_addrs   &&&           =/usr/lib/python3/dist-packages/twisted/web/test/test_proxy.py_testRender%ReverseProxyResourceTests._testRender   s   
 z/'T8WMh)Dz46	$$T*y)..5Ws]-VVW8?8J8J5	4$g{+t$ 	g'9:{313DE    c                &    V P                  RR4      # )z
Test that L{ReverseProxyResource.render} initiates a connection to the
given server with a L{ProxyClientFactory} as parameter.
s   /indexr   r/   r    s   &r.   test_render%ReverseProxyResourceTests.test_render8   s    
 	844r1   c                &    V P                  RR4      # )z
Test that L{ReverseProxyResource.render} will instantiate a child
resource that will initiate a connection to the given server
requesting the apropiate url subpath.
s   /index/page1s   /path/page1r3   r4   s   &r.   test_render_subpage-ReverseProxyResourceTests.test_render_subpage?   s     @@r1   c                &    V P                  RR4      # )zZ
Test that L{ReverseProxyResource.render} passes query parameters to the
created factory.
s   /index?foo=bars   /path?foo=barr3   r4   s   &r.   test_renderWithQuery.ReverseProxyResourceTests.test_renderWithQueryG   s    
  13CDDr1   c                z   \        4       p\        RRRV4      pVP                  RR4      pV P                  V\        4       V P	                  VP
                  R4       V P	                  VP                  R4       V P	                  VP                  R4       V P                  VP                  VP                  4       R# )z
The L{ReverseProxyResource.getChild} method should return a resource
instance with the same class as the originating resource, forward
port, host, and reactor values, and update the path value with the
value passed.
r   r   r      fooNs	   /path/foo)
r   r	   getChildr   r   pathr*   r)   assertIdenticalr$   )r    r$   r%   childs   &   r.   test_getChild'ReverseProxyResourceTests.test_getChildN   s      /'T8WM!!&$/e%9:\2T*[1U]]H,<,<=r1   c                |    \        RRR4      pVP                  RR4      pV P                  VP                  R4       R# )z]
The L{ReverseProxyResource} return by C{getChild} has a path which has
already been quoted.
r   r   r   s    /%Ns   /path/%20%2F%25)r	   r?   r   r@   )r    r%   rB   s   &  r.   test_getChildWithSpecial2ReverseProxyResourceTests.test_getChildWithSpecial_   s9    
 (T8D!!&$/%78r1    N)__name__
__module____qualname____firstlineno____doc__r/   r5   r8   r;   rC   rF   __static_attributes____classdictcell____classdict__s   @r.   r   r      s0     F:5AE>"9 9r1   r   c                   <   a  ] tR t^it o RtR tR tR tR tRt	V t
R# )DummyChannelz
A dummy HTTP channel, that does nothing but holds a transport and saves
connection lost.

@ivar transport: the transport used by the client.
@ivar lostReason: the reason saved at connection lost.
c                     Wn         RV n        R# )z$
Hold a reference to the transport.
N)r'   
lostReason)r    r'   s   &&r.   __init__DummyChannel.__init__r   s     #r1   c                    Wn         R# )z+
Keep track of the connection lost reason.
N)rU   )r    reasons   &&r.   r   DummyChannel.connectionLosty   s	     !r1   c                6    V P                   P                  4       # )z*
Get peer information from the transport.
)r'   getPeerr4   s   &r.   r\   DummyChannel.getPeer        ~~%%''r1   c                6    V P                   P                  4       # )z*
Get host information from the transport.
)r'   getHostr4   s   &r.   r`   DummyChannel.getHost   r^   r1   )rU   r'   N)rI   rJ   rK   rL   rM   rV   r   r\   r`   rN   rO   rP   s   @r.   rS   rS   i   s#     !(( (r1   rS   c                      a  ] tR t^t o RtR tR tRR ltR tR t	R t
R	 tRR
 ltR tR tR tR tR tR tR tR tRtV tR# )ProxyClientTestsz
Tests for L{ProxyClient}.
c                    VP                  R4      w  r#VP                  R4      pVP                  ^ 4      pV\        R V 4       4      V3# )a  
Parse the headers out of some web content.

@param content: Bytes received from a web server.
@return: A tuple of (requestLine, headers, body). C{headers} is a dict
    of headers, C{requestLine} is the first line (e.g. "POST /foo ...")
    and C{body} is whatever is left.
s   

   
c              3   B   "   T F  qP                  R 4      x  K  	  R# 5i)   : N)split).0headers   & r.   	<genexpr>4ProxyClientTests._parseOutHeaders.<locals>.<genexpr>   s     !LG&,,u"5"5Gs   )rh   popdict)r    contentr   bodyrequestLines   &&   r.   _parseOutHeaders!ProxyClientTests._parseOutHeaders   sG      k2--(kk!nT!LG!LLdSSr1   c                    \        V4      # )z}
Make a dummy request object for the URL path.

@param path: A URL path, beginning with a slash.
@return: A L{DummyRequest}.
r   )r    r@   s   &&r.   makeRequestProxyClientTests.makeRequest   s     D!!r1   Nc                T    Vf   RR/pRVP                   ,           p\        W%RW4V4      # )al  
Make a L{ProxyClient} object used for testing.

@param request: The request to use.
@param method: The HTTP method to use, GET by default.
@param headers: The HTTP headers to use expressed as a dict. If not
    provided, defaults to {'accept': 'text/html'}.
@param requestBody: The body of the request. Defaults to the empty
    string.
@return: A L{ProxyClient}
   accept	   text/html   /   HTTP/1.0)postpathr   )r    requestmethodr   requestBodyr@   s   &&&&& r.   makeProxyClient ProxyClientTests.makeProxyClient   s4     ? ,/Gg&&&6gGTTr1   c                H    \        4       pWn        VP                  V4       V# )z
Connect a proxy client to a L{StringTransportWithDisconnection}.

@param proxyClient: A L{ProxyClient}.
@return: The L{StringTransportWithDisconnection}.
)r   protocolr   )r    proxyClientclientTransports   && r.   connectProxyProxyClientTests.connectProxy   s&     ;<#. ""?3r1   c                    V P                  V4       VP                  P                  4       pV P                  V4      w  rVpV P	                  WR4       V P	                  Wc4       V# )a(  
Assert that C{proxyClient} sends C{headers} when it connects.

@param proxyClient: A L{ProxyClient}.
@param requestLine: The request line we expect to be sent.
@param headers: A dict of headers we expect to be sent.
@return: If the assertion is successful, return the request body as
    bytes.
)r   r'   valuerr   r   )r    r   rq   r   requestContentreceivedLinereceivedHeadersrp   s   &&&&    r.   assertForwardsHeaders&ProxyClientTests.assertForwardsHeaders   s\     	+&$..446.2.C.CN.S+t32r1   c                   R \        V4      P                  R4      ,           R,           V,           .pV F-  w  rgV F"  pVP                  VR,           V,           4       K$  	  K/  	  VP                  RV.4       RP	                  V4      # )s	   HTTP/1.0 ascii    rg   r1   re   )strencodeappendextendjoin)	r    codemessager   rp   linesrj   valuesr   s	   &&&&&    r.   makeResponseBytes"ProxyClientTests.makeResponseBytes   st    D	 0 0 99D@7JK%NFVe^e34   & 	c4[!||E""r1   c                   V P                  VP                  V4       V P                  VP                  V4       \        VP                  P                  4       4      pVP                  4        VR,          pVP                  4        V P                  Wg4       V P                  RP                  VP                  4      V4       R# )a  
Assert that C{request} has forwarded a response from the server.

@param request: A L{DummyRequest}.
@param code: The expected HTTP response code.
@param message: The expected HTTP message.
@param headers: The expected HTTP headers.
@param body: The expected response body.
:NNNr1   N)	r   responseCoderesponseMessagelistresponseHeadersgetAllRawHeaderssortr   written)r    r}   r   r   r   rp   r   expectedHeaderss   &&&&&&  r.   assertForwardsResponse'ProxyClientTests.assertForwardsResponse   s     	--t400':w66GGIJ!!*:'//2D9r1   c                   V P                  R4      pV P                  WRR/V4      p	V P                  V	VR,           RRRR/4      p
V P                  W4       V	P	                  V P                  WW44      4       V P                  WW#V4       V'       d   V	P                  P                  4        V P                  V	P                  P                  4       V P                  VP                  ^4       R# )zs
Build a fake proxy connection, and send C{data} over it, checking that
it's forwarded to the originating request.
r>   rx   ry   s    /foo HTTP/1.0
   connection   closeN)ru   r   r   r   r   r   r   r'   loseConnectionassertFalse	connectedfinished)r    r   r   r   rp   r~   r   r   r}   clientreceivedBodys   &&&&&&&&   r.   _testDataForward!ProxyClientTests._testDataForward   s     ""6*%%i6
 11&&Hi>
 	3 	D224'PQ 	##G7TJ ++-
 	))334))1-r1   c                4    V P                  ^RRRR.3.R4      # )z
When connected to the server, L{ProxyClient} should send the saved
request, with modifications of the headers, and then forward the result
to the parent request.
   OK   Foo   bars   baz   Some data
r   r4   s   &r.   test_forwardProxyClientTests.test_forward  s.     $$&66"2346F
 	
r1   c                6    V P                  ^RRR.3.RRR4      # )zf
Try to post content in the request, and check that the proxy client
forward the body of the request.
r   r   r   r      POST   Some contentr   r4   s   &r.   test_postDataProxyClientTests.test_postData'  s/    
 $$&6(+,.>
 	
r1   c                *    V P                  RR. R4      # )zz
If the response contains a status with a message, it should be
forwarded to the parent request with all the information.
i  s	   Not Foundr1   r   r4   s   &r.   test_statusWithMessage'ProxyClientTests.test_statusWithMessage0  s    
 $$S,C@@r1   c           
     x    RpV P                  ^RR\        \        V4      4      P                  R4      .3.V4      # )z
If the response contains a I{Content-Length} header, the inbound
request object should still only have C{finish} called on it once.
   foo bar bazr      Content-Lengthr   r   r   lenr   r    datas   & r.   test_contentLength#ProxyClientTests.test_contentLength7  sE    
 $$+c#d)n.C.CG.L-MNOQU
 	
r1   c           
     |    RpV P                  ^RR\        \        V4      4      P                  R4      .3.VRR7      # )z
If the response contains a I{Content-Length} header, the outgoing
connection is closed when all response body data has been received.
r   r   r   r   F)r   r   r   s   & r.   test_losesConnection%ProxyClientTests.test_losesConnectionA  sN    
 $$#c$i."7"7"@!ABC  % 
 	
r1   c           	     n    \        RRRRRRR/RR	4      pV P                  VP                  RRR
R/4       R	# )z
The headers given at initialization should be modified:
B{proxy-connection} should be removed if present, and B{connection}
should be added.
   GET   /foor{   rx   ry   s   proxy-connectionr>   r1   Nr   r   )r   r   r   )r    r   s   & r.   test_headersCleanups%ProxyClientTests.test_headersCleanupsO  sL     &96B
 	NNYmXN	
r1   c                    RRRRRR/pVP                  4       pRVR&   VR \        RRR	VR
R4      pV P                  VRV4       R# )z
The proxy doesn't really know what to do with keepalive things from
the remote server, so we stomp over any keepalive header we get from
the client.
rx   ry   s
   keep-alives   300r   r   r   r   r{   r1   Ns   GET /foo HTTP/1.0)copyr   r   )r    r   r   r   s   &   r.   test_keepaliveNotForwarded+ProxyClientTests.test_keepaliveNotForwardeda  s`     |6=

 ",,.)1&M*VWk7CN""6+?Qr1   c           	        V P                  R4      pVP                  P                  RR.4       VP                  P                  RR.4       VP                  P                  RR.4       V P                  VRR	/R
7      pV P	                  V4       RR.RR.RR./pVP                  V P                  ^RVP                  4       R4      4       V P                  V^R\        VP                  4       4      R4       R# )z
L{server.Request} within the proxy sets certain response headers by
default. When we get these headers back from the remote server, the
defaults are overridden rather than simply appended.
r>   s   servers   old-bars   dates   old-bazs   content-types   old/quxrx   ry   )r   s   Serverr   s   Dates
   2010-01-01   Content-Types   application/x-bazr   r1   N)
ru   r   setRawHeadersr   r   r   r   itemsr   r   )r    r}   r   r   s   &   r.   test_defaultHeadersOverridden.ProxyClientTests.test_defaultHeadersOverriddenr  s     ""6*--i*F--g
|D--o
|L%%g	<7P%Q&!xm_23

 	D223w}}PSTU##GS%gmmo9NPSTr1   rH   )r   Nr1   )r   r1   T)rI   rJ   rK   rL   rM   rr   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rN   rO   rP   s   @r.   rc   rc      sf     T"U"
"#:&*.X

A


$R"U Ur1   rc   c                   0   a  ] tR tRt o RtR tR tRtV tR# )ProxyClientFactoryTestsi  z"
Tests for L{ProxyClientFactory}.
c                   \        R.4      p\        RRRRR/RV4      pVP                  RR4       V P                  VP                  R	4       V P                  VP
                  R
4       V P                  \        VP                  P                  4       4      RR.3.4       V P                  RP                  VP                  4      R4       V P                  VP                  ^4       R# )zk
Check that L{ProxyClientFactory.clientConnectionFailed} produces
a B{501} response to the parent request.
r>   r   r   r{   rx   ry    Ni  s   Gateway errorr   r1   s   <H1>Could not connect</H1>)r   r   clientConnectionFailedr   r   r   r   r   r   r   r   r   )r    r}   r+   s   &  r.   test_connectionFailed-ProxyClientFactoryTests.test_connectionFailed  s    
 x($G[9l*CR
 	&&tT2--s3002BC((99;<~./	
 	'//24QR))1-r1   c                `   \        RRRRR/RR4      pVP                  R4      pV P                  V\        4       V P	                  VP
                  R4       V P	                  VP                  R4       V P	                  VP                  R4       V P	                  VP                  RRRR	/4       R# )
z
L{ProxyClientFactory.buildProtocol} should produce a L{ProxyClient}
with the same values of attributes (with updates on the headers).
r   r   r{   rx   ry   s	   Some dataNr   r   )	r   r   r   r   r   commandr   r   r   )r    r+   protos   &  r.   test_buildProtocol*ProxyClientFactoryTests.test_buildProtocol  s    
 %G[9l*C\SW
 %%d+e[1/W-\2MMI|]HM	
r1   rH   N)	rI   rJ   rK   rL   rM   r   r   rN   rO   rP   s   @r.   r   r     s     .(
 
r1   r   c                   F   a  ] tR tRt o RtR
R ltR tR tR tR t	Rt
V tR	# )ProxyRequestTestsi  z
Tests for L{ProxyRequest}.
c                   \        4       p\        V4      p\        4       p\        VRV4      pVP	                  \        V4      4       VP                  V4       VP                  VRV,           R4       V P                  \        VP                  4      ^4       V P                  VP                  ^ ,          ^ ,          R4       V P                  VP                  ^ ,          ^,          ^P4       VP                  ^ ,          ^,          p	V P                  V	\        4       V P                  V	P                  V4       V P                  V	P                  R4       V P                  V	P                  RR/4       V P                  V	P                  V4       V P                  V	P                   V4       V P                  V	P"                  V4       R# )zm
Build a request pointing at C{uri}, and check that a proxied request
is created, pointing a C{expectedURI}.
Fs   http://example.comr{   example.comr      example.comN)r   rS   r   r   	gotLengthr   handleContentChunkrequestReceivedr   r   r   r   r   versionr   r   r   father)
r    r!   r"   r~   r   r'   r(   r$   r}   r+   s
   &&&&&     r.   _testProcessProxyRequestTests._testProcess  sa   
 56	y)/ww7#d)$""4((=(C[QW//0!4++A.q1=A++A.q126$$Q'*g'9:&1+67N*CDt,{31r1   c                &    V P                  RR4      # )aA  
L{ProxyRequest.process} should create a connection to the given server,
with a L{ProxyClientFactory} as connection factory, with the correct
parameters:
    - forward comment, version and data values
    - update headers with the B{host} value
    - remove the host from the URL
    - pass the request as parent request
   /foo/barr   r4   s   &r.   test_processProxyRequestTests.test_process  s       k::r1   c                &    V P                  RR4      # )z
If the incoming request doesn't contain a slash,
L{ProxyRequest.process} should add one when instantiating
L{ProxyClientFactory}.
r1   rz   r   r4   s   &r.    test_processWithoutTrailingSlash2ProxyRequestTests.test_processWithoutTrailingSlash  s       d++r1   c                *    V P                  RRRR4      # )zT
L{ProxyRequest.process} should be able to retrieve request body and
to forward it.
r   r   r   r   r4   s   &r.   test_processWithData&ProxyRequestTests.test_processWithData  s    
   k7OTTr1   c                   \        4       p\        V4      p\        4       p\        VRV4      pVP	                  ^ 4       VP                  RRR4       V P                  \        VP                  4      ^4       V P                  VP                  ^ ,          ^ ,          R4       V P                  VP                  ^ ,          ^,          R4       R# )z
Check that L{ProxyRequest.process} correctly parse port in the incoming
URL, and create an outgoing connection with this port.
Fr   s   http://example.com:1234/foo/barr{   r   r   N)	r   rS   r   r   r   r   r   r   r   )r    r'   r(   r$   r}   s   &    r.   test_processWithPort&ProxyRequestTests.test_processWithPort  s    
 56	y)/ww7!(JKX 	W//0!4++A.q1=A++A.q148r1   rH   N)r   r1   )rI   rJ   rK   rL   rM   r   r   r   r   r  rN   rO   rP   s   @r.   r   r     s)     24
;,U9 9r1   r   c                   *   a  ] tR tRt o RtR tRtV tR# )DummyFactoryi  z6
A simple holder for C{host} and C{port} information.
c                    Wn         W n        R # )Nr)   r*   )r    r)   r*   s   &&&r.   rV   DummyFactory.__init__  s    		r1   r  N)rI   rJ   rK   rL   rM   rV   rN   rO   rP   s   @r.   r  r    s      r1   r  c                   *   a  ] tR tRt o RtR tRtV tR# )ReverseProxyRequestTestsi  z#
Tests for L{ReverseProxyRequest}.
c                V   \        4       p\        V4      p\        4       p\        VRV4      p\	        RR4      Vn        VP                  ^ 4       VP                  RRR4       V P                  \        VP                  4      ^4       V P                  VP                  ^ ,          ^ ,          R4       V P                  VP                  ^ ,          ^,          R4       VP                  ^ ,          ^,          pV P                  V\        4       V P                  VP                  RR/4       R	# )
z
L{ReverseProxyRequest.process} should create a connection to its
factory host/port, using a L{ProxyClientFactory} instantiated with the
correct parameters, and particularly set the B{host} header to the
factory host.
Fr   r   r   r   r{   r   r   N)r   rS   r   r   r  r+   r   r   r   r   r   r   r   r   )r    r'   r(   r$   r}   r+   s   &     r.   r   %ReverseProxyRequestTests.test_process  s     56	y)/%gug>&}d;![A 	W//0!4++A.q1=A++A.q148 $$Q'*g'9:7N*CDr1   rH   N)rI   rJ   rK   rL   rM   r   rN   rO   rP   s   @r.   r
  r
    s     E Er1   r
  N)rM   twisted.internet.testingr   r   twisted.trial.unittestr   twisted.web.proxyr   r   r   r   r	   twisted.web.resourcer
   twisted.web.serverr   twisted.web.test.test_webr   r   rS   rc   r   r   r  r
  rH   r1   r.   <module>r     s    U +  * # 2P9 P9f (  (FxUx xUv(
h (
VI9 I9X Ex Er1   