2525import pytest
2626import six
2727from six .moves import http_client
28+ from six .moves .urllib .parse import urlencode
2829
2930from google .cloud .storage .retry import DEFAULT_RETRY
3031from google .cloud .storage .retry import DEFAULT_RETRY_IF_ETAG_IN_JSON
@@ -2041,8 +2042,6 @@ def _do_multipart_success(
20412042 mtls = False ,
20422043 retry = None ,
20432044 ):
2044- from six .moves .urllib .parse import urlencode
2045-
20462045 bucket = _Bucket (name = "w00t" , user_project = user_project )
20472046 blob = self ._make_one (u"blob-name" , bucket = bucket , kms_key_name = kms_key_name )
20482047 self .assertIsNone (blob .chunk_size )
@@ -2286,7 +2285,6 @@ def _initiate_resumable_helper(
22862285 mtls = False ,
22872286 retry = None ,
22882287 ):
2289- from six .moves .urllib .parse import urlencode
22902288 from google .resumable_media .requests import ResumableUpload
22912289 from google .cloud .storage .blob import _DEFAULT_CHUNKSIZE
22922290
@@ -3248,7 +3246,15 @@ def test_upload_from_string_w_text_w_num_retries(self):
32483246 self ._upload_from_string_helper (data , num_retries = 2 )
32493247
32503248 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 ,
32523258 ):
32533259 bucket = _Bucket (name = "alex-trebek" )
32543260 blob = self ._make_one ("blob-name" , bucket = bucket )
@@ -3280,6 +3286,11 @@ def _create_resumable_upload_session_helper(
32803286 size = size ,
32813287 origin = origin ,
32823288 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 ,
32833294 ** timeout_kwarg
32843295 )
32853296
@@ -3289,10 +3300,23 @@ def _create_resumable_upload_session_helper(
32893300
32903301 # Check the mocks.
32913302 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"
32953304 )
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 )
32963320 payload = b'{"name": "blob-name"}'
32973321 expected_headers = {
32983322 "content-type" : "application/json; charset=UTF-8" ,
@@ -3318,6 +3342,26 @@ def test_create_resumable_upload_session_with_custom_timeout(self):
33183342 def test_create_resumable_upload_session_with_origin (self ):
33193343 self ._create_resumable_upload_session_helper (origin = "http://google.com" )
33203344
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+
33213365 def test_create_resumable_upload_session_with_failure (self ):
33223366 from google .resumable_media import InvalidResponse
33233367 from google .cloud import exceptions
0 commit comments