| 
									
										
										
										
											2023-08-22 00:39:10 +08:00
										 |  |  | import importlib.metadata | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-20 07:34:46 +08:00
										 |  |  | import click | 
					
						
							| 
									
										
										
										
											2014-09-01 03:56:15 +08:00
										 |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2014-09-04 03:02:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-01 23:35:03 +08:00
										 |  |  | import flask | 
					
						
							| 
									
										
										
										
											2019-05-17 23:39:16 +08:00
										 |  |  | from flask import appcontext_popped | 
					
						
							| 
									
										
										
										
											2018-02-20 07:34:46 +08:00
										 |  |  | from flask.cli import ScriptInfo | 
					
						
							| 
									
										
										
										
											2022-07-09 20:41:35 +08:00
										 |  |  | from flask.globals import _cv_request | 
					
						
							| 
									
										
										
										
											2015-04-02 06:08:06 +08:00
										 |  |  | from flask.json import jsonify | 
					
						
							| 
									
										
										
										
											2019-06-01 23:35:03 +08:00
										 |  |  | from flask.testing import EnvironBuilder | 
					
						
							|  |  |  | from flask.testing import FlaskCliRunner | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_environ_defaults_from_config(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config["SERVER_NAME"] = "example.com:1234" | 
					
						
							|  |  |  |     app.config["APPLICATION_ROOT"] = "/foo" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  |     def index(): | 
					
						
							|  |  |  |         return flask.request.url | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx = app.test_request_context() | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert ctx.request.url == "http://example.com:1234/foo/" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/") | 
					
						
							|  |  |  |     assert rv.data == b"http://example.com:1234/foo/" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_environ_defaults(app, client, app_ctx, req_ctx): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  |     def index(): | 
					
						
							|  |  |  |         return flask.request.url | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx = app.test_request_context() | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert ctx.request.url == "http://localhost/" | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |     with client: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = client.get("/") | 
					
						
							|  |  |  |         assert rv.data == b"http://localhost/" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 00:39:10 +08:00
										 |  |  | def test_environ_base_default(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2016-10-12 14:54:25 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2023-08-22 00:39:10 +08:00
										 |  |  |         flask.g.remote_addr = flask.request.remote_addr | 
					
						
							|  |  |  |         flask.g.user_agent = flask.request.user_agent.string | 
					
						
							|  |  |  |         return "" | 
					
						
							| 
									
										
										
										
											2016-10-12 14:54:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 00:39:10 +08:00
										 |  |  |     with client: | 
					
						
							|  |  |  |         client.get("/") | 
					
						
							|  |  |  |         assert flask.g.remote_addr == "127.0.0.1" | 
					
						
							|  |  |  |         assert flask.g.user_agent == ( | 
					
						
							|  |  |  |             f"Werkzeug/{importlib.metadata.version('werkzeug')}" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2016-10-12 14:54:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 00:39:10 +08:00
										 |  |  | def test_environ_base_modified(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2016-10-12 14:54:25 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2023-08-22 00:39:10 +08:00
										 |  |  |         flask.g.remote_addr = flask.request.remote_addr | 
					
						
							|  |  |  |         flask.g.user_agent = flask.request.user_agent.string | 
					
						
							|  |  |  |         return "" | 
					
						
							| 
									
										
										
										
											2016-10-12 14:54:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 00:39:10 +08:00
										 |  |  |     client.environ_base["REMOTE_ADDR"] = "192.168.0.22" | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     client.environ_base["HTTP_USER_AGENT"] = "Foo" | 
					
						
							| 
									
										
										
										
											2016-10-12 14:54:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 00:39:10 +08:00
										 |  |  |     with client: | 
					
						
							|  |  |  |         client.get("/") | 
					
						
							|  |  |  |         assert flask.g.remote_addr == "192.168.0.22" | 
					
						
							|  |  |  |         assert flask.g.user_agent == "Foo" | 
					
						
							| 
									
										
										
										
											2016-10-12 14:54:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-13 23:42:53 +08:00
										 |  |  | def test_client_open_environ(app, client, request): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/index") | 
					
						
							| 
									
										
										
										
											2017-07-13 23:42:53 +08:00
										 |  |  |     def index(): | 
					
						
							|  |  |  |         return flask.request.remote_addr | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-31 22:49:23 +08:00
										 |  |  |     builder = EnvironBuilder(app, path="/index", method="GET") | 
					
						
							| 
									
										
										
										
											2017-07-13 23:42:53 +08:00
										 |  |  |     request.addfinalizer(builder.close) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     rv = client.open(builder) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert rv.data == b"127.0.0.1" | 
					
						
							| 
									
										
										
										
											2017-07-13 23:42:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     environ = builder.get_environ() | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     client.environ_base["REMOTE_ADDR"] = "127.0.0.2" | 
					
						
							| 
									
										
										
										
											2017-07-13 23:42:53 +08:00
										 |  |  |     rv = client.open(environ) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert rv.data == b"127.0.0.2" | 
					
						
							| 
									
										
										
										
											2017-07-13 23:42:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-28 22:25:15 +08:00
										 |  |  | def test_specify_url_scheme(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2017-05-28 22:25:15 +08:00
										 |  |  |     def index(): | 
					
						
							|  |  |  |         return flask.request.url | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     ctx = app.test_request_context(url_scheme="https") | 
					
						
							|  |  |  |     assert ctx.request.url == "https://localhost/" | 
					
						
							| 
									
										
										
										
											2017-05-28 22:25:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/", url_scheme="https") | 
					
						
							|  |  |  |     assert rv.data == b"https://localhost/" | 
					
						
							| 
									
										
										
										
											2017-05-28 22:25:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-30 04:03:08 +08:00
										 |  |  | def test_path_is_url(app): | 
					
						
							| 
									
										
										
										
											2019-05-31 22:49:23 +08:00
										 |  |  |     eb = EnvironBuilder(app, "https://example.com/") | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert eb.url_scheme == "https" | 
					
						
							|  |  |  |     assert eb.host == "example.com" | 
					
						
							|  |  |  |     assert eb.script_root == "" | 
					
						
							|  |  |  |     assert eb.path == "/" | 
					
						
							| 
									
										
										
										
											2017-07-30 04:03:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-31 22:49:23 +08:00
										 |  |  | def test_environbuilder_json_dumps(app): | 
					
						
							|  |  |  |     """EnvironBuilder.json_dumps() takes settings from the app.""" | 
					
						
							| 
									
										
										
										
											2022-07-13 22:41:43 +08:00
										 |  |  |     app.json.ensure_ascii = False | 
					
						
							| 
									
										
										
										
											2020-04-05 00:43:06 +08:00
										 |  |  |     eb = EnvironBuilder(app, json="\u20ac") | 
					
						
							|  |  |  |     assert eb.input_stream.read().decode("utf8") == '"\u20ac"' | 
					
						
							| 
									
										
										
										
											2019-05-31 22:49:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-20 04:20:03 +08:00
										 |  |  | def test_blueprint_with_subdomain(): | 
					
						
							|  |  |  |     app = flask.Flask(__name__, subdomain_matching=True) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config["SERVER_NAME"] = "example.com:1234" | 
					
						
							|  |  |  |     app.config["APPLICATION_ROOT"] = "/foo" | 
					
						
							| 
									
										
										
										
											2018-02-20 04:20:03 +08:00
										 |  |  |     client = app.test_client() | 
					
						
							| 
									
										
										
										
											2017-05-28 22:25:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     bp = flask.Blueprint("company", __name__, subdomain="xxx") | 
					
						
							| 
									
										
										
										
											2017-05-28 22:25:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @bp.route("/") | 
					
						
							| 
									
										
										
										
											2017-05-28 22:25:15 +08:00
										 |  |  |     def index(): | 
					
						
							|  |  |  |         return flask.request.url | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     app.register_blueprint(bp) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     ctx = app.test_request_context("/", subdomain="xxx") | 
					
						
							|  |  |  |     assert ctx.request.url == "http://xxx.example.com:1234/foo/" | 
					
						
							| 
									
										
										
										
											2017-05-28 22:25:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 03:40:01 +08:00
										 |  |  |     with ctx: | 
					
						
							|  |  |  |         assert ctx.request.blueprint == bp.name | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/", subdomain="xxx") | 
					
						
							|  |  |  |     assert rv.data == b"http://xxx.example.com:1234/foo/" | 
					
						
							| 
									
										
										
										
											2017-05-28 22:25:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_redirect_keep_session(app, client, app_ctx): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/", methods=["GET", "POST"]) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         if flask.request.method == "POST": | 
					
						
							|  |  |  |             return flask.redirect("/getsession") | 
					
						
							|  |  |  |         flask.session["data"] = "foo" | 
					
						
							|  |  |  |         return "index" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/getsession") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  |     def get_session(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return flask.session.get("data", "<missing>") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |     with client: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = client.get("/getsession") | 
					
						
							|  |  |  |         assert rv.data == b"<missing>" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = client.get("/") | 
					
						
							|  |  |  |         assert rv.data == b"index" | 
					
						
							|  |  |  |         assert flask.session.get("data") == "foo" | 
					
						
							| 
									
										
										
										
											2020-04-05 00:25:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = client.post("/", data={}, follow_redirects=True) | 
					
						
							|  |  |  |         assert rv.data == b"foo" | 
					
						
							| 
									
										
										
										
											2020-04-05 00:25:54 +08:00
										 |  |  |         assert flask.session.get("data") == "foo" | 
					
						
							| 
									
										
										
										
											2011-11-26 04:08:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = client.get("/getsession") | 
					
						
							|  |  |  |         assert rv.data == b"foo" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_session_transactions(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2020-04-04 09:33:40 +08:00
										 |  |  |         return str(flask.session["foo"]) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |     with client: | 
					
						
							|  |  |  |         with client.session_transaction() as sess: | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  |             assert len(sess) == 0 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             sess["foo"] = [42] | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  |             assert len(sess) == 1 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = client.get("/") | 
					
						
							|  |  |  |         assert rv.data == b"[42]" | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |         with client.session_transaction() as sess: | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  |             assert len(sess) == 1 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             assert sess["foo"] == [42] | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  | def test_session_transactions_no_null_sessions(): | 
					
						
							|  |  |  |     app = flask.Flask(__name__) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with app.test_client() as c: | 
					
						
							| 
									
										
										
										
											2016-03-04 19:30:40 +08:00
										 |  |  |         with pytest.raises(RuntimeError) as e: | 
					
						
							| 
									
										
										
										
											2019-06-01 02:53:26 +08:00
										 |  |  |             with c.session_transaction(): | 
					
						
							| 
									
										
										
										
											2011-08-26 20:47:01 +08:00
										 |  |  |                 pass | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         assert "Session backend did not open a session" in str(e.value) | 
					
						
							| 
									
										
										
										
											2011-08-26 20:47:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_session_transactions_keep_context(app, client, req_ctx): | 
					
						
							| 
									
										
										
										
											2019-06-01 02:53:26 +08:00
										 |  |  |     client.get("/") | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |     req = flask.request._get_current_object() | 
					
						
							|  |  |  |     assert req is not None | 
					
						
							|  |  |  |     with client.session_transaction(): | 
					
						
							|  |  |  |         assert req is flask.request._get_current_object() | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_session_transaction_needs_cookies(app): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  |     c = app.test_client(use_cookies=False) | 
					
						
							| 
									
										
										
										
											2023-04-13 01:33:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(TypeError, match="Cookies are disabled."): | 
					
						
							| 
									
										
										
										
											2019-06-01 02:53:26 +08:00
										 |  |  |         with c.session_transaction(): | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |             pass | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_test_client_context_binding(app, client): | 
					
						
							|  |  |  |     app.testing = False | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  |     def index(): | 
					
						
							|  |  |  |         flask.g.value = 42 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "Hello World!" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/other") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  |     def other(): | 
					
						
							| 
									
										
										
										
											2023-11-10 01:20:27 +08:00
										 |  |  |         raise ZeroDivisionError | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |     with client: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         resp = client.get("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  |         assert flask.g.value == 42 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         assert resp.data == b"Hello World!" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  |         assert resp.status_code == 200 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-10 01:20:27 +08:00
										 |  |  |     with client: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         resp = client.get("/other") | 
					
						
							|  |  |  |         assert not hasattr(flask.g, "value") | 
					
						
							|  |  |  |         assert b"Internal Server Error" in resp.data | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  |         assert resp.status_code == 500 | 
					
						
							|  |  |  |         flask.g.value = 23 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-10 01:20:27 +08:00
										 |  |  |     with pytest.raises(RuntimeError): | 
					
						
							|  |  |  |         flask.g.value  # noqa: B018 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_reuse_client(client): | 
					
						
							|  |  |  |     c = client | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     with c: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         assert client.get("/").status_code == 404 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     with c: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         assert client.get("/").status_code == 404 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_full_url_request(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/action", methods=["POST"]) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  |     def action(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "x" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |     with client: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = client.post("http://domain.com/action?vodka=42", data={"gin": 43}) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  |         assert rv.status_code == 200 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         assert "gin" in flask.request.form | 
					
						
							|  |  |  |         assert "vodka" in flask.request.args | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 05:05:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-05 00:42:19 +08:00
										 |  |  | def test_json_request_and_response(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/echo", methods=["POST"]) | 
					
						
							| 
									
										
										
										
											2015-04-02 06:08:06 +08:00
										 |  |  |     def echo(): | 
					
						
							| 
									
										
										
										
											2017-06-05 02:44:00 +08:00
										 |  |  |         return jsonify(flask.request.get_json()) | 
					
						
							| 
									
										
										
										
											2015-03-30 05:05:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-05 00:42:19 +08:00
										 |  |  |     with client: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         json_data = {"drink": {"gin": 1, "tonic": True}, "price": 10} | 
					
						
							|  |  |  |         rv = client.post("/echo", json=json_data) | 
					
						
							| 
									
										
										
										
											2015-04-02 06:08:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Request should be in JSON | 
					
						
							| 
									
										
										
										
											2015-03-30 06:05:40 +08:00
										 |  |  |         assert flask.request.is_json | 
					
						
							| 
									
										
										
										
											2015-03-30 05:05:32 +08:00
										 |  |  |         assert flask.request.get_json() == json_data | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-02 06:08:06 +08:00
										 |  |  |         # Response should be in JSON | 
					
						
							|  |  |  |         assert rv.status_code == 200 | 
					
						
							|  |  |  |         assert rv.is_json | 
					
						
							|  |  |  |         assert rv.get_json() == json_data | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-05 00:42:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-17 23:39:16 +08:00
										 |  |  | def test_client_json_no_app_context(app, client): | 
					
						
							|  |  |  |     @app.route("/hello", methods=["POST"]) | 
					
						
							|  |  |  |     def hello(): | 
					
						
							| 
									
										
										
										
											2020-04-05 02:39:03 +08:00
										 |  |  |         return f"Hello, {flask.request.json['name']}!" | 
					
						
							| 
									
										
										
										
											2019-05-17 23:39:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-05 00:43:06 +08:00
										 |  |  |     class Namespace: | 
					
						
							| 
									
										
										
										
											2019-05-17 23:39:16 +08:00
										 |  |  |         count = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def add(self, app): | 
					
						
							|  |  |  |             self.count += 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ns = Namespace() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with appcontext_popped.connected_to(ns.add, app): | 
					
						
							|  |  |  |         rv = client.post("/hello", json={"name": "Flask"}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert rv.get_data(as_text=True) == "Hello, Flask!" | 
					
						
							|  |  |  |     assert ns.count == 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-20 04:20:03 +08:00
										 |  |  | def test_subdomain(): | 
					
						
							|  |  |  |     app = flask.Flask(__name__, subdomain_matching=True) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config["SERVER_NAME"] = "example.com" | 
					
						
							| 
									
										
										
										
											2018-02-20 04:20:03 +08:00
										 |  |  |     client = app.test_client() | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/", subdomain="<company_id>") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  |     def view(company_id): | 
					
						
							|  |  |  |         return company_id | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         url = flask.url_for("view", company_id="xxx") | 
					
						
							| 
									
										
										
										
											2012-10-07 18:48:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |     with client: | 
					
						
							|  |  |  |         response = client.get(url) | 
					
						
							| 
									
										
										
										
											2012-10-07 18:48:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  |     assert 200 == response.status_code | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert b"xxx" == response.data | 
					
						
							| 
									
										
										
										
											2012-10-07 18:48:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_nosubdomain(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config["SERVER_NAME"] = "example.com" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/<company_id>") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  |     def view(company_id): | 
					
						
							|  |  |  |         return company_id | 
					
						
							| 
									
										
										
										
											2012-10-07 18:48:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  |     with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         url = flask.url_for("view", company_id="xxx") | 
					
						
							| 
									
										
										
										
											2012-10-07 18:48:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |     with client: | 
					
						
							|  |  |  |         response = client.get(url) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:30:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     assert 200 == response.status_code | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert b"xxx" == response.data | 
					
						
							| 
									
										
										
										
											2018-02-20 07:34:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_cli_runner_class(app): | 
					
						
							|  |  |  |     runner = app.test_cli_runner() | 
					
						
							|  |  |  |     assert isinstance(runner, FlaskCliRunner) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class SubRunner(FlaskCliRunner): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     app.test_cli_runner_class = SubRunner | 
					
						
							|  |  |  |     runner = app.test_cli_runner() | 
					
						
							|  |  |  |     assert isinstance(runner, SubRunner) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_cli_invoke(app): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.cli.command("hello") | 
					
						
							| 
									
										
										
										
											2018-02-20 07:34:46 +08:00
										 |  |  |     def hello_command(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         click.echo("Hello, World!") | 
					
						
							| 
									
										
										
										
											2018-02-20 07:34:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     runner = app.test_cli_runner() | 
					
						
							|  |  |  |     # invoke with command name | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     result = runner.invoke(args=["hello"]) | 
					
						
							|  |  |  |     assert "Hello" in result.output | 
					
						
							| 
									
										
										
										
											2018-02-20 07:34:46 +08:00
										 |  |  |     # invoke with command object | 
					
						
							|  |  |  |     result = runner.invoke(hello_command) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert "Hello" in result.output | 
					
						
							| 
									
										
										
										
											2018-02-20 07:34:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_cli_custom_obj(app): | 
					
						
							| 
									
										
										
										
											2020-04-05 00:43:06 +08:00
										 |  |  |     class NS: | 
					
						
							| 
									
										
										
										
											2018-02-20 07:34:46 +08:00
										 |  |  |         called = False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def create_app(): | 
					
						
							|  |  |  |         NS.called = True | 
					
						
							|  |  |  |         return app | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.cli.command("hello") | 
					
						
							| 
									
										
										
										
											2018-02-20 07:34:46 +08:00
										 |  |  |     def hello_command(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         click.echo("Hello, World!") | 
					
						
							| 
									
										
										
										
											2018-02-20 07:34:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     script_info = ScriptInfo(create_app=create_app) | 
					
						
							|  |  |  |     runner = app.test_cli_runner() | 
					
						
							|  |  |  |     runner.invoke(hello_command, obj=script_info) | 
					
						
							|  |  |  |     assert NS.called | 
					
						
							| 
									
										
										
										
											2019-04-17 06:29:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_client_pop_all_preserved(app, req_ctx, client): | 
					
						
							|  |  |  |     @app.route("/") | 
					
						
							|  |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2022-06-30 12:02:44 +08:00
										 |  |  |         # stream_with_context pushes a third context, preserved by response | 
					
						
							|  |  |  |         return flask.stream_with_context("hello") | 
					
						
							| 
									
										
										
										
											2019-04-17 06:29:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-30 12:02:44 +08:00
										 |  |  |     # req_ctx fixture pushed an initial context | 
					
						
							| 
									
										
										
										
											2019-04-17 06:29:17 +08:00
										 |  |  |     with client: | 
					
						
							|  |  |  |         # request pushes a second request context, preserved by client | 
					
						
							| 
									
										
										
										
											2022-06-30 12:02:44 +08:00
										 |  |  |         rv = client.get("/") | 
					
						
							| 
									
										
										
										
											2019-04-17 06:29:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-30 12:02:44 +08:00
										 |  |  |     # close the response, releasing the context held by stream_with_context | 
					
						
							|  |  |  |     rv.close() | 
					
						
							| 
									
										
										
										
											2019-04-17 06:29:17 +08:00
										 |  |  |     # only req_ctx fixture should still be pushed | 
					
						
							| 
									
										
										
										
											2022-07-09 20:41:35 +08:00
										 |  |  |     assert _cv_request.get(None) is req_ctx |