@@ -327,7 +327,9 @@ def test_download_as_string(self):
327327 fetched = blob .download_as_string ()
328328 self .assertEqual (fetched , b'abcdef' )
329329
330- def test_upload_from_file_simple (self ):
330+ def _upload_from_file_simple_test_helper (self , properties = None ,
331+ content_type_arg = None ,
332+ expected_content_type = None ):
331333 from six .moves .http_client import OK
332334 from six .moves .urllib .parse import parse_qsl
333335 from six .moves .urllib .parse import urlsplit
@@ -339,12 +341,13 @@ def test_upload_from_file_simple(self):
339341 (response , b'' ),
340342 )
341343 bucket = _Bucket (connection )
342- blob = self ._makeOne (BLOB_NAME , bucket = bucket )
344+ blob = self ._makeOne (BLOB_NAME , bucket = bucket , properties = properties )
343345 blob .CHUNK_SIZE = 5
344346 with NamedTemporaryFile () as fh :
345347 fh .write (DATA )
346348 fh .flush ()
347- blob .upload_from_file (fh , rewind = True )
349+ blob .upload_from_file (fh , rewind = True ,
350+ content_type = content_type_arg )
348351 rq = connection .http ._requested
349352 self .assertEqual (len (rq ), 1 )
350353 self .assertEqual (rq [0 ]['method' ], 'POST' )
@@ -358,7 +361,31 @@ def test_upload_from_file_simple(self):
358361 headers = dict (
359362 [(x .title (), str (y )) for x , y in rq [0 ]['headers' ].items ()])
360363 self .assertEqual (headers ['Content-Length' ], '6' )
361- self .assertEqual (headers ['Content-Type' ], 'application/unknown' )
364+ self .assertEqual (headers ['Content-Type' ], expected_content_type )
365+
366+ def test_upload_from_file_simple (self ):
367+ self ._upload_from_file_simple_test_helper (
368+ expected_content_type = 'application/octet-stream' )
369+
370+ def test_upload_from_file_simple_with_content_type (self ):
371+ EXPECTED_CONTENT_TYPE = 'foo/bar'
372+ self ._upload_from_file_simple_test_helper (
373+ properties = {'contentType' : EXPECTED_CONTENT_TYPE },
374+ expected_content_type = EXPECTED_CONTENT_TYPE )
375+
376+ def test_upload_from_file_simple_with_content_type_passed (self ):
377+ EXPECTED_CONTENT_TYPE = 'foo/bar'
378+ self ._upload_from_file_simple_test_helper (
379+ content_type_arg = EXPECTED_CONTENT_TYPE ,
380+ expected_content_type = EXPECTED_CONTENT_TYPE )
381+
382+ def test_upload_from_file_simple_both_content_type_sources (self ):
383+ EXPECTED_CONTENT_TYPE = 'foo/bar'
384+ ALT_CONTENT_TYPE = 'foo/baz'
385+ self ._upload_from_file_simple_test_helper (
386+ properties = {'contentType' : ALT_CONTENT_TYPE },
387+ content_type_arg = EXPECTED_CONTENT_TYPE ,
388+ expected_content_type = EXPECTED_CONTENT_TYPE )
362389
363390 def test_upload_from_file_resumable (self ):
364391 from six .moves .http_client import OK
@@ -403,7 +430,7 @@ def test_upload_from_file_resumable(self):
403430 [(x .title (), str (y )) for x , y in rq [0 ]['headers' ].items ()])
404431 self .assertEqual (headers ['X-Upload-Content-Length' ], '6' )
405432 self .assertEqual (headers ['X-Upload-Content-Type' ],
406- 'application/unknown ' )
433+ 'application/octet-stream ' )
407434 self .assertEqual (rq [1 ]['method' ], 'PUT' )
408435 self .assertEqual (rq [1 ]['uri' ], UPLOAD_URL )
409436 headers = dict (
@@ -457,9 +484,11 @@ def test_upload_from_file_w_slash_in_name(self):
457484 headers = dict (
458485 [(x .title (), str (y )) for x , y in rq [0 ]['headers' ].items ()])
459486 self .assertEqual (headers ['Content-Length' ], '6' )
460- self .assertEqual (headers ['Content-Type' ], 'application/unknown ' )
487+ self .assertEqual (headers ['Content-Type' ], 'application/octet-stream ' )
461488
462- def test_upload_from_filename (self ):
489+ def _upload_from_filename_test_helper (self , properties = None ,
490+ content_type_arg = None ,
491+ expected_content_type = None ):
463492 from six .moves .http_client import OK
464493 from six .moves .urllib .parse import parse_qsl
465494 from six .moves .urllib .parse import urlsplit
@@ -478,12 +507,13 @@ def test_upload_from_filename(self):
478507 (chunk2_response , '' ),
479508 )
480509 bucket = _Bucket (connection )
481- blob = self ._makeOne (BLOB_NAME , bucket = bucket )
510+ blob = self ._makeOne (BLOB_NAME , bucket = bucket ,
511+ properties = properties )
482512 blob .CHUNK_SIZE = 5
483513 with NamedTemporaryFile (suffix = '.jpeg' ) as fh :
484514 fh .write (DATA )
485515 fh .flush ()
486- blob .upload_from_filename (fh .name )
516+ blob .upload_from_filename (fh .name , content_type = content_type_arg )
487517 rq = connection .http ._requested
488518 self .assertEqual (len (rq ), 1 )
489519 self .assertEqual (rq [0 ]['method' ], 'POST' )
@@ -497,7 +527,31 @@ def test_upload_from_filename(self):
497527 headers = dict (
498528 [(x .title (), str (y )) for x , y in rq [0 ]['headers' ].items ()])
499529 self .assertEqual (headers ['Content-Length' ], '6' )
500- self .assertEqual (headers ['Content-Type' ], 'image/jpeg' )
530+ self .assertEqual (headers ['Content-Type' ], expected_content_type )
531+
532+ def test_upload_from_filename (self ):
533+ self ._upload_from_filename_test_helper (
534+ expected_content_type = 'image/jpeg' )
535+
536+ def test_upload_from_filename_with_content_type (self ):
537+ EXPECTED_CONTENT_TYPE = 'foo/bar'
538+ self ._upload_from_filename_test_helper (
539+ properties = {'contentType' : EXPECTED_CONTENT_TYPE },
540+ expected_content_type = EXPECTED_CONTENT_TYPE )
541+
542+ def test_upload_from_filename_with_content_type_passed (self ):
543+ EXPECTED_CONTENT_TYPE = 'foo/bar'
544+ self ._upload_from_filename_test_helper (
545+ content_type_arg = EXPECTED_CONTENT_TYPE ,
546+ expected_content_type = EXPECTED_CONTENT_TYPE )
547+
548+ def test_upload_from_filename_both_content_type_sources (self ):
549+ EXPECTED_CONTENT_TYPE = 'foo/bar'
550+ ALT_CONTENT_TYPE = 'foo/baz'
551+ self ._upload_from_filename_test_helper (
552+ properties = {'contentType' : ALT_CONTENT_TYPE },
553+ content_type_arg = EXPECTED_CONTENT_TYPE ,
554+ expected_content_type = EXPECTED_CONTENT_TYPE )
501555
502556 def test_upload_from_string_w_bytes (self ):
503557 from six .moves .http_client import OK
0 commit comments