
    X                    d   d dl mZ d dlmZ d dlZd dl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 d dl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mZ d dl m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(mZm)Z) d dl*m+Z+ d dl,m-Z- d dl.m/Z/m0Z0 ddl1m2Z2m3Z3  G d de      Z4 G d de      Z5 G d d      Z6y)    )annotationsN)HTTPException)Empty)error)SSLError)SHORT_TIMEOUT)Mockpatch)
DEFAULT_CA)Retry)HTTPConnection)HTTPConnectionPoolHTTPSConnectionPool_url_from_poolconnection_from_url)
ClosedPoolErrorEmptyPoolErrorFullPoolErrorHostChangedErrorLocationValueErrorMaxRetryErrorProtocolErrorReadTimeoutErrorr   TimeoutError)HTTPResponse)CertificateError)_DEFAULT_TIMEOUTTimeout   )MockChunkedEncodingResponseMockSockc                  "     e Zd Zdd fdZ xZS )HTTPUnixConnectionc                N    t         |   d       || _        || _        d | _        y )N	localhost)super__init__unix_sockettimeoutsock)selfhostr)   kwargs	__class__s       ?platform/gsutil/third_party/urllib3/test/test_connectionpool.pyr'   zHTTPUnixConnection.__init__,   s&    %	    )<   )r,   strr)   intr-   
typing.AnyreturnNone)__name__
__module____qualname__r'   __classcell__r.   s   @r/   r#   r#   +   s     r0   r#   c                      e Zd ZdZeZy)HTTPUnixConnectionPoolz	http+unixN)r7   r8   r9   schemer#   ConnectionCls r0   r/   r=   r=   3   s    F&Mr0   r=   c                      e Zd ZdZej
                  j                  dg d      d'd       Zej
                  j                  dg d      d'd       Zej
                  j                  dg d      d'd       Z	ej
                  j                  dg d	      d'd
       Z
ej
                  j                  dg d      d'd       Zej
                  j                  dg d      d'd       Zej
                  j                  dg d      d'd       Zej
                  j                  dg 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(d%Z!y&)*TestConnectionPoolz
    Tests in this suite should exercise the ConnectionPool functionality
    without actually making any network requests or connections.
    za, b))http://google.com//)rC   rC   )rC   http://google.com)rC   http://google.com/abra/cadabra)zhttp://google.com:42/z http://google.com:42/abracadabra)http://google.com:80/zhttp://google.com/abracadabra)rC    http://google.com:80/abracadabra)zhttps://google.com:443/https://google.com/abracadabra)https://google.com/"https://google.com:443/abracadabra)z)http://[2607:f8b0:4005:805::200e%25eth0]/z'http://[2607:f8b0:4005:805::200e%eth0]/)z.https://[2607:f8b0:4005:805::200e%25eth0]:443/z,https://[2607:f8b0:4005:805::200e%eth0]:443/)zhttp://[::1]/zhttp://[::1])z2http://[2001:558:fc00:200:f816:3eff:fef9:b954%lo]/z3http://[2001:558:fc00:200:f816:3eff:fef9:b954%25lo]c                l    t        |      5 }|j                  |      sJ 	 d d d        y # 1 sw Y   y xY wNr   is_same_hostr+   abcs       r/   test_same_hostz!TestConnectionPool.test_same_host>   s,    : !#q>>!$$$ $##   *3))rJ   rC   )rC   rJ   )zhttp://yahoo.com/rC   )http://google.com:42rI   )rE   https://google.net/)rV   rE   )zhttps://google.com:42https://google.com)http://google.com:443rE   )https://google.com:80rX   )rY   rX   )rZ   rE   )zhttps://google.com:443rE   )http://google.com:80rX   )zhttp://[dead::beef]zhttps://[dead::beef%en5]/c                    t        |      5 }|j                  |      rJ 	 d d d        t        |      5 }|j                  |      rJ 	 d d d        y # 1 sw Y   2xY w# 1 sw Y   y xY wrM   rN   rP   s       r/   test_not_same_hostz%TestConnectionPool.test_not_same_host^   sc    . !#q~~a(((( $ !#q~~a(((( $# $# $#   AAAA')
google.comrD   r`   rC   )r`   rE   )r`   rF   )r`   rH   c                l    t        |      5 }|j                  |      sJ 	 d d d        y # 1 sw Y   y xY wrM   r   rO   rP   s       r/   test_same_host_no_port_httpz.TestConnectionPool.test_same_host_no_port_http{   s,      "a>>!$$$ #""rU   )r_   r`   rJ   )r`   rX   )r`   zhttps://google.com/abra/cadabra)r`   rK   c                l    t        |      5 }|j                  |      sJ 	 d d d        y # 1 sw Y   y xY wrM   r   rO   rP   s       r/   test_same_host_no_port_httpsz/TestConnectionPool.test_same_host_no_port_https   s,     !#q>>!$$$ $##rU   )re   )	yahoo.comrC   r`   rW   )r`   zhttp://google.com./c                    t        |      5 }|j                  |      rJ 	 d d d        t        |      5 }|j                  |      rJ 	 d d d        y # 1 sw Y   2xY w# 1 sw Y   y xY wrM   rc   rP   s       r/   test_not_same_host_no_port_httpz2TestConnectionPool.test_not_same_host_no_port_http   sc      "a~~a(((( #  "a~~a(((( #" #" #"r^   )ra   )ri   rJ   rj   )r`   zhttps://google.com./c                    t        |      5 }|j                  |      rJ 	 d d d        t        |      5 }|j                  |      rJ 	 d d d        y # 1 sw Y   2xY w# 1 sw Y   y xY wrM   rg   rP   s       r/    test_not_same_host_no_port_httpsz3TestConnectionPool.test_not_same_host_no_port_https   sc     !#q~~a(((( $ !#q~~a(((( $# $# $#r^   ))%2Fvar%2Frun%2Fdocker.sockz&http+unix://%2Fvar%2Frun%2Fdocker.sock)ro   z'http+unix://%2Fvar%2Frun%2Fdocker.sock/)ro   z2http+unix://%2Fvar%2Frun%2Fdocker.sock/abracadabra)%2Ftmp%2FTEST.sockhttp+unix://%2Ftmp%2FTEST.sock)rp   http+unix://%2Ftmp%2FTEST.sock/)rp   *http+unix://%2Ftmp%2FTEST.sock/abracadabrac                l    t        |      5 }|j                  |      sJ 	 d d d        y # 1 sw Y   y xY wrM   r=   rO   rP   s       r/   test_same_host_custom_protocolz1TestConnectionPool.test_same_host_custom_protocol   s,     $A&!>>!$$$ '&&rU   ))%2Ftmp%2Ftest.sockrq   )rw   rr   )rw   rs   )ro   rq   c                l    t        |      5 }|j                  |      rJ 	 d d d        y # 1 sw Y   y xY wrM   ru   rP   s       r/   "test_not_same_host_custom_protocolz5TestConnectionPool.test_not_same_host_custom_protocol   s/     $A&!~~a(((( '&&rU   c                   t        ddd      5 }|j                  t               t        j                  t
              5  |j                  t               d d d        t        j                  t
              5  |j                  ddt               d d d        |j                  dk(  sJ 	 d d d        y # 1 sw Y   ^xY w# 1 sw Y   0xY w# 1 sw Y   y xY w)	Nr%   r   Tr,   maxsizeblockr)   GETrD   )pool_timeout)r   	_get_connr   pytestraisesr   requestnum_connectionsr+   pools     r/   test_max_connectionsz'TestConnectionPool.test_max_connections   s    [!4HDNN=N1~.}5 / ~.UCmD / ''1,,, IH /. /. IHs:   0CB3!C7B?C3B<	8C?C	CCc                r   t        ddd      5 }|j                         }|j                         }t        j                  |d      5 }t        j                  |d      5 }|j	                  |       |j	                  |       ddd       ddd       j
                  du sJ j
                  du sJ ||j                         k(  sJ ||j                         k7  sJ |j                  dk(  sJ d	|j                  v sJ d
|j                  v sJ 	 ddd       y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   yxY w)z
        If maxsize = n and we _put_conn n + 1 conns, the n + 1th conn will
        get closed and will not get added to the pool.
        r%   r   Fr{   closeNT   z.Connection pool is full, discarding connectionzConnection pool size: 1)r   r   r
   object	_put_conncalledr   text)r+   caplogr   conn1conn2conn1_closeconn2_closes          r/   +test_put_conn_when_pool_is_full_nonblockingz>TestConnectionPool.test_put_conn_when_pool_is_full_nonblocking   s     [!5ITNN$E NN$EeW-\\%1[NN5)NN5) 2 .
 %%...%%---DNN,,,,DNN,,,,''1,,,Cv{{RRR,;;;' JI 21 .- JIs<   7D-D!#D D!BD-DD!!D*	&D--D6c           	     B   t        ddd      5 }|j                         }|j                         }t        j                  |d      5 }t        j                  |d      5 }|j                  |       t        j                  t              5  |j                  |       ddd       ddd       ddd       j                  du sJ j                  du sJ ||j                         k(  sJ 	 ddd       y# 1 sw Y   XxY w# 1 sw Y   \xY w# 1 sw Y   `xY w# 1 sw Y   yxY w)zs
        If maxsize = n and we _put_conn n + 1 conns, the n + 1th conn will
        cause a FullPoolError.
        r%   r   Tr{   r   NF)
r   r   	_new_connr
   r   r   r   r   r   r   )r+   r   r   r   r   r   s         r/   (test_put_conn_when_pool_is_full_blockingz;TestConnectionPool.test_put_conn_when_pool_is_full_blocking  s    
  [!4HDNN$ENN$EeW-\\%1[NN5)}5u- 6 2 . %%...%%---DNN,,,, IH 65 21 .-	 IHsS   7DD	+C=C1	C="D	*=D1C:6C==DD		D	DDc                @   t        ddd      5 }|j                         }t        j                  |d      5 }|j	                          |j
                  J |j                  |       d d d        j                  du sJ 	 d d d        y # 1 sw Y   #xY w# 1 sw Y   y xY w)Nr%   r   Tr{   r   )r   r   r
   r   r   r   r   r   )r+   r   r   r   s       r/   test_put_conn_closed_poolz,TestConnectionPool.test_put_conn_closed_pool  s    [!4HDNN$EeW-

yy((( u% . %%--- IH-- IHs"   'B0B&BB	BBc                L    t        t        t        d      d            dk(  sJ y )Nr%   r,   Test.z6HTTPConnectionPool(host='localhost', port=None): Test.)r2   r   r   r+   s    r/   test_exception_strz%TestConnectionPool.test_exception_str&  s+    1{CWMNGH	
Hr0   c                    t        t        t        d      dd             dk(  sJ t        d      }t        t        t        d      d|            d|z  k(  sJ y )Nr%   r   r   zfHTTPConnectionPool(host='localhost', port=None): Max retries exceeded with url: Test. (Caused by None)TestzdHTTPConnectionPool(host='localhost', port=None): Max retries exceeded with url: Test. (Caused by %r))r2   r   r   SocketError)r+   errs     r/   test_retry_exception_strz+TestConnectionPool.test_retry_exception_str,  sq    0kBGTRSDD	
D
 &!
 0kBGSQR"##	
#r0   c           	        dt        dd      5 	 d	 	 	 	 	 	 	 dfd} |t        t        t                |t        t        t               t        j                  dt                     5  t        j                  t              5  j                  dd	dt        
       d d d        d d d        j                  J j                  j                         k(  sJ 	 d d d        y # 1 sw Y   HxY w# 1 sw Y   LxY w# 1 sw Y   y xY w)Nr   r%   Tr{   c                   t        j                  d |              5  t        j                  |      5 }j	                  dd       d d d        d d d        |"t        j                  j                  |      sJ j                  J j                  j                         k(  sJ y # 1 sw Y   cxY w# 1 sw Y   gxY w)N_make_requestside_effectr   rD   )
r
   r   r   r   r   
isinstancevaluereasonr   qsize)	exceptionexpectr   excinfo	POOL_SIZEr   s       r/   _testz0TestConnectionPool.test_pool_size.<locals>._testD  s    
 \\$Y[Qv.'UC0 / R %%gmm&:&:FCCCyy,,,yy(I555 /. RQs"   B6B*B6*B3	/B66B?r   r   r   rD   )retriesr   rM   )r   type[BaseException]r   r   r   ztype[BaseException] | Noner5   r6   )r   BaseSSLErrorr   r   r   r
   r   r   r   r   r   r   r   r   )r+   r   r   r   s     @@r/   test_pool_sizez!TestConnectionPool.test_pool_size>  s    	it

 6:6.6+6 36 	6 ,x8"M8< dOQ]]=1LLQ]LS 2 R 99(((99??$	111=
 
6 21 RQ5
 
s<   AD0C7
C+$C7,5D+C40C77D 	<DDc           	        t        ddd      5 }t        j                  |dt        |d            5  t        j                  |dt	        d	            5  t        j                  t              5  |j                  d
d       ddd       ddd       ddd       ddd       y# 1 sw Y   "xY w# 1 sw Y   &xY w# 1 sw Y   *xY w# 1 sw Y   yxY w)z6Do not put None back in the pool if the pool was emptyr%   r   Tr{   r   zPool is emptyr   r   zUnexpected _put_connr   rD   N)r   r
   r   r   AssertionErrorr   r   r   r   s     r/   test_empty_does_not_put_connz/TestConnectionPool.test_empty_does_not_put_conn`  s      [!4HDk~dO/T \\ ./E F
  ~6UC0 7 IH 76   IHsR   #C"B:B..B"	B.	B:C"B+'B..B73B::C	?CCc                    t        d      5 }t        j                  t              5  |j	                  ddd       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr[   r   zhttp://yahoo.com:80T)assert_same_host)r   r   r   r   r   )r+   rS   s     r/   test_assert_same_hostz(TestConnectionPool.test_assert_same_hosto  sJ     !78A/0		%!6	N 1 9800 98s!   AAAA	AA!c                   t        d      }|j                         }|j                         }|j                         }|j                  |       |j                  |       |j                  }|j	                          |j                  J t        j                  t              5  |j                          d d d        |j                  |       t        j                  t              5  |j                          d d d        t        j                  t              5  |J |j                  d       d d d        y # 1 sw Y   xY w# 1 sw Y   NxY w# 1 sw Y   y xY wNr[   F)r}   )
r   r   r   r   r   r   r   r   r   getr+   r   r   r   conn3old_pool_queues         r/   test_pool_closez"TestConnectionPool.test_pool_closet  s    "#9:    uu

yy   ]]?+NN , 	u]]?+NN , ]]5!!---U+ "! ,+
 ,+ "!s$   !D6$EE6D?EEc                D   t        d      }|j                         }|j                         }|j                  |       |j                  |       |j                          |j                  J 	 |j                          y # t
        $ r t        j                  d       Y y w xY w)Nr[   z<Pool of the ConnectionPool is None and has no attribute get.)r   r   r   r   r   AttributeErrorr   fail)r+   r   r   r   s       r/   test_pool_close_twicez(TestConnectionPool.test_pool_close_twice  s    "#9:   uu

yy   	XJJL 	XKKVW	Xs   -A> >BBc                J   t        d      5 }|j                         }|j                  t        k(  sJ |j                  j                  t
        k(  sJ |j                  j                  t        k(  sJ |j                  j                  t        k(  sJ |j                  j                  J t        dt              }|j                  j                  t        k(  sJ |j                  j                  t        k(  sJ |j                  j                  J 	 d d d        y # 1 sw Y   y xY w)Nr%   r   )r,   r)   )r   r   r.   r   r)   r   _readr   _connecttotalr   r+   r   conns      r/   test_pool_timeoutsz%TestConnectionPool.test_pool_timeouts  s    [1T>>#D>>^333<<))W444<<%%)9999<<((,<<<<<<%%---%;ND<<%%666<<((M999<<%%--- 211s   DDD"c                v    t        j                  t              5  t        d        d d d        y # 1 sw Y   y xY wrM   )r   r   r   r   r   s    r/   test_no_hostzTestConnectionPool.test_no_host  s#    ]]-.t$ /..s   /8c                   t        d      5 }|j                         }|j                         }|j                         }|j                  |       |j                  |       |j                  }d d d        j                  J t	        j
                  t              5  |j                          d d d        |j                         t	        j
                  t              5  |j                          d d d        t	        j
                  t              5  J |j                  d       d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   ZxY w# 1 sw Y   y xY wr   )	r   r   r   r   r   r   r   r   r   r   s         r/   test_contextmanagerz&TestConnectionPool.test_contextmanager  s     !78DNN$ENN$ENN$ENN5!NN5!!YYN 9 yy   ]]?+NN , 	u]]?+NN ,]]5!!---U+ "!# 98 ,+ ,+!!s0   AD/D;EE/D8;EEEc                r    t        d      5 }d}d| t        ||      k(  sJ 	 d d d        y # 1 sw Y   y xY w)Nr[   zpath?query=foorG   )r   r   )r+   r   paths      r/   test_url_from_poolz%TestConnectionPool.test_url_from_pool  s:     !78D#D*4&1^D$5OOOO 988s   -6c                    t        dt              5 }|j                         }|j                  t        j
                  k(  sJ 	 d d d        y # 1 sw Y   y xY w)NrZ   )ca_certs)r   r   r   	cert_reqssslCERT_REQUIREDr   s      r/   #test_ca_certs_default_cert_requiredz6TestConnectionPool.test_ca_certs_default_cert_required  sA     !8:NRV>>#D>>S%6%6666 ONNs   0AAc                    G d dt               dfd}t        d      5 }t        j                  |d|      5  |j                  J |j                  j                         }	 |j                  ddd	
       ddd       |j                  j                         }|k(  sJ 	 ddd       y# $ r Y :w xY w# 1 sw Y   ?xY w# 1 sw Y   yxY w)z
        This test validates that we clean up properly even on exceptions that
        we'd not otherwise catch, i.e. those that inherit from BaseException
        like KeyboardInterrupt or gevent.Timeout. See #805 for more details.
        c                      e Zd Zy)LTestConnectionPool.test_cleanup_on_extreme_connection_error.<locals>.RealBadN)r7   r8   r9   r@   r0   r/   RealBadr     s    r0   r   c                             rM   r@   )argsr-   r   s     r/   kaboomzKTestConnectionPool.test_cleanup_on_extreme_connection_error.<locals>.kaboom  s
    )Or0   zhttp://localhost:80r   Nr   rD   F)release_conn)r   r4   r-   r4   r5   r6   )BaseExceptionr   r
   r   r   r   urlopen)r+   r   rS   initial_pool_sizenew_pool_sizer   s        @r/   (test_cleanup_on_extreme_connection_errorz;TestConnectionPool.test_cleanup_on_extreme_connection_error  s    	m 		 !!671a&9vv)))$%FFLLN! IIeSuI= : FFLLNM$555 87   :9 87s@   C)B6$B+8)C+B30B62B33B66B?	;CCc                     G d d      dfd} |t                 |t                |t                |t               y)aZ  For successful ```urlopen(release_conn=False)```,
        the connection isn't released, even after a retry.

        This is a regression test for issue #651 [1], where the connection
        would be released if the initial request failed, even if a retry
        succeeded.

        [1] <https://github.com/urllib3/urllib3/issues/651>
        c                  T     e Zd ZdZ	 	 	 	 	 	 d fdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ xZS )yTestConnectionPool.test_release_conn_param_is_respected_after_http_error_retry.<locals>._raise_once_make_request_functionzCallable that can mimic `_make_request()`.

            Raises the given exception on its first call, but returns a
            successful response on subsequent calls.
            c                >    t         |           || _        || _        y rM   )r&   r'   _ex_pool)r+   exr   r.   s      r/   r'   zTestConnectionPool.test_release_conn_param_is_respected_after_http_error_retry.<locals>._raise_once_make_request_function.__init__   s      "79!
r0   c                  | j                   r| j                   d c}| _          |       t        j                  t              }t	        g d      |_        t        j                         x|_        |_        |j                  d      }	t        ||j                  |j                  |j                  |j                  ||||d|	| j                        }
|
S )N)   f   or   response_connF)bodyheadersstatusversionr   original_responser   request_methodrequest_urlpreload_content
connectionr   )r   httplibr   r!   r    fpHTTPMessager   msgr   r   r   r   r   )r+   r   methodurlr   r   r-   r   httplib_responser   responses              r/   __call__zTestConnectionPool.test_release_conn_param_is_respected_after_http_error_retry.<locals>._raise_once_make_request_function.__call__  s     88#'88TLB$J#*#7#7#A &ABT&U #BIBUBUBWW (+;+?7=zz/7R'),44+22,44+22&6##) #$),  r0   )r   r   r   r   r5   r6   )r   r   r  r2   r  r2   r   r4   r   r   r-   r4   r5   r   )r7   r8   r9   __doc__r'   r  r:   r;   s   @r/   !_raise_once_make_request_functionr     sn    "-"5G""  $      	  
 "      %     r0   r  c           
        t        ddd      5 }t        j                  |d | |            5  |j                  dddddd	      }d d d        |j                  J |j                  j                         d
k(  sJ |j                  dk(  sJ j                  J |j                          |j                  j                         dk(  sJ |j                  J 	 d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr%   r   Tr{   r   r   rD   F)r   r   r   chunkedr      )	r   r
   r   r   r   r   r   r   r   )r   r   r  r  s      r/   r   z]TestConnectionPool.test_release_conn_param_is_respected_after_http_error_retry.<locals>._test)  s   #atLPT \\#5iF
  $|| !%*(- $  ,  H yy,,,yy(A---++q000**666%%'yy(A---**2221 ML 	 MLs#   C.C"BC."C+	'C..C7N)r   r   r5   r6   )r   r   r   r   )r+   r   r  s     @r/   ;test_release_conn_param_is_respected_after_http_error_retryzNTestConnectionPool.test_release_conn_param_is_respected_after_http_error_retry  s5    .	  .	 `	38 	lmkmr0   c           	     v   t        dd      5 }t        t              }d|_        t	        j
                  t        dd      5  t        ddd      }t        j                  t              5  |j                  |dd|	       d d d        d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)
Nr%   r   )r,   r|   )specFread_timeoutr    r~   )r   r	   r   	is_closedr
   r   r   r   r   r   r   )r+   r   r   r)   s       r/   8test_read_timeout_0_does_not_raise_bad_status_line_errorzKTestConnectionPool.test_read_timeout_0_does_not_raise_bad_status_line_errorJ  s    [!<^,D"DNg~q9!!Q*]]#34&&tRW&E 5 :	 =< 54 :9	 =<s;   3B/'B#(B>B#B/B B##B,	(B//B8N)rQ   r2   rR   r2   r5   r6   )r5   r6   )r   zpytest.LogCaptureFixturer5   r6   )"r7   r8   r9   r  r   markparametrizerT   r]   rd   rh   rl   rn   rv   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r@   r0   r/   rB   rB   8   s   
 [[	
8%98% [[	
,)-,) [[	

%
% [[	

%
% [[	
)) [[	
)) [[
	
%% [[	
))
-<.<	<8-(.

$ 2D1O
,6X".%,,P
7
68ZxFr0   rB   )7
__future__r   http.clientclientr   r   typingr   queuer   socketr   r   r   r   testr   unittest.mockr	   r
   r   dummyserver.socketserverr   urllib3r   urllib3.connectionr   urllib3.connectionpoolr   r   r   r   urllib3.exceptionsr   r   r   r   r   r   r   r   r   urllib3.responser   urllib3.util.ssl_match_hostnamer   urllib3.util.timeoutr   r   test_responser    r!   r#   r=   rB   r@   r0   r/   <module>r%     s}    "  
  %  ' (  %  /  -    * < : @ '/ '
ZF ZFr0   