2626import mock
2727import pytest
2828
29+ from google .cloud .storage ._helpers import _get_default_headers
2930from google .cloud .storage .retry import (
3031 DEFAULT_RETRY ,
3132 DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED ,
@@ -2212,16 +2213,19 @@ def test__set_metadata_to_none(self):
22122213 def test__get_upload_arguments (self ):
22132214 name = u"blob-name"
22142215 key = b"[pXw@,p@@AfBfrR3x-2b2SCHR,.?YwRO"
2216+ client = mock .Mock (_connection = _Connection )
2217+ client ._connection .user_agent = "testing 1.2.3"
22152218 blob = self ._make_one (name , bucket = None , encryption_key = key )
22162219 blob .content_disposition = "inline"
22172220
22182221 content_type = u"image/jpeg"
2219- info = blob ._get_upload_arguments (content_type )
2222+ info = blob ._get_upload_arguments (client , content_type )
22202223
22212224 headers , object_metadata , new_content_type = info
22222225 header_key_value = "W3BYd0AscEBAQWZCZnJSM3gtMmIyU0NIUiwuP1l3Uk8="
22232226 header_key_hash_value = "G0++dxF4q5rG4o9kE8gvEKn15RH6wLm0wXV1MgAlXOg="
22242227 expected_headers = {
2228+ ** _get_default_headers (client ._connection .user_agent , content_type ),
22252229 "X-Goog-Encryption-Algorithm" : "AES256" ,
22262230 "X-Goog-Encryption-Key" : header_key_value ,
22272231 "X-Goog-Encryption-Key-Sha256" : header_key_hash_value ,
@@ -2368,7 +2372,11 @@ def _do_multipart_success(
23682372 + data_read
23692373 + b"\r \n --==0==--"
23702374 )
2371- headers = {"content-type" : b'multipart/related; boundary="==0=="' }
2375+ headers = _get_default_headers (
2376+ client ._connection .user_agent ,
2377+ b'multipart/related; boundary="==0=="' ,
2378+ "application/xml" ,
2379+ )
23722380 client ._http .request .assert_called_once_with (
23732381 "POST" , upload_url , data = payload , headers = headers , timeout = expected_timeout
23742382 )
@@ -2614,10 +2622,17 @@ def _initiate_resumable_helper(
26142622
26152623 self .assertEqual (upload .upload_url , upload_url )
26162624 if extra_headers is None :
2617- self .assertEqual (upload ._headers , {})
2625+ self .assertEqual (
2626+ upload ._headers ,
2627+ _get_default_headers (client ._connection .user_agent , content_type ),
2628+ )
26182629 else :
2619- self .assertEqual (upload ._headers , extra_headers )
2620- self .assertIsNot (upload ._headers , extra_headers )
2630+ expected_headers = {
2631+ ** _get_default_headers (client ._connection .user_agent , content_type ),
2632+ ** extra_headers ,
2633+ }
2634+ self .assertEqual (upload ._headers , expected_headers )
2635+ self .assertIsNot (upload ._headers , expected_headers )
26212636 self .assertFalse (upload .finished )
26222637 if chunk_size is None :
26232638 if blob_chunk_size is None :
@@ -2656,10 +2671,9 @@ def _initiate_resumable_helper(
26562671 # Check the mocks.
26572672 blob ._get_writable_metadata .assert_called_once_with ()
26582673 payload = json .dumps (object_metadata ).encode ("utf-8" )
2659- expected_headers = {
2660- "content-type" : "application/json; charset=UTF-8" ,
2661- "x-upload-content-type" : content_type ,
2662- }
2674+ expected_headers = _get_default_headers (
2675+ client ._connection .user_agent , x_upload_content_type = content_type
2676+ )
26632677 if size is not None :
26642678 expected_headers ["x-upload-content-length" ] = str (size )
26652679 if extra_headers is not None :
@@ -2778,6 +2792,7 @@ def _make_resumable_transport(
27782792
27792793 @staticmethod
27802794 def _do_resumable_upload_call0 (
2795+ client ,
27812796 blob ,
27822797 content_type ,
27832798 size = None ,
@@ -2796,10 +2811,9 @@ def _do_resumable_upload_call0(
27962811 )
27972812 if predefined_acl is not None :
27982813 upload_url += "&predefinedAcl={}" .format (predefined_acl )
2799- expected_headers = {
2800- "content-type" : "application/json; charset=UTF-8" ,
2801- "x-upload-content-type" : content_type ,
2802- }
2814+ expected_headers = _get_default_headers (
2815+ client ._connection .user_agent , x_upload_content_type = content_type
2816+ )
28032817 if size is not None :
28042818 expected_headers ["x-upload-content-length" ] = str (size )
28052819 payload = json .dumps ({"name" : blob .name }).encode ("utf-8" )
@@ -2809,6 +2823,7 @@ def _do_resumable_upload_call0(
28092823
28102824 @staticmethod
28112825 def _do_resumable_upload_call1 (
2826+ client ,
28122827 blob ,
28132828 content_type ,
28142829 data ,
@@ -2828,6 +2843,9 @@ def _do_resumable_upload_call1(
28282843 content_range = "bytes 0-{:d}/{:d}" .format (blob .chunk_size - 1 , size )
28292844
28302845 expected_headers = {
2846+ ** _get_default_headers (
2847+ client ._connection .user_agent , x_upload_content_type = content_type
2848+ ),
28312849 "content-type" : content_type ,
28322850 "content-range" : content_range ,
28332851 }
@@ -2842,6 +2860,7 @@ def _do_resumable_upload_call1(
28422860
28432861 @staticmethod
28442862 def _do_resumable_upload_call2 (
2863+ client ,
28452864 blob ,
28462865 content_type ,
28472866 data ,
@@ -2859,6 +2878,9 @@ def _do_resumable_upload_call2(
28592878 blob .chunk_size , total_bytes - 1 , total_bytes
28602879 )
28612880 expected_headers = {
2881+ ** _get_default_headers (
2882+ client ._connection .user_agent , x_upload_content_type = content_type
2883+ ),
28622884 "content-type" : content_type ,
28632885 "content-range" : content_range ,
28642886 }
@@ -2884,13 +2906,11 @@ def _do_resumable_helper(
28842906 data_corruption = False ,
28852907 retry = None ,
28862908 ):
2887- bucket = _Bucket (name = "yesterday" )
2888- blob = self ._make_one (u"blob-name" , bucket = bucket )
2889- blob .chunk_size = blob ._CHUNK_SIZE_MULTIPLE
2890- self .assertIsNotNone (blob .chunk_size )
2891-
2909+ CHUNK_SIZE = 256 * 1024
2910+ USER_AGENT = "testing 1.2.3"
2911+ content_type = u"text/html"
28922912 # Data to be uploaded.
2893- data = b"<html>" + (b"A" * blob . chunk_size ) + b"</html>"
2913+ data = b"<html>" + (b"A" * CHUNK_SIZE ) + b"</html>"
28942914 total_bytes = len (data )
28952915 if use_size :
28962916 size = total_bytes
@@ -2899,17 +2919,29 @@ def _do_resumable_helper(
28992919
29002920 # Create mocks to be checked for doing transport.
29012921 resumable_url = "http://test.invalid?upload_id=and-then-there-was-1"
2902- headers1 = {"location" : resumable_url }
2903- headers2 = {"range" : "bytes=0-{:d}" .format (blob .chunk_size - 1 )}
2922+ headers1 = {
2923+ ** _get_default_headers (USER_AGENT , content_type ),
2924+ "location" : resumable_url ,
2925+ }
2926+ headers2 = {
2927+ ** _get_default_headers (USER_AGENT , content_type ),
2928+ "range" : "bytes=0-{:d}" .format (CHUNK_SIZE - 1 ),
2929+ }
2930+ headers3 = _get_default_headers (USER_AGENT , content_type )
29042931 transport , responses = self ._make_resumable_transport (
2905- headers1 , headers2 , {} , total_bytes , data_corruption = data_corruption
2932+ headers1 , headers2 , headers3 , total_bytes , data_corruption = data_corruption
29062933 )
29072934
29082935 # Create some mock arguments and call the method under test.
29092936 client = mock .Mock (_http = transport , _connection = _Connection , spec = ["_http" ])
29102937 client ._connection .API_BASE_URL = "https://storage.googleapis.com"
2938+ client ._connection .user_agent = USER_AGENT
29112939 stream = io .BytesIO (data )
2912- content_type = u"text/html"
2940+
2941+ bucket = _Bucket (name = "yesterday" )
2942+ blob = self ._make_one (u"blob-name" , bucket = bucket )
2943+ blob .chunk_size = blob ._CHUNK_SIZE_MULTIPLE
2944+ self .assertIsNotNone (blob .chunk_size )
29132945
29142946 if timeout is None :
29152947 expected_timeout = self ._get_default_timeout ()
@@ -2939,6 +2971,7 @@ def _do_resumable_helper(
29392971
29402972 # Check the mocks.
29412973 call0 = self ._do_resumable_upload_call0 (
2974+ client ,
29422975 blob ,
29432976 content_type ,
29442977 size = size ,
@@ -2950,6 +2983,7 @@ def _do_resumable_helper(
29502983 timeout = expected_timeout ,
29512984 )
29522985 call1 = self ._do_resumable_upload_call1 (
2986+ client ,
29532987 blob ,
29542988 content_type ,
29552989 data ,
@@ -2963,6 +2997,7 @@ def _do_resumable_helper(
29632997 timeout = expected_timeout ,
29642998 )
29652999 call2 = self ._do_resumable_upload_call2 (
3000+ client ,
29663001 blob ,
29673002 content_type ,
29683003 data ,
@@ -3510,6 +3545,7 @@ def _create_resumable_upload_session_helper(
35103545 size = 10000
35113546 client = mock .Mock (_http = transport , _connection = _Connection , spec = [u"_http" ])
35123547 client ._connection .API_BASE_URL = "https://storage.googleapis.com"
3548+ client ._connection .user_agent = "testing 1.2.3"
35133549
35143550 if timeout is None :
35153551 expected_timeout = self ._get_default_timeout ()
@@ -3556,7 +3592,9 @@ def _create_resumable_upload_session_helper(
35563592 upload_url += "?" + urlencode (qs_params )
35573593 payload = b'{"name": "blob-name"}'
35583594 expected_headers = {
3559- "content-type" : "application/json; charset=UTF-8" ,
3595+ ** _get_default_headers (
3596+ client ._connection .user_agent , x_upload_content_type = content_type
3597+ ),
35603598 "x-upload-content-length" : str (size ),
35613599 "x-upload-content-type" : content_type ,
35623600 }
@@ -5739,6 +5777,7 @@ class _Connection(object):
57395777
57405778 API_BASE_URL = "http://example.com"
57415779 USER_AGENT = "testing 1.2.3"
5780+ user_agent = "testing 1.2.3"
57425781 credentials = object ()
57435782
57445783
0 commit comments