S3 - Add RequestId to responses (#3836)

This commit is contained in:
Bert Blommers 2021-08-28 08:32:14 +01:00 committed by GitHub
parent 6a6a71ebff
commit 532386327d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -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

View File

@ -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"])