2017-07-28 14:55:52 -07:00
|
|
|
|
import datetime
|
2018-10-18 23:30:03 +01:00
|
|
|
|
import io
|
2011-08-26 11:21:26 +01:00
|
|
|
|
import os
|
2019-10-28 21:37:25 -03:00
|
|
|
|
import sys
|
2016-09-26 12:43:46 +02:00
|
|
|
|
|
2017-07-28 14:55:52 -07:00
|
|
|
|
import pytest
|
2016-09-26 12:43:46 +02:00
|
|
|
|
from werkzeug.datastructures import Range
|
2019-06-01 08:35:03 -07:00
|
|
|
|
from werkzeug.exceptions import BadRequest
|
|
|
|
|
|
from werkzeug.exceptions import NotFound
|
|
|
|
|
|
from werkzeug.http import http_date
|
|
|
|
|
|
from werkzeug.http import parse_cache_control_header
|
|
|
|
|
|
from werkzeug.http import parse_options_header
|
2017-07-28 14:55:52 -07:00
|
|
|
|
|
|
|
|
|
|
import flask
|
2019-06-01 08:35:03 -07:00
|
|
|
|
from flask.helpers import get_debug_flag
|
|
|
|
|
|
from flask.helpers import get_env
|
2011-08-26 11:21:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
2020-04-04 09:43:06 -07:00
|
|
|
|
class FakePath:
|
2019-01-03 17:17:45 -08:00
|
|
|
|
"""Fake object to represent a ``PathLike object``.
|
|
|
|
|
|
|
|
|
|
|
|
This represents a ``pathlib.Path`` object in python 3.
|
|
|
|
|
|
See: https://www.python.org/dev/peps/pep-0519/
|
|
|
|
|
|
"""
|
2019-05-06 15:39:41 -04:00
|
|
|
|
|
2019-01-03 17:17:45 -08:00
|
|
|
|
def __init__(self, path):
|
|
|
|
|
|
self.path = path
|
|
|
|
|
|
|
|
|
|
|
|
def __fspath__(self):
|
|
|
|
|
|
return self.path
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-04-04 09:43:06 -07:00
|
|
|
|
class PyBytesIO:
|
2019-11-18 23:34:45 -08:00
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
self._io = io.BytesIO(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
|
|
return getattr(self._io, name)
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-04-04 09:43:06 -07:00
|
|
|
|
class TestSendfile:
|
2017-05-23 15:18:39 -07:00
|
|
|
|
def test_send_file_regular(self, app, req_ctx):
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = flask.send_file("static/index.html")
|
2017-05-23 15:18:39 -07:00
|
|
|
|
assert rv.direct_passthrough
|
2019-05-06 15:39:41 -04:00
|
|
|
|
assert rv.mimetype == "text/html"
|
|
|
|
|
|
with app.open_resource("static/index.html") as f:
|
2017-05-23 15:18:39 -07:00
|
|
|
|
rv.direct_passthrough = False
|
|
|
|
|
|
assert rv.data == f.read()
|
|
|
|
|
|
rv.close()
|
2011-08-26 11:21:26 +01:00
|
|
|
|
|
2019-06-07 13:23:13 -07:00
|
|
|
|
def test_send_file_xsendfile(self, app, req_ctx):
|
2011-08-26 11:21:26 +01:00
|
|
|
|
app.use_x_sendfile = True
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = flask.send_file("static/index.html")
|
2017-05-23 15:18:39 -07:00
|
|
|
|
assert rv.direct_passthrough
|
2019-05-06 15:39:41 -04:00
|
|
|
|
assert "x-sendfile" in rv.headers
|
|
|
|
|
|
assert rv.headers["x-sendfile"] == os.path.join(
|
|
|
|
|
|
app.root_path, "static/index.html"
|
|
|
|
|
|
)
|
|
|
|
|
|
assert rv.mimetype == "text/html"
|
2017-05-23 15:18:39 -07:00
|
|
|
|
rv.close()
|
2011-08-26 11:21:26 +01:00
|
|
|
|
|
2017-05-23 15:18:39 -07:00
|
|
|
|
def test_send_file_last_modified(self, app, client):
|
2016-06-05 12:42:34 -07:00
|
|
|
|
last_modified = datetime.datetime(1999, 1, 1)
|
|
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
@app.route("/")
|
2016-06-05 12:42:34 -07:00
|
|
|
|
def index():
|
2019-05-06 15:39:41 -04:00
|
|
|
|
return flask.send_file(
|
2019-11-18 23:34:45 -08:00
|
|
|
|
io.BytesIO(b"party like it's"),
|
2019-05-06 15:39:41 -04:00
|
|
|
|
last_modified=last_modified,
|
|
|
|
|
|
mimetype="text/plain",
|
|
|
|
|
|
)
|
2016-06-05 12:42:34 -07:00
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = client.get("/")
|
2016-06-05 12:42:34 -07:00
|
|
|
|
assert rv.last_modified == last_modified
|
2016-06-02 13:00:42 -07:00
|
|
|
|
|
2017-05-23 15:18:39 -07:00
|
|
|
|
def test_send_file_object_without_mimetype(self, app, req_ctx):
|
|
|
|
|
|
with pytest.raises(ValueError) as excinfo:
|
2019-11-18 23:34:45 -08:00
|
|
|
|
flask.send_file(io.BytesIO(b"LOL"))
|
2019-07-01 10:54:31 -07:00
|
|
|
|
assert "Unable to infer MIME-type" in str(excinfo.value)
|
|
|
|
|
|
assert "no filename is available" in str(excinfo.value)
|
2016-09-05 03:28:05 +04:00
|
|
|
|
|
2019-11-18 23:34:45 -08:00
|
|
|
|
flask.send_file(io.BytesIO(b"LOL"), attachment_filename="filename")
|
2016-06-03 13:56:42 +02:00
|
|
|
|
|
2019-11-18 23:34:45 -08:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"opener",
|
|
|
|
|
|
[
|
|
|
|
|
|
lambda app: open(os.path.join(app.static_folder, "index.html"), "rb"),
|
|
|
|
|
|
lambda app: io.BytesIO(b"Test"),
|
2020-04-03 18:33:40 -07:00
|
|
|
|
lambda app: PyBytesIO(b"Test"),
|
2019-11-18 23:34:45 -08:00
|
|
|
|
],
|
|
|
|
|
|
)
|
|
|
|
|
|
@pytest.mark.usefixtures("req_ctx")
|
|
|
|
|
|
def test_send_file_object(self, app, opener):
|
|
|
|
|
|
file = opener(app)
|
2011-08-26 11:21:26 +01:00
|
|
|
|
app.use_x_sendfile = True
|
2019-11-18 23:34:45 -08:00
|
|
|
|
rv = flask.send_file(file, mimetype="text/plain")
|
2017-05-23 15:18:39 -07:00
|
|
|
|
rv.direct_passthrough = False
|
2019-11-18 23:34:45 -08:00
|
|
|
|
assert rv.data
|
2019-05-06 15:39:41 -04:00
|
|
|
|
assert rv.mimetype == "text/plain"
|
2019-11-18 23:34:45 -08:00
|
|
|
|
assert "x-sendfile" not in rv.headers
|
2017-05-23 15:18:39 -07:00
|
|
|
|
rv.close()
|
|
|
|
|
|
|
2019-11-18 23:34:45 -08:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"opener",
|
|
|
|
|
|
[
|
2020-04-04 09:43:06 -07:00
|
|
|
|
lambda app: io.StringIO("Test"),
|
2020-04-03 18:33:40 -07:00
|
|
|
|
lambda app: open(os.path.join(app.static_folder, "index.html")),
|
2019-11-18 23:34:45 -08:00
|
|
|
|
],
|
|
|
|
|
|
)
|
|
|
|
|
|
@pytest.mark.usefixtures("req_ctx")
|
|
|
|
|
|
def test_send_file_text_fails(self, app, opener):
|
|
|
|
|
|
file = opener(app)
|
2016-06-03 13:56:42 +02:00
|
|
|
|
|
2019-11-18 23:34:45 -08:00
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
|
flask.send_file(file, mimetype="text/plain")
|
2011-08-26 11:21:26 +01:00
|
|
|
|
|
2019-11-18 23:34:45 -08:00
|
|
|
|
file.close()
|
2016-06-03 13:56:42 +02:00
|
|
|
|
|
2019-01-03 17:17:45 -08:00
|
|
|
|
def test_send_file_pathlike(self, app, req_ctx):
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = flask.send_file(FakePath("static/index.html"))
|
2019-01-03 17:17:45 -08:00
|
|
|
|
assert rv.direct_passthrough
|
2019-05-06 15:39:41 -04:00
|
|
|
|
assert rv.mimetype == "text/html"
|
|
|
|
|
|
with app.open_resource("static/index.html") as f:
|
2019-01-03 17:17:45 -08:00
|
|
|
|
rv.direct_passthrough = False
|
|
|
|
|
|
assert rv.data == f.read()
|
|
|
|
|
|
rv.close()
|
|
|
|
|
|
|
2016-09-26 12:43:46 +02:00
|
|
|
|
@pytest.mark.skipif(
|
2019-05-06 15:39:41 -04:00
|
|
|
|
not callable(getattr(Range, "to_content_range_header", None)),
|
|
|
|
|
|
reason="not implemented within werkzeug",
|
2016-09-26 12:43:46 +02:00
|
|
|
|
)
|
2017-05-23 15:18:39 -07:00
|
|
|
|
def test_send_file_range_request(self, app, client):
|
2019-05-06 15:39:41 -04:00
|
|
|
|
@app.route("/")
|
2016-09-26 12:43:46 +02:00
|
|
|
|
def index():
|
2019-05-06 15:39:41 -04:00
|
|
|
|
return flask.send_file("static/index.html", conditional=True)
|
2016-09-26 12:43:46 +02:00
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = client.get("/", headers={"Range": "bytes=4-15"})
|
2016-09-26 12:43:46 +02:00
|
|
|
|
assert rv.status_code == 206
|
2019-05-06 15:39:41 -04:00
|
|
|
|
with app.open_resource("static/index.html") as f:
|
2016-09-26 12:43:46 +02:00
|
|
|
|
assert rv.data == f.read()[4:16]
|
|
|
|
|
|
rv.close()
|
|
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = client.get("/", headers={"Range": "bytes=4-"})
|
2016-09-26 12:43:46 +02:00
|
|
|
|
assert rv.status_code == 206
|
2019-05-06 15:39:41 -04:00
|
|
|
|
with app.open_resource("static/index.html") as f:
|
2016-09-26 12:43:46 +02:00
|
|
|
|
assert rv.data == f.read()[4:]
|
|
|
|
|
|
rv.close()
|
|
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = client.get("/", headers={"Range": "bytes=4-1000"})
|
2016-09-26 12:43:46 +02:00
|
|
|
|
assert rv.status_code == 206
|
2019-05-06 15:39:41 -04:00
|
|
|
|
with app.open_resource("static/index.html") as f:
|
2016-09-26 12:43:46 +02:00
|
|
|
|
assert rv.data == f.read()[4:]
|
|
|
|
|
|
rv.close()
|
|
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = client.get("/", headers={"Range": "bytes=-10"})
|
2016-09-26 12:43:46 +02:00
|
|
|
|
assert rv.status_code == 206
|
2019-05-06 15:39:41 -04:00
|
|
|
|
with app.open_resource("static/index.html") as f:
|
2016-09-26 12:43:46 +02:00
|
|
|
|
assert rv.data == f.read()[-10:]
|
|
|
|
|
|
rv.close()
|
|
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = client.get("/", headers={"Range": "bytes=1000-"})
|
2016-09-26 12:43:46 +02:00
|
|
|
|
assert rv.status_code == 416
|
|
|
|
|
|
rv.close()
|
|
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = client.get("/", headers={"Range": "bytes=-"})
|
2016-09-26 12:43:46 +02:00
|
|
|
|
assert rv.status_code == 416
|
|
|
|
|
|
rv.close()
|
|
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = client.get("/", headers={"Range": "somethingsomething"})
|
2016-09-26 12:43:46 +02:00
|
|
|
|
assert rv.status_code == 416
|
|
|
|
|
|
rv.close()
|
|
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
last_modified = datetime.datetime.utcfromtimestamp(
|
|
|
|
|
|
os.path.getmtime(os.path.join(app.root_path, "static/index.html"))
|
|
|
|
|
|
).replace(microsecond=0)
|
2016-09-26 12:43:46 +02:00
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = client.get(
|
|
|
|
|
|
"/", headers={"Range": "bytes=4-15", "If-Range": http_date(last_modified)}
|
|
|
|
|
|
)
|
2016-09-26 12:43:46 +02:00
|
|
|
|
assert rv.status_code == 206
|
|
|
|
|
|
rv.close()
|
|
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = client.get(
|
|
|
|
|
|
"/",
|
|
|
|
|
|
headers={
|
|
|
|
|
|
"Range": "bytes=4-15",
|
|
|
|
|
|
"If-Range": http_date(datetime.datetime(1999, 1, 1)),
|
|
|
|
|
|
},
|
|
|
|
|
|
)
|
2016-09-26 12:43:46 +02:00
|
|
|
|
assert rv.status_code == 200
|
|
|
|
|
|
rv.close()
|
|
|
|
|
|
|
2018-10-18 23:30:03 +01:00
|
|
|
|
def test_send_file_range_request_bytesio(self, app, client):
|
2019-05-06 15:39:41 -04:00
|
|
|
|
@app.route("/")
|
2018-10-18 23:30:03 +01:00
|
|
|
|
def index():
|
2019-05-06 15:39:41 -04:00
|
|
|
|
file = io.BytesIO(b"somethingsomething")
|
2018-10-18 23:30:03 +01:00
|
|
|
|
return flask.send_file(
|
2019-05-06 15:39:41 -04:00
|
|
|
|
file, attachment_filename="filename", conditional=True
|
2018-10-18 23:30:03 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = client.get("/", headers={"Range": "bytes=4-15"})
|
2018-10-18 23:30:03 +01:00
|
|
|
|
assert rv.status_code == 206
|
2019-05-06 15:39:41 -04:00
|
|
|
|
assert rv.data == b"somethingsomething"[4:16]
|
2018-10-18 23:30:03 +01:00
|
|
|
|
rv.close()
|
|
|
|
|
|
|
2017-11-25 00:44:07 +01:00
|
|
|
|
def test_send_file_range_request_xsendfile_invalid(self, app, client):
|
2017-11-23 10:32:13 +01:00
|
|
|
|
# https://github.com/pallets/flask/issues/2526
|
|
|
|
|
|
app.use_x_sendfile = True
|
|
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
@app.route("/")
|
2017-11-25 00:53:43 +01:00
|
|
|
|
def index():
|
2019-05-06 15:39:41 -04:00
|
|
|
|
return flask.send_file("static/index.html", conditional=True)
|
2017-11-25 00:53:43 +01:00
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = client.get("/", headers={"Range": "bytes=1000-"})
|
2017-11-23 10:32:13 +01:00
|
|
|
|
assert rv.status_code == 416
|
|
|
|
|
|
rv.close()
|
|
|
|
|
|
|
2017-05-23 15:18:39 -07:00
|
|
|
|
def test_attachment(self, app, req_ctx):
|
2011-08-26 11:21:26 +01:00
|
|
|
|
app = flask.Flask(__name__)
|
2016-06-03 13:56:42 +02:00
|
|
|
|
with app.test_request_context():
|
2019-11-18 23:34:45 -08:00
|
|
|
|
with open(os.path.join(app.root_path, "static/index.html"), "rb") as f:
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = flask.send_file(
|
|
|
|
|
|
f, as_attachment=True, attachment_filename="index.html"
|
|
|
|
|
|
)
|
|
|
|
|
|
value, options = parse_options_header(rv.headers["Content-Disposition"])
|
|
|
|
|
|
assert value == "attachment"
|
2016-06-03 14:19:25 +02:00
|
|
|
|
rv.close()
|
2016-06-03 13:56:42 +02:00
|
|
|
|
|
2019-11-18 23:34:45 -08:00
|
|
|
|
with open(os.path.join(app.root_path, "static/index.html"), "rb") as f:
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = flask.send_file(
|
|
|
|
|
|
f, as_attachment=True, attachment_filename="index.html"
|
|
|
|
|
|
)
|
|
|
|
|
|
value, options = parse_options_header(rv.headers["Content-Disposition"])
|
|
|
|
|
|
assert value == "attachment"
|
|
|
|
|
|
assert options["filename"] == "index.html"
|
|
|
|
|
|
assert "filename*" not in rv.headers["Content-Disposition"]
|
2013-05-30 14:31:36 +01:00
|
|
|
|
rv.close()
|
2011-08-26 11:21:26 +01:00
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = flask.send_file("static/index.html", as_attachment=True)
|
|
|
|
|
|
value, options = parse_options_header(rv.headers["Content-Disposition"])
|
|
|
|
|
|
assert value == "attachment"
|
|
|
|
|
|
assert options["filename"] == "index.html"
|
2017-05-23 15:18:39 -07:00
|
|
|
|
rv.close()
|
2011-08-26 11:21:26 +01:00
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = flask.send_file(
|
2019-11-18 23:34:45 -08:00
|
|
|
|
io.BytesIO(b"Test"),
|
2019-05-06 15:39:41 -04:00
|
|
|
|
as_attachment=True,
|
|
|
|
|
|
attachment_filename="index.txt",
|
|
|
|
|
|
add_etags=False,
|
|
|
|
|
|
)
|
|
|
|
|
|
assert rv.mimetype == "text/plain"
|
|
|
|
|
|
value, options = parse_options_header(rv.headers["Content-Disposition"])
|
|
|
|
|
|
assert value == "attachment"
|
|
|
|
|
|
assert options["filename"] == "index.txt"
|
2017-05-23 15:18:39 -07:00
|
|
|
|
rv.close()
|
2017-04-07 18:02:31 -07:00
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
@pytest.mark.usefixtures("req_ctx")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
("filename", "ascii", "utf8"),
|
|
|
|
|
|
(
|
|
|
|
|
|
("index.html", "index.html", False),
|
|
|
|
|
|
(
|
2020-04-04 09:43:06 -07:00
|
|
|
|
"Ñandú/pingüino.txt",
|
2019-05-06 15:39:41 -04:00
|
|
|
|
'"Nandu/pinguino.txt"',
|
|
|
|
|
|
"%C3%91and%C3%BA%EF%BC%8Fping%C3%BCino.txt",
|
|
|
|
|
|
),
|
2020-04-04 09:43:06 -07:00
|
|
|
|
("Vögel.txt", "Vogel.txt", "V%C3%B6gel.txt"),
|
2019-07-01 09:52:06 -07:00
|
|
|
|
# ":/" are not safe in filename* value
|
2020-04-04 09:43:06 -07:00
|
|
|
|
("те:/ст", '":/"', "%D1%82%D0%B5%3A%2F%D1%81%D1%82"),
|
2019-05-06 15:39:41 -04:00
|
|
|
|
),
|
|
|
|
|
|
)
|
2018-05-28 06:26:27 -07:00
|
|
|
|
def test_attachment_filename_encoding(self, filename, ascii, utf8):
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = flask.send_file(
|
|
|
|
|
|
"static/index.html", as_attachment=True, attachment_filename=filename
|
|
|
|
|
|
)
|
2017-05-23 15:18:39 -07:00
|
|
|
|
rv.close()
|
2019-05-06 15:39:41 -04:00
|
|
|
|
content_disposition = rv.headers["Content-Disposition"]
|
2020-04-04 11:39:03 -07:00
|
|
|
|
assert f"filename={ascii}" in content_disposition
|
2018-05-28 06:26:27 -07:00
|
|
|
|
if utf8:
|
2020-04-04 11:39:03 -07:00
|
|
|
|
assert f"filename*=UTF-8''{utf8}" in content_disposition
|
2018-05-28 06:26:27 -07:00
|
|
|
|
else:
|
|
|
|
|
|
assert "filename*=UTF-8''" not in content_disposition
|
2017-04-07 18:02:31 -07:00
|
|
|
|
|
2017-05-23 15:18:39 -07:00
|
|
|
|
def test_static_file(self, app, req_ctx):
|
2012-03-13 16:34:16 -07:00
|
|
|
|
# default cache timeout is 12 hours
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
|
|
|
|
|
# Test with static file handler.
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = app.send_static_file("index.html")
|
|
|
|
|
|
cc = parse_cache_control_header(rv.headers["Cache-Control"])
|
2017-05-23 15:18:39 -07:00
|
|
|
|
assert cc.max_age == 12 * 60 * 60
|
|
|
|
|
|
rv.close()
|
|
|
|
|
|
# Test again with direct use of send_file utility.
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = flask.send_file("static/index.html")
|
|
|
|
|
|
cc = parse_cache_control_header(rv.headers["Cache-Control"])
|
2017-05-23 15:18:39 -07:00
|
|
|
|
assert cc.max_age == 12 * 60 * 60
|
|
|
|
|
|
rv.close()
|
2019-05-06 15:39:41 -04:00
|
|
|
|
app.config["SEND_FILE_MAX_AGE_DEFAULT"] = 3600
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
|
|
|
|
|
# Test with static file handler.
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = app.send_static_file("index.html")
|
|
|
|
|
|
cc = parse_cache_control_header(rv.headers["Cache-Control"])
|
2017-05-23 15:18:39 -07:00
|
|
|
|
assert cc.max_age == 3600
|
|
|
|
|
|
rv.close()
|
|
|
|
|
|
# Test again with direct use of send_file utility.
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = flask.send_file("static/index.html")
|
|
|
|
|
|
cc = parse_cache_control_header(rv.headers["Cache-Control"])
|
2017-05-23 15:18:39 -07:00
|
|
|
|
assert cc.max_age == 3600
|
|
|
|
|
|
rv.close()
|
|
|
|
|
|
|
2019-01-03 17:17:45 -08:00
|
|
|
|
# Test with static file handler.
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = app.send_static_file(FakePath("index.html"))
|
|
|
|
|
|
cc = parse_cache_control_header(rv.headers["Cache-Control"])
|
2019-01-03 17:17:45 -08:00
|
|
|
|
assert cc.max_age == 3600
|
|
|
|
|
|
rv.close()
|
|
|
|
|
|
|
2012-03-12 11:19:17 -04:00
|
|
|
|
class StaticFileApp(flask.Flask):
|
2012-04-24 01:48:05 -04:00
|
|
|
|
def get_send_file_max_age(self, filename):
|
|
|
|
|
|
return 10
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
2012-03-13 14:37:48 -07:00
|
|
|
|
app = StaticFileApp(__name__)
|
2012-03-12 11:19:17 -04:00
|
|
|
|
with app.test_request_context():
|
2012-04-24 01:48:05 -04:00
|
|
|
|
# Test with static file handler.
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = app.send_static_file("index.html")
|
|
|
|
|
|
cc = parse_cache_control_header(rv.headers["Cache-Control"])
|
2014-09-02 05:26:52 +02:00
|
|
|
|
assert cc.max_age == 10
|
2013-05-30 14:31:36 +01:00
|
|
|
|
rv.close()
|
2012-04-24 01:48:05 -04:00
|
|
|
|
# Test again with direct use of send_file utility.
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = flask.send_file("static/index.html")
|
|
|
|
|
|
cc = parse_cache_control_header(rv.headers["Cache-Control"])
|
2014-09-02 05:26:52 +02:00
|
|
|
|
assert cc.max_age == 10
|
2013-05-30 14:31:36 +01:00
|
|
|
|
rv.close()
|
2012-03-12 11:19:17 -04:00
|
|
|
|
|
2017-05-23 15:18:39 -07:00
|
|
|
|
def test_send_from_directory(self, app, req_ctx):
|
2019-05-06 15:39:41 -04:00
|
|
|
|
app.root_path = os.path.join(
|
|
|
|
|
|
os.path.dirname(__file__), "test_apps", "subdomaintestmodule"
|
|
|
|
|
|
)
|
|
|
|
|
|
rv = flask.send_from_directory("static", "hello.txt")
|
2017-05-23 15:18:39 -07:00
|
|
|
|
rv.direct_passthrough = False
|
2019-05-06 15:39:41 -04:00
|
|
|
|
assert rv.data.strip() == b"Hello Subdomain"
|
2017-05-23 15:18:39 -07:00
|
|
|
|
rv.close()
|
2014-02-09 13:06:54 +00:00
|
|
|
|
|
2019-01-03 17:17:45 -08:00
|
|
|
|
def test_send_from_directory_pathlike(self, app, req_ctx):
|
2019-05-06 15:39:41 -04:00
|
|
|
|
app.root_path = os.path.join(
|
|
|
|
|
|
os.path.dirname(__file__), "test_apps", "subdomaintestmodule"
|
|
|
|
|
|
)
|
|
|
|
|
|
rv = flask.send_from_directory(FakePath("static"), FakePath("hello.txt"))
|
2019-01-03 17:17:45 -08:00
|
|
|
|
rv.direct_passthrough = False
|
2019-05-06 15:39:41 -04:00
|
|
|
|
assert rv.data.strip() == b"Hello Subdomain"
|
2019-01-03 17:17:45 -08:00
|
|
|
|
rv.close()
|
|
|
|
|
|
|
2019-10-28 21:37:25 -03:00
|
|
|
|
def test_send_from_directory_null_character(self, app, req_ctx):
|
2019-05-06 15:39:41 -04:00
|
|
|
|
app.root_path = os.path.join(
|
|
|
|
|
|
os.path.dirname(__file__), "test_apps", "subdomaintestmodule"
|
|
|
|
|
|
)
|
2011-08-26 11:21:26 +01:00
|
|
|
|
|
2019-10-28 21:37:25 -03:00
|
|
|
|
if sys.version_info >= (3, 8):
|
|
|
|
|
|
exception = NotFound
|
|
|
|
|
|
else:
|
|
|
|
|
|
exception = BadRequest
|
|
|
|
|
|
|
|
|
|
|
|
with pytest.raises(exception):
|
2019-05-06 15:39:41 -04:00
|
|
|
|
flask.send_from_directory("static", "bad\x00")
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
2011-08-26 11:21:26 +01:00
|
|
|
|
|
2020-04-04 09:43:06 -07:00
|
|
|
|
class TestUrlFor:
|
2017-05-23 15:18:39 -07:00
|
|
|
|
def test_url_for_with_anchor(self, app, req_ctx):
|
2019-05-06 15:39:41 -04:00
|
|
|
|
@app.route("/")
|
2011-10-06 10:57:03 -04:00
|
|
|
|
def index():
|
2019-05-06 15:39:41 -04:00
|
|
|
|
return "42"
|
2011-10-06 10:57:03 -04:00
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
assert flask.url_for("index", _anchor="x y") == "/#x%20y"
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
|
|
|
|
|
def test_url_for_with_scheme(self, app, req_ctx):
|
2019-05-06 15:39:41 -04:00
|
|
|
|
@app.route("/")
|
2013-01-17 15:08:45 -08:00
|
|
|
|
def index():
|
2019-05-06 15:39:41 -04:00
|
|
|
|
return "42"
|
2013-01-17 15:08:45 -08:00
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
assert (
|
|
|
|
|
|
flask.url_for("index", _external=True, _scheme="https")
|
|
|
|
|
|
== "https://localhost/"
|
|
|
|
|
|
)
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
|
|
|
|
|
def test_url_for_with_scheme_not_external(self, app, req_ctx):
|
2019-05-06 15:39:41 -04:00
|
|
|
|
@app.route("/")
|
2013-01-17 15:08:45 -08:00
|
|
|
|
def index():
|
2019-05-06 15:39:41 -04:00
|
|
|
|
return "42"
|
2013-01-17 15:08:45 -08:00
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
pytest.raises(ValueError, flask.url_for, "index", _scheme="https")
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
|
|
|
|
|
def test_url_for_with_alternating_schemes(self, app, req_ctx):
|
2019-05-06 15:39:41 -04:00
|
|
|
|
@app.route("/")
|
2016-05-22 10:45:25 +02:00
|
|
|
|
def index():
|
2019-05-06 15:39:41 -04:00
|
|
|
|
return "42"
|
2016-05-22 10:45:25 +02:00
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
assert flask.url_for("index", _external=True) == "http://localhost/"
|
|
|
|
|
|
assert (
|
|
|
|
|
|
flask.url_for("index", _external=True, _scheme="https")
|
|
|
|
|
|
== "https://localhost/"
|
|
|
|
|
|
)
|
|
|
|
|
|
assert flask.url_for("index", _external=True) == "http://localhost/"
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
|
|
|
|
|
def test_url_with_method(self, app, req_ctx):
|
2011-11-04 02:46:22 +01:00
|
|
|
|
from flask.views import MethodView
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
2011-11-04 02:46:22 +01:00
|
|
|
|
class MyView(MethodView):
|
|
|
|
|
|
def get(self, id=None):
|
|
|
|
|
|
if id is None:
|
2019-05-06 15:39:41 -04:00
|
|
|
|
return "List"
|
2020-04-04 11:39:03 -07:00
|
|
|
|
return f"Get {id:d}"
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
2011-11-04 02:46:22 +01:00
|
|
|
|
def post(self):
|
2019-05-06 15:39:41 -04:00
|
|
|
|
return "Create"
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
myview = MyView.as_view("myview")
|
|
|
|
|
|
app.add_url_rule("/myview/", methods=["GET"], view_func=myview)
|
|
|
|
|
|
app.add_url_rule("/myview/<int:id>", methods=["GET"], view_func=myview)
|
|
|
|
|
|
app.add_url_rule("/myview/create", methods=["POST"], view_func=myview)
|
2011-11-04 02:46:22 +01:00
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
assert flask.url_for("myview", _method="GET") == "/myview/"
|
|
|
|
|
|
assert flask.url_for("myview", id=42, _method="GET") == "/myview/42"
|
|
|
|
|
|
assert flask.url_for("myview", _method="POST") == "/myview/create"
|
2011-11-04 02:46:22 +01:00
|
|
|
|
|
2011-08-26 11:21:26 +01:00
|
|
|
|
|
2020-04-04 09:43:06 -07:00
|
|
|
|
class TestNoImports:
|
2012-01-09 10:25:06 -05:00
|
|
|
|
"""Test Flasks are created without import.
|
|
|
|
|
|
|
|
|
|
|
|
Avoiding ``__import__`` helps create Flask instances where there are errors
|
|
|
|
|
|
at import time. Those runtime errors will be apparent to the user soon
|
|
|
|
|
|
enough, but tools which build Flask instances meta-programmatically benefit
|
|
|
|
|
|
from a Flask which does not ``__import__``. Instead of importing to
|
|
|
|
|
|
retrieve file paths or metadata on a module or package, use the pkgutil and
|
|
|
|
|
|
imp modules in the Python standard library.
|
|
|
|
|
|
"""
|
2012-01-07 17:50:11 -05:00
|
|
|
|
|
2014-09-04 15:22:57 +02:00
|
|
|
|
def test_name_with_import_error(self, modules_tmpdir):
|
2019-05-06 15:39:41 -04:00
|
|
|
|
modules_tmpdir.join("importerror.py").write("raise NotImplementedError()")
|
2012-01-07 17:50:11 -05:00
|
|
|
|
try:
|
2019-05-06 15:39:41 -04:00
|
|
|
|
flask.Flask("importerror")
|
2012-01-07 17:50:11 -05:00
|
|
|
|
except NotImplementedError:
|
2019-05-31 14:53:26 -04:00
|
|
|
|
AssertionError("Flask(import_name) is importing import_name.")
|
2012-01-07 17:50:11 -05:00
|
|
|
|
|
|
|
|
|
|
|
2020-04-04 09:43:06 -07:00
|
|
|
|
class TestStreaming:
|
2017-05-23 15:18:39 -07:00
|
|
|
|
def test_streaming_with_context(self, app, client):
|
2019-05-06 15:39:41 -04:00
|
|
|
|
@app.route("/")
|
2012-06-27 15:06:39 +01:00
|
|
|
|
def index():
|
|
|
|
|
|
def generate():
|
2019-05-06 15:39:41 -04:00
|
|
|
|
yield "Hello "
|
|
|
|
|
|
yield flask.request.args["name"]
|
|
|
|
|
|
yield "!"
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
2012-06-27 15:06:39 +01:00
|
|
|
|
return flask.Response(flask.stream_with_context(generate()))
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = client.get("/?name=World")
|
|
|
|
|
|
assert rv.data == b"Hello World!"
|
2012-06-27 15:06:39 +01:00
|
|
|
|
|
2017-05-23 15:18:39 -07:00
|
|
|
|
def test_streaming_with_context_as_decorator(self, app, client):
|
2019-05-06 15:39:41 -04:00
|
|
|
|
@app.route("/")
|
2012-06-27 15:06:39 +01:00
|
|
|
|
def index():
|
|
|
|
|
|
@flask.stream_with_context
|
2016-04-02 07:17:45 +08:00
|
|
|
|
def generate(hello):
|
|
|
|
|
|
yield hello
|
2019-05-06 15:39:41 -04:00
|
|
|
|
yield flask.request.args["name"]
|
|
|
|
|
|
yield "!"
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
return flask.Response(generate("Hello "))
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = client.get("/?name=World")
|
|
|
|
|
|
assert rv.data == b"Hello World!"
|
2012-06-27 15:06:39 +01:00
|
|
|
|
|
2017-05-23 15:18:39 -07:00
|
|
|
|
def test_streaming_with_context_and_custom_close(self, app, client):
|
2012-06-27 15:06:39 +01:00
|
|
|
|
called = []
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
2020-04-04 09:43:06 -07:00
|
|
|
|
class Wrapper:
|
2012-06-27 15:06:39 +01:00
|
|
|
|
def __init__(self, gen):
|
|
|
|
|
|
self._gen = gen
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
2012-06-27 15:06:39 +01:00
|
|
|
|
def __iter__(self):
|
|
|
|
|
|
return self
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
2012-06-27 15:06:39 +01:00
|
|
|
|
def close(self):
|
|
|
|
|
|
called.append(42)
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
2013-05-25 19:46:26 +02:00
|
|
|
|
def __next__(self):
|
2013-05-22 21:40:30 +02:00
|
|
|
|
return next(self._gen)
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
2013-06-02 17:23:53 +01:00
|
|
|
|
next = __next__
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
@app.route("/")
|
2012-06-27 15:06:39 +01:00
|
|
|
|
def index():
|
|
|
|
|
|
def generate():
|
2019-05-06 15:39:41 -04:00
|
|
|
|
yield "Hello "
|
|
|
|
|
|
yield flask.request.args["name"]
|
|
|
|
|
|
yield "!"
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
return flask.Response(flask.stream_with_context(Wrapper(generate())))
|
2017-05-23 15:18:39 -07:00
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = client.get("/?name=World")
|
|
|
|
|
|
assert rv.data == b"Hello World!"
|
2014-09-02 05:26:52 +02:00
|
|
|
|
assert called == [42]
|
2016-06-04 11:26:44 +02:00
|
|
|
|
|
2017-06-02 11:07:53 -07:00
|
|
|
|
def test_stream_keeps_session(self, app, client):
|
2019-05-06 15:39:41 -04:00
|
|
|
|
@app.route("/")
|
2017-06-02 11:07:53 -07:00
|
|
|
|
def index():
|
2019-05-06 15:39:41 -04:00
|
|
|
|
flask.session["test"] = "flask"
|
2017-06-02 11:07:53 -07:00
|
|
|
|
|
|
|
|
|
|
@flask.stream_with_context
|
|
|
|
|
|
def gen():
|
2019-05-06 15:39:41 -04:00
|
|
|
|
yield flask.session["test"]
|
2017-06-02 11:07:53 -07:00
|
|
|
|
|
|
|
|
|
|
return flask.Response(gen())
|
|
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = client.get("/")
|
|
|
|
|
|
assert rv.data == b"flask"
|
2017-06-02 11:07:53 -07:00
|
|
|
|
|
2016-06-04 11:26:44 +02:00
|
|
|
|
|
2020-04-04 09:43:06 -07:00
|
|
|
|
class TestSafeJoin:
|
2020-10-11 22:16:17 -04:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"args, expected",
|
|
|
|
|
|
(
|
2019-05-06 15:39:41 -04:00
|
|
|
|
(("a/b/c",), "a/b/c"),
|
|
|
|
|
|
(("/", "a/", "b/", "c/"), "/a/b/c"),
|
|
|
|
|
|
(("a", "b", "c"), "a/b/c"),
|
|
|
|
|
|
(("/a", "b/c"), "/a/b/c"),
|
|
|
|
|
|
(("a/b", "X/../c"), "a/b/c"),
|
|
|
|
|
|
(("/a/b", "c/X/.."), "/a/b/c"),
|
2016-06-04 11:26:44 +02:00
|
|
|
|
# If last path is '' add a slash
|
2019-05-06 15:39:41 -04:00
|
|
|
|
(("/a/b/c", ""), "/a/b/c/"),
|
2016-06-04 11:26:44 +02:00
|
|
|
|
# Preserve dot slash
|
2019-05-06 15:39:41 -04:00
|
|
|
|
(("/a/b/c", "./"), "/a/b/c/."),
|
|
|
|
|
|
(("a/b/c", "X/.."), "a/b/c/."),
|
2016-06-04 11:26:44 +02:00
|
|
|
|
# Base directory is always considered safe
|
2019-05-06 15:39:41 -04:00
|
|
|
|
(("../", "a/b/c"), "../a/b/c"),
|
|
|
|
|
|
(("/..",), "/.."),
|
2020-10-11 22:16:17 -04:00
|
|
|
|
),
|
|
|
|
|
|
)
|
|
|
|
|
|
def test_safe_join(self, args, expected):
|
|
|
|
|
|
assert flask.safe_join(*args) == expected
|
2016-06-04 11:26:44 +02:00
|
|
|
|
|
2020-10-11 22:16:17 -04:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"args",
|
|
|
|
|
|
(
|
2016-06-04 11:26:44 +02:00
|
|
|
|
# path.isabs and ``..'' checks
|
2019-05-06 15:39:41 -04:00
|
|
|
|
("/a", "b", "/c"),
|
|
|
|
|
|
("/a", "../b/c"),
|
|
|
|
|
|
("/a", "..", "b/c"),
|
2016-06-04 11:26:44 +02:00
|
|
|
|
# Boundaries violations after path normalization
|
2019-05-06 15:39:41 -04:00
|
|
|
|
("/a", "b/../b/../../c"),
|
|
|
|
|
|
("/a", "b", "c/../.."),
|
|
|
|
|
|
("/a", "b/../../c"),
|
2020-10-11 22:16:17 -04:00
|
|
|
|
),
|
|
|
|
|
|
)
|
|
|
|
|
|
def test_safe_join_exceptions(self, args):
|
|
|
|
|
|
with pytest.raises(NotFound):
|
|
|
|
|
|
print(flask.safe_join(*args))
|
2017-05-22 16:15:48 -07:00
|
|
|
|
|
|
|
|
|
|
|
2020-04-04 09:43:06 -07:00
|
|
|
|
class TestHelpers:
|
2019-05-06 15:39:41 -04:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"debug, expected_flag, expected_default_flag",
|
|
|
|
|
|
[
|
|
|
|
|
|
("", False, False),
|
|
|
|
|
|
("0", False, False),
|
|
|
|
|
|
("False", False, False),
|
|
|
|
|
|
("No", False, False),
|
|
|
|
|
|
("True", True, True),
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
|
|
|
|
|
def test_get_debug_flag(
|
|
|
|
|
|
self, monkeypatch, debug, expected_flag, expected_default_flag
|
|
|
|
|
|
):
|
|
|
|
|
|
monkeypatch.setenv("FLASK_DEBUG", debug)
|
2017-05-23 07:51:57 -07:00
|
|
|
|
if expected_flag is None:
|
|
|
|
|
|
assert get_debug_flag() is None
|
|
|
|
|
|
else:
|
|
|
|
|
|
assert get_debug_flag() == expected_flag
|
2018-01-06 17:07:56 +01:00
|
|
|
|
assert get_debug_flag() == expected_default_flag
|
|
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"env, ref_env, debug",
|
|
|
|
|
|
[
|
|
|
|
|
|
("", "production", False),
|
|
|
|
|
|
("production", "production", False),
|
|
|
|
|
|
("development", "development", True),
|
|
|
|
|
|
("other", "other", False),
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
2018-01-06 17:07:56 +01:00
|
|
|
|
def test_get_env(self, monkeypatch, env, ref_env, debug):
|
2019-05-06 15:39:41 -04:00
|
|
|
|
monkeypatch.setenv("FLASK_ENV", env)
|
2018-01-06 17:07:56 +01:00
|
|
|
|
assert get_debug_flag() == debug
|
|
|
|
|
|
assert get_env() == ref_env
|
2017-05-22 16:15:48 -07:00
|
|
|
|
|
2017-05-22 20:49:37 -07:00
|
|
|
|
def test_make_response(self):
|
2017-05-22 16:15:48 -07:00
|
|
|
|
app = flask.Flask(__name__)
|
2017-05-22 20:49:37 -07:00
|
|
|
|
with app.test_request_context():
|
|
|
|
|
|
rv = flask.helpers.make_response()
|
|
|
|
|
|
assert rv.status_code == 200
|
2019-05-06 15:39:41 -04:00
|
|
|
|
assert rv.mimetype == "text/html"
|
2017-05-22 16:15:48 -07:00
|
|
|
|
|
2019-05-06 15:39:41 -04:00
|
|
|
|
rv = flask.helpers.make_response("Hello")
|
2017-05-22 20:49:37 -07:00
|
|
|
|
assert rv.status_code == 200
|
2019-05-06 15:39:41 -04:00
|
|
|
|
assert rv.data == b"Hello"
|
|
|
|
|
|
assert rv.mimetype == "text/html"
|
2019-05-06 10:56:35 -04:00
|
|
|
|
|
2019-05-17 13:20:31 -07:00
|
|
|
|
@pytest.mark.parametrize("mode", ("r", "rb", "rt"))
|
2019-05-06 10:56:35 -04:00
|
|
|
|
def test_open_resource(self, mode):
|
|
|
|
|
|
app = flask.Flask(__name__)
|
2019-05-17 13:20:31 -07:00
|
|
|
|
|
|
|
|
|
|
with app.open_resource("static/index.html", mode) as f:
|
|
|
|
|
|
assert "<h1>Hello World!</h1>" in str(f.read())
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ("w", "x", "a", "r+"))
|
2019-05-06 10:56:35 -04:00
|
|
|
|
def test_open_resource_exceptions(self, mode):
|
|
|
|
|
|
app = flask.Flask(__name__)
|
|
|
|
|
|
|
2019-05-17 13:20:31 -07:00
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
|
app.open_resource("static/index.html", mode)
|