flask/tests/test_helpers.py

330 lines
9.7 KiB
Python
Raw Normal View History

2018-10-19 06:30:03 +08:00
import io
import os
import pytest
import flask
from flask.helpers import get_debug_flag
from flask.helpers import get_env
2020-04-05 00:43:06 +08:00
class FakePath:
"""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/
"""
def __init__(self, path):
self.path = path
def __fspath__(self):
return self.path
2020-04-05 00:43:06 +08:00
class PyBytesIO:
2019-11-19 15: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-05 00:43:06 +08:00
class TestSendfile:
def test_send_file(self, app, req_ctx):
rv = flask.send_file("static/index.html")
assert rv.direct_passthrough
assert rv.mimetype == "text/html"
with app.open_resource("static/index.html") as f:
rv.direct_passthrough = False
assert rv.data == f.read()
rv.close()
def test_static_file(self, app, req_ctx):
# Default max_age is None.
# Test with static file handler.
rv = app.send_static_file("index.html")
assert rv.cache_control.max_age is None
rv.close()
# Test with direct use of send_file.
rv = flask.send_file("static/index.html")
assert rv.cache_control.max_age is None
rv.close()
app.config["SEND_FILE_MAX_AGE_DEFAULT"] = 3600
# Test with static file handler.
rv = app.send_static_file("index.html")
assert rv.cache_control.max_age == 3600
rv.close()
# Test with direct use of send_file.
rv = flask.send_file("static/index.html")
assert rv.cache_control.max_age == 3600
rv.close()
# Test with pathlib.Path.
rv = app.send_static_file(FakePath("index.html"))
assert rv.cache_control.max_age == 3600
rv.close()
class StaticFileApp(flask.Flask):
def get_send_file_max_age(self, filename):
return 10
app = StaticFileApp(__name__)
with app.test_request_context():
# Test with static file handler.
rv = app.send_static_file("index.html")
assert rv.cache_control.max_age == 10
rv.close()
# Test with direct use of send_file.
rv = flask.send_file("static/index.html")
assert rv.cache_control.max_age == 10
rv.close()
def test_send_from_directory(self, app, req_ctx):
app.root_path = os.path.join(
os.path.dirname(__file__), "test_apps", "subdomaintestmodule"
)
rv = flask.send_from_directory("static", "hello.txt")
rv.direct_passthrough = False
assert rv.data.strip() == b"Hello Subdomain"
rv.close()
2020-04-05 00:43:06 +08:00
class TestUrlFor:
def test_url_for_with_anchor(self, app, req_ctx):
@app.route("/")
def index():
return "42"
assert flask.url_for("index", _anchor="x y") == "/#x%20y"
def test_url_for_with_scheme(self, app, req_ctx):
@app.route("/")
def index():
return "42"
assert (
flask.url_for("index", _external=True, _scheme="https")
== "https://localhost/"
)
def test_url_for_with_scheme_not_external(self, app, req_ctx):
@app.route("/")
def index():
return "42"
pytest.raises(ValueError, flask.url_for, "index", _scheme="https")
def test_url_for_with_alternating_schemes(self, app, req_ctx):
@app.route("/")
def index():
return "42"
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/"
def test_url_with_method(self, app, req_ctx):
2011-11-04 09:46:22 +08:00
from flask.views import MethodView
2011-11-04 09:46:22 +08:00
class MyView(MethodView):
def get(self, id=None):
if id is None:
return "List"
2020-04-05 02:39:03 +08:00
return f"Get {id:d}"
2011-11-04 09:46:22 +08:00
def post(self):
return "Create"
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 09:46:22 +08: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 09:46:22 +08:00
2022-05-03 00:44:15 +08:00
def test_redirect_no_app():
response = flask.redirect("https://localhost", 307)
assert response.location == "https://localhost"
assert response.status_code == 307
def test_redirect_with_app(app):
def redirect(location, code=302):
raise ValueError
app.redirect = redirect
with app.app_context(), pytest.raises(ValueError):
flask.redirect("other")
2020-04-05 00:43:06 +08:00
class TestNoImports:
2012-01-09 23:25:06 +08: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.
"""
2014-09-04 21:22:57 +08:00
def test_name_with_import_error(self, modules_tmpdir):
modules_tmpdir.join("importerror.py").write("raise NotImplementedError()")
try:
flask.Flask("importerror")
except NotImplementedError:
2019-06-01 02:53:26 +08:00
AssertionError("Flask(import_name) is importing import_name.")
2020-04-05 00:43:06 +08:00
class TestStreaming:
def test_streaming_with_context(self, app, client):
@app.route("/")
2012-06-27 22:06:39 +08:00
def index():
def generate():
yield "Hello "
yield flask.request.args["name"]
yield "!"
2012-06-27 22:06:39 +08:00
return flask.Response(flask.stream_with_context(generate()))
rv = client.get("/?name=World")
assert rv.data == b"Hello World!"
2012-06-27 22:06:39 +08:00
def test_streaming_with_context_as_decorator(self, app, client):
@app.route("/")
2012-06-27 22:06:39 +08:00
def index():
@flask.stream_with_context
def generate(hello):
yield hello
yield flask.request.args["name"]
yield "!"
return flask.Response(generate("Hello "))
rv = client.get("/?name=World")
assert rv.data == b"Hello World!"
2012-06-27 22:06:39 +08:00
def test_streaming_with_context_and_custom_close(self, app, client):
2012-06-27 22:06:39 +08:00
called = []
2020-04-05 00:43:06 +08:00
class Wrapper:
2012-06-27 22:06:39 +08:00
def __init__(self, gen):
self._gen = gen
2012-06-27 22:06:39 +08:00
def __iter__(self):
return self
2012-06-27 22:06:39 +08:00
def close(self):
called.append(42)
2013-05-26 01:46:26 +08:00
def __next__(self):
2013-05-23 03:40:30 +08:00
return next(self._gen)
2013-06-03 00:23:53 +08:00
next = __next__
@app.route("/")
2012-06-27 22:06:39 +08:00
def index():
def generate():
yield "Hello "
yield flask.request.args["name"]
yield "!"
return flask.Response(flask.stream_with_context(Wrapper(generate())))
rv = client.get("/?name=World")
assert rv.data == b"Hello World!"
2014-09-02 11:26:52 +08:00
assert called == [42]
2016-06-04 17:26:44 +08:00
2017-06-03 02:07:53 +08:00
def test_stream_keeps_session(self, app, client):
@app.route("/")
2017-06-03 02:07:53 +08:00
def index():
flask.session["test"] = "flask"
2017-06-03 02:07:53 +08:00
@flask.stream_with_context
def gen():
yield flask.session["test"]
2017-06-03 02:07:53 +08:00
return flask.Response(gen())
rv = client.get("/")
assert rv.data == b"flask"
2017-06-03 02:07:53 +08:00
2016-06-04 17:26:44 +08:00
2020-04-05 00:43:06 +08:00
class TestHelpers:
@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 22:51:57 +08:00
if expected_flag is None:
assert get_debug_flag() is None
else:
assert get_debug_flag() == expected_flag
assert get_debug_flag() == expected_default_flag
@pytest.mark.parametrize(
"env, ref_env, debug",
[
("", "production", False),
("production", "production", False),
("development", "development", True),
("other", "other", False),
],
)
def test_get_env(self, monkeypatch, env, ref_env, debug):
monkeypatch.setenv("FLASK_ENV", env)
assert get_debug_flag() == debug
assert get_env() == ref_env
2017-05-23 11:49:37 +08:00
def test_make_response(self):
app = flask.Flask(__name__)
2017-05-23 11:49:37 +08:00
with app.test_request_context():
rv = flask.helpers.make_response()
assert rv.status_code == 200
assert rv.mimetype == "text/html"
rv = flask.helpers.make_response("Hello")
2017-05-23 11:49:37 +08:00
assert rv.status_code == 200
assert rv.data == b"Hello"
assert rv.mimetype == "text/html"
2019-05-18 04:20:31 +08:00
@pytest.mark.parametrize("mode", ("r", "rb", "rt"))
def test_open_resource(self, mode):
app = flask.Flask(__name__)
2019-05-18 04:20:31 +08: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+"))
def test_open_resource_exceptions(self, mode):
app = flask.Flask(__name__)
2019-05-18 04:20:31 +08:00
with pytest.raises(ValueError):
app.open_resource("static/index.html", mode)