| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | # -*- coding: utf-8 -*- | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2014-09-01 03:54:45 +08:00
										 |  |  |     tests.basic | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |     ~~~~~~~~~~~~~~~~~~~~~ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     The basic functionality. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-09 02:57:40 +08:00
										 |  |  |     :copyright: © 2010 by the Pallets team. | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |     :license: BSD, see LICENSE for more details. | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2011-09-02 00:35:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											2019-05-13 16:29:38 +08:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2014-02-10 00:07:56 +08:00
										 |  |  | import time | 
					
						
							| 
									
										
										
										
											2017-05-30 01:09:24 +08:00
										 |  |  | import uuid | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | from datetime import datetime | 
					
						
							| 
									
										
										
										
											2014-02-10 00:07:56 +08:00
										 |  |  | from threading import Thread | 
					
						
							| 
									
										
										
										
											2017-05-30 01:09:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import pytest | 
					
						
							|  |  |  | import werkzeug.serving | 
					
						
							|  |  |  | from werkzeug.exceptions import BadRequest, Forbidden, NotFound | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | from werkzeug.http import parse_date | 
					
						
							| 
									
										
										
										
											2012-04-23 00:30:15 +08:00
										 |  |  | from werkzeug.routing import BuildError | 
					
						
							| 
									
										
										
										
											2017-05-30 01:09:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import flask | 
					
						
							|  |  |  | from flask._compat import text_type | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_options_work(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/", methods=["GET", "POST"]) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "Hello World" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.open("/", method="OPTIONS") | 
					
						
							|  |  |  |     assert sorted(rv.allow) == ["GET", "HEAD", "OPTIONS", "POST"] | 
					
						
							|  |  |  |     assert rv.data == b"" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_options_on_multiple_rules(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/", methods=["GET", "POST"]) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "Hello World" | 
					
						
							| 
									
										
										
										
											2013-07-31 04:35:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/", methods=["PUT"]) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index_put(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "Aha!" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.open("/", method="OPTIONS") | 
					
						
							|  |  |  |     assert sorted(rv.allow) == ["GET", "HEAD", "OPTIONS", "POST", "PUT"] | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-21 22:16:09 +08:00
										 |  |  | def test_provide_automatic_options_attr(): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     app = flask.Flask(__name__) | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "Hello World!" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     index.provide_automatic_options = False | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.route("/")(index) | 
					
						
							|  |  |  |     rv = app.test_client().open("/", method="OPTIONS") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert rv.status_code == 405 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     app = flask.Flask(__name__) | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index2(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "Hello World!" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     index2.provide_automatic_options = True | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.route("/", methods=["OPTIONS"])(index2) | 
					
						
							|  |  |  |     rv = app.test_client().open("/", method="OPTIONS") | 
					
						
							|  |  |  |     assert sorted(rv.allow) == ["OPTIONS"] | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_provide_automatic_options_kwarg(app, client): | 
					
						
							| 
									
										
										
										
											2017-04-21 22:16:09 +08:00
										 |  |  |     def index(): | 
					
						
							|  |  |  |         return flask.request.method | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def more(): | 
					
						
							|  |  |  |         return flask.request.method | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.add_url_rule("/", view_func=index, provide_automatic_options=False) | 
					
						
							| 
									
										
										
										
											2017-04-21 22:16:09 +08:00
										 |  |  |     app.add_url_rule( | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         "/more", | 
					
						
							|  |  |  |         view_func=more, | 
					
						
							|  |  |  |         methods=["GET", "POST"], | 
					
						
							|  |  |  |         provide_automatic_options=False, | 
					
						
							| 
									
										
										
										
											2017-04-21 22:16:09 +08:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert client.get("/").data == b"GET" | 
					
						
							| 
									
										
										
										
											2017-04-21 22:16:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.post("/") | 
					
						
							| 
									
										
										
										
											2017-04-21 22:16:09 +08:00
										 |  |  |     assert rv.status_code == 405 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert sorted(rv.allow) == ["GET", "HEAD"] | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-21 22:16:09 +08:00
										 |  |  |     # Older versions of Werkzeug.test.Client don't have an options method | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     if hasattr(client, "options"): | 
					
						
							|  |  |  |         rv = client.options("/") | 
					
						
							| 
									
										
										
										
											2017-04-21 22:16:09 +08:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = client.open("/", method="OPTIONS") | 
					
						
							| 
									
										
										
										
											2017-04-21 22:16:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     assert rv.status_code == 405 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.head("/") | 
					
						
							| 
									
										
										
										
											2017-04-21 22:16:09 +08:00
										 |  |  |     assert rv.status_code == 200 | 
					
						
							|  |  |  |     assert not rv.data  # head truncates | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert client.post("/more").data == b"POST" | 
					
						
							|  |  |  |     assert client.get("/more").data == b"GET" | 
					
						
							| 
									
										
										
										
											2017-04-21 22:16:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.delete("/more") | 
					
						
							| 
									
										
										
										
											2017-04-21 22:16:09 +08:00
										 |  |  |     assert rv.status_code == 405 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert sorted(rv.allow) == ["GET", "HEAD", "POST"] | 
					
						
							| 
									
										
										
										
											2017-04-21 22:16:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     if hasattr(client, "options"): | 
					
						
							|  |  |  |         rv = client.options("/more") | 
					
						
							| 
									
										
										
										
											2017-04-21 22:16:09 +08:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = client.open("/more", method="OPTIONS") | 
					
						
							| 
									
										
										
										
											2017-04-21 22:16:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     assert rv.status_code == 405 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_request_dispatching(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							|  |  |  |         return flask.request.method | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/more", methods=["GET", "POST"]) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def more(): | 
					
						
							|  |  |  |         return flask.request.method | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert client.get("/").data == b"GET" | 
					
						
							|  |  |  |     rv = client.post("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert rv.status_code == 405 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert sorted(rv.allow) == ["GET", "HEAD", "OPTIONS"] | 
					
						
							|  |  |  |     rv = client.head("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert rv.status_code == 200 | 
					
						
							|  |  |  |     assert not rv.data  # head truncates | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert client.post("/more").data == b"POST" | 
					
						
							|  |  |  |     assert client.get("/more").data == b"GET" | 
					
						
							|  |  |  |     rv = client.delete("/more") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert rv.status_code == 405 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert sorted(rv.allow) == ["GET", "HEAD", "OPTIONS", "POST"] | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-30 03:31:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_disallow_string_for_allowed_methods(app): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     with pytest.raises(TypeError): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         @app.route("/", methods="GET POST") | 
					
						
							| 
									
										
										
										
											2013-01-30 03:31:45 +08:00
										 |  |  |         def index(): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |             return "Hey" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_url_mapping(app, client): | 
					
						
							| 
									
										
										
										
											2014-12-25 00:16:57 +08:00
										 |  |  |     random_uuid4 = "7eb41166-9ebf-4d26-b771-ea3f54f8b383" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							|  |  |  |         return flask.request.method | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def more(): | 
					
						
							|  |  |  |         return flask.request.method | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-25 00:16:57 +08:00
										 |  |  |     def options(): | 
					
						
							|  |  |  |         return random_uuid4 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.add_url_rule("/", "index", index) | 
					
						
							|  |  |  |     app.add_url_rule("/more", "more", more, methods=["GET", "POST"]) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-25 00:16:57 +08:00
										 |  |  |     # Issue 1288: Test that automatic options are not added when non-uppercase 'options' in methods | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.add_url_rule("/options", "options", options, methods=["options"]) | 
					
						
							| 
									
										
										
										
											2014-12-25 00:16:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert client.get("/").data == b"GET" | 
					
						
							|  |  |  |     rv = client.post("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert rv.status_code == 405 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert sorted(rv.allow) == ["GET", "HEAD", "OPTIONS"] | 
					
						
							|  |  |  |     rv = client.head("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert rv.status_code == 200 | 
					
						
							|  |  |  |     assert not rv.data  # head truncates | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert client.post("/more").data == b"POST" | 
					
						
							|  |  |  |     assert client.get("/more").data == b"GET" | 
					
						
							|  |  |  |     rv = client.delete("/more") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert rv.status_code == 405 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert sorted(rv.allow) == ["GET", "HEAD", "OPTIONS", "POST"] | 
					
						
							|  |  |  |     rv = client.open("/options", method="OPTIONS") | 
					
						
							| 
									
										
										
										
											2014-12-25 00:16:57 +08:00
										 |  |  |     assert rv.status_code == 200 | 
					
						
							|  |  |  |     assert random_uuid4 in rv.data.decode("utf-8") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_werkzeug_routing(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     from werkzeug.routing import Submount, Rule | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     app.url_map.add( | 
					
						
							|  |  |  |         Submount("/foo", [Rule("/bar", endpoint="bar"), Rule("/", endpoint="index")]) | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def bar(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "bar" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "index" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.view_functions["bar"] = bar | 
					
						
							|  |  |  |     app.view_functions["index"] = index | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert client.get("/foo/").data == b"index" | 
					
						
							|  |  |  |     assert client.get("/foo/bar").data == b"bar" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_endpoint_decorator(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     from werkzeug.routing import Submount, Rule | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.url_map.add( | 
					
						
							|  |  |  |         Submount("/foo", [Rule("/bar", endpoint="bar"), Rule("/", endpoint="index")]) | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.endpoint("bar") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def bar(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "bar" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.endpoint("index") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "index" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert client.get("/foo/").data == b"index" | 
					
						
							|  |  |  |     assert client.get("/foo/bar").data == b"bar" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_session(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/set", methods=["POST"]) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def set(): | 
					
						
							| 
									
										
										
										
											2018-01-05 00:40:12 +08:00
										 |  |  |         assert not flask.session.accessed | 
					
						
							|  |  |  |         assert not flask.session.modified | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.session["value"] = flask.request.form["value"] | 
					
						
							| 
									
										
										
										
											2018-01-05 00:40:12 +08:00
										 |  |  |         assert flask.session.accessed | 
					
						
							|  |  |  |         assert flask.session.modified | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "value set" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/get") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def get(): | 
					
						
							| 
									
										
										
										
											2018-01-05 00:40:12 +08:00
										 |  |  |         assert not flask.session.accessed | 
					
						
							|  |  |  |         assert not flask.session.modified | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         v = flask.session.get("value", "None") | 
					
						
							| 
									
										
										
										
											2018-01-05 00:40:12 +08:00
										 |  |  |         assert flask.session.accessed | 
					
						
							|  |  |  |         assert not flask.session.modified | 
					
						
							|  |  |  |         return v | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert client.post("/set", data={"value": "42"}).data == b"value set" | 
					
						
							|  |  |  |     assert client.get("/get").data == b"42" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_session_using_server_name(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config.update(SERVER_NAME="example.com") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.session["testing"] = 42 | 
					
						
							|  |  |  |         return "Hello World" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/", "http://example.com/") | 
					
						
							|  |  |  |     assert "domain=.example.com" in rv.headers["set-cookie"].lower() | 
					
						
							|  |  |  |     assert "httponly" in rv.headers["set-cookie"].lower() | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_session_using_server_name_and_port(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config.update(SERVER_NAME="example.com:8080") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.session["testing"] = 42 | 
					
						
							|  |  |  |         return "Hello World" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/", "http://example.com:8080/") | 
					
						
							|  |  |  |     assert "domain=.example.com" in rv.headers["set-cookie"].lower() | 
					
						
							|  |  |  |     assert "httponly" in rv.headers["set-cookie"].lower() | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_session_using_server_name_port_and_path(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config.update(SERVER_NAME="example.com:8080", APPLICATION_ROOT="/foo") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.session["testing"] = 42 | 
					
						
							|  |  |  |         return "Hello World" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/", "http://example.com:8080/foo") | 
					
						
							|  |  |  |     assert "domain=example.com" in rv.headers["set-cookie"].lower() | 
					
						
							|  |  |  |     assert "path=/foo" in rv.headers["set-cookie"].lower() | 
					
						
							|  |  |  |     assert "httponly" in rv.headers["set-cookie"].lower() | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_session_using_application_root(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     class PrefixPathMiddleware(object): | 
					
						
							|  |  |  |         def __init__(self, app, prefix): | 
					
						
							|  |  |  |             self.app = app | 
					
						
							|  |  |  |             self.prefix = prefix | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def __call__(self, environ, start_response): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             environ["SCRIPT_NAME"] = self.prefix | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |             return self.app(environ, start_response) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.wsgi_app = PrefixPathMiddleware(app.wsgi_app, "/bar") | 
					
						
							|  |  |  |     app.config.update(APPLICATION_ROOT="/bar") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.session["testing"] = 42 | 
					
						
							|  |  |  |         return "Hello World" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/", "http://example.com:8080/") | 
					
						
							|  |  |  |     assert "path=/bar" in rv.headers["set-cookie"].lower() | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_session_using_session_settings(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     app.config.update( | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         SERVER_NAME="www.example.com:8080", | 
					
						
							|  |  |  |         APPLICATION_ROOT="/test", | 
					
						
							|  |  |  |         SESSION_COOKIE_DOMAIN=".example.com", | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |         SESSION_COOKIE_HTTPONLY=False, | 
					
						
							|  |  |  |         SESSION_COOKIE_SECURE=True, | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         SESSION_COOKIE_SAMESITE="Lax", | 
					
						
							|  |  |  |         SESSION_COOKIE_PATH="/", | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.session["testing"] = 42 | 
					
						
							|  |  |  |         return "Hello World" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/", "http://www.example.com:8080/test/") | 
					
						
							|  |  |  |     cookie = rv.headers["set-cookie"].lower() | 
					
						
							|  |  |  |     assert "domain=.example.com" in cookie | 
					
						
							|  |  |  |     assert "path=/" in cookie | 
					
						
							|  |  |  |     assert "secure" in cookie | 
					
						
							|  |  |  |     assert "httponly" not in cookie | 
					
						
							|  |  |  |     assert "samesite" in cookie | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 21:57:50 +08:00
										 |  |  | def test_session_using_samesite_attribute(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2018-01-23 21:57:50 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.session["testing"] = 42 | 
					
						
							|  |  |  |         return "Hello World" | 
					
						
							| 
									
										
										
										
											2018-01-23 21:57:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config.update(SESSION_COOKIE_SAMESITE="invalid") | 
					
						
							| 
									
										
										
										
											2018-01-24 07:11:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 21:57:50 +08:00
										 |  |  |     with pytest.raises(ValueError): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         client.get("/") | 
					
						
							| 
									
										
										
										
											2018-01-23 21:57:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     app.config.update(SESSION_COOKIE_SAMESITE=None) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/") | 
					
						
							|  |  |  |     cookie = rv.headers["set-cookie"].lower() | 
					
						
							|  |  |  |     assert "samesite" not in cookie | 
					
						
							| 
									
										
										
										
											2018-01-23 21:57:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config.update(SESSION_COOKIE_SAMESITE="Strict") | 
					
						
							|  |  |  |     rv = client.get("/") | 
					
						
							|  |  |  |     cookie = rv.headers["set-cookie"].lower() | 
					
						
							|  |  |  |     assert "samesite=strict" in cookie | 
					
						
							| 
									
										
										
										
											2018-01-23 21:57:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config.update(SESSION_COOKIE_SAMESITE="Lax") | 
					
						
							|  |  |  |     rv = client.get("/") | 
					
						
							|  |  |  |     cookie = rv.headers["set-cookie"].lower() | 
					
						
							|  |  |  |     assert "samesite=lax" in cookie | 
					
						
							| 
									
										
										
										
											2018-01-23 21:57:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-24 07:11:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_session_localhost_warning(recwarn, app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config.update(SERVER_NAME="localhost:5000") | 
					
						
							| 
									
										
										
										
											2017-05-14 12:31:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2017-05-14 12:31:46 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.session["testing"] = 42 | 
					
						
							|  |  |  |         return "testing" | 
					
						
							| 
									
										
										
										
											2017-05-14 12:31:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/", "http://localhost:5000/") | 
					
						
							|  |  |  |     assert "domain" not in rv.headers["set-cookie"].lower() | 
					
						
							| 
									
										
										
										
											2017-05-14 12:31:46 +08:00
										 |  |  |     w = recwarn.pop(UserWarning) | 
					
						
							|  |  |  |     assert '"localhost" is not a valid cookie domain' in str(w.message) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_session_ip_warning(recwarn, app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config.update(SERVER_NAME="127.0.0.1:5000") | 
					
						
							| 
									
										
										
										
											2017-05-14 12:31:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2017-05-14 12:31:46 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.session["testing"] = 42 | 
					
						
							|  |  |  |         return "testing" | 
					
						
							| 
									
										
										
										
											2017-05-14 12:31:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/", "http://127.0.0.1:5000/") | 
					
						
							|  |  |  |     assert "domain=127.0.0.1" in rv.headers["set-cookie"].lower() | 
					
						
							| 
									
										
										
										
											2017-05-14 12:31:46 +08:00
										 |  |  |     w = recwarn.pop(UserWarning) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert "cookie domain is an IP" in str(w.message) | 
					
						
							| 
									
										
										
										
											2017-05-14 12:31:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-28 22:58:06 +08:00
										 |  |  | def test_missing_session(app): | 
					
						
							|  |  |  |     app.secret_key = None | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def expect_exception(f, *args, **kwargs): | 
					
						
							| 
									
										
										
										
											2016-03-04 19:30:40 +08:00
										 |  |  |         e = pytest.raises(RuntimeError, f, *args, **kwargs) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         assert e.value.args and "session is unavailable" in e.value.args[0] | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         assert flask.session.get("missing_key") is None | 
					
						
							|  |  |  |         expect_exception(flask.session.__setitem__, "foo", 42) | 
					
						
							|  |  |  |         expect_exception(flask.session.pop, "foo") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_session_expiration(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     permanent = True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.session["test"] = 42 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |         flask.session.permanent = permanent | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/test") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def test(): | 
					
						
							|  |  |  |         return text_type(flask.session.permanent) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/") | 
					
						
							|  |  |  |     assert "set-cookie" in rv.headers | 
					
						
							|  |  |  |     match = re.search(r"(?i)\bexpires=([^;]+)", rv.headers["set-cookie"]) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     expires = parse_date(match.group()) | 
					
						
							|  |  |  |     expected = datetime.utcnow() + app.permanent_session_lifetime | 
					
						
							|  |  |  |     assert expires.year == expected.year | 
					
						
							|  |  |  |     assert expires.month == expected.month | 
					
						
							|  |  |  |     assert expires.day == expected.day | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/test") | 
					
						
							|  |  |  |     assert rv.data == b"True" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     permanent = False | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/") | 
					
						
							|  |  |  |     assert "set-cookie" in rv.headers | 
					
						
							|  |  |  |     match = re.search(r"\bexpires=([^;]+)", rv.headers["set-cookie"]) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert match is None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_session_stored_last(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     @app.after_request | 
					
						
							|  |  |  |     def modify_session(response): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.session["foo"] = 42 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |         return response | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def dump_session_contents(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return repr(flask.session.get("foo")) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert client.get("/").data == b"None" | 
					
						
							|  |  |  |     assert client.get("/").data == b"42" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_session_special_types(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     now = datetime.utcnow().replace(microsecond=0) | 
					
						
							|  |  |  |     the_uuid = uuid.uuid4() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def dump_session_contents(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.session["t"] = (1, 2, 3) | 
					
						
							|  |  |  |         flask.session["b"] = b"\xff" | 
					
						
							|  |  |  |         flask.session["m"] = flask.Markup("<html>") | 
					
						
							|  |  |  |         flask.session["u"] = the_uuid | 
					
						
							|  |  |  |         flask.session["d"] = now | 
					
						
							|  |  |  |         flask.session["t_tag"] = {" t": "not-a-tuple"} | 
					
						
							|  |  |  |         flask.session["di_t_tag"] = {" t__": "not-a-tuple"} | 
					
						
							|  |  |  |         flask.session["di_tag"] = {" di": "not-a-dict"} | 
					
						
							|  |  |  |         return "", 204 | 
					
						
							| 
									
										
										
										
											2017-06-03 01:01:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     with client: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         client.get("/") | 
					
						
							| 
									
										
										
										
											2017-06-03 01:01:30 +08:00
										 |  |  |         s = flask.session | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         assert s["t"] == (1, 2, 3) | 
					
						
							|  |  |  |         assert type(s["b"]) == bytes | 
					
						
							|  |  |  |         assert s["b"] == b"\xff" | 
					
						
							|  |  |  |         assert type(s["m"]) == flask.Markup | 
					
						
							|  |  |  |         assert s["m"] == flask.Markup("<html>") | 
					
						
							|  |  |  |         assert s["u"] == the_uuid | 
					
						
							|  |  |  |         assert s["d"] == now | 
					
						
							|  |  |  |         assert s["t_tag"] == {" t": "not-a-tuple"} | 
					
						
							|  |  |  |         assert s["di_t_tag"] == {" t__": "not-a-tuple"} | 
					
						
							|  |  |  |         assert s["di_tag"] == {" di": "not-a-dict"} | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_session_cookie_setting(app): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     is_permanent = True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/bump") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def bump(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = flask.session["foo"] = flask.session.get("foo", 0) + 1 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |         flask.session.permanent = is_permanent | 
					
						
							|  |  |  |         return str(rv) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/read") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def read(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return str(flask.session.get("foo", 0)) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def run_test(expect_header): | 
					
						
							|  |  |  |         with app.test_client() as c: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             assert c.get("/bump").data == b"1" | 
					
						
							|  |  |  |             assert c.get("/bump").data == b"2" | 
					
						
							|  |  |  |             assert c.get("/bump").data == b"3" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             rv = c.get("/read") | 
					
						
							|  |  |  |             set_cookie = rv.headers.get("set-cookie") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |             assert (set_cookie is not None) == expect_header | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             assert rv.data == b"3" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     is_permanent = True | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config["SESSION_REFRESH_EACH_REQUEST"] = True | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     run_test(expect_header=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     is_permanent = True | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config["SESSION_REFRESH_EACH_REQUEST"] = False | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     run_test(expect_header=False) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     is_permanent = False | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config["SESSION_REFRESH_EACH_REQUEST"] = True | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     run_test(expect_header=False) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     is_permanent = False | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config["SESSION_REFRESH_EACH_REQUEST"] = False | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     run_test(expect_header=False) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_session_vary_cookie(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/set") | 
					
						
							| 
									
										
										
										
											2017-05-20 00:44:06 +08:00
										 |  |  |     def set_session(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.session["test"] = "test" | 
					
						
							|  |  |  |         return "" | 
					
						
							| 
									
										
										
										
											2017-05-20 00:44:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/get") | 
					
						
							| 
									
										
										
										
											2017-05-21 04:00:17 +08:00
										 |  |  |     def get(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return flask.session.get("test") | 
					
						
							| 
									
										
										
										
											2017-05-20 00:44:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/getitem") | 
					
						
							| 
									
										
										
										
											2017-05-21 04:00:17 +08:00
										 |  |  |     def getitem(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return flask.session["test"] | 
					
						
							| 
									
										
										
										
											2017-05-21 04:00:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/setdefault") | 
					
						
							| 
									
										
										
										
											2017-05-21 04:00:17 +08:00
										 |  |  |     def setdefault(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return flask.session.setdefault("test", "default") | 
					
						
							| 
									
										
										
										
											2017-05-20 00:44:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/vary-cookie-header-set") | 
					
						
							| 
									
										
										
										
											2017-05-24 07:07:07 +08:00
										 |  |  |     def vary_cookie_header_set(): | 
					
						
							|  |  |  |         response = flask.Response() | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         response.vary.add("Cookie") | 
					
						
							|  |  |  |         flask.session["test"] = "test" | 
					
						
							| 
									
										
										
										
											2017-05-24 07:07:07 +08:00
										 |  |  |         return response | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/vary-header-set") | 
					
						
							| 
									
										
										
										
											2017-05-24 07:07:07 +08:00
										 |  |  |     def vary_header_set(): | 
					
						
							|  |  |  |         response = flask.Response() | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         response.vary.update(("Accept-Encoding", "Accept-Language")) | 
					
						
							|  |  |  |         flask.session["test"] = "test" | 
					
						
							| 
									
										
										
										
											2017-05-24 07:07:07 +08:00
										 |  |  |         return response | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/no-vary-header") | 
					
						
							| 
									
										
										
										
											2017-05-20 00:44:06 +08:00
										 |  |  |     def no_vary_header(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "" | 
					
						
							| 
									
										
										
										
											2017-05-20 00:44:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     def expect(path, header_value="Cookie"): | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |         rv = client.get(path) | 
					
						
							| 
									
										
										
										
											2017-05-20 00:44:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 07:07:07 +08:00
										 |  |  |         if header_value: | 
					
						
							|  |  |  |             # The 'Vary' key should exist in the headers only once. | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             assert len(rv.headers.get_all("Vary")) == 1 | 
					
						
							|  |  |  |             assert rv.headers["Vary"] == header_value | 
					
						
							| 
									
										
										
										
											2017-05-21 04:00:17 +08:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             assert "Vary" not in rv.headers | 
					
						
							| 
									
										
										
										
											2017-05-20 00:44:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     expect("/set") | 
					
						
							|  |  |  |     expect("/get") | 
					
						
							|  |  |  |     expect("/getitem") | 
					
						
							|  |  |  |     expect("/setdefault") | 
					
						
							|  |  |  |     expect("/vary-cookie-header-set") | 
					
						
							|  |  |  |     expect("/vary-header-set", "Accept-Encoding, Accept-Language, Cookie") | 
					
						
							|  |  |  |     expect("/no-vary-header", None) | 
					
						
							| 
									
										
										
										
											2017-05-20 00:44:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_flashes(app, req_ctx): | 
					
						
							|  |  |  |     assert not flask.session.modified | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     flask.flash("Zap") | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |     flask.session.modified = False | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     flask.flash("Zip") | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |     assert flask.session.modified | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert list(flask.get_flashed_messages()) == ["Zap", "Zip"] | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_extended_flashing(app): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     # Be sure app.testing=True below, else tests can fail silently. | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # Specifically, if app.testing is not set to True, the AssertionErrors | 
					
						
							|  |  |  |     # in the view functions will cause a 500 response to the test client | 
					
						
							|  |  |  |     # instead of propagating exceptions. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.flash(u"Hello World") | 
					
						
							|  |  |  |         flask.flash(u"Hello World", "error") | 
					
						
							|  |  |  |         flask.flash(flask.Markup(u"<em>Testing</em>"), "warning") | 
					
						
							|  |  |  |         return "" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/test/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def test(): | 
					
						
							|  |  |  |         messages = flask.get_flashed_messages() | 
					
						
							|  |  |  |         assert list(messages) == [ | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             u"Hello World", | 
					
						
							|  |  |  |             u"Hello World", | 
					
						
							|  |  |  |             flask.Markup(u"<em>Testing</em>"), | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |         ] | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/test_with_categories/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def test_with_categories(): | 
					
						
							|  |  |  |         messages = flask.get_flashed_messages(with_categories=True) | 
					
						
							|  |  |  |         assert len(messages) == 3 | 
					
						
							|  |  |  |         assert list(messages) == [ | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             ("message", u"Hello World"), | 
					
						
							|  |  |  |             ("error", u"Hello World"), | 
					
						
							|  |  |  |             ("warning", flask.Markup(u"<em>Testing</em>")), | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |         ] | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/test_filter/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def test_filter(): | 
					
						
							|  |  |  |         messages = flask.get_flashed_messages( | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             category_filter=["message"], with_categories=True | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         assert list(messages) == [("message", u"Hello World")] | 
					
						
							|  |  |  |         return "" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/test_filters/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def test_filters(): | 
					
						
							|  |  |  |         messages = flask.get_flashed_messages( | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             category_filter=["message", "warning"], with_categories=True | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |         assert list(messages) == [ | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             ("message", u"Hello World"), | 
					
						
							|  |  |  |             ("warning", flask.Markup(u"<em>Testing</em>")), | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |         ] | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/test_filters_without_returning_categories/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def test_filters2(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         messages = flask.get_flashed_messages(category_filter=["message", "warning"]) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |         assert len(messages) == 2 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         assert messages[0] == u"Hello World" | 
					
						
							|  |  |  |         assert messages[1] == flask.Markup(u"<em>Testing</em>") | 
					
						
							|  |  |  |         return "" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create new test client on each test to clean flashed messages. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |     client = app.test_client() | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     client.get("/") | 
					
						
							|  |  |  |     client.get("/test_with_categories/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |     client = app.test_client() | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     client.get("/") | 
					
						
							|  |  |  |     client.get("/test_filter/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |     client = app.test_client() | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     client.get("/") | 
					
						
							|  |  |  |     client.get("/test_filters/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |     client = app.test_client() | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     client.get("/") | 
					
						
							|  |  |  |     client.get("/test_filters_without_returning_categories/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_request_processing(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     evts = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.before_request | 
					
						
							|  |  |  |     def before_request(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         evts.append("before") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @app.after_request | 
					
						
							|  |  |  |     def after_request(response): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         response.data += b"|after" | 
					
						
							|  |  |  |         evts.append("after") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |         return response | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         assert "before" in evts | 
					
						
							|  |  |  |         assert "after" not in evts | 
					
						
							|  |  |  |         return "request" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert "after" not in evts | 
					
						
							|  |  |  |     rv = client.get("/").data | 
					
						
							|  |  |  |     assert "after" in evts | 
					
						
							|  |  |  |     assert rv == b"request|after" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_request_preprocessing_early_return(app, client): | 
					
						
							| 
									
										
										
										
											2015-02-06 05:13:14 +08:00
										 |  |  |     evts = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.before_request | 
					
						
							| 
									
										
										
										
											2015-02-07 05:11:23 +08:00
										 |  |  |     def before_request1(): | 
					
						
							|  |  |  |         evts.append(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.before_request | 
					
						
							|  |  |  |     def before_request2(): | 
					
						
							|  |  |  |         evts.append(2) | 
					
						
							| 
									
										
										
										
											2015-02-06 05:13:14 +08:00
										 |  |  |         return "hello" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-07 05:11:23 +08:00
										 |  |  |     @app.before_request | 
					
						
							|  |  |  |     def before_request3(): | 
					
						
							|  |  |  |         evts.append(3) | 
					
						
							|  |  |  |         return "bye" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2015-02-06 05:13:14 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         evts.append("index") | 
					
						
							| 
									
										
										
										
											2015-02-06 05:13:14 +08:00
										 |  |  |         return "damnit" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/").data.strip() | 
					
						
							|  |  |  |     assert rv == b"hello" | 
					
						
							| 
									
										
										
										
											2015-02-07 05:11:23 +08:00
										 |  |  |     assert evts == [1, 2] | 
					
						
							| 
									
										
										
										
											2015-02-06 05:13:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_after_request_processing(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							|  |  |  |         @flask.after_this_request | 
					
						
							|  |  |  |         def foo(response): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             response.headers["X-Foo"] = "a header" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |             return response | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "Test" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     resp = client.get("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert resp.status_code == 200 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert resp.headers["X-Foo"] == "a header" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_teardown_request_handler(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     called = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.teardown_request | 
					
						
							|  |  |  |     def teardown_request(exc): | 
					
						
							|  |  |  |         called.append(True) | 
					
						
							|  |  |  |         return "Ignored" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def root(): | 
					
						
							|  |  |  |         return "Response" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert rv.status_code == 200 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert b"Response" in rv.data | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert len(called) == 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_teardown_request_handler_debug_mode(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     called = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.teardown_request | 
					
						
							|  |  |  |     def teardown_request(exc): | 
					
						
							|  |  |  |         called.append(True) | 
					
						
							|  |  |  |         return "Ignored" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def root(): | 
					
						
							|  |  |  |         return "Response" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert rv.status_code == 200 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert b"Response" in rv.data | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert len(called) == 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_teardown_request_handler_error(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     called = [] | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |     app.testing = False | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @app.teardown_request | 
					
						
							|  |  |  |     def teardown_request1(exc): | 
					
						
							|  |  |  |         assert type(exc) == ZeroDivisionError | 
					
						
							|  |  |  |         called.append(True) | 
					
						
							|  |  |  |         # This raises a new error and blows away sys.exc_info(), so we can | 
					
						
							|  |  |  |         # test that all teardown_requests get passed the same original | 
					
						
							|  |  |  |         # exception. | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             raise TypeError() | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             pass | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     @app.teardown_request | 
					
						
							|  |  |  |     def teardown_request2(exc): | 
					
						
							|  |  |  |         assert type(exc) == ZeroDivisionError | 
					
						
							|  |  |  |         called.append(True) | 
					
						
							|  |  |  |         # This raises a new error and blows away sys.exc_info(), so we can | 
					
						
							|  |  |  |         # test that all teardown_requests get passed the same original | 
					
						
							|  |  |  |         # exception. | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             raise TypeError() | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             pass | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def fails(): | 
					
						
							|  |  |  |         1 // 0 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert rv.status_code == 500 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert b"Internal Server Error" in rv.data | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert len(called) == 2 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_before_after_request_order(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     called = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.before_request | 
					
						
							|  |  |  |     def before1(): | 
					
						
							|  |  |  |         called.append(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.before_request | 
					
						
							|  |  |  |     def before2(): | 
					
						
							|  |  |  |         called.append(2) | 
					
						
							| 
									
										
										
										
											2011-11-20 23:54:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     @app.after_request | 
					
						
							|  |  |  |     def after1(response): | 
					
						
							|  |  |  |         called.append(4) | 
					
						
							|  |  |  |         return response | 
					
						
							| 
									
										
										
										
											2011-11-20 23:54:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     @app.after_request | 
					
						
							|  |  |  |     def after2(response): | 
					
						
							|  |  |  |         called.append(3) | 
					
						
							|  |  |  |         return response | 
					
						
							| 
									
										
										
										
											2011-11-20 23:54:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     @app.teardown_request | 
					
						
							|  |  |  |     def finish1(exc): | 
					
						
							|  |  |  |         called.append(6) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.teardown_request | 
					
						
							|  |  |  |     def finish2(exc): | 
					
						
							|  |  |  |         called.append(5) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "42" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/") | 
					
						
							|  |  |  |     assert rv.data == b"42" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert called == [1, 2, 3, 4, 5, 6] | 
					
						
							| 
									
										
										
										
											2012-08-11 09:36:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_error_handling(app, client): | 
					
						
							|  |  |  |     app.testing = False | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @app.errorhandler(404) | 
					
						
							|  |  |  |     def not_found(e): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "not found", 404 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @app.errorhandler(500) | 
					
						
							|  |  |  |     def internal_server_error(e): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "internal server error", 500 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     @app.errorhandler(Forbidden) | 
					
						
							|  |  |  |     def forbidden(e): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "forbidden", 403 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							|  |  |  |         flask.abort(404) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/error") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def error(): | 
					
						
							|  |  |  |         1 // 0 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/forbidden") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def error2(): | 
					
						
							|  |  |  |         flask.abort(403) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert rv.status_code == 404 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert rv.data == b"not found" | 
					
						
							|  |  |  |     rv = client.get("/error") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert rv.status_code == 500 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert b"internal server error" == rv.data | 
					
						
							|  |  |  |     rv = client.get("/forbidden") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert rv.status_code == 403 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert b"forbidden" == rv.data | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-01 09:02:23 +08:00
										 |  |  | def test_error_handler_unknown_code(app): | 
					
						
							|  |  |  |     with pytest.raises(KeyError) as exc_info: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         app.register_error_handler(999, lambda e: ("999", 999)) | 
					
						
							| 
									
										
										
										
											2017-06-01 09:02:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert "Use a subclass" in exc_info.value.args[0] | 
					
						
							| 
									
										
										
										
											2017-06-01 09:02:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_error_handling_processing(app, client): | 
					
						
							|  |  |  |     app.testing = False | 
					
						
							| 
									
										
										
										
											2016-09-08 16:55:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @app.errorhandler(500) | 
					
						
							|  |  |  |     def internal_server_error(e): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "internal server error", 500 | 
					
						
							| 
									
										
										
										
											2016-09-08 16:55:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2016-09-08 16:55:59 +08:00
										 |  |  |     def broken_func(): | 
					
						
							|  |  |  |         1 // 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.after_request | 
					
						
							|  |  |  |     def after_request(resp): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         resp.mimetype = "text/x-special" | 
					
						
							| 
									
										
										
										
											2016-09-08 16:55:59 +08:00
										 |  |  |         return resp | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     resp = client.get("/") | 
					
						
							|  |  |  |     assert resp.mimetype == "text/x-special" | 
					
						
							|  |  |  |     assert resp.data == b"internal server error" | 
					
						
							| 
									
										
										
										
											2016-09-08 16:55:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_baseexception_error_handling(app, client): | 
					
						
							|  |  |  |     app.testing = False | 
					
						
							| 
									
										
										
										
											2017-03-24 00:15:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2017-03-24 00:15:00 +08:00
										 |  |  |     def broken_func(): | 
					
						
							|  |  |  |         raise KeyboardInterrupt() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |     with pytest.raises(KeyboardInterrupt): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         client.get("/") | 
					
						
							| 
									
										
										
										
											2012-01-18 08:33:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-24 00:15:00 +08:00
										 |  |  |         ctx = flask._request_ctx_stack.top | 
					
						
							|  |  |  |         assert ctx.preserved | 
					
						
							|  |  |  |         assert type(ctx._preserved_exc) is KeyboardInterrupt | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_before_request_and_routing_errors(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     @app.before_request | 
					
						
							|  |  |  |     def attach_something(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.g.something = "value" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @app.errorhandler(404) | 
					
						
							|  |  |  |     def return_something(error): | 
					
						
							|  |  |  |         return flask.g.something, 404 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert rv.status_code == 404 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert rv.data == b"value" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_user_error_handling(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     class MyException(Exception): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.errorhandler(MyException) | 
					
						
							|  |  |  |     def handle_my_exception(e): | 
					
						
							|  |  |  |         assert isinstance(e, MyException) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "42" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							|  |  |  |         raise MyException() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert client.get("/").data == b"42" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_http_error_subclass_handling(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     class ForbiddenSubclass(Forbidden): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.errorhandler(ForbiddenSubclass) | 
					
						
							|  |  |  |     def handle_forbidden_subclass(e): | 
					
						
							|  |  |  |         assert isinstance(e, ForbiddenSubclass) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "banana" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @app.errorhandler(403) | 
					
						
							|  |  |  |     def handle_forbidden_subclass(e): | 
					
						
							|  |  |  |         assert not isinstance(e, ForbiddenSubclass) | 
					
						
							|  |  |  |         assert isinstance(e, Forbidden) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "apple" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/1") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index1(): | 
					
						
							|  |  |  |         raise ForbiddenSubclass() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/2") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index2(): | 
					
						
							|  |  |  |         flask.abort(403) | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/3") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index3(): | 
					
						
							|  |  |  |         raise Forbidden() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert client.get("/1").data == b"banana" | 
					
						
							|  |  |  |     assert client.get("/2").data == b"apple" | 
					
						
							|  |  |  |     assert client.get("/3").data == b"apple" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-05 21:14:13 +08:00
										 |  |  | def test_errorhandler_precedence(app, client): | 
					
						
							|  |  |  |     class E1(Exception): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class E2(Exception): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class E3(E1, E2): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.errorhandler(E2) | 
					
						
							|  |  |  |     def handle_e2(e): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "E2" | 
					
						
							| 
									
										
										
										
											2017-06-05 21:14:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @app.errorhandler(Exception) | 
					
						
							|  |  |  |     def handle_exception(e): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "Exception" | 
					
						
							| 
									
										
										
										
											2017-06-05 21:14:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/E1") | 
					
						
							| 
									
										
										
										
											2017-06-05 21:14:13 +08:00
										 |  |  |     def raise_e1(): | 
					
						
							|  |  |  |         raise E1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/E3") | 
					
						
							| 
									
										
										
										
											2017-06-05 21:14:13 +08:00
										 |  |  |     def raise_e3(): | 
					
						
							|  |  |  |         raise E3 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/E1") | 
					
						
							|  |  |  |     assert rv.data == b"Exception" | 
					
						
							| 
									
										
										
										
											2017-06-05 21:14:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/E3") | 
					
						
							|  |  |  |     assert rv.data == b"E2" | 
					
						
							| 
									
										
										
										
											2017-06-05 21:14:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_trapping_of_bad_request_key_errors(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/key") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def fail(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.request.form["missing_key"] | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/abort") | 
					
						
							| 
									
										
										
										
											2018-04-28 21:51:08 +08:00
										 |  |  |     def allow_abort(): | 
					
						
							|  |  |  |         flask.abort(400) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/key") | 
					
						
							| 
									
										
										
										
											2017-05-30 10:41:07 +08:00
										 |  |  |     assert rv.status_code == 400 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert b"missing_key" not in rv.data | 
					
						
							|  |  |  |     rv = client.get("/abort") | 
					
						
							| 
									
										
										
										
											2018-04-28 21:51:08 +08:00
										 |  |  |     assert rv.status_code == 400 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-28 21:51:08 +08:00
										 |  |  |     app.debug = True | 
					
						
							| 
									
										
										
										
											2016-03-04 19:30:40 +08:00
										 |  |  |     with pytest.raises(KeyError) as e: | 
					
						
							| 
									
										
										
										
											2018-04-28 21:51:08 +08:00
										 |  |  |         client.get("/key") | 
					
						
							| 
									
										
										
										
											2016-03-04 19:30:40 +08:00
										 |  |  |     assert e.errisinstance(BadRequest) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert "missing_key" in e.value.get_description() | 
					
						
							|  |  |  |     rv = client.get("/abort") | 
					
						
							| 
									
										
										
										
											2018-04-28 21:51:08 +08:00
										 |  |  |     assert rv.status_code == 400 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     app.debug = False | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config["TRAP_BAD_REQUEST_ERRORS"] = True | 
					
						
							| 
									
										
										
										
											2018-04-28 21:51:08 +08:00
										 |  |  |     with pytest.raises(KeyError): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         client.get("/key") | 
					
						
							| 
									
										
										
										
											2018-04-28 21:51:08 +08:00
										 |  |  |     with pytest.raises(BadRequest): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         client.get("/abort") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_trapping_of_all_http_exceptions(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config["TRAP_HTTP_EXCEPTIONS"] = True | 
					
						
							| 
									
										
										
										
											2012-01-17 11:44:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/fail") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def fail(): | 
					
						
							|  |  |  |         flask.abort(404) | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     with pytest.raises(NotFound): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         client.get("/fail") | 
					
						
							| 
									
										
										
										
											2012-01-17 11:22:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-29 05:55:52 +08:00
										 |  |  | def test_error_handler_after_processor_error(app, client): | 
					
						
							|  |  |  |     app.testing = False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.before_request | 
					
						
							|  |  |  |     def before_request(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         if trigger == "before": | 
					
						
							| 
									
										
										
										
											2017-07-29 05:55:52 +08:00
										 |  |  |             1 // 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.after_request | 
					
						
							|  |  |  |     def after_request(response): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         if trigger == "after": | 
					
						
							| 
									
										
										
										
											2017-07-29 05:55:52 +08:00
										 |  |  |             1 // 0 | 
					
						
							|  |  |  |         return response | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2017-07-29 05:55:52 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "Foo" | 
					
						
							| 
									
										
										
										
											2017-07-29 05:55:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @app.errorhandler(500) | 
					
						
							|  |  |  |     def internal_server_error(e): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "Hello Server Error", 500 | 
					
						
							| 
									
										
										
										
											2017-07-29 05:55:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     for trigger in "before", "after": | 
					
						
							|  |  |  |         rv = client.get("/") | 
					
						
							| 
									
										
										
										
											2017-07-29 05:55:52 +08:00
										 |  |  |         assert rv.status_code == 500 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         assert rv.data == b"Hello Server Error" | 
					
						
							| 
									
										
										
										
											2017-07-29 05:55:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_enctype_debug_helper(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     from flask.debughelpers import DebugFilesKeyError | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     app.debug = True | 
					
						
							| 
									
										
										
										
											2012-01-17 11:22:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/fail", methods=["POST"]) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return flask.request.files["foo"].filename | 
					
						
							| 
									
										
										
										
											2012-01-17 11:22:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     # with statement is important because we leave an exception on the | 
					
						
							|  |  |  |     # stack otherwise and we want to ensure that this is not the case | 
					
						
							|  |  |  |     # to not negatively affect other tests. | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |     with client: | 
					
						
							| 
									
										
										
										
											2016-03-04 19:30:40 +08:00
										 |  |  |         with pytest.raises(DebugFilesKeyError) as e: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             client.post("/fail", data={"foo": "index.txt"}) | 
					
						
							|  |  |  |         assert "no file contents were transmitted" in str(e.value) | 
					
						
							| 
									
										
										
										
											2016-03-04 19:30:40 +08:00
										 |  |  |         assert 'This was submitted: "index.txt"' in str(e.value) | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-08 20:14:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_response_types(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/text") | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  |     def from_text(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return u"Hällo Wörld" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/bytes") | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  |     def from_bytes(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return u"Hällo Wörld".encode("utf-8") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/full_tuple") | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  |     def from_full_tuple(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return ( | 
					
						
							|  |  |  |             "Meh", | 
					
						
							|  |  |  |             400, | 
					
						
							|  |  |  |             {"X-Foo": "Testing", "Content-Type": "text/plain; charset=utf-8"}, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/text_headers") | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  |     def from_text_headers(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "Hello", {"X-Foo": "Test", "Content-Type": "text/plain; charset=utf-8"} | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/text_status") | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  |     def from_text_status(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "Hi, status!", 400 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/response_headers") | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  |     def from_response_headers(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return ( | 
					
						
							|  |  |  |             flask.Response("Hello world", 404, {"X-Foo": "Baz"}), | 
					
						
							|  |  |  |             {"X-Foo": "Bar", "X-Bar": "Foo"}, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/response_status") | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  |     def from_response_status(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return app.response_class("Hello world", 400), 500 | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/wsgi") | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  |     def from_wsgi(): | 
					
						
							|  |  |  |         return NotFound() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 01:34:29 +08:00
										 |  |  |     @app.route('/dict') | 
					
						
							|  |  |  |     def from_dict(): | 
					
						
							|  |  |  |         return {"foo": "bar"}, 201 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert client.get('/text').data == u'Hällo Wörld'.encode('utf-8') | 
					
						
							|  |  |  |     assert client.get('/bytes').data == u'Hällo Wörld'.encode('utf-8') | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/full_tuple") | 
					
						
							|  |  |  |     assert rv.data == b"Meh" | 
					
						
							|  |  |  |     assert rv.headers["X-Foo"] == "Testing" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert rv.status_code == 400 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert rv.mimetype == "text/plain" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/text_headers") | 
					
						
							|  |  |  |     assert rv.data == b"Hello" | 
					
						
							|  |  |  |     assert rv.headers["X-Foo"] == "Test" | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  |     assert rv.status_code == 200 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert rv.mimetype == "text/plain" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/text_status") | 
					
						
							|  |  |  |     assert rv.data == b"Hi, status!" | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  |     assert rv.status_code == 400 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     rv = client.get("/response_headers") | 
					
						
							|  |  |  |     assert rv.data == b"Hello world" | 
					
						
							|  |  |  |     assert rv.headers.getlist("X-Foo") == ["Baz", "Bar"] | 
					
						
							|  |  |  |     assert rv.headers["X-Bar"] == "Foo" | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  |     assert rv.status_code == 404 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/response_status") | 
					
						
							|  |  |  |     assert rv.data == b"Hello world" | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  |     assert rv.status_code == 500 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/wsgi") | 
					
						
							|  |  |  |     assert b"Not Found" in rv.data | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  |     assert rv.status_code == 404 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 01:34:29 +08:00
										 |  |  |     rv = client.get('/dict') | 
					
						
							|  |  |  |     assert rv.json == {"foo": "bar"} | 
					
						
							|  |  |  |     assert rv.status_code == 201 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_response_type_errors(): | 
					
						
							| 
									
										
										
										
											2014-10-14 04:30:45 +08:00
										 |  |  |     app = flask.Flask(__name__) | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  |     app.testing = True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/none") | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  |     def from_none(): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/small_tuple") | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  |     def from_small_tuple(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return ("Hello",) | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/large_tuple") | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  |     def from_large_tuple(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "Hello", 234, {"X-Foo": "Bar"}, "???" | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/bad_type") | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  |     def from_bad_type(): | 
					
						
							|  |  |  |         return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/bad_wsgi") | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  |     def from_bad_wsgi(): | 
					
						
							|  |  |  |         return lambda: None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     c = app.test_client() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(TypeError) as e: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         c.get("/none") | 
					
						
							|  |  |  |         assert "returned None" in str(e) | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(TypeError) as e: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         c.get("/small_tuple") | 
					
						
							|  |  |  |         assert "tuple must have the form" in str(e) | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     pytest.raises(TypeError, c.get, "/large_tuple") | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(TypeError) as e: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         c.get("/bad_type") | 
					
						
							|  |  |  |         assert "it was a bool" in str(e) | 
					
						
							| 
									
										
										
										
											2017-04-25 05:11:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     pytest.raises(TypeError, c.get, "/bad_wsgi") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_make_response(app, req_ctx): | 
					
						
							|  |  |  |     rv = flask.make_response() | 
					
						
							|  |  |  |     assert rv.status_code == 200 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert rv.data == b"" | 
					
						
							|  |  |  |     assert rv.mimetype == "text/html" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = flask.make_response("Awesome") | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |     assert rv.status_code == 200 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert rv.data == b"Awesome" | 
					
						
							|  |  |  |     assert rv.mimetype == "text/html" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = flask.make_response("W00t", 404) | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |     assert rv.status_code == 404 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert rv.data == b"W00t" | 
					
						
							|  |  |  |     assert rv.mimetype == "text/html" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_make_response_with_response_instance(app, req_ctx): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = flask.make_response(flask.jsonify({"msg": "W00t"}), 400) | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |     assert rv.status_code == 400 | 
					
						
							|  |  |  |     assert rv.data == b'{"msg":"W00t"}\n' | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert rv.mimetype == "application/json" | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = flask.make_response(flask.Response(""), 400) | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |     assert rv.status_code == 400 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert rv.data == b"" | 
					
						
							|  |  |  |     assert rv.mimetype == "text/html" | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     rv = flask.make_response( | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.Response("", headers={"Content-Type": "text/html"}), | 
					
						
							|  |  |  |         400, | 
					
						
							|  |  |  |         [("X-Foo", "bar")], | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |     assert rv.status_code == 400 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert rv.headers["Content-Type"] == "text/html" | 
					
						
							|  |  |  |     assert rv.headers["X-Foo"] == "bar" | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_jsonify_no_prettyprint(app, req_ctx): | 
					
						
							| 
									
										
										
										
											2014-10-14 04:30:45 +08:00
										 |  |  |     app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": False}) | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |     compressed_msg = b'{"msg":{"submsg":"W00t"},"msg2":"foobar"}\n' | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     uncompressed_msg = {"msg": {"submsg": "W00t"}, "msg2": "foobar"} | 
					
						
							| 
									
										
										
										
											2014-10-14 04:30:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = flask.make_response(flask.jsonify(uncompressed_msg), 200) | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |     assert rv.data == compressed_msg | 
					
						
							| 
									
										
										
										
											2014-10-14 04:30:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-22 01:11:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_jsonify_prettyprint(app, req_ctx): | 
					
						
							| 
									
										
										
										
											2014-10-14 04:30:45 +08:00
										 |  |  |     app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": True}) | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |     compressed_msg = {"msg": {"submsg": "W00t"}, "msg2": "foobar"} | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     pretty_response = ( | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |         b'{\n  "msg": {\n    "submsg": "W00t"\n  }, \n  "msg2": "foobar"\n}\n' | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-10-14 04:30:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = flask.make_response(flask.jsonify(compressed_msg), 200) | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |     assert rv.data == pretty_response | 
					
						
							| 
									
										
										
										
											2014-10-14 04:30:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-22 01:11:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_jsonify_mimetype(app, req_ctx): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config.update({"JSONIFY_MIMETYPE": "application/vnd.api+json"}) | 
					
						
							|  |  |  |     msg = {"msg": {"submsg": "W00t"}} | 
					
						
							|  |  |  |     rv = flask.make_response(flask.jsonify(msg), 200) | 
					
						
							|  |  |  |     assert rv.mimetype == "application/vnd.api+json" | 
					
						
							| 
									
										
										
										
											2016-04-09 06:30:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-13 16:29:38 +08:00
										 |  |  | @pytest.mark.skipif(sys.version_info < (3, 7), reason="requires Python >= 3.7") | 
					
						
							|  |  |  | def test_json_dump_dataclass(app, req_ctx): | 
					
						
							|  |  |  |     from dataclasses import make_dataclass | 
					
						
							|  |  |  |     Data = make_dataclass("Data", [("name", str)]) | 
					
						
							|  |  |  |     value = flask.json.dumps(Data("Flask"), app=app) | 
					
						
							|  |  |  |     value = flask.json.loads(value, app=app) | 
					
						
							|  |  |  |     assert value == {"name": "Flask"} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_jsonify_args_and_kwargs_check(app, req_ctx): | 
					
						
							|  |  |  |     with pytest.raises(TypeError) as e: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.jsonify("fake args", kwargs="fake") | 
					
						
							|  |  |  |     assert "behavior undefined" in str(e.value) | 
					
						
							| 
									
										
										
										
											2016-06-04 01:58:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_url_generation(app, req_ctx): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/hello/<name>", methods=["POST"]) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def hello(): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert flask.url_for("hello", name="test x") == "/hello/test%20x" | 
					
						
							|  |  |  |     assert ( | 
					
						
							|  |  |  |         flask.url_for("hello", name="test x", _external=True) | 
					
						
							|  |  |  |         == "http://localhost/hello/test%20x" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_build_error_handler(app): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     # Test base case, a URL which results in a BuildError. | 
					
						
							|  |  |  |     with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         pytest.raises(BuildError, flask.url_for, "spam") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Verify the error is re-raised if not the current exception. | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             flask.url_for("spam") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     except BuildError as err: | 
					
						
							|  |  |  |         error = err | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         raise RuntimeError("Test case where BuildError is not current.") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     except RuntimeError: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         pytest.raises(BuildError, app.handle_url_build_error, error, "spam", {}) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Test a custom handler. | 
					
						
							|  |  |  |     def handler(error, endpoint, values): | 
					
						
							|  |  |  |         # Just a test. | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "/test_handler/" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     app.url_build_error_handlers.append(handler) | 
					
						
							|  |  |  |     with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         assert flask.url_for("spam") == "/test_handler/" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_build_error_handler_reraise(app): | 
					
						
							| 
									
										
										
										
											2015-07-16 18:05:07 +08:00
										 |  |  |     # Test a custom handler which reraises the BuildError | 
					
						
							|  |  |  |     def handler_raises_build_error(error, endpoint, values): | 
					
						
							|  |  |  |         raise error | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-16 18:05:07 +08:00
										 |  |  |     app.url_build_error_handlers.append(handler_raises_build_error) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         pytest.raises(BuildError, flask.url_for, "not.existing") | 
					
						
							| 
									
										
										
										
											2015-07-16 18:05:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_url_for_passes_special_values_to_build_error_handler(app): | 
					
						
							| 
									
										
										
										
											2017-04-20 23:52:37 +08:00
										 |  |  |     @app.url_build_error_handlers.append | 
					
						
							|  |  |  |     def handler(error, endpoint, values): | 
					
						
							|  |  |  |         assert values == { | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             "_external": False, | 
					
						
							|  |  |  |             "_anchor": None, | 
					
						
							|  |  |  |             "_method": None, | 
					
						
							|  |  |  |             "_scheme": None, | 
					
						
							| 
									
										
										
										
											2017-04-20 23:52:37 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "handled" | 
					
						
							| 
									
										
										
										
											2017-04-20 23:52:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.url_for("/") | 
					
						
							| 
									
										
										
										
											2017-04-20 23:52:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_custom_converters(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     from werkzeug.routing import BaseConverter | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class ListConverter(BaseConverter): | 
					
						
							|  |  |  |         def to_python(self, value): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             return value.split(",") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def to_url(self, value): | 
					
						
							|  |  |  |             base_to_url = super(ListConverter, self).to_url | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             return ",".join(base_to_url(x) for x in value) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.url_map.converters["list"] = ListConverter | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/<list:args>") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(args): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "|".join(args) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert client.get("/1,2,3").data == b"1|2|3" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_static_files(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/static/index.html") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert rv.status_code == 200 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert rv.data.strip() == b"<h1>Hello World!</h1>" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         assert flask.url_for("static", filename="index.html") == "/static/index.html" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     rv.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-03 08:56:08 +08:00
										 |  |  | def test_static_url_path(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app = flask.Flask(__name__, static_url_path="/foo") | 
					
						
							| 
									
										
										
										
											2016-06-03 08:56:08 +08:00
										 |  |  |     app.testing = True | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = app.test_client().get("/foo/index.html") | 
					
						
							| 
									
										
										
										
											2016-06-03 08:56:08 +08:00
										 |  |  |     assert rv.status_code == 200 | 
					
						
							| 
									
										
										
										
											2016-06-03 20:19:25 +08:00
										 |  |  |     rv.close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-03 08:56:08 +08:00
										 |  |  |     with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         assert flask.url_for("static", filename="index.html") == "/foo/index.html" | 
					
						
							| 
									
										
										
										
											2016-06-03 08:56:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:13:09 +08:00
										 |  |  | def test_static_url_path_with_ending_slash(): | 
					
						
							|  |  |  |     app = flask.Flask(__name__, static_url_path="/foo/") | 
					
						
							|  |  |  |     app.testing = True | 
					
						
							|  |  |  |     rv = app.test_client().get("/foo/index.html") | 
					
						
							|  |  |  |     assert rv.status_code == 200 | 
					
						
							|  |  |  |     rv.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with app.test_request_context(): | 
					
						
							|  |  |  |         assert flask.url_for("static", filename="index.html") == "/foo/index.html" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-07 22:31:54 +08:00
										 |  |  | def test_static_route_with_host_matching(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app = flask.Flask(__name__, host_matching=True, static_host="example.com") | 
					
						
							| 
									
										
										
										
											2017-04-07 22:31:54 +08:00
										 |  |  |     c = app.test_client() | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = c.get("http://example.com/static/index.html") | 
					
						
							| 
									
										
										
										
											2017-04-07 22:31:54 +08:00
										 |  |  |     assert rv.status_code == 200 | 
					
						
							|  |  |  |     rv.close() | 
					
						
							|  |  |  |     with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = flask.url_for("static", filename="index.html", _external=True) | 
					
						
							|  |  |  |         assert rv == "http://example.com/static/index.html" | 
					
						
							| 
									
										
										
										
											2017-04-07 22:31:54 +08:00
										 |  |  |     # Providing static_host without host_matching=True should error. | 
					
						
							|  |  |  |     with pytest.raises(Exception): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.Flask(__name__, static_host="example.com") | 
					
						
							| 
									
										
										
										
											2017-04-07 22:31:54 +08:00
										 |  |  |     # Providing host_matching=True with static_folder but without static_host should error. | 
					
						
							|  |  |  |     with pytest.raises(Exception): | 
					
						
							|  |  |  |         flask.Flask(__name__, host_matching=True) | 
					
						
							|  |  |  |     # Providing host_matching=True without static_host but with static_folder=None should not error. | 
					
						
							|  |  |  |     flask.Flask(__name__, host_matching=True, static_folder=None) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_request_locals(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert repr(flask.g) == "<LocalProxy unbound>" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert not flask.g | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-20 04:20:03 +08:00
										 |  |  | def test_test_app_proper_environ(): | 
					
						
							|  |  |  |     app = flask.Flask(__name__, subdomain_matching=True) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config.update(SERVER_NAME="localhost.localdomain:5000") | 
					
						
							| 
									
										
										
										
											2018-02-20 04:20:03 +08:00
										 |  |  |     client = app.test_client() | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "Foo" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/", subdomain="foo") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def subdomain(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "Foo SubDomain" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/") | 
					
						
							|  |  |  |     assert rv.data == b"Foo" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/", "http://localhost.localdomain:5000") | 
					
						
							|  |  |  |     assert rv.data == b"Foo" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/", "https://localhost.localdomain:5000") | 
					
						
							|  |  |  |     assert rv.data == b"Foo" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config.update(SERVER_NAME="localhost.localdomain") | 
					
						
							|  |  |  |     rv = client.get("/", "https://localhost.localdomain") | 
					
						
							|  |  |  |     assert rv.data == b"Foo" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         app.config.update(SERVER_NAME="localhost.localdomain:443") | 
					
						
							|  |  |  |         rv = client.get("/", "https://localhost.localdomain") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |         # Werkzeug 0.8 | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |         assert rv.status_code == 404 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     except ValueError as e: | 
					
						
							|  |  |  |         # Werkzeug 0.7 | 
					
						
							|  |  |  |         assert str(e) == ( | 
					
						
							|  |  |  |             "the server name provided " | 
					
						
							|  |  |  |             "('localhost.localdomain:443') does not match the " | 
					
						
							|  |  |  |             "server name from the WSGI environment ('localhost.localdomain')" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         app.config.update(SERVER_NAME="localhost.localdomain") | 
					
						
							|  |  |  |         rv = client.get("/", "http://foo.localhost") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |         # Werkzeug 0.8 | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |         assert rv.status_code == 404 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     except ValueError as e: | 
					
						
							|  |  |  |         # Werkzeug 0.7 | 
					
						
							|  |  |  |         assert str(e) == ( | 
					
						
							|  |  |  |             "the server name provided " | 
					
						
							|  |  |  |             "('localhost.localdomain') does not match the " | 
					
						
							|  |  |  |             "server name from the WSGI environment ('foo.localhost')" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/", "http://foo.localhost.localdomain") | 
					
						
							|  |  |  |     assert rv.data == b"Foo SubDomain" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_exception_propagation(app, client): | 
					
						
							| 
									
										
										
										
											2015-03-28 00:30:34 +08:00
										 |  |  |     def apprunner(config_key): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         @app.route("/") | 
					
						
							| 
									
										
										
										
											2015-03-30 05:03:38 +08:00
										 |  |  |         def index(): | 
					
						
							|  |  |  |             1 // 0 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 05:03:38 +08:00
										 |  |  |         if config_key is not None: | 
					
						
							|  |  |  |             app.config[config_key] = True | 
					
						
							| 
									
										
										
										
											2016-03-04 19:30:40 +08:00
										 |  |  |             with pytest.raises(Exception): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |                 client.get("/") | 
					
						
							| 
									
										
										
										
											2015-03-30 05:03:38 +08:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             assert client.get("/").status_code == 500 | 
					
						
							| 
									
										
										
										
											2015-03-29 19:40:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 05:03:38 +08:00
										 |  |  |     # we have to run this test in an isolated thread because if the | 
					
						
							|  |  |  |     # debug flag is set to true and an exception happens the context is | 
					
						
							|  |  |  |     # not torn down.  This causes other tests that run after this fail | 
					
						
							| 
									
										
										
										
											2015-03-29 19:40:35 +08:00
										 |  |  |     # when they expect no exception on the stack. | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     for config_key in "TESTING", "PROPAGATE_EXCEPTIONS", "DEBUG", None: | 
					
						
							| 
									
										
										
										
											2015-03-30 05:03:38 +08:00
										 |  |  |         t = Thread(target=apprunner, args=(config_key,)) | 
					
						
							|  |  |  |         t.start() | 
					
						
							|  |  |  |         t.join() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-18 06:40:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  | @pytest.mark.parametrize("debug", [True, False]) | 
					
						
							|  |  |  | @pytest.mark.parametrize("use_debugger", [True, False]) | 
					
						
							|  |  |  | @pytest.mark.parametrize("use_reloader", [True, False]) | 
					
						
							|  |  |  | @pytest.mark.parametrize("propagate_exceptions", [None, True, False]) | 
					
						
							|  |  |  | def test_werkzeug_passthrough_errors( | 
					
						
							|  |  |  |     monkeypatch, debug, use_debugger, use_reloader, propagate_exceptions, app | 
					
						
							|  |  |  | ): | 
					
						
							| 
									
										
										
										
											2016-01-03 02:56:02 +08:00
										 |  |  |     rv = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Mocks werkzeug.serving.run_simple method | 
					
						
							|  |  |  |     def run_simple_mock(*args, **kwargs): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv["passthrough_errors"] = kwargs.get("passthrough_errors") | 
					
						
							| 
									
										
										
										
											2016-01-03 02:56:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     monkeypatch.setattr(werkzeug.serving, "run_simple", run_simple_mock) | 
					
						
							|  |  |  |     app.config["PROPAGATE_EXCEPTIONS"] = propagate_exceptions | 
					
						
							| 
									
										
										
										
											2016-01-03 02:56:02 +08:00
										 |  |  |     app.run(debug=debug, use_debugger=use_debugger, use_reloader=use_reloader) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_max_content_length(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config["MAX_CONTENT_LENGTH"] = 64 | 
					
						
							| 
									
										
										
										
											2013-08-18 06:40:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     @app.before_request | 
					
						
							|  |  |  |     def always_first(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.request.form["myfile"] | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |         assert False | 
					
						
							| 
									
										
										
										
											2013-08-18 06:40:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/accept", methods=["POST"]) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def accept_file(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.request.form["myfile"] | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |         assert False | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     @app.errorhandler(413) | 
					
						
							|  |  |  |     def catcher(error): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "42" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.post("/accept", data={"myfile": "foo" * 100}) | 
					
						
							|  |  |  |     assert rv.data == b"42" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_url_processors(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     @app.url_defaults | 
					
						
							|  |  |  |     def add_language_code(endpoint, values): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         if flask.g.lang_code is not None and app.url_map.is_endpoint_expecting( | 
					
						
							|  |  |  |             endpoint, "lang_code" | 
					
						
							|  |  |  |         ): | 
					
						
							|  |  |  |             values.setdefault("lang_code", flask.g.lang_code) | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     @app.url_value_preprocessor | 
					
						
							|  |  |  |     def pull_lang_code(endpoint, values): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.g.lang_code = values.pop("lang_code", None) | 
					
						
							| 
									
										
										
										
											2014-01-01 05:16:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/<lang_code>/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return flask.url_for("about") | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/<lang_code>/about") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def about(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return flask.url_for("something_else") | 
					
						
							| 
									
										
										
										
											2012-04-23 00:51:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/foo") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def something_else(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return flask.url_for("about", lang_code="en") | 
					
						
							| 
									
										
										
										
											2012-04-23 00:51:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert client.get("/de/").data == b"/de/about" | 
					
						
							|  |  |  |     assert client.get("/de/about").data == b"/foo" | 
					
						
							|  |  |  |     assert client.get("/foo").data == b"/en/about" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_inject_blueprint_url_defaults(app): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     bp = flask.Blueprint("foo.bar.baz", __name__, template_folder="template") | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     @bp.url_defaults | 
					
						
							|  |  |  |     def bp_defaults(endpoint, values): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         values["page"] = "login" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @bp.route("/<page>") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def view(page): | 
					
						
							|  |  |  |         pass | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     app.register_blueprint(bp) | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     values = dict() | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.inject_url_defaults("foo.bar.baz.view", values) | 
					
						
							|  |  |  |     expected = dict(page="login") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert values == expected | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     with app.test_request_context("/somepage"): | 
					
						
							|  |  |  |         url = flask.url_for("foo.bar.baz.view") | 
					
						
							|  |  |  |     expected = "/login" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert url == expected | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_nonascii_pathinfo(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route(u"/киртест") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "Hello World!" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get(u"/киртест") | 
					
						
							|  |  |  |     assert rv.data == b"Hello World!" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_debug_mode_complains_after_first_request(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     app.debug = True | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "Awesome" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert not app.got_first_request | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert client.get("/").data == b"Awesome" | 
					
						
							| 
									
										
										
										
											2016-03-04 19:30:40 +08:00
										 |  |  |     with pytest.raises(AssertionError) as e: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         @app.route("/foo") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |         def broken(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             return "Meh" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert "A setup function was called" in str(e) | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     app.debug = False | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/foo") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def working(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "Meh" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert client.get("/foo").data == b"Meh" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert app.got_first_request | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_before_first_request_functions(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     got = [] | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     @app.before_first_request | 
					
						
							|  |  |  |     def foo(): | 
					
						
							|  |  |  |         got.append(42) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     client.get("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert got == [42] | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     client.get("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert got == [42] | 
					
						
							|  |  |  |     assert app.got_first_request | 
					
						
							| 
									
										
										
										
											2013-05-22 23:14:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-24 16:32:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_before_first_request_functions_concurrent(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     got = [] | 
					
						
							| 
									
										
										
										
											2012-04-24 16:32:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     @app.before_first_request | 
					
						
							|  |  |  |     def foo(): | 
					
						
							|  |  |  |         time.sleep(0.2) | 
					
						
							|  |  |  |         got.append(42) | 
					
						
							| 
									
										
										
										
											2012-04-24 16:32:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def get_and_assert(): | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |         client.get("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |         assert got == [42] | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     t = Thread(target=get_and_assert) | 
					
						
							|  |  |  |     t.start() | 
					
						
							|  |  |  |     get_and_assert() | 
					
						
							|  |  |  |     t.join() | 
					
						
							|  |  |  |     assert app.got_first_request | 
					
						
							| 
									
										
										
										
											2013-06-03 06:24:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_routing_redirect_debugging(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     app.debug = True | 
					
						
							| 
									
										
										
										
											2013-06-03 06:24:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/foo/", methods=["GET", "POST"]) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def foo(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "success" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |     with client: | 
					
						
							| 
									
										
										
										
											2016-03-04 19:30:40 +08:00
										 |  |  |         with pytest.raises(AssertionError) as e: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             client.post("/foo", data={}) | 
					
						
							|  |  |  |         assert "http://localhost/foo/" in str(e) | 
					
						
							|  |  |  |         assert ("Make sure to directly send " "your POST-request to this URL") in str(e) | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = client.get("/foo", data={}, follow_redirects=True) | 
					
						
							|  |  |  |         assert rv.data == b"success" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     app.debug = False | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  |     with client: | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv = client.post("/foo", data={}, follow_redirects=True) | 
					
						
							|  |  |  |         assert rv.data == b"success" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-10 00:07:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_route_decorator_custom_endpoint(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     app.debug = True | 
					
						
							| 
									
										
										
										
											2014-02-10 00:07:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/foo/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def foo(): | 
					
						
							|  |  |  |         return flask.request.endpoint | 
					
						
							| 
									
										
										
										
											2014-02-10 00:07:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/bar/", endpoint="bar") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def for_bar(): | 
					
						
							|  |  |  |         return flask.request.endpoint | 
					
						
							| 
									
										
										
										
											2013-10-04 17:09:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/bar/123", endpoint="123") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def for_bar_foo(): | 
					
						
							|  |  |  |         return flask.request.endpoint | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         assert flask.url_for("foo") == "/foo/" | 
					
						
							|  |  |  |         assert flask.url_for("bar") == "/bar/" | 
					
						
							|  |  |  |         assert flask.url_for("123") == "/bar/123" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert client.get("/foo/").data == b"foo" | 
					
						
							|  |  |  |     assert client.get("/bar/").data == b"bar" | 
					
						
							|  |  |  |     assert client.get("/bar/123").data == b"123" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-31 17:32:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 08:27:36 +08:00
										 |  |  | def test_preserve_only_once(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     app.debug = True | 
					
						
							| 
									
										
										
										
											2011-08-31 17:32:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/fail") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def fail_func(): | 
					
						
							|  |  |  |         1 // 0 | 
					
						
							| 
									
										
										
										
											2011-08-31 17:32:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     for x in range(3): | 
					
						
							|  |  |  |         with pytest.raises(ZeroDivisionError): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             client.get("/fail") | 
					
						
							| 
									
										
										
										
											2011-08-31 17:32:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert flask._request_ctx_stack.top is not None | 
					
						
							|  |  |  |     assert flask._app_ctx_stack.top is not None | 
					
						
							|  |  |  |     # implicit appctx disappears too | 
					
						
							|  |  |  |     flask._request_ctx_stack.top.pop() | 
					
						
							|  |  |  |     assert flask._request_ctx_stack.top is None | 
					
						
							|  |  |  |     assert flask._app_ctx_stack.top is None | 
					
						
							| 
									
										
										
										
											2011-08-31 17:32:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_preserve_remembers_exception(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     app.debug = True | 
					
						
							|  |  |  |     errors = [] | 
					
						
							| 
									
										
										
										
											2011-09-25 02:27:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/fail") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def fail_func(): | 
					
						
							|  |  |  |         1 // 0 | 
					
						
							| 
									
										
										
										
											2011-09-25 02:27:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/success") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def success_func(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "Okay" | 
					
						
							| 
									
										
										
										
											2013-06-03 04:47:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     @app.teardown_request | 
					
						
							|  |  |  |     def teardown_handler(exc): | 
					
						
							|  |  |  |         errors.append(exc) | 
					
						
							| 
									
										
										
										
											2013-06-03 04:47:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     # After this failure we did not yet call the teardown handler | 
					
						
							|  |  |  |     with pytest.raises(ZeroDivisionError): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         client.get("/fail") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert errors == [] | 
					
						
							| 
									
										
										
										
											2013-06-03 04:47:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     # But this request triggers it, and it's an error | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     client.get("/success") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert len(errors) == 2 | 
					
						
							|  |  |  |     assert isinstance(errors[0], ZeroDivisionError) | 
					
						
							| 
									
										
										
										
											2013-06-03 04:47:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     # At this point another request does nothing. | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     client.get("/success") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     assert len(errors) == 3 | 
					
						
							|  |  |  |     assert errors[1] is None | 
					
						
							| 
									
										
										
										
											2013-06-03 04:47:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_get_method_on_g(app_ctx): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert flask.g.get("x") is None | 
					
						
							|  |  |  |     assert flask.g.get("x", 11) == 11 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |     flask.g.x = 42 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert flask.g.get("x") == 42 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |     assert flask.g.x == 42 | 
					
						
							| 
									
										
										
										
											2013-06-09 19:06:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_g_iteration_protocol(app_ctx): | 
					
						
							|  |  |  |     flask.g.foo = 23 | 
					
						
							|  |  |  |     flask.g.bar = 42 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert "foo" in flask.g | 
					
						
							|  |  |  |     assert "foos" not in flask.g | 
					
						
							|  |  |  |     assert sorted(flask.g) == ["bar", "foo"] | 
					
						
							| 
									
										
										
										
											2013-06-05 17:02:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-20 04:20:03 +08:00
										 |  |  | def test_subdomain_basic_support(): | 
					
						
							|  |  |  |     app = flask.Flask(__name__, subdomain_matching=True) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config["SERVER_NAME"] = "localhost.localdomain" | 
					
						
							| 
									
										
										
										
											2018-02-20 04:20:03 +08:00
										 |  |  |     client = app.test_client() | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def normal_index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "normal index" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/", subdomain="test") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def test_index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "test index" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/", "http://localhost.localdomain/") | 
					
						
							|  |  |  |     assert rv.data == b"normal index" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/", "http://test.localhost.localdomain/") | 
					
						
							|  |  |  |     assert rv.data == b"test index" | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-20 04:20:03 +08:00
										 |  |  | def test_subdomain_matching(): | 
					
						
							|  |  |  |     app = flask.Flask(__name__, subdomain_matching=True) | 
					
						
							|  |  |  |     client = app.test_client() | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config["SERVER_NAME"] = "localhost.localdomain" | 
					
						
							| 
									
										
										
										
											2011-08-26 21:51:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/", subdomain="<user>") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(user): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "index for %s" % user | 
					
						
							| 
									
										
										
										
											2011-08-26 21:51:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/", "http://mitsuhiko.localhost.localdomain/") | 
					
						
							|  |  |  |     assert rv.data == b"index for mitsuhiko" | 
					
						
							| 
									
										
										
										
											2013-06-14 15:28:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-20 04:20:03 +08:00
										 |  |  | def test_subdomain_matching_with_ports(): | 
					
						
							|  |  |  |     app = flask.Flask(__name__, subdomain_matching=True) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config["SERVER_NAME"] = "localhost.localdomain:3000" | 
					
						
							| 
									
										
										
										
											2018-02-20 04:20:03 +08:00
										 |  |  |     client = app.test_client() | 
					
						
							| 
									
										
										
										
											2013-06-14 15:28:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/", subdomain="<user>") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     def index(user): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "index for %s" % user | 
					
						
							| 
									
										
										
										
											2013-06-14 15:28:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/", "http://mitsuhiko.localhost.localdomain:3000/") | 
					
						
							|  |  |  |     assert rv.data == b"index for mitsuhiko" | 
					
						
							| 
									
										
										
										
											2013-06-14 15:28:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  | @pytest.mark.parametrize("matching", (False, True)) | 
					
						
							| 
									
										
										
										
											2018-02-23 23:51:34 +08:00
										 |  |  | def test_subdomain_matching_other_name(matching): | 
					
						
							|  |  |  |     app = flask.Flask(__name__, subdomain_matching=matching) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config["SERVER_NAME"] = "localhost.localdomain:3000" | 
					
						
							| 
									
										
										
										
											2018-02-23 23:51:34 +08:00
										 |  |  |     client = app.test_client() | 
					
						
							| 
									
										
										
										
											2018-02-20 04:20:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2018-02-23 23:51:34 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "", 204 | 
					
						
							| 
									
										
										
										
											2018-02-20 04:20:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-23 23:51:34 +08:00
										 |  |  |     # ip address can't match name | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/", "http://127.0.0.1:3000/") | 
					
						
							| 
									
										
										
										
											2018-02-23 23:51:34 +08:00
										 |  |  |     assert rv.status_code == 404 if matching else 204 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # allow all subdomains if matching is disabled | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.get("/", "http://www.localhost.localdomain:3000/") | 
					
						
							| 
									
										
										
										
											2018-02-23 23:51:34 +08:00
										 |  |  |     assert rv.status_code == 404 if matching else 204 | 
					
						
							| 
									
										
										
										
											2018-02-20 04:20:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_multi_route_rules(app, client): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							|  |  |  |     @app.route("/<test>/") | 
					
						
							|  |  |  |     def index(test="a"): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |         return test | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.open("/") | 
					
						
							|  |  |  |     assert rv.data == b"a" | 
					
						
							|  |  |  |     rv = client.open("/b/") | 
					
						
							|  |  |  |     assert rv.data == b"b" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_multi_route_class_views(app, client): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |     class View(object): | 
					
						
							|  |  |  |         def __init__(self, app): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |             app.add_url_rule("/", "index", self.index) | 
					
						
							|  |  |  |             app.add_url_rule("/<test>/", "index", self.index) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         def index(self, test="a"): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:37:09 +08:00
										 |  |  |             return test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _ = View(app) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     rv = client.open("/") | 
					
						
							|  |  |  |     assert rv.data == b"a" | 
					
						
							|  |  |  |     rv = client.open("/b/") | 
					
						
							|  |  |  |     assert rv.data == b"b" | 
					
						
							| 
									
										
										
										
											2014-09-14 22:53:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_run_defaults(monkeypatch, app): | 
					
						
							| 
									
										
										
										
											2014-09-14 22:53:05 +08:00
										 |  |  |     rv = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Mocks werkzeug.serving.run_simple method | 
					
						
							|  |  |  |     def run_simple_mock(*args, **kwargs): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv["result"] = "running..." | 
					
						
							| 
									
										
										
										
											2014-09-14 22:53:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     monkeypatch.setattr(werkzeug.serving, "run_simple", run_simple_mock) | 
					
						
							| 
									
										
										
										
											2014-09-14 22:53:05 +08:00
										 |  |  |     app.run() | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert rv["result"] == "running..." | 
					
						
							| 
									
										
										
										
											2014-09-14 22:53:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_run_server_port(monkeypatch, app): | 
					
						
							| 
									
										
										
										
											2014-09-14 22:53:05 +08:00
										 |  |  |     rv = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Mocks werkzeug.serving.run_simple method | 
					
						
							|  |  |  |     def run_simple_mock(hostname, port, application, *args, **kwargs): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         rv["result"] = "running on %s:%s ..." % (hostname, port) | 
					
						
							| 
									
										
										
										
											2014-09-14 22:53:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     monkeypatch.setattr(werkzeug.serving, "run_simple", run_simple_mock) | 
					
						
							|  |  |  |     hostname, port = "localhost", 8000 | 
					
						
							| 
									
										
										
										
											2014-09-14 22:53:05 +08:00
										 |  |  |     app.run(hostname, port, debug=True) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert rv["result"] == "running on %s:%s ..." % (hostname, port) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @pytest.mark.parametrize( | 
					
						
							| 
									
										
										
										
											2018-09-30 00:38:32 +08:00
										 |  |  |     "host,port,server_name,expect_host,expect_port", | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     ( | 
					
						
							| 
									
										
										
										
											2018-09-30 00:38:32 +08:00
										 |  |  |         (None, None, "pocoo.org:8080", "pocoo.org", 8080), | 
					
						
							|  |  |  |         ("localhost", None, "pocoo.org:8080", "localhost", 8080), | 
					
						
							|  |  |  |         (None, 80, "pocoo.org:8080", "pocoo.org", 80), | 
					
						
							|  |  |  |         ("localhost", 80, "pocoo.org:8080", "localhost", 80), | 
					
						
							|  |  |  |         ("localhost", 0, "localhost:8080", "localhost", 0), | 
					
						
							|  |  |  |         (None, None, "localhost:8080", "localhost", 8080), | 
					
						
							|  |  |  |         (None, None, "localhost:0", "localhost", 0), | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     ), | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2018-09-30 00:38:32 +08:00
										 |  |  | def test_run_from_config(monkeypatch, host, port, server_name, expect_host, expect_port, app): | 
					
						
							| 
									
										
										
										
											2017-01-18 06:08:33 +08:00
										 |  |  |     def run_simple_mock(hostname, port, *args, **kwargs): | 
					
						
							|  |  |  |         assert hostname == expect_host | 
					
						
							|  |  |  |         assert port == expect_port | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     monkeypatch.setattr(werkzeug.serving, "run_simple", run_simple_mock) | 
					
						
							| 
									
										
										
										
											2018-09-30 00:38:32 +08:00
										 |  |  |     app.config["SERVER_NAME"] = server_name | 
					
						
							| 
									
										
										
										
											2017-01-18 06:08:33 +08:00
										 |  |  |     app.run(host, port) | 
					
						
							| 
									
										
										
										
											2017-04-19 20:08:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_max_cookie_size(app, client, recwarn): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config["MAX_COOKIE_SIZE"] = 100 | 
					
						
							| 
									
										
										
										
											2017-04-19 20:08:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # outside app context, default to Werkzeug static value, | 
					
						
							|  |  |  |     # which is also the default config | 
					
						
							|  |  |  |     response = flask.Response() | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     default = flask.Flask.default_config["MAX_COOKIE_SIZE"] | 
					
						
							| 
									
										
										
										
											2017-04-19 20:08:53 +08:00
										 |  |  |     assert response.max_cookie_size == default | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # inside app context, use app config | 
					
						
							|  |  |  |     with app.app_context(): | 
					
						
							|  |  |  |         assert flask.Response().max_cookie_size == 100 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/") | 
					
						
							| 
									
										
										
										
											2017-04-19 20:08:53 +08:00
										 |  |  |     def index(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         r = flask.Response("", status=204) | 
					
						
							|  |  |  |         r.set_cookie("foo", "bar" * 100) | 
					
						
							| 
									
										
										
										
											2017-04-19 20:08:53 +08:00
										 |  |  |         return r | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     client.get("/") | 
					
						
							| 
									
										
										
										
											2017-04-19 20:08:53 +08:00
										 |  |  |     assert len(recwarn) == 1 | 
					
						
							|  |  |  |     w = recwarn.pop() | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert "cookie is too large" in str(w.message) | 
					
						
							| 
									
										
										
										
											2017-04-19 20:08:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.config["MAX_COOKIE_SIZE"] = 0 | 
					
						
							| 
									
										
										
										
											2017-04-19 20:08:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     client.get("/") | 
					
						
							| 
									
										
										
										
											2017-04-19 20:08:53 +08:00
										 |  |  |     assert len(recwarn) == 0 |