
                         L   d Z ddlmZ ddlmZ ddlmZ ddl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c mZ ddlZddlZddlZdd
lmZmZ  e eddd             ddlmZ ddlmZ ej,                  rdZndZ G d dej0                        Z G d dej0                        Zy)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                       e Zd ZdZ fdZ ej                  e      d        Z ej                  e      d        Z	 ej                  e      d        Z
 ej                  e      d        Z ej                  e      d        Z ej                  e      d        Z ej                  d	       ej                  d
      d               Z xZS )TestUploadCallbackConnectionz)Tests for the upload callback connection.c                     t         t        |           t               | _        t        | j                  ddd      | _         | j                  j                         d      | _        y )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   z"TestUploadCallbackConnection.setUp8   sZ    	
&3546D="	$D
 <D&&99;FCDM    c                    d|j                   _        | j                  j                  dd       | j                  j                  dd       | j                  j                  dd       | j                  | j                  j                  d       y)	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    testHeaderDefaultBehaviorz6TestUploadCallbackConnection.testHeaderDefaultBehaviorB   sj     (,I$MM.7MM,d3MMO_=4==66=r!   c                     d|j                   _        | j                  j                  dd       | j                  j                  dd       | j                  | j                  j                  d       y)z?Test that the gzip content-encoding is required to modify size.Nr%   r&   r'   bytes 0-99/*      ?r(   r-   s     r    testHeaderIgnoreWithoutGzipz8TestUploadCallbackConnection.testHeaderIgnoreWithoutGzipL   sU     (,I$MM,d3MMO^<4==66<r!   c                     | j                   j                  dd       | j                   j                  dd       | j                   j                  dd       | j                  | j                   j                  d       y)	z'Test content-range header format X-Y/* r#   r$   r%   r&   r'   r1         $@Nr   r)   r+   r,   r-   s     r    !testHeaderRangeFormatX_YSlashStarz>TestUploadCallbackConnection.testHeaderRangeFormatX_YSlashStarU   s\     	MM.7MM,d3MMO^<4==66=r!   c                     | j                   j                  dd       | j                   j                  dd       | j                   j                  dd       | j                  | j                   j                  d       y)	z'Test content-range header format */100 r#   r$   r%   r&   r'   zbytes */100r2   Nr6   r-   s     r    !testHeaderRangeFormatStarSlash100z>TestUploadCallbackConnection.testHeaderRangeFormatStarSlash100^   s\     	MM.7MM,d3MMO];4==66<r!   c                     | j                   j                  dd       | j                   j                  dd       | j                   j                  dd       | j                  | j                   j                  d       y)	z*Test content-range header format 0-99/100 r#   r$   r%   r&   r'   zbytes 0-99/100r5   Nr6   r-   s     r    !testHeaderRangeFormat0_99Slash100z>TestUploadCallbackConnection.testHeaderRangeFormat0_99Slash100g   s]     	MM.7MM,d3MMO-=>4==66=r!   c                    d|j                   _        | j                  j                  dd       | j                  j                  dd       | j                  j                  dd       | j                  | j                  j                  d       y)	z5Test incorrect header values do not raise exceptions.Nr#   r$   r%   zbytes 10r'   znot a numberr2   r(   r-   s     r    testHeaderParseFailurez3TestUploadCallbackConnection.testHeaderParseFailurep   sj     (,I$MM.7MM,j9MMO^<4==66<r!   z3gslib.progress_callback.ProgressCallbackWithTimeoutz#httplib2.HTTPSConnectionWithTimeoutc                    d |j                   _        d| j                  _        d| j                  _        || j                  _        d}| j                  j                  |       | j                  |j                   j                         |j                   j                  d   \  \  }}}| j                  ||       | j                  |j                  j                         |j                  j                  d   \  \  }}| j                  |d       y )N   Ts
   0123456789r      )sendr*   r   r,   processed_initial_bytescallback_processor
assertTruecalledcall_args_listassertEqualProgress)r   r.   mock_callbacksample_data_	sent_data
sent_bytess          r    testSendDefaultBehaviorz4TestUploadCallbackConnection.testSendDefaultBehaviorz   s     #'INN"#DMM,0DMM)'4DMM$KMM{#OOINN))*!55a8NQ	AY,OOM**112#,,;;A>OLZ!Z$r!   )__name__
__module____qualname____doc__r   r   patchhttps_connectionr/   r3   r7   r9   r;   r=   rN   __classcell__)r   s   @r    r   r   5   s   1D 4::>  > 4::=  = 4::>  > 4::=  = 4::>  > 4::=  = 4::CD4::34% 5 E%r!   r   c                       e Zd Zd Zy)TestHttpWithDownloadStreamc                 B   d}d}t        j                  t        j                        }d|_        d|_        t        j                  |_        |dg|j                  _	        d|fg|j                  _        ||j                  _        t        j                  d|z        }t        j                   rt        j"                  |      |_        nt        j&                  |      |_        t        j                  t        j(                        }||j*                  _        t-               }t        j                  t        j.                        |_        d	|j0                  _        |j5                  |d
ddd       | j7                  t9        |j$                  d         t;        |             y )Nr   z#byte count less than content length)specreasonversion zContent-Lengths   Content-Length:%dwburiGETbodyheadersr%   )r   Mockr   HTTPResponserZ   r[   OKstatusreadside_effect
getheadersr*   	getheaderioBytesIOsixPY2HTTPMessagemsgparse_headersHTTPConnectiongetresponser   BufferedIOBasestreammode_conn_requestrG   intlen)r   expected_content_lengthbytes_returned_by_serverhttp_responseheaders_streammock_connectionhttps          r    6test_incomplete_download_sets_content_length_correctlyzQTestHttpWithDownloadStream.test_incomplete_download_sets_content_length_correctly   sO   !DII;#;#;<M#M%M&>>M&>%CM".>.E.G -HM) ,CM(ZZ 47N NON
ww%11.Am%33NCmii[%?%?@O/<O,!#D))!2!23DKDKKufiH
 	S**+;<=124r!   N)rO   rP   rQ   r    r!   r    rW   rW      s    !4r!   rW   )rR   
__future__r   r   r   r   emailgslib.gcs_json_mediar   r   r	   gslib.tests.testcaseteststestcasehttplib2rj   rl   r
   r   	six.movesr   r   rm   rT   GsUtilUnitTestCaser   rW   r   r!   r    <module>r      s   , E & %  '  : 7 E ' '  	 
 % VV_	5 6 ! 
 77.2V%8#>#> V%r#4!<!< #4r!   