@@ -111,6 +111,27 @@ def test_s3_upload_file( moto_boto, path, url, ftype, expected,fs, caplog):
111111 otherwise an exception will be raised.
112112 '''
113113 assert client .Object ('tesk' , 'folder/file.txt' ).load () == None
114+ # Check the ContentType metadata is set correctly for text files
115+ head = client .meta .client .head_object (Bucket = trans .bucket , Key = trans .file_path )
116+ assert head ['ContentType' ] == 'text/plain'
117+
118+
119+ @pytest .mark .parametrize ("filename, url, expected_content" , [
120+ ("file.txt" , "s3://tesk/folder/file.txt" , "text/plain" ),
121+ ("file.zip" , "s3://tesk/folder/file.zip" , "application/zip" ),
122+ ])
123+ def test_s3_upload_file_content_type (moto_boto , filename , url , expected_content , fs ):
124+ """
125+ Ensure uploaded objects have correct Content-Type metadata based on file extension
126+ """
127+ fs .create_file (f"/home/user/filer_test/{ filename } " )
128+ client = boto3 .resource ('s3' , endpoint_url = "http://s3.amazonaws.com" )
129+ trans = S3Transput (f"/home/user/filer_test/{ filename } " , url , "FILE" )
130+ trans .bucket_obj = client .Bucket (trans .bucket )
131+ assert trans .upload_file () == 0
132+ head = client .meta .client .head_object (Bucket = trans .bucket , Key = trans .file_path )
133+ assert head ['ContentType' ] == expected_content
134+
114135
115136
116137
@@ -133,8 +154,8 @@ def test_s3_upload_directory(path, url, ftype, expected, moto_boto, caplog):
133154 Checking if the file was uploaded, if the object is found load() method will return None
134155 otherwise an exception will be raised.
135156 '''
136- assert client .Object ('tesk' , 'folder1/folder2/test_filer.py' ).load () == None
137-
157+ assert client .Object ('tesk' , 'folder1/folder2/test_filer.py' ).load () == None head = client . meta . client . head_object ( Bucket = trans . bucket , Key = 'folder1/folder2/test_filer.py' )
158+ assert head [ 'ContentType' ]. startswith ( 'text/' )
138159def test_upload_directory_for_unknown_file_type (moto_boto , fs , monkeypatch , caplog ):
139160 """
140161 Checking whether an exception is raised when the object type is neither file or directory
0 commit comments