Merge pull request #433 from dave-shawley/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Introduced a method to adjust static file cache-control headers based on filename
authored by Ron DuPlain · committed by Ron DuPlain · parents f7bb2b5, 06b2246 · Browse files · 2 files·+34 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+34 added−3 removed
- other:+34 added−3 removed
Provenance
- Commit
f4a31db- Tree
89f90db- Author
Ron DuPlain <ron.duplain@gmail.com>- Committer
Ron DuPlain <ron.duplain@gmail.com>- Parent 1
f7bb2b5- Parent 2
06b2246
2 files changed+34 added−3 removed
modified:flask/testsuite/helpers.py+25 added-1 removed
15 unmodified lines16 import unittest17 from logging import StreamHandler18 from StringIO import StringIO19 from flask.testsuite import FlaskTestCase, catch_warnings, catch_stderr20-from werkzeug.http import parse_options_header20+from werkzeug.http import parse_cache_control_header, parse_options_header21 22 23 def has_encoding(name):24 try:178 unmodified lines203 value, options = parse_options_header(rv.headers['Content-Disposition'])204 self.assert_equal(value, 'attachment')205 self.assert_equal(options['filename'], 'index.txt')206 207+ def test_static_file(self):208+ app = flask.Flask(__name__)209+ # default cache timeout is 12 hours (hard-coded)210+ with app.test_request_context():211+ rv = app.send_static_file('index.html')212+ cc = parse_cache_control_header(rv.headers['Cache-Control'])213+ self.assert_equal(cc.max_age, 12 * 60 * 60)214+ # override get_static_file_options with some new values and check them215+ class StaticFileApp(flask.Flask):216+ def __init__(self):217+ super(StaticFileApp, self).__init__(__name__)218+ def get_static_file_options(self, filename):219+ opts = super(StaticFileApp, self).get_static_file_options(filename)220+ opts['cache_timeout'] = 10221+ # this test catches explicit inclusion of the conditional222+ # keyword arg in the guts223+ opts['conditional'] = True224+ return opts225+ app = StaticFileApp()226+ with app.test_request_context():227+ rv = app.send_static_file('index.html')228+ cc = parse_cache_control_header(rv.headers['Cache-Control'])229+ self.assert_equal(cc.max_age, 10)230 231+232 class LoggingTestCase(FlaskTestCase):233 234 def test_logger_cache(self):235 app = flask.Flask(__name__)148 unmodified linesmodified:flask/helpers.py+9 added-2 removed
493 unmodified lines494 """495 filename = safe_join(directory, filename)496 if not os.path.isfile(filename):497 raise NotFound()498+ options.setdefault('conditional', True)498- return send_file(filename, conditional=True, **options)499+ return send_file(filename, **options)500 501 502 def get_root_path(import_name):503 """Returns the path to a package or cwd if that cannot be found. This147 unmodified lines651 if self.template_folder is not None:652 return FileSystemLoader(os.path.join(self.root_path,653 self.template_folder))654 655+ def get_static_file_options(self, filename):656+ """Function used internally to determine what keyword arguments657+ to send to :func:`send_from_directory` for a specific file."""658+ return {}659+660 def send_static_file(self, filename):661 """Function used internally to send static files from the static662 folder to the browser.663 664 .. versionadded:: 0.5665 """666 if not self.has_static_folder:667 raise RuntimeError('No static folder for this object')662- return send_from_directory(self.static_folder, filename)668+ return send_from_directory(self.static_folder, filename,669+ **self.get_static_file_options(filename))670 671 def open_resource(self, resource, mode='rb'):672 """Opens a resource from the application's resource folder. To see673 how this works, consider the following folder structure::22 unmodified lines