2014-09-02 11:26:52 +08:00
|
|
|
import pytest
|
|
|
|
|
2012-04-09 22:04:35 +08:00
|
|
|
import flask
|
2022-07-05 21:33:03 +08:00
|
|
|
from flask.globals import app_ctx
|
|
|
|
from flask.globals import request_ctx
|
2012-04-09 22:04:35 +08:00
|
|
|
|
|
|
|
|
2017-05-24 06:18:39 +08:00
|
|
|
def test_basic_url_generation(app):
|
2019-05-07 03:39:41 +08:00
|
|
|
app.config["SERVER_NAME"] = "localhost"
|
|
|
|
app.config["PREFERRED_URL_SCHEME"] = "https"
|
2014-09-04 02:50:54 +08:00
|
|
|
|
2019-05-07 03:39:41 +08:00
|
|
|
@app.route("/")
|
2014-09-04 02:50:54 +08:00
|
|
|
def index():
|
|
|
|
pass
|
|
|
|
|
|
|
|
with app.app_context():
|
2019-05-07 03:39:41 +08:00
|
|
|
rv = flask.url_for("index")
|
|
|
|
assert rv == "https://localhost/"
|
2014-09-04 02:50:54 +08:00
|
|
|
|
2017-05-24 06:18:39 +08:00
|
|
|
|
|
|
|
def test_url_generation_requires_server_name(app):
|
2014-09-04 02:50:54 +08:00
|
|
|
with app.app_context():
|
2014-09-02 11:26:52 +08:00
|
|
|
with pytest.raises(RuntimeError):
|
2019-05-07 03:39:41 +08:00
|
|
|
flask.url_for("index")
|
2012-04-09 22:16:09 +08:00
|
|
|
|
2017-05-24 06:18:39 +08:00
|
|
|
|
2014-09-04 02:50:54 +08:00
|
|
|
def test_url_generation_without_context_fails():
|
|
|
|
with pytest.raises(RuntimeError):
|
2019-05-07 03:39:41 +08:00
|
|
|
flask.url_for("index")
|
2014-09-04 02:50:54 +08:00
|
|
|
|
2017-05-24 06:18:39 +08:00
|
|
|
|
|
|
|
def test_request_context_means_app_context(app):
|
2014-09-04 02:50:54 +08:00
|
|
|
with app.test_request_context():
|
2022-07-05 21:33:03 +08:00
|
|
|
assert flask.current_app._get_current_object() is app
|
|
|
|
assert not flask.current_app
|
2014-09-04 02:50:54 +08:00
|
|
|
|
2017-05-24 06:18:39 +08:00
|
|
|
|
|
|
|
def test_app_context_provides_current_app(app):
|
2014-09-04 02:50:54 +08:00
|
|
|
with app.app_context():
|
2022-07-05 21:33:03 +08:00
|
|
|
assert flask.current_app._get_current_object() is app
|
|
|
|
assert not flask.current_app
|
2014-09-04 02:50:54 +08:00
|
|
|
|
2017-05-24 06:18:39 +08:00
|
|
|
|
|
|
|
def test_app_tearing_down(app):
|
2014-09-04 02:50:54 +08:00
|
|
|
cleanup_stuff = []
|
2017-05-24 06:18:39 +08:00
|
|
|
|
2014-09-04 02:50:54 +08:00
|
|
|
@app.teardown_appcontext
|
|
|
|
def cleanup(exception):
|
|
|
|
cleanup_stuff.append(exception)
|
|
|
|
|
|
|
|
with app.app_context():
|
|
|
|
pass
|
|
|
|
|
|
|
|
assert cleanup_stuff == [None]
|
|
|
|
|
2017-05-24 06:18:39 +08:00
|
|
|
|
|
|
|
def test_app_tearing_down_with_previous_exception(app):
|
2014-09-04 02:50:54 +08:00
|
|
|
cleanup_stuff = []
|
2017-05-24 06:18:39 +08:00
|
|
|
|
2014-09-04 02:50:54 +08:00
|
|
|
@app.teardown_appcontext
|
|
|
|
def cleanup(exception):
|
|
|
|
cleanup_stuff.append(exception)
|
|
|
|
|
|
|
|
try:
|
2019-05-07 03:39:41 +08:00
|
|
|
raise Exception("dummy")
|
2014-09-04 02:50:54 +08:00
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
with app.app_context():
|
|
|
|
pass
|
|
|
|
|
|
|
|
assert cleanup_stuff == [None]
|
2015-03-23 23:17:19 +08:00
|
|
|
|
2017-05-24 06:18:39 +08:00
|
|
|
|
2017-07-08 08:34:44 +08:00
|
|
|
def test_app_tearing_down_with_handled_exception_by_except_block(app):
|
2015-03-23 23:17:19 +08:00
|
|
|
cleanup_stuff = []
|
2017-05-24 06:18:39 +08:00
|
|
|
|
2015-03-23 23:17:19 +08:00
|
|
|
@app.teardown_appcontext
|
|
|
|
def cleanup(exception):
|
|
|
|
cleanup_stuff.append(exception)
|
|
|
|
|
|
|
|
with app.app_context():
|
|
|
|
try:
|
2019-05-07 03:39:41 +08:00
|
|
|
raise Exception("dummy")
|
2015-03-23 23:17:19 +08:00
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
assert cleanup_stuff == [None]
|
2014-09-04 02:50:54 +08:00
|
|
|
|
2017-07-08 08:34:44 +08:00
|
|
|
|
2017-07-10 22:10:47 +08:00
|
|
|
def test_app_tearing_down_with_handled_exception_by_app_handler(app, client):
|
2019-05-07 03:39:41 +08:00
|
|
|
app.config["PROPAGATE_EXCEPTIONS"] = True
|
2017-07-08 08:34:44 +08:00
|
|
|
cleanup_stuff = []
|
|
|
|
|
|
|
|
@app.teardown_appcontext
|
|
|
|
def cleanup(exception):
|
|
|
|
cleanup_stuff.append(exception)
|
|
|
|
|
2019-05-07 03:39:41 +08:00
|
|
|
@app.route("/")
|
2017-07-08 08:34:44 +08:00
|
|
|
def index():
|
2019-05-07 03:39:41 +08:00
|
|
|
raise Exception("dummy")
|
2017-07-08 08:34:44 +08:00
|
|
|
|
|
|
|
@app.errorhandler(Exception)
|
|
|
|
def handler(f):
|
|
|
|
return flask.jsonify(str(f))
|
|
|
|
|
|
|
|
with app.app_context():
|
2019-05-07 03:39:41 +08:00
|
|
|
client.get("/")
|
2017-07-08 08:34:44 +08:00
|
|
|
|
|
|
|
assert cleanup_stuff == [None]
|
|
|
|
|
|
|
|
|
2017-07-10 22:10:47 +08:00
|
|
|
def test_app_tearing_down_with_unhandled_exception(app, client):
|
2019-05-07 03:39:41 +08:00
|
|
|
app.config["PROPAGATE_EXCEPTIONS"] = True
|
2017-07-08 08:34:44 +08:00
|
|
|
cleanup_stuff = []
|
|
|
|
|
|
|
|
@app.teardown_appcontext
|
|
|
|
def cleanup(exception):
|
|
|
|
cleanup_stuff.append(exception)
|
|
|
|
|
2019-05-07 03:39:41 +08:00
|
|
|
@app.route("/")
|
2017-07-08 08:34:44 +08:00
|
|
|
def index():
|
2023-01-19 02:31:08 +08:00
|
|
|
raise ValueError("dummy")
|
2017-07-08 08:34:44 +08:00
|
|
|
|
2023-01-19 02:31:08 +08:00
|
|
|
with pytest.raises(ValueError, match="dummy"):
|
2017-07-08 08:34:44 +08:00
|
|
|
with app.app_context():
|
2019-05-07 03:39:41 +08:00
|
|
|
client.get("/")
|
2017-07-08 08:34:44 +08:00
|
|
|
|
|
|
|
assert len(cleanup_stuff) == 1
|
2023-01-19 02:31:08 +08:00
|
|
|
assert isinstance(cleanup_stuff[0], ValueError)
|
2019-05-07 03:39:41 +08:00
|
|
|
assert str(cleanup_stuff[0]) == "dummy"
|
2017-07-08 08:34:44 +08:00
|
|
|
|
2017-05-24 06:18:39 +08:00
|
|
|
|
|
|
|
def test_app_ctx_globals_methods(app, app_ctx):
|
|
|
|
# get
|
2019-05-07 03:39:41 +08:00
|
|
|
assert flask.g.get("foo") is None
|
|
|
|
assert flask.g.get("foo", "bar") == "bar"
|
2017-05-24 06:18:39 +08:00
|
|
|
# __contains__
|
2019-05-07 03:39:41 +08:00
|
|
|
assert "foo" not in flask.g
|
|
|
|
flask.g.foo = "bar"
|
|
|
|
assert "foo" in flask.g
|
2017-05-24 06:18:39 +08:00
|
|
|
# setdefault
|
2019-05-07 03:39:41 +08:00
|
|
|
flask.g.setdefault("bar", "the cake is a lie")
|
|
|
|
flask.g.setdefault("bar", "hello world")
|
|
|
|
assert flask.g.bar == "the cake is a lie"
|
2017-05-24 06:18:39 +08:00
|
|
|
# pop
|
2019-05-07 03:39:41 +08:00
|
|
|
assert flask.g.pop("bar") == "the cake is a lie"
|
2017-05-24 06:18:39 +08:00
|
|
|
with pytest.raises(KeyError):
|
2019-05-07 03:39:41 +08:00
|
|
|
flask.g.pop("bar")
|
|
|
|
assert flask.g.pop("bar", "more cake") == "more cake"
|
2017-05-24 06:18:39 +08:00
|
|
|
# __iter__
|
2019-05-07 03:39:41 +08:00
|
|
|
assert list(flask.g) == ["foo"]
|
|
|
|
# __repr__
|
2018-05-29 23:51:08 +08:00
|
|
|
assert repr(flask.g) == "<flask.g of 'flask_test'>"
|
2017-05-24 06:18:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_custom_app_ctx_globals_class(app):
|
2020-04-05 00:43:06 +08:00
|
|
|
class CustomRequestGlobals:
|
2014-09-04 02:50:54 +08:00
|
|
|
def __init__(self):
|
2019-05-07 03:39:41 +08:00
|
|
|
self.spam = "eggs"
|
2017-05-24 06:18:39 +08:00
|
|
|
|
2014-09-04 02:50:54 +08:00
|
|
|
app.app_ctx_globals_class = CustomRequestGlobals
|
|
|
|
with app.app_context():
|
2019-05-07 03:39:41 +08:00
|
|
|
assert flask.render_template_string("{{ g.spam }}") == "eggs"
|
2014-09-04 02:50:54 +08:00
|
|
|
|
2017-05-24 06:18:39 +08:00
|
|
|
|
|
|
|
def test_context_refcounts(app, client):
|
2014-09-04 02:50:54 +08:00
|
|
|
called = []
|
2017-05-24 06:18:39 +08:00
|
|
|
|
2014-09-04 02:50:54 +08:00
|
|
|
@app.teardown_request
|
|
|
|
def teardown_req(error=None):
|
2019-05-07 03:39:41 +08:00
|
|
|
called.append("request")
|
2017-05-24 06:18:39 +08:00
|
|
|
|
2014-09-04 02:50:54 +08:00
|
|
|
@app.teardown_appcontext
|
|
|
|
def teardown_app(error=None):
|
2019-05-07 03:39:41 +08:00
|
|
|
called.append("app")
|
2017-05-24 06:18:39 +08:00
|
|
|
|
2019-05-07 03:39:41 +08:00
|
|
|
@app.route("/")
|
2014-09-04 02:50:54 +08:00
|
|
|
def index():
|
2022-07-05 21:33:03 +08:00
|
|
|
with app_ctx:
|
|
|
|
with request_ctx:
|
2014-09-04 02:50:54 +08:00
|
|
|
pass
|
2022-07-05 21:33:03 +08:00
|
|
|
|
|
|
|
assert flask.request.environ["werkzeug.request"] is not None
|
2020-04-05 00:43:06 +08:00
|
|
|
return ""
|
2017-05-24 06:18:39 +08:00
|
|
|
|
2019-05-07 03:39:41 +08:00
|
|
|
res = client.get("/")
|
2014-09-04 02:50:54 +08:00
|
|
|
assert res.status_code == 200
|
2019-05-07 03:39:41 +08:00
|
|
|
assert res.data == b""
|
|
|
|
assert called == ["request", "app"]
|
2016-05-27 03:46:56 +08:00
|
|
|
|
|
|
|
|
2017-05-24 06:18:39 +08:00
|
|
|
def test_clean_pop(app):
|
|
|
|
app.testing = False
|
2016-05-27 03:46:56 +08:00
|
|
|
called = []
|
|
|
|
|
|
|
|
@app.teardown_request
|
|
|
|
def teardown_req(error=None):
|
2023-11-10 01:20:27 +08:00
|
|
|
raise ZeroDivisionError
|
2016-05-27 03:46:56 +08:00
|
|
|
|
|
|
|
@app.teardown_appcontext
|
|
|
|
def teardown_app(error=None):
|
2019-05-07 03:39:41 +08:00
|
|
|
called.append("TEARDOWN")
|
2016-05-27 03:46:56 +08:00
|
|
|
|
2023-11-10 01:20:27 +08:00
|
|
|
with app.app_context():
|
|
|
|
called.append(flask.current_app.name)
|
2016-05-27 03:46:56 +08:00
|
|
|
|
2019-05-07 03:39:41 +08:00
|
|
|
assert called == ["flask_test", "TEARDOWN"]
|
2016-05-27 03:46:56 +08:00
|
|
|
assert not flask.current_app
|