| 
									
										
										
										
											2018-10-19 06:30:03 +08:00
										 |  |  | import io | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2016-09-26 18:43:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-29 05:55:52 +08:00
										 |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2022-05-03 00:16:12 +08:00
										 |  |  | import werkzeug.exceptions | 
					
						
							| 
									
										
										
										
											2017-07-29 05:55:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import flask | 
					
						
							| 
									
										
										
										
											2019-06-01 23:35:03 +08:00
										 |  |  | from flask.helpers import get_debug_flag | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-05 00:43:06 +08:00
										 |  |  | class FakePath: | 
					
						
							| 
									
										
										
										
											2019-01-04 09: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-07 03:39:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-04 09:17:45 +08:00
										 |  |  |     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: | 
					
						
							| 
									
										
										
										
											2020-11-06 01:00:57 +08:00
										 |  |  |     def test_send_file(self, app, req_ctx): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = flask.send_file("static/index.html") | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |         assert rv.direct_passthrough | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         assert rv.mimetype == "text/html" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         with app.open_resource("static/index.html") as f: | 
					
						
							| 
									
										
										
										
											2019-01-04 09:17:45 +08:00
										 |  |  |             rv.direct_passthrough = False | 
					
						
							|  |  |  |             assert rv.data == f.read() | 
					
						
							| 
									
										
										
										
											2016-09-26 18:43:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         rv.close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |     def test_static_file(self, app, req_ctx): | 
					
						
							| 
									
										
										
										
											2020-11-06 01:00:57 +08:00
										 |  |  |         # Default max_age is None. | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Test with static file handler. | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = app.send_static_file("index.html") | 
					
						
							| 
									
										
										
										
											2020-11-06 01:00:57 +08:00
										 |  |  |         assert rv.cache_control.max_age is None | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |         rv.close() | 
					
						
							| 
									
										
										
										
											2020-11-06 01:00:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Test with direct use of send_file. | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = flask.send_file("static/index.html") | 
					
						
							| 
									
										
										
										
											2020-11-06 01:00:57 +08:00
										 |  |  |         assert rv.cache_control.max_age is None | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |         rv.close() | 
					
						
							| 
									
										
										
										
											2020-11-06 01:00:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         app.config["SEND_FILE_MAX_AGE_DEFAULT"] = 3600 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Test with static file handler. | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = app.send_static_file("index.html") | 
					
						
							| 
									
										
										
										
											2020-11-06 01:00:57 +08:00
										 |  |  |         assert rv.cache_control.max_age == 3600 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |         rv.close() | 
					
						
							| 
									
										
										
										
											2020-11-06 01:00:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Test with direct use of send_file. | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = flask.send_file("static/index.html") | 
					
						
							| 
									
										
										
										
											2020-11-06 01:00:57 +08:00
										 |  |  |         assert rv.cache_control.max_age == 3600 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |         rv.close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 01:00:57 +08:00
										 |  |  |         # Test with pathlib.Path. | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = app.send_static_file(FakePath("index.html")) | 
					
						
							| 
									
										
										
										
											2020-11-06 01:00:57 +08:00
										 |  |  |         assert rv.cache_control.max_age == 3600 | 
					
						
							| 
									
										
										
										
											2019-01-04 09:17:45 +08:00
										 |  |  |         rv.close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-12 23:19:17 +08:00
										 |  |  |         class StaticFileApp(flask.Flask): | 
					
						
							| 
									
										
										
										
											2012-04-24 13:48:05 +08:00
										 |  |  |             def get_send_file_max_age(self, filename): | 
					
						
							|  |  |  |                 return 10 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-14 05:37:48 +08:00
										 |  |  |         app = StaticFileApp(__name__) | 
					
						
							| 
									
										
										
										
											2020-11-06 01:00:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-12 23:19:17 +08:00
										 |  |  |         with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2012-04-24 13:48:05 +08:00
										 |  |  |             # Test with static file handler. | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             rv = app.send_static_file("index.html") | 
					
						
							| 
									
										
										
										
											2020-11-06 01:00:57 +08:00
										 |  |  |             assert rv.cache_control.max_age == 10 | 
					
						
							| 
									
										
										
										
											2013-05-30 21:31:36 +08:00
										 |  |  |             rv.close() | 
					
						
							| 
									
										
										
										
											2020-11-06 01:00:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # Test with direct use of send_file. | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             rv = flask.send_file("static/index.html") | 
					
						
							| 
									
										
										
										
											2020-11-06 01:00:57 +08:00
										 |  |  |             assert rv.cache_control.max_age == 10 | 
					
						
							| 
									
										
										
										
											2013-05-30 21:31:36 +08:00
										 |  |  |             rv.close() | 
					
						
							| 
									
										
										
										
											2012-03-12 23:19:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |     def test_send_from_directory(self, app, req_ctx): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         app.root_path = os.path.join( | 
					
						
							|  |  |  |             os.path.dirname(__file__), "test_apps", "subdomaintestmodule" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         rv = flask.send_from_directory("static", "hello.txt") | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |         rv.direct_passthrough = False | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         assert rv.data.strip() == b"Hello Subdomain" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |         rv.close() | 
					
						
							| 
									
										
										
										
											2014-02-09 21:06:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-05 00:43:06 +08:00
										 |  |  | class TestUrlFor: | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |     def test_url_for_with_anchor(self, app, req_ctx): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         @app.route("/") | 
					
						
							| 
									
										
										
										
											2011-10-06 22:57:03 +08:00
										 |  |  |         def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             return "42" | 
					
						
							| 
									
										
										
										
											2011-10-06 22:57:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         assert flask.url_for("index", _anchor="x y") == "/#x%20y" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_url_for_with_scheme(self, app, req_ctx): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         @app.route("/") | 
					
						
							| 
									
										
										
										
											2013-01-18 07:08:45 +08:00
										 |  |  |         def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             return "42" | 
					
						
							| 
									
										
										
										
											2013-01-18 07:08:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         assert ( | 
					
						
							|  |  |  |             flask.url_for("index", _external=True, _scheme="https") | 
					
						
							|  |  |  |             == "https://localhost/" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_url_for_with_scheme_not_external(self, app, req_ctx): | 
					
						
							| 
									
										
										
										
											2022-05-15 03:43:38 +08:00
										 |  |  |         app.add_url_rule("/", endpoint="index") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Implicit external with scheme. | 
					
						
							|  |  |  |         url = flask.url_for("index", _scheme="https") | 
					
						
							|  |  |  |         assert url == "https://localhost/" | 
					
						
							| 
									
										
										
										
											2013-01-18 07:08:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-15 03:43:38 +08:00
										 |  |  |         # Error when external=False with scheme | 
					
						
							|  |  |  |         with pytest.raises(ValueError): | 
					
						
							|  |  |  |             flask.url_for("index", _scheme="https", _external=False) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_url_for_with_alternating_schemes(self, app, req_ctx): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         @app.route("/") | 
					
						
							| 
									
										
										
										
											2016-05-22 16:45:25 +08:00
										 |  |  |         def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             return "42" | 
					
						
							| 
									
										
										
										
											2016-05-22 16:45:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08: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-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_url_with_method(self, app, req_ctx): | 
					
						
							| 
									
										
										
										
											2011-11-04 09:46:22 +08:00
										 |  |  |         from flask.views import MethodView | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-04 09:46:22 +08:00
										 |  |  |         class MyView(MethodView): | 
					
						
							|  |  |  |             def get(self, id=None): | 
					
						
							|  |  |  |                 if id is None: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |                     return "List" | 
					
						
							| 
									
										
										
										
											2020-04-05 02:39:03 +08:00
										 |  |  |                 return f"Get {id:d}" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-04 09:46:22 +08:00
										 |  |  |             def post(self): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |                 return "Create" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08: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 09:46:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-24 23:16:55 +08:00
										 |  |  |     def test_url_for_with_self(self, app, req_ctx): | 
					
						
							|  |  |  |         @app.route("/<self>") | 
					
						
							|  |  |  |         def index(self): | 
					
						
							|  |  |  |             return "42" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         assert flask.url_for("index", self="2") == "/2" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +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") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-03 00:16:12 +08:00
										 |  |  | def test_abort_no_app(): | 
					
						
							|  |  |  |     with pytest.raises(werkzeug.exceptions.Unauthorized): | 
					
						
							|  |  |  |         flask.abort(401) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(LookupError): | 
					
						
							|  |  |  |         flask.abort(900) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_app_aborter_class(): | 
					
						
							|  |  |  |     class MyAborter(werkzeug.exceptions.Aborter): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class MyFlask(flask.Flask): | 
					
						
							|  |  |  |         aborter_class = MyAborter | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     app = MyFlask(__name__) | 
					
						
							|  |  |  |     assert isinstance(app.aborter, MyAborter) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_abort_with_app(app): | 
					
						
							|  |  |  |     class My900Error(werkzeug.exceptions.HTTPException): | 
					
						
							|  |  |  |         code = 900 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     app.aborter.mapping[900] = My900Error | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with app.app_context(), pytest.raises(My900Error): | 
					
						
							|  |  |  |         flask.abort(900) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2012-01-08 06:50:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-03 01:38:27 +08:00
										 |  |  |     def test_name_with_import_error(self, modules_tmp_path): | 
					
						
							|  |  |  |         (modules_tmp_path / "importerror.py").write_text("raise NotImplementedError()") | 
					
						
							| 
									
										
										
										
											2012-01-08 06:50:11 +08:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             flask.Flask("importerror") | 
					
						
							| 
									
										
										
										
											2012-01-08 06:50:11 +08:00
										 |  |  |         except NotImplementedError: | 
					
						
							| 
									
										
										
										
											2019-06-01 02:53:26 +08:00
										 |  |  |             AssertionError("Flask(import_name) is importing import_name.") | 
					
						
							| 
									
										
										
										
											2012-01-08 06:50:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-05 00:43:06 +08:00
										 |  |  | class TestStreaming: | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |     def test_streaming_with_context(self, app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         @app.route("/") | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  |         def index(): | 
					
						
							|  |  |  |             def generate(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |                 yield "Hello " | 
					
						
							|  |  |  |                 yield flask.request.args["name"] | 
					
						
							|  |  |  |                 yield "!" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  |             return flask.Response(flask.stream_with_context(generate())) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = client.get("/?name=World") | 
					
						
							|  |  |  |         assert rv.data == b"Hello World!" | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |     def test_streaming_with_context_as_decorator(self, app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         @app.route("/") | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  |         def index(): | 
					
						
							|  |  |  |             @flask.stream_with_context | 
					
						
							| 
									
										
										
										
											2016-04-02 07:17:45 +08:00
										 |  |  |             def generate(hello): | 
					
						
							|  |  |  |                 yield hello | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |                 yield flask.request.args["name"] | 
					
						
							|  |  |  |                 yield "!" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             return flask.Response(generate("Hello ")) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = client.get("/?name=World") | 
					
						
							|  |  |  |         assert rv.data == b"Hello World!" | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |     def test_streaming_with_context_and_custom_close(self, app, client): | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  |         called = [] | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  |             def __iter__(self): | 
					
						
							|  |  |  |                 return self | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  |             def close(self): | 
					
						
							|  |  |  |                 called.append(42) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-26 01:46:26 +08:00
										 |  |  |             def __next__(self): | 
					
						
							| 
									
										
										
										
											2013-05-23 03:40:30 +08:00
										 |  |  |                 return next(self._gen) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-03 00:23:53 +08:00
										 |  |  |             next = __next__ | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         @app.route("/") | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  |         def index(): | 
					
						
							|  |  |  |             def generate(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |                 yield "Hello " | 
					
						
							|  |  |  |                 yield flask.request.args["name"] | 
					
						
							|  |  |  |                 yield "!" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             return flask.Response(flask.stream_with_context(Wrapper(generate()))) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         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): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         @app.route("/") | 
					
						
							| 
									
										
										
										
											2017-06-03 02:07:53 +08:00
										 |  |  |         def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             flask.session["test"] = "flask" | 
					
						
							| 
									
										
										
										
											2017-06-03 02:07:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             @flask.stream_with_context | 
					
						
							|  |  |  |             def gen(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |                 yield flask.session["test"] | 
					
						
							| 
									
										
										
										
											2017-06-03 02:07:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return flask.Response(gen()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         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: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @pytest.mark.parametrize( | 
					
						
							| 
									
										
										
										
											2023-02-24 00:35:16 +08:00
										 |  |  |         ("debug", "expect"), | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         [ | 
					
						
							| 
									
										
										
										
											2023-02-24 00:35:16 +08:00
										 |  |  |             ("", False), | 
					
						
							|  |  |  |             ("0", False), | 
					
						
							|  |  |  |             ("False", False), | 
					
						
							|  |  |  |             ("No", False), | 
					
						
							|  |  |  |             ("True", True), | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         ], | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2023-02-24 00:35:16 +08:00
										 |  |  |     def test_get_debug_flag(self, monkeypatch, debug, expect): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         monkeypatch.setenv("FLASK_DEBUG", debug) | 
					
						
							| 
									
										
										
										
											2023-02-24 00:35:16 +08:00
										 |  |  |         assert get_debug_flag() == expect | 
					
						
							| 
									
										
										
										
											2018-01-07 00:07:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-23 11:49:37 +08:00
										 |  |  |     def test_make_response(self): | 
					
						
							| 
									
										
										
										
											2017-05-23 07:15:48 +08:00
										 |  |  |         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 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             assert rv.mimetype == "text/html" | 
					
						
							| 
									
										
										
										
											2017-05-23 07:15:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             rv = flask.helpers.make_response("Hello") | 
					
						
							| 
									
										
										
										
											2017-05-23 11:49:37 +08:00
										 |  |  |             assert rv.status_code == 200 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             assert rv.data == b"Hello" | 
					
						
							|  |  |  |             assert rv.mimetype == "text/html" | 
					
						
							| 
									
										
										
										
											2019-05-06 22:56:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-18 04:20:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-10 22:37:32 +08:00
										 |  |  | @pytest.mark.parametrize("mode", ("r", "rb", "rt")) | 
					
						
							|  |  |  | def test_open_resource(mode): | 
					
						
							|  |  |  |     app = flask.Flask(__name__) | 
					
						
							| 
									
										
										
										
											2019-05-18 04:20:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-10 22:37:32 +08:00
										 |  |  |     with app.open_resource("static/index.html", mode) as f: | 
					
						
							|  |  |  |         assert "<h1>Hello World!</h1>" in str(f.read()) | 
					
						
							| 
									
										
										
										
											2019-05-06 22:56:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-10 22:37:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | @pytest.mark.parametrize("mode", ("w", "x", "a", "r+")) | 
					
						
							|  |  |  | def test_open_resource_exceptions(mode): | 
					
						
							|  |  |  |     app = flask.Flask(__name__) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(ValueError): | 
					
						
							|  |  |  |         app.open_resource("static/index.html", mode) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @pytest.mark.parametrize("encoding", ("utf-8", "utf-16-le")) | 
					
						
							|  |  |  | def test_open_resource_with_encoding(tmp_path, encoding): | 
					
						
							|  |  |  |     app = flask.Flask(__name__, root_path=os.fspath(tmp_path)) | 
					
						
							|  |  |  |     (tmp_path / "test").write_text("test", encoding=encoding) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with app.open_resource("test", mode="rt", encoding=encoding) as f: | 
					
						
							|  |  |  |         assert f.read() == "test" |