26
26
import mock
27
27
import pytest
28
28
29
+ from google .cloud .storage ._helpers import _get_default_headers
29
30
from google .cloud .storage .retry import (
30
31
DEFAULT_RETRY ,
31
32
DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED ,
@@ -2212,16 +2213,19 @@ def test__set_metadata_to_none(self):
2212
2213
def test__get_upload_arguments (self ):
2213
2214
name = u"blob-name"
2214
2215
key = b"[pXw@,p@@AfBfrR3x-2b2SCHR,.?YwRO"
2216
+ client = mock .Mock (_connection = _Connection )
2217
+ client ._connection .user_agent = "testing 1.2.3"
2215
2218
blob = self ._make_one (name , bucket = None , encryption_key = key )
2216
2219
blob .content_disposition = "inline"
2217
2220
2218
2221
content_type = u"image/jpeg"
2219
- info = blob ._get_upload_arguments (content_type )
2222
+ info = blob ._get_upload_arguments (client , content_type )
2220
2223
2221
2224
headers , object_metadata , new_content_type = info
2222
2225
header_key_value = "W3BYd0AscEBAQWZCZnJSM3gtMmIyU0NIUiwuP1l3Uk8="
2223
2226
header_key_hash_value = "G0++dxF4q5rG4o9kE8gvEKn15RH6wLm0wXV1MgAlXOg="
2224
2227
expected_headers = {
2228
+ ** _get_default_headers (client ._connection .user_agent , content_type ),
2225
2229
"X-Goog-Encryption-Algorithm" : "AES256" ,
2226
2230
"X-Goog-Encryption-Key" : header_key_value ,
2227
2231
"X-Goog-Encryption-Key-Sha256" : header_key_hash_value ,
@@ -2368,7 +2372,11 @@ def _do_multipart_success(
2368
2372
+ data_read
2369
2373
+ b"\r \n --==0==--"
2370
2374
)
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
+ )
2372
2380
client ._http .request .assert_called_once_with (
2373
2381
"POST" , upload_url , data = payload , headers = headers , timeout = expected_timeout
2374
2382
)
@@ -2614,10 +2622,17 @@ def _initiate_resumable_helper(
2614
2622
2615
2623
self .assertEqual (upload .upload_url , upload_url )
2616
2624
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
+ )
2618
2629
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 )
2621
2636
self .assertFalse (upload .finished )
2622
2637
if chunk_size is None :
2623
2638
if blob_chunk_size is None :
@@ -2656,10 +2671,9 @@ def _initiate_resumable_helper(
2656
2671
# Check the mocks.
2657
2672
blob ._get_writable_metadata .assert_called_once_with ()
2658
2673
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
+ )
2663
2677
if size is not None :
2664
2678
expected_headers ["x-upload-content-length" ] = str (size )
2665
2679
if extra_headers is not None :
@@ -2778,6 +2792,7 @@ def _make_resumable_transport(
2778
2792
2779
2793
@staticmethod
2780
2794
def _do_resumable_upload_call0 (
2795
+ client ,
2781
2796
blob ,
2782
2797
content_type ,
2783
2798
size = None ,
@@ -2796,10 +2811,9 @@ def _do_resumable_upload_call0(
2796
2811
)
2797
2812
if predefined_acl is not None :
2798
2813
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
+ )
2803
2817
if size is not None :
2804
2818
expected_headers ["x-upload-content-length" ] = str (size )
2805
2819
payload = json .dumps ({"name" : blob .name }).encode ("utf-8" )
@@ -2809,6 +2823,7 @@ def _do_resumable_upload_call0(
2809
2823
2810
2824
@staticmethod
2811
2825
def _do_resumable_upload_call1 (
2826
+ client ,
2812
2827
blob ,
2813
2828
content_type ,
2814
2829
data ,
@@ -2828,6 +2843,9 @@ def _do_resumable_upload_call1(
2828
2843
content_range = "bytes 0-{:d}/{:d}" .format (blob .chunk_size - 1 , size )
2829
2844
2830
2845
expected_headers = {
2846
+ ** _get_default_headers (
2847
+ client ._connection .user_agent , x_upload_content_type = content_type
2848
+ ),
2831
2849
"content-type" : content_type ,
2832
2850
"content-range" : content_range ,
2833
2851
}
@@ -2842,6 +2860,7 @@ def _do_resumable_upload_call1(
2842
2860
2843
2861
@staticmethod
2844
2862
def _do_resumable_upload_call2 (
2863
+ client ,
2845
2864
blob ,
2846
2865
content_type ,
2847
2866
data ,
@@ -2859,6 +2878,9 @@ def _do_resumable_upload_call2(
2859
2878
blob .chunk_size , total_bytes - 1 , total_bytes
2860
2879
)
2861
2880
expected_headers = {
2881
+ ** _get_default_headers (
2882
+ client ._connection .user_agent , x_upload_content_type = content_type
2883
+ ),
2862
2884
"content-type" : content_type ,
2863
2885
"content-range" : content_range ,
2864
2886
}
@@ -2884,13 +2906,11 @@ def _do_resumable_helper(
2884
2906
data_corruption = False ,
2885
2907
retry = None ,
2886
2908
):
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"
2892
2912
# 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>"
2894
2914
total_bytes = len (data )
2895
2915
if use_size :
2896
2916
size = total_bytes
@@ -2899,17 +2919,29 @@ def _do_resumable_helper(
2899
2919
2900
2920
# Create mocks to be checked for doing transport.
2901
2921
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 )
2904
2931
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
2906
2933
)
2907
2934
2908
2935
# Create some mock arguments and call the method under test.
2909
2936
client = mock .Mock (_http = transport , _connection = _Connection , spec = ["_http" ])
2910
2937
client ._connection .API_BASE_URL = "https://storage.googleapis.com"
2938
+ client ._connection .user_agent = USER_AGENT
2911
2939
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 )
2913
2945
2914
2946
if timeout is None :
2915
2947
expected_timeout = self ._get_default_timeout ()
@@ -2939,6 +2971,7 @@ def _do_resumable_helper(
2939
2971
2940
2972
# Check the mocks.
2941
2973
call0 = self ._do_resumable_upload_call0 (
2974
+ client ,
2942
2975
blob ,
2943
2976
content_type ,
2944
2977
size = size ,
@@ -2950,6 +2983,7 @@ def _do_resumable_helper(
2950
2983
timeout = expected_timeout ,
2951
2984
)
2952
2985
call1 = self ._do_resumable_upload_call1 (
2986
+ client ,
2953
2987
blob ,
2954
2988
content_type ,
2955
2989
data ,
@@ -2963,6 +2997,7 @@ def _do_resumable_helper(
2963
2997
timeout = expected_timeout ,
2964
2998
)
2965
2999
call2 = self ._do_resumable_upload_call2 (
3000
+ client ,
2966
3001
blob ,
2967
3002
content_type ,
2968
3003
data ,
@@ -3510,6 +3545,7 @@ def _create_resumable_upload_session_helper(
3510
3545
size = 10000
3511
3546
client = mock .Mock (_http = transport , _connection = _Connection , spec = [u"_http" ])
3512
3547
client ._connection .API_BASE_URL = "https://storage.googleapis.com"
3548
+ client ._connection .user_agent = "testing 1.2.3"
3513
3549
3514
3550
if timeout is None :
3515
3551
expected_timeout = self ._get_default_timeout ()
@@ -3556,7 +3592,9 @@ def _create_resumable_upload_session_helper(
3556
3592
upload_url += "?" + urlencode (qs_params )
3557
3593
payload = b'{"name": "blob-name"}'
3558
3594
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
+ ),
3560
3598
"x-upload-content-length" : str (size ),
3561
3599
"x-upload-content-type" : content_type ,
3562
3600
}
@@ -5739,6 +5777,7 @@ class _Connection(object):
5739
5777
5740
5778
API_BASE_URL = "http://example.com"
5741
5779
USER_AGENT = "testing 1.2.3"
5780
+ user_agent = "testing 1.2.3"
5742
5781
credentials = object ()
5743
5782
5744
5783
0 commit comments