From ecdd395bece7557c70100a6b34cbe132955c65a8 Mon Sep 17 00:00:00 2001 From: Vincent Barbaresi Date: Sat, 30 Oct 2021 11:49:42 +0200 Subject: [PATCH] Fix range headers: use response_headers parameters instead of creating a new dict (#4502) --- moto/s3/responses.py | 3 +-- tests/test_s3/test_s3.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/moto/s3/responses.py b/moto/s3/responses.py index 9c642b95e..3f2172c61 100644 --- a/moto/s3/responses.py +++ b/moto/s3/responses.py @@ -984,8 +984,7 @@ class ResponseObject(_TemplateEnvironmentMixin, ActionAuthenticatorMixin): template.render(deleted=deleted_objects, delete_errors=error_names), ) - def _handle_range_header(self, request, headers, response_content): - response_headers = {} + def _handle_range_header(self, request, response_headers, response_content): length = len(response_content) last = length - 1 _, rspec = request.headers.get("range").split("=") diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py index 67a0b9ce2..e7b89dac4 100644 --- a/tests/test_s3/test_s3.py +++ b/tests/test_s3/test_s3.py @@ -6486,3 +6486,24 @@ def test_head_object_should_return_default_content_type(): s3.Object("testbucket", "testobject").content_type.should.equal( "binary/octet-stream" ) + + +@mock_s3 +def test_request_partial_content_should_contain_all_metadata(): + # github.com/spulec/moto/issues/4203 + bucket = "bucket" + object_key = "key" + body = "some text" + query_range = "0-3" + + s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) + s3.create_bucket(Bucket=bucket) + obj = boto3.resource("s3").Object(bucket, object_key) + obj.put(Body=body) + + response = obj.get(Range="bytes={}".format(query_range)) + + assert response["ETag"] == obj.e_tag + assert response["LastModified"] == obj.last_modified + assert response["ContentLength"] == 4 + assert response["ContentRange"] == "bytes {}/{}".format(query_range, len(body))