
    ;                        d dl Z d dlZd dlmZmZ d dlZd dlmZ ddlm	Z	 d Z
d Zd Zd	 Zd
 Zd Zd Zd Zd ZddgfddgfdddgfgZg ZeD ]"  \  ZZeD ]  Zej1                  e def        $ eeD  cg c]  \  } }| j3                         |f c}} z  Zej4                  j7                  de      d        Zd Zd Zd Zd Z yc c}} w )    N)Serverconsume_socket_content)JSONDecodeError   )override_environc                 `    t        | d      }dt        |      |fz  }| j                  |       y)zDSimple handler that will take request and echo it back to requester.      ?timeouts)   HTTP/1.1 200 OK
Content-Length: %d

%sN)r   lensend)sockrequest_contenttext_200s      ;platform/gsutil/third_party/requests/tests/test_lowlevel.pyecho_response_handlerr      s7    ,T3?O	 
_	/	0H
 	IIh    c                  f   t        j                         } t        j                  |       }t	        g d      }|5 \  }}d| d| d}t        j                  ||d      }| j                          ddd       j                  d	k(  sJ |j                  j                  d
   dk(  sJ y# 1 sw Y   9xY w)zcan safely send generatorswait_to_close_event   a   b   chttp://:/TdatastreamN   zTransfer-Encodingchunked)	threadingEventr   basic_response_serveriterrequestspostsetstatus_coderequestheaders)close_serverserverr   hostporturlrs          r   test_chunked_uploadr3      s    ??$L))lKF"#D	<D$vQtfA&MM#D6 

 ==C9901Y>>> 
s   5B''B0c                  \   d } t        j                         }t        |       }|5 \  }}d| d| d}t        j                  t
        j                  j                        5  t        j                  |       ddd       |j                          ddd       y# 1 sw Y   "xY w# 1 sw Y   yxY w)z?get a ChunkedEncodingError if the server returns a bad responsec                 B    t        | d      }| j                  d       |S )Nr	   r
   s-   HTTP/1.1 200 OK
Transfer-Encoding: chunked
r   r   )r   r   s     r   #incomplete_chunked_response_handlerzHtest_chunked_encoding_error.<locals>.incomplete_chunked_response_handler*   s)    0sC 			.	

 r   r   r   r   N)
r#   r$   r   pytestraisesr'   
exceptionsChunkedEncodingErrorgetr)   )r7   r-   r.   r/   r0   r1   s         r   test_chunked_encoding_errorr=   '   s    	 ??$L78F	<D$vQtfA&]]8..CCDLL E	 
DD 
#   :B"B5B"B	B""B+c                     t        j                         } t        t        |       }t	        g d      }d}|5 \  }}d| d| d}t        j                  ||d|id	      }| j                          d
d
d
       d|j                  d      z  }|j                  v sJ |j                  j                  d      dk(  sJ y
# 1 sw Y   NxY w)zBEnsure we use only the specified Host header for chunked requests.r   r   zsample-hostr   r   r   HostT)r   r,   r    N
   Host: %s
utf-8   Host: r   r#   r$   r   r   r&   r'   r(   r)   encodecontentcount)	r-   r.   r   custom_hostr/   r0   r1   r2   expected_headers	            r   3test_chunked_upload_uses_only_specified_host_headerrJ   ?   s    ??$L)|LF"#DK	<D$vQtfA&MM#D6;2GPTU 

 &(:(:7(CCOaii'''99??9%*** 
s   8B<<Cc                     t        j                         } t        t        |       }t	        g d      }|5 \  }}| d| }d| d| d}t        j                  ||d      }| j                          ddd       d	j                  d
      z  }|j                  v sJ |j                  j                  d      dk(  sJ y# 1 sw Y   NxY w)z<Ensure we don't omit all Host headers with chunked requests.r   r   r   r   r   Tr   NrA   rB   rC   r   rD   )	r-   r.   r   r/   r0   expected_hostr1   r2   rI   s	            r   +test_chunked_upload_doesnt_skip_host_headerrM   Q   s    ??$L)|LF"#D	<D$&$(vQtfA&MM#D6	 
 &(<(<W(EEOaii'''99??9%*** 
s   <B>>Cc                  \   d } t        j                         }t        |       }|5 \  }}d| d| d}t        j                  t
        j                  j                        5  t        j                  |       ddd       |j                          ddd       y# 1 sw Y   "xY w# 1 sw Y   yxY w)zvEnsure we correctly throw an InvalidHeader error if multiple
    conflicting Content-Length headers are returned.
    c                 F    t        | d      }d}| j                  |       |S )Nr	   r
   sw   HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 16
Content-Length: 32

-- Bad Actor -- Original Content
r6   )r   r   responses      r   (multiple_content_length_response_handlerzRtest_conflicting_content_lengths.<locals>.multiple_content_length_response_handlerh   s,    0sC4 	 			(r   r   r   r   N)
r#   r$   r   r8   r9   r'   r:   InvalidHeaderr<   r)   )rQ   r-   r.   r/   r0   r1   s         r    test_conflicting_content_lengthsrS   c   s    
 ??$L<=F	<D$vQtfA&]]8..<<=LL >	 
== 
r>   c                  (  	
 dd
d	dt         j                  j                  dd      } 	
fd}t        j                         }t        ||      }|5 \  }}d	| d
| d}t        j                  ||       }|j                  dk(  sJ d|j                  j                  v sJ |j                  j                  d   j                  d      sJ |j                  d   j                  dk(  sJ |j                          ddd       y# 1 sw Y   yxY w)zEnsure we correctly reset num_401_calls after a successful digest auth,
    followed by a 302 redirect to another digest auth prompt.

    See https://github.com/psf/requests/issues/1979.
       HTTP/1.1 401 UNAUTHORIZED
Content-Length: 0
WWW-Authenticate: Digest nonce="6bf5d6e4da1ce66918800195d6b9130d", opaque="372825293d1c26955496c80ed6426e9e", realm="me@kennethreitz.com", qop=auth

s6   HTTP/1.1 302 FOUND
Content-Length: 0
Location: /

s&   HTTP/1.1 200 OK
Content-Length: 0

u   Authorization: Digest username="user", realm="me@kennethreitz.com", nonce="6bf5d6e4da1ce66918800195d6b9130d", uri="/"userpassc                 B   t        | d      }|j                  d      sJ | j                         t        | d      }|v sJ | j                         t        | d      }d|vsJ | j                         t        | d      }|v sJ | j                         |S )Nr	   r
      GET / HTTP/1.1s   Authorization:r   
startswithr   )r   r   expected_digestr   text_302text_401s     r   digest_response_handlerzLtest_digestauth_401_count_reset_on_redirect.<locals>.digest_response_handler   s    0sC))*;<<<		( 1sC/111		( 1sC 777		( 1sC/111		(r   r   r   r   r   authr!   AuthorizationzDigest r   i.  N)r'   rb   HTTPDigestAuthr#   r$   r   r<   r*   r+   r,   r\   historyr)   )rb   r`   r-   r.   r/   r0   r1   r2   r]   r   r^   r_   s           @@@@r   +test_digestauth_401_count_reset_on_redirectrf      s   AH'H-HLO ==''7D0 ??$L+NF	<D$vQtfA&LL4(}}###!))"3"3333yy  1<<YGGGyy|''3... 
s   B'DDc                    	 d	dt         j                  j                  dd      } 	fd}t        j                         }t        ||      }|5 \  }}d| d| d	}t        j                  || 
      }|j                  dk(  sJ |j                  d   j                  dk(  sJ |j                          ddd       y# 1 sw Y   yxY w)zkEnsure we correctly respond to a 401 challenge once, and then
    stop responding if challenged again.
    rU   rV   rW   rX   c                     t        | d      }|j                  d      sJ | j                         t        | d      }|v sJ | j                         t        | d      }|dk(  sJ |S Nr	   r
   rZ   r   r[   )r   r   r]   r_   s     r   digest_failed_response_handlerzJtest_digestauth_401_only_sent_once.<locals>.digest_failed_response_handler   sz    0sC))*;<<<		( 1sC/111		( 1sC#%%%r   r   r   r   r   ra   i  r   N)
r'   rb   rd   r#   r$   r   r<   r*   re   r)   )
rb   rj   r-   r.   r/   r0   r1   r2   r]   r_   s
           @@r   "test_digestauth_401_only_sent_oncerk      s    AHLO ==''7D$ ??$L2UF	<D$vQtfA&LL4(}}###yy|''3... 
s   A#B<<Cc                     dt         j                  j                  dd      } fd}t        j                         }t        ||      }|5 \  }}d| d| d}t        j                  || 	      }|j                  d
k(  sJ t        |j                        dk(  sJ |j                          ddd       y# 1 sw Y   yxY w)zlEnsure we only send digestauth on 4xx challenges.

    See https://github.com/psf/requests/issues/3772.
    s   HTTP/1.1 200 OK
Content-Length: 0
WWW-Authenticate: Digest nonce="6bf5d6e4da1ce66918800195d6b9130d", opaque="372825293d1c26955496c80ed6426e9e", realm="me@kennethreitz.com", qop=auth

rW   rX   c                     t        | d      }|j                  d      sJ | j                         t        | d      }|dk(  sJ |S ri   r[   )r   r   text_200_chals     r   r`   z<test_digestauth_only_on_4xx.<locals>.digest_response_handler   sR    0sC))*;<<<		-  1sC#%%%r   r   r   r   r   ra   r!   r   N)r'   rb   rd   r#   r$   r   r<   r*   r   re   r)   )	rb   r`   r-   r.   r/   r0   r1   r2   rn   s	           @r   test_digestauth_only_on_4xxro      s    
FM ==''7D
 ??$L+NF	<D$vQtfA&LL4(}}###199~""" 
s   AB44B=httphttpsall_proxyz
var,schemec                    | d}t               }|5 \  }}d| d| }||i}t        di |5  t        j                  t        j
                  j                        5  t	        j                  |       d d d        d d d        t        |j                        dk(  sJ t        |j                  d         dkD  sJ 	 d d d        y # 1 sw Y   RxY w# 1 sw Y   VxY w# 1 sw Y   y xY w)Nz://httpbin.orgz	socks5://r   r   r    )
r   r   r8   r9   r'   r:   ConnectionErrorr<   r   handler_results)	httpbinvarschemer1   
fake_proxyr/   r0   	proxy_urlkwargss	            r   test_use_proxy_from_environmentr~   !  s    HN
#CJ	|dvQtf-	y!''x22BBCS! D ( :--.!333 :--a01A555 

 DC (' 
s:   C.CC1C9?CCCC	CC#c                  &  	 dd} g 		fd}t        j                         }t        ||      }|5 \  d d }t        j                  |d      }|j
                  d	k(  sJ t        |j                        d
k(  sJ |j                  d   j
                  dk(  sJ 	d   j                  d| z   dz         sJ |j                  dj                  || j                  d            k(  sJ |j                          d d d        y # 1 sw Y   y xY w)Nu   šs   %C5%A1c                     t        | d       d d d }| j                  d|j                  d      z         j                  t        | d             | j                  d       y )	Nr	   r
   z//r   r   sC   HTTP/1.1 301 Moved Permanently
Content-Length: 0
Location: %s

utf8   HTTP/1.1 200 OK

)r   r   rE   append)r   locationr/   pathr0   redirect_requests     r   redirect_resp_handlerzJtest_redirect_rfc1808_to_non_ascii_location.<locals>.redirect_resp_handler9  sn    tS1vQtfAdV,		 '(	
 	 6tS IJ		,-r   r   r   r   T)r1   allow_redirectsr!   r   r   i-  s   GET /s	    HTTP/1.1z{}/{}ascii)r#   r$   r   r'   r<   r*   r   re   r\   r1   formatdecoder)   )
expected_pathr   r-   r.   r1   r2   r/   r   r0   r   s
         @@@@r   +test_redirect_rfc1808_to_non_ascii_locationr   4  s   DM. ??$L)|LF	<D$vQtf%LLS$7}}###199~"""yy|''3..."--h.F.UVVVuusM,@,@,IJJJJ 
s   CDDc                     t        j                         } t        t        |       }|5 \  }}d| d| d}t	        j
                  |      }|j                  }|j                  dk(  sJ |j                  dd      \  }}|j                  dd      \  }	}|	d	k(  sJ d
D ]  }
|
|vsJ |
|vrJ  | j                          ddd       y# 1 sw Y   yxY w)zCVerify that the fragment portion of a URI isn't sent to the server.r   r   r   '/path/to/thing/#view=edit&token=hunter2r!   s   

r   s   
s   GET /path/to/thing/ HTTP/1.1)s   views   edits   tokens   hunter2N)
r#   r$   r   r   r'   r<   rF   r*   splitr)   )r-   r.   r/   r0   r1   r2   raw_requestr,   bodystatus_linefrags              r   #test_fragment_not_sent_with_requestr   V  s    ??$L)|LF	<D$vQtf$KLLLii}}####))+q9&}}Wa8W====<Dw&&&t### = 	 
s   A?C(CCc                     d } t        j                         }t        | |      }|5 \  }}d| d| d}t        j                  |      }|j
                  dk(  sJ t        |j                        dk(  sJ |j                  d   j                  j                  |k(  sJ |j                  d	   j                  j                  d| d| d
k(  sJ |j                  d| d| dk(  sJ |j                          ddd       y# 1 sw Y   yxY w)zVerify we only append previous fragment if one doesn't exist on new
    location. If a new fragment is encountered in a Location header, it should
    be added to all subsequent requests.
    c                     t        | d       | j                  d       t        | d       | j                  d       t        | d       | j                  d       y )Nr	   r
   sJ   HTTP/1.1 302 FOUND
Content-Length: 0
Location: /get#relevant-section

s@   HTTP/1.1 302 FOUND
Content-Length: 0
Location: /final-url/

r   r6   r   s    r   response_handlerz:test_fragment_update_on_redirect.<locals>.response_handlerr  sU    tS1		7	

 	tS1		-	

 	tS1		&	
r   r   r   r   r   r!      r   r   z/get#relevant-sectionz/final-url/#relevant-sectionN)r#   r$   r   r'   r<   r*   r   re   r+   r1   r)   )r   r-   r.   r/   r0   r1   r2   s          r    test_fragment_update_on_redirectr   l  s   
$ ??$L$,GF	<D$vQtf$KLLL}}###199~"""yy|##''3... yy|##''WTF!D6AV+WWWWuu'$q.JKKKK 
s   CC<<Dc                  L   d } t        j                         }t        | |      }|5 \  }}d| d| d}t        j                  |      }d d d        d _        t        j                  t        j                  j                        5 }|j                          d d d        t        j                  t        j                  j                        sJ t        |j                  t              sJ |j                  t        |j                        vsJ y # 1 sw Y   xY w# 1 sw Y   xY w)Nc                 @    t        | d       | j                  d       y )Nr	   r
   s;   HTTP/1.1 200 OK
Content-Length: 18

{ " K0" = " 0" 
r6   r   s    r   r   zNtest_json_decode_compatibility_for_alt_utf_encodings.<locals>.response_handler  s    tS1		A	
r   r   r   r   r   )r#   r$   r   r'   r<   encodingr8   r9   r:   r   json
isinstancevalueRequestExceptiontextstr)r   r-   r.   r/   r0   r1   r2   excinfos           r   4test_json_decode_compatibility_for_alt_utf_encodingsr     s    
 ??$L$,GF	<D$vQtfA&LL 
 AJ	x**::	;w	 
<gmmX%8%8%I%IJJJgmm_55566W]]++++ 
 
<	;s   "DDDD#)!r#   r8   tests.testserver.serverr   r   r'   requests.compatr   utilsr   r   r3   r=   rJ   rM   rS   rf   rk   ro   _schemes_by_var_prefix_proxy_combosprefixschemesrz   r   uppermarkparametrizer~   r   r   r   r   )ry   rz   s   00r   <module>r      s     B  + #	?0+$+$8>B+\"L fXwi
VW  -OFG/89  . =I=KC399;'=I I }56 66$D,(V,s Js   :C