diff --git a/flask/helpers.py b/flask/helpers.py index 37c458f5..4129ed30 100644 --- a/flask/helpers.py +++ b/flask/helpers.py @@ -483,8 +483,9 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False, (default), this value is set by :meth:`~Flask.get_send_file_max_age` of :data:`~flask.current_app`. - :param last_modified: the Datetime object representing timestamp for when - the input file was last modified. + :param last_modified: set the ``Last-Modified`` header to this value, + a :class:`~datetime.datetime` or timestamp. + If a file was passed, this overrides its mtime. """ mtime = None if isinstance(filename_or_fp, string_types): @@ -528,15 +529,10 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False, rv = current_app.response_class(data, mimetype=mimetype, headers=headers, direct_passthrough=True) - # if we know the file modification date, we can store it as - # the time of the last modification. if last_modified is not None: - if PY2: - rv.last_modified = int(last_modified.strftime("%s")) - else: - rv.last_modified = last_modified.timestamp() + rv.last_modified = last_modified elif mtime is not None: - rv.last_modified = int(mtime) + rv.last_modified = mtime rv.cache_control.public = True if cache_timeout is None: diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 9a07e0b9..3ff5900b 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -351,14 +351,15 @@ class TestSendfile(object): def test_send_file_last_modified(self): app = flask.Flask(__name__) - with app.test_request_context(): - dtm = datetime.datetime.now() - rv = flask.send_file('static/index.html', last_modified=dtm) - if PY2: - assert rv.last_modified == int(dtm.strftime("%s")) - else: - assert rv.last_modified == dtm.timestamp() - rv.close() + last_modified = datetime.datetime(1999, 1, 1) + + @app.route('/') + def index(): + return flask.send_file(StringIO("party like it's"), last_modified=last_modified) + + c = app.test_client() + rv = c.get('/') + assert rv.last_modified == last_modified def test_send_file_object(self): app = flask.Flask(__name__)