S3 - Return default content type (#4439)

This commit is contained in:
Bert Blommers 2021-10-19 16:46:23 +00:00 committed by GitHub
parent a192a42e8f
commit e388a25141
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 1 deletions

View File

@ -25,6 +25,7 @@ from moto.core.utils import (
)
from moto.cloudwatch.models import MetricDatum
from moto.utilities.tagging_service import TaggingService
from moto.utilities.utils import LowercaseDict
from moto.s3.exceptions import (
AccessDeniedByLock,
BucketAlreadyExists,
@ -113,7 +114,7 @@ class FakeKey(BaseModel):
self.acl = get_canned_acl("private")
self.website_redirect_location = None
self._storage_class = storage if storage else "STANDARD"
self._metadata = {}
self._metadata = LowercaseDict()
self._expiry = None
self._etag = etag
self._version_id = version_id
@ -136,6 +137,9 @@ class FakeKey(BaseModel):
self.lock_legal_status = lock_legal_status
self.lock_until = lock_until
# Default metadata values
self._metadata["Content-Type"] = "binary/octet-stream"
@property
def version_id(self):
return self._version_id

View File

@ -4,6 +4,9 @@ import string
import pkgutil
from collections.abc import MutableMapping
def str2bool(v):
if v in ("yes", True, "true", "True", "TRUE", "t", "1"):
return True
@ -52,3 +55,32 @@ def filter_resources(resources, filters, attr_pairs):
result.remove(resource)
break
return result
class LowercaseDict(MutableMapping):
"""A dictionary that lowercases all keys"""
def __init__(self, *args, **kwargs):
self.store = dict()
self.update(dict(*args, **kwargs)) # use the free update to set keys
def __getitem__(self, key):
return self.store[self._keytransform(key)]
def __setitem__(self, key, value):
self.store[self._keytransform(key)] = value
def __delitem__(self, key):
del self.store[self._keytransform(key)]
def __iter__(self):
return iter(self.store)
def __len__(self):
return len(self.store)
def __repr__(self):
return str(self.store)
def _keytransform(self, key):
return key.lower()

View File

@ -6468,3 +6468,21 @@ def test_delete_objects_with_empty_keyname():
client.delete_object(Bucket=bucket_name, Key=key_name)
client.list_objects(Bucket=bucket_name).shouldnt.have.key("Contents")
@mock_s3
def test_head_object_should_return_default_content_type():
s3 = boto3.resource("s3", region_name="us-east-1")
s3.create_bucket(Bucket="testbucket")
s3.Bucket("testbucket").upload_fileobj(BytesIO(b"foobar"), Key="testobject")
s3_client = boto3.client("s3", region_name="us-east-1")
resp = s3_client.head_object(Bucket="testbucket", Key="testobject")
resp["ContentType"].should.equal("binary/octet-stream")
resp["ResponseMetadata"]["HTTPHeaders"]["content-type"].should.equal(
"binary/octet-stream"
)
s3.Object("testbucket", "testobject").content_type.should.equal(
"binary/octet-stream"
)