From eeb32bd048553b326d89836183e7aa3fdb49d9dc Mon Sep 17 00:00:00 2001 From: rafcio19 Date: Sat, 20 May 2023 21:47:08 +0100 Subject: [PATCH] Core: add date to response metadata (#6329) --- moto/core/responses.py | 5 ++++- tests/test_core/test_responses.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/moto/core/responses.py b/moto/core/responses.py index 8fc794261..a50aa80a4 100644 --- a/moto/core/responses.py +++ b/moto/core/responses.py @@ -306,7 +306,10 @@ class BaseResponse(_TemplateEnvironmentMixin, ActionAuthenticatorMixin): self.headers = request.headers if "host" not in self.headers: self.headers["host"] = urlparse(full_url).netloc - self.response_headers = {"server": "amazon.com"} + self.response_headers = { + "server": "amazon.com", + "date": datetime.datetime.now().strftime("%a, %d %b %Y %H:%M:%S GMT"), + } # Register visit with IAM from moto.iam.models import mark_account_as_visited diff --git a/tests/test_core/test_responses.py b/tests/test_core/test_responses.py index 728f6012d..f306348ff 100644 --- a/tests/test_core/test_responses.py +++ b/tests/test_core/test_responses.py @@ -1,3 +1,4 @@ +import datetime from unittest import SkipTest, mock import sure # noqa # pylint: disable=unused-import @@ -8,6 +9,7 @@ from botocore.awsrequest import AWSPreparedRequest from moto.core.responses import AWSServiceSpec, BaseResponse from moto.core.responses import flatten_json_request_body from moto import settings +from freezegun import freeze_time def test_flatten_json_request_body(): @@ -225,3 +227,21 @@ def test_jinja_render_prettify(m_env_var): xml_string = template.render() assert xml_string == expected_output assert m_env_var + + +def test_response_metadata(): + # Setup + frozen_time = datetime.datetime( + 2023, 5, 20, 10, 20, 30, tzinfo=datetime.timezone.utc + ) + request = AWSPreparedRequest("GET", "http://request", {}, None, False) + + # Execute + with freeze_time(frozen_time): + bc = BaseResponse() + bc.setup_class(request, request.url, request.headers) + + # Verify + assert "date" in bc.response_headers + if not settings.TEST_SERVER_MODE: + assert bc.response_headers["date"] == "Sat, 20 May 2023 10:20:30 GMT"