From 532386327d41c698f5d259f86486b83337f7735a Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Sat, 28 Aug 2021 08:32:14 +0100 Subject: [PATCH] S3 - Add RequestId to responses (#3836) --- moto/s3/responses.py | 3 +++ tests/test_s3/test_s3_metadata.py | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/test_s3/test_s3_metadata.py diff --git a/moto/s3/responses.py b/moto/s3/responses.py index 895c01259..9ded421e0 100644 --- a/moto/s3/responses.py +++ b/moto/s3/responses.py @@ -8,6 +8,7 @@ import sys from botocore.awsrequest import AWSPreparedRequest from moto.core.utils import ( + amzn_request_id, str_to_rfc_1123_datetime, py2_strip_unicode_keys, unix_time_millis, @@ -259,6 +260,7 @@ class ResponseObject(_TemplateEnvironmentMixin, ActionAuthenticatorMixin): # Using path-based buckets return self.bucket_response(request, full_url, headers) + @amzn_request_id def bucket_response(self, request, full_url, headers): self.method = request.method self.path = self._get_path(request) @@ -1026,6 +1028,7 @@ class ResponseObject(_TemplateEnvironmentMixin, ActionAuthenticatorMixin): line = body_io.readline() return bytes(new_body) + @amzn_request_id def key_or_control_response(self, request, full_url, headers): # Key and Control are lumped in because splitting out the regex is too much of a pain :/ self.method = request.method diff --git a/tests/test_s3/test_s3_metadata.py b/tests/test_s3/test_s3_metadata.py new file mode 100644 index 000000000..38987b6cd --- /dev/null +++ b/tests/test_s3/test_s3_metadata.py @@ -0,0 +1,27 @@ +import boto3 + +from moto import mock_s3 +from moto.s3.responses import DEFAULT_REGION_NAME + +import sure # noqa + + +@mock_s3 +def test_s3_returns_requestid(): + s3 = boto3.client("s3", region_name=DEFAULT_REGION_NAME) + resp = s3.create_bucket(Bucket="mybucket") + _check_metadata(resp) + + resp = s3.put_object(Bucket="mybucket", Key="steve", Body=b"is awesome") + _check_metadata(resp) + + resp = s3.get_object(Bucket="mybucket", Key="steve") + _check_metadata(resp) + + +def _check_metadata(resp): + meta = resp["ResponseMetadata"] + headers = meta["HTTPHeaders"] + meta.should.have.key("RequestId") + headers.should.have.key("x-amzn-requestid") + meta["RequestId"].should.equal(headers["x-amzn-requestid"])