
                         V   S r SSKJr  SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKJ	r	  SS	KJ
r
  SSKJs  Jr  SSKrSSKrSSKrSS
KJrJr  \" \" SSS5      5        SSKJr  SSKJr  \R,                  (       a  SrOSr " S S\R0                  5      r " S S\R0                  5      rg)z>Tests for media helper functions and classes for GCS JSON API.    )absolute_import)print_function)division)unicode_literalsN)BytesTransferredContainer)HttpWithDownloadStream)$UploadCallbackConnectionClassFactory)add_moveMovedModulemockzunittest.mock)http_client)r   zhttplib.HTTPSConnectionzhttp.client.HTTPSConnectionc                     ^  \ rS rSrSrU 4S jr\R                  " \5      S 5       r	\R                  " \5      S 5       r
\R                  " \5      S 5       r\R                  " \5      S 5       r\R                  " \5      S 5       r\R                  " \5      S	 5       r\R                  " S
5      \R                  " S5      S 5       5       rSrU =r$ )TestUploadCallbackConnection5   z)Tests for the upload callback connection.c                    > [         [        U ]  5         [        5       U l        [        U R                  SSSS9U l        U R                  R                  5       " S5      U l        g )N2   d   Sample)buffer_size
total_sizeprogress_callbackhost)	superr   setUpr   bytes_containerr	   class_factoryGetConnectionClassinstance)self	__class__s    2platform/gsutil/gslib/tests/test_gcs_json_media.pyr   "TestUploadCallbackConnection.setUp8   sX    	
&3546D="	$D
 &&99;FCDM    c                    SUR                   l        U R                  R                  SS5        U R                  R                  SS5        U R                  R                  SS5        U R                  U R                  R                  S5        g)	z9Test the size modifier is correct under expected headers.Ncontent-encodinggzipcontent-length10content-rangezbytes 0-104/*g      %@	putheaderreturn_valuer   assertAlmostEqualsize_modifierr   	mock_conns     r!   testHeaderDefaultBehavior6TestUploadCallbackConnection.testHeaderDefaultBehaviorB   sj     (,I$MM.7MM,d3MMO_=4==66=r#   c                     SUR                   l        U R                  R                  SS5        U R                  R                  SS5        U R                  U R                  R                  S5        g)z?Test that the gzip content-encoding is required to modify size.Nr'   r(   r)   bytes 0-99/*      ?r*   r/   s     r!   testHeaderIgnoreWithoutGzip8TestUploadCallbackConnection.testHeaderIgnoreWithoutGzipL   sU     (,I$MM,d3MMO^<4==66<r#   c                     U R                   R                  SS5        U R                   R                  SS5        U R                   R                  SS5        U R                  U R                   R                  S5        g)	z'Test content-range header format X-Y/* r%   r&   r'   r(   r)   r4         $@Nr   r+   r-   r.   r/   s     r!   !testHeaderRangeFormatX_YSlashStar>TestUploadCallbackConnection.testHeaderRangeFormatX_YSlashStarU   s\     	MM.7MM,d3MMO^<4==66=r#   c                     U R                   R                  SS5        U R                   R                  SS5        U R                   R                  SS5        U R                  U R                   R                  S5        g)	z'Test content-range header format */100 r%   r&   r'   r(   r)   zbytes */100r5   Nr:   r/   s     r!   !testHeaderRangeFormatStarSlash100>TestUploadCallbackConnection.testHeaderRangeFormatStarSlash100^   s\     	MM.7MM,d3MMO];4==66<r#   c                     U R                   R                  SS5        U R                   R                  SS5        U R                   R                  SS5        U R                  U R                   R                  S5        g)	z*Test content-range header format 0-99/100 r%   r&   r'   r(   r)   zbytes 0-99/100r9   Nr:   r/   s     r!   !testHeaderRangeFormat0_99Slash100>TestUploadCallbackConnection.testHeaderRangeFormat0_99Slash100g   s]     	MM.7MM,d3MMO-=>4==66=r#   c                    SUR                   l        U R                  R                  SS5        U R                  R                  SS5        U R                  R                  SS5        U R                  U R                  R                  S5        g)	z5Test incorrect header values do not raise exceptions.Nr%   r&   r'   zbytes 10r)   znot a numberr5   r*   r/   s     r!   testHeaderParseFailure3TestUploadCallbackConnection.testHeaderParseFailurep   sj     (,I$MM.7MM,j9MMO^<4==66<r#   z3gslib.progress_callback.ProgressCallbackWithTimeoutz#httplib2.HTTPSConnectionWithTimeoutc                    S UR                   l        SU R                  l        SU R                  l        X R                  l        SnU R                  R                  U5        U R                  UR                   R                  5        UR                   R                  S   u  u  pEnU R                  XS5        U R                  UR                  R                  5        UR                  R                  S   u  u  pdU R                  US5        g )N   Ts
   0123456789r      )sendr,   r   r.   processed_initial_bytescallback_processor
assertTruecalledcall_args_listassertEqualProgress)r   r0   mock_callbacksample_data_	sent_data
sent_bytess          r!   testSendDefaultBehavior4TestUploadCallbackConnection.testSendDefaultBehaviorz   s     #'INN"#DMM,0DMM)'4MM$KMM{#OOINN))*!55a8NQAY,OOM**112#,,;;A>OLZZ$r#   )r   r   r   )__name__
__module____qualname____firstlineno____doc__r   r   patchhttps_connectionr1   r6   r;   r>   rA   rD   rV   __static_attributes____classcell__)r    s   @r!   r   r   5   s    1D ::>  > ::=  = ::>  > ::=  = ::>  > ::=  = ::CD::34% 5 E%r#   r   c                       \ rS rSrS rSrg)TestHttpWithDownloadStream   c                 B   SnSn[         R                  " [        R                  S9nSUl        SUl        [        R                  Ul        US/UR                  l	        SU4/UR                  l        XR                  l        [        R                  " SU-  5      n[        R                   (       a  [        R"                  " U5      Ul        O[        R&                  " U5      Ul        [         R                  " [        R(                  S9nX5R*                  l        [-        5       n[         R                  " [        R.                  S9Ul        S	UR0                  l        UR5                  US
SSS5        U R7                  [9        UR$                  S   5      [;        U5      5        g )Nr   z#byte count less than content length)specreasonversion zContent-Lengths   Content-Length:%dwburiGETbodyheadersr'   )r   Mockr   HTTPResponserf   rg   OKstatusreadside_effect
getheadersr,   	getheaderioBytesIOsixPY2HTTPMessagemsgparse_headersHTTPConnectiongetresponser   BufferedIOBasestreammode_conn_requestrO   intlen)r   expected_content_lengthbytes_returned_by_serverhttp_responseheaders_streammock_connectionhttps          r!   6test_incomplete_download_sets_content_length_correctlyQTestHttpWithDownloadStream.test_incomplete_download_sets_content_length_correctly   sK   !DII;#;#;<M#M%M&>>M&>%CM".>.E.G -HM) ,C(ZZ 47N NON
ww%11.Am%33NCmii[%?%?@O/<,!#D))!2!23DKDKKufiH
 	S**+;<=124r#    N)rX   rY   rZ   r[   r   r_   r   r#   r!   rb   rb      s    !4r#   rb   )r\   
__future__r   r   r   r   emailgslib.gcs_json_mediar   r   r	   gslib.tests.testcaseteststestcasehttplib2rv   rx   r
   r   	six.movesr   r   ry   r^   GsUtilUnitTestCaser   rb   r   r#   r!   <module>r      s   , E & %  '  : 7 E ' '  	 
 % VV_	5 6 ! 
 77.2V%8#>#> V%r#4!<!< #4r#   