Skip to content

Commit

Permalink
web,demos: Remove more uses of deprecated datetime utc methods
Browse files Browse the repository at this point in the history
Add a simple test case to give us some basic coverage of this
code path.

Closes tornadoweb#3335
  • Loading branch information
bdarnell committed Nov 2, 2023
1 parent 55db80e commit c60d80c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion demos/blog/templates/feed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{% if len(entries) > 0 %}
<updated>{{ max(e.updated for e in entries).strftime(date_format) }}</updated>
{% else %}
<updated>{{ datetime.datetime.utcnow().strftime(date_format) }}</updated>
<updated>{{ datetime.datetime.now(datetime.timezone.utc).strftime(date_format) }}</updated>
{% end %}
<id>http://{{ request.host }}/</id>
<link rel="alternate" href="http://{{ request.host }}/" title="{{ handler.settings["blog_title"] }}" type="text/html"/>
Expand Down
4 changes: 3 additions & 1 deletion demos/s3server/s3server.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ def get(self):
buckets.append(
{
"Name": name,
"CreationDate": datetime.datetime.utcfromtimestamp(info.st_ctime),
"CreationDate": datetime.datetime.fromtimestamp(
info.st_ctime, datetime.timezone.utc
),
}
)
self.render_xml({"ListAllMyBucketsResult": {"Buckets": {"Bucket": buckets}}})
Expand Down
9 changes: 9 additions & 0 deletions tornado/test/web_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,15 @@ def test_static_files(self):
self.assertTrue(b"Disallow: /" in response.body)
self.assertEqual(response.headers.get("Content-Type"), "text/plain")

def test_static_files_cacheable(self):
# Test that the version parameter triggers cache-control headers. This
# test is pretty weak but it gives us coverage of the code path which
# was important for detecting the deprecation of datetime.utcnow.
response = self.fetch("/robots.txt?v=12345")
self.assertTrue(b"Disallow: /" in response.body)
self.assertIn("Cache-Control", response.headers)
self.assertIn("Expires", response.headers)

def test_static_compressed_files(self):
response = self.fetch("/static/sample.xml.gz")
self.assertEqual(response.headers.get("Content-Type"), "application/gzip")
Expand Down
3 changes: 2 additions & 1 deletion tornado/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -2797,7 +2797,8 @@ def set_headers(self) -> None:
if cache_time > 0:
self.set_header(
"Expires",
datetime.datetime.utcnow() + datetime.timedelta(seconds=cache_time),
datetime.datetime.now(datetime.timezone.utc)
+ datetime.timedelta(seconds=cache_time),
)
self.set_header("Cache-Control", "max-age=" + str(cache_time))

Expand Down

0 comments on commit c60d80c

Please sign in to comment.