25
25
import pytest
26
26
import six
27
27
from six .moves import http_client
28
+ from six .moves .urllib .parse import urlencode
28
29
29
30
from google .cloud .storage .retry import DEFAULT_RETRY
30
31
from google .cloud .storage .retry import DEFAULT_RETRY_IF_ETAG_IN_JSON
@@ -2041,8 +2042,6 @@ def _do_multipart_success(
2041
2042
mtls = False ,
2042
2043
retry = None ,
2043
2044
):
2044
- from six .moves .urllib .parse import urlencode
2045
-
2046
2045
bucket = _Bucket (name = "w00t" , user_project = user_project )
2047
2046
blob = self ._make_one (u"blob-name" , bucket = bucket , kms_key_name = kms_key_name )
2048
2047
self .assertIsNone (blob .chunk_size )
@@ -2286,7 +2285,6 @@ def _initiate_resumable_helper(
2286
2285
mtls = False ,
2287
2286
retry = None ,
2288
2287
):
2289
- from six .moves .urllib .parse import urlencode
2290
2288
from google .resumable_media .requests import ResumableUpload
2291
2289
from google .cloud .storage .blob import _DEFAULT_CHUNKSIZE
2292
2290
@@ -3248,7 +3246,15 @@ def test_upload_from_string_w_text_w_num_retries(self):
3248
3246
self ._upload_from_string_helper (data , num_retries = 2 )
3249
3247
3250
3248
def _create_resumable_upload_session_helper (
3251
- self , origin = None , side_effect = None , timeout = None
3249
+ self ,
3250
+ origin = None ,
3251
+ side_effect = None ,
3252
+ timeout = None ,
3253
+ if_generation_match = None ,
3254
+ if_generation_not_match = None ,
3255
+ if_metageneration_match = None ,
3256
+ if_metageneration_not_match = None ,
3257
+ retry = None ,
3252
3258
):
3253
3259
bucket = _Bucket (name = "alex-trebek" )
3254
3260
blob = self ._make_one ("blob-name" , bucket = bucket )
@@ -3280,6 +3286,11 @@ def _create_resumable_upload_session_helper(
3280
3286
size = size ,
3281
3287
origin = origin ,
3282
3288
client = client ,
3289
+ if_generation_match = if_generation_match ,
3290
+ if_generation_not_match = if_generation_not_match ,
3291
+ if_metageneration_match = if_metageneration_match ,
3292
+ if_metageneration_not_match = if_metageneration_not_match ,
3293
+ retry = retry ,
3283
3294
** timeout_kwarg
3284
3295
)
3285
3296
@@ -3289,10 +3300,23 @@ def _create_resumable_upload_session_helper(
3289
3300
3290
3301
# Check the mocks.
3291
3302
upload_url = (
3292
- "https://storage.googleapis.com/upload/storage/v1"
3293
- + bucket .path
3294
- + "/o?uploadType=resumable"
3303
+ "https://storage.googleapis.com/upload/storage/v1" + bucket .path + "/o"
3295
3304
)
3305
+
3306
+ qs_params = [("uploadType" , "resumable" )]
3307
+ if if_generation_match is not None :
3308
+ qs_params .append (("ifGenerationMatch" , if_generation_match ))
3309
+
3310
+ if if_generation_not_match is not None :
3311
+ qs_params .append (("ifGenerationNotMatch" , if_generation_not_match ))
3312
+
3313
+ if if_metageneration_match is not None :
3314
+ qs_params .append (("ifMetagenerationMatch" , if_metageneration_match ))
3315
+
3316
+ if if_metageneration_not_match is not None :
3317
+ qs_params .append (("ifMetaGenerationNotMatch" , if_metageneration_not_match ))
3318
+
3319
+ upload_url += "?" + urlencode (qs_params )
3296
3320
payload = b'{"name": "blob-name"}'
3297
3321
expected_headers = {
3298
3322
"content-type" : "application/json; charset=UTF-8" ,
@@ -3318,6 +3342,26 @@ def test_create_resumable_upload_session_with_custom_timeout(self):
3318
3342
def test_create_resumable_upload_session_with_origin (self ):
3319
3343
self ._create_resumable_upload_session_helper (origin = "http://google.com" )
3320
3344
3345
+ def test_create_resumable_upload_session_with_generation_match (self ):
3346
+ self ._create_resumable_upload_session_helper (
3347
+ if_generation_match = 123456 , if_metageneration_match = 2
3348
+ )
3349
+
3350
+ def test_create_resumable_upload_session_with_generation_not_match (self ):
3351
+ self ._create_resumable_upload_session_helper (
3352
+ if_generation_not_match = 0 , if_metageneration_not_match = 3
3353
+ )
3354
+
3355
+ def test_create_resumable_upload_session_with_conditional_retry_success (self ):
3356
+ self ._create_resumable_upload_session_helper (
3357
+ retry = DEFAULT_RETRY_IF_GENERATION_SPECIFIED , if_generation_match = 123456
3358
+ )
3359
+
3360
+ def test_create_resumable_upload_session_with_conditional_retry_failure (self ):
3361
+ self ._create_resumable_upload_session_helper (
3362
+ retry = DEFAULT_RETRY_IF_GENERATION_SPECIFIED
3363
+ )
3364
+
3321
3365
def test_create_resumable_upload_session_with_failure (self ):
3322
3366
from google .resumable_media import InvalidResponse
3323
3367
from google .cloud import exceptions
0 commit comments