]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
web,demos: Remove more uses of deprecated datetime utc methods 3341/head
authorBen Darnell <ben@bendarnell.com>
Thu, 2 Nov 2023 01:40:54 +0000 (21:40 -0400)
committerBen Darnell <ben@bendarnell.com>
Thu, 2 Nov 2023 01:40:54 +0000 (21:40 -0400)
Add a simple test case to give us some basic coverage of this
code path.

Closes #3335

demos/blog/templates/feed.xml
demos/s3server/s3server.py
tornado/test/web_test.py
tornado/web.py

index a98826c8d302376fa3a79be57911c7d4c31286a9..c63ef306a979c067ceacf7922809dd7066993b80 100644 (file)
@@ -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"/>
index 5c5e6af2bafd6b1f55c345bd425e917b1f9ddc8c..b798c6b64b6e7a0bec6137eea8af9c2c90295d52 100644 (file)
@@ -138,7 +138,9 @@ class RootHandler(BaseRequestHandler):
             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}}})
index c8dce68c80d492ad685f7e397afc86473107fb43..fec66f39ac8aeaa36436415d4e7b42a6774ea8ef 100644 (file)
@@ -1128,6 +1128,15 @@ class StaticFileTest(WebTestCase):
         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")
index 333f736808213c44c3f0f0ec969552d75ea79732..039396470f8c701a8284a24612b18793b2b5840f 100644 (file)
@@ -2797,7 +2797,8 @@ class StaticFileHandler(RequestHandler):
         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))