Added _PackageBoundObject.get_static_file_options.

This method receives the name of a static file that is going to be served
up and generates a dict of options to use when serving the file.  The
default set is empty so code will fall back to the existing behavior if
the method is not overridden.

I needed this method to adjust the cache control headers for .js files that
one of my applications was statically serving.  The default expiration is
buried in an argument to send_file and is set to 12 hours.  There was no
good way to adjust this value previously.
This commit is contained in:
Dave Shawley 2012-03-12 11:19:17 -04:00
parent 8d7ca29a35
commit 06b224676d
2 changed files with 34 additions and 3 deletions

View file

@ -17,7 +17,7 @@ import unittest
from logging import StreamHandler
from StringIO import StringIO
from flask.testsuite import FlaskTestCase, catch_warnings, catch_stderr
from werkzeug.http import parse_options_header
from werkzeug.http import parse_cache_control_header, parse_options_header
def has_encoding(name):
@ -204,6 +204,30 @@ class SendfileTestCase(FlaskTestCase):
self.assert_equal(value, 'attachment')
self.assert_equal(options['filename'], 'index.txt')
def test_static_file(self):
app = flask.Flask(__name__)
# default cache timeout is 12 hours (hard-coded)
with app.test_request_context():
rv = app.send_static_file('index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assert_equal(cc.max_age, 12 * 60 * 60)
# override get_static_file_options with some new values and check them
class StaticFileApp(flask.Flask):
def __init__(self):
super(StaticFileApp, self).__init__(__name__)
def get_static_file_options(self, filename):
opts = super(StaticFileApp, self).get_static_file_options(filename)
opts['cache_timeout'] = 10
# this test catches explicit inclusion of the conditional
# keyword arg in the guts
opts['conditional'] = True
return opts
app = StaticFileApp()
with app.test_request_context():
rv = app.send_static_file('index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assert_equal(cc.max_age, 10)
class LoggingTestCase(FlaskTestCase):