]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Use 'get_version' to calculate static file hashes.
authorDavid Wolever <david@wolever.net>
Thu, 16 May 2013 22:45:06 +0000 (18:45 -0400)
committerDavid Wolever <david@wolever.net>
Thu, 16 May 2013 22:46:59 +0000 (18:46 -0400)
tornado/test/web_test.py
tornado/web.py

index a1df2eb83f39202c6229a406bb4cadfbb72858ab..3afdae5da9813cdefe081e420a18edad8f790bfd 100644 (file)
@@ -753,6 +753,10 @@ class ErrorResponseTest(WebTestCase):
 
 @wsgi_safe
 class StaticFileTest(WebTestCase):
+    # The expected MD5 hash of robots.txt, used in tests that call
+    # StaticFileHandler.get_version
+    robots_txt_hash = b"f71d20196d4caf35b6a670db8c70b03d"
+
     def get_handlers(self):
         class StaticUrlHandler(RequestHandler):
             def get(self, path):
@@ -802,12 +806,15 @@ class StaticFileTest(WebTestCase):
 
     def test_static_url(self):
         response = self.fetch("/static_url/robots.txt")
-        self.assertEqual(response.body, b"/static/robots.txt?v=f71d2")
+        self.assertEqual(response.body,
+                         b"/static/robots.txt?v=" + self.robots_txt_hash)
 
     def test_absolute_static_url(self):
         response = self.fetch("/abs_static_url/robots.txt")
         self.assertEqual(response.body,
-                         utf8(self.get_url("/") + "static/robots.txt?v=f71d2"))
+                         utf8(self.get_url("/") +
+                              "static/robots.txt?v=" +
+                              self.robots_txt_hash))
 
     def test_include_host_override(self):
         self._trigger_include_host_check(False)
@@ -855,6 +862,11 @@ class StaticFileTest(WebTestCase):
                 'If-Modified-Since': format_timestamp(stat.st_mtime + 1)})
         self.assertEqual(response.code, 304)
 
+    def test_static_etag(self):
+        response = self.fetch('/static/robots.txt')
+        self.assertEqual(response.headers.get("Etag"),
+                         b'"%s"' %(self.robots_txt_hash, ))
+
 
 @wsgi_safe
 class CustomStaticFileTest(WebTestCase):
index af5ea5127fe268df3a90ea1e7bd7cf616e22e112..8b4d31738f2e49a7661992c4913c89344050ef52 100644 (file)
@@ -1822,7 +1822,7 @@ class StaticFileHandler(RequestHandler):
                     hashes[abs_path] = None
             hsh = hashes.get(abs_path)
             if hsh:
-                return hsh[:5]
+                return hsh
         return None
 
     def parse_url_path(self, url_path):
@@ -1836,6 +1836,15 @@ class StaticFileHandler(RequestHandler):
             url_path = url_path.replace("/", os.path.sep)
         return url_path
 
+    def compute_etag(self):
+        # Note: compute the etag for static files using get_version so that the
+        # entire file is always considered, even when the request includes a
+        # Range header, so the response will only be a portion of the file.
+        version_hash = self.get_version(self.settings, self.path_args[0])
+        if not version_hash:
+            return None
+        return '"%s"' %(version_hash, )
+
 
 class FallbackHandler(RequestHandler):
     """A `RequestHandler` that wraps another HTTP server callback.