test_views.py
This file
Path
tests/test_views.py
271 lines · 6.9 KiB · Python
Last changed by
use header propertiesDavid Lordbefore adoption8b4fd5d
History of this fileTake it
The walk stopped after 200 commits: lines older than 73739a2 point at it.
Merge branch '2.2.x'David Lordbefore adoption73739a2
lines 1–3why, one step back1import pytest23import flask.viewsfix provide_automatic_options override (#5917)David Lordbefore adoption12e95c9
line 4why, one step back4from flask.testing import FlaskClientMerge branch '2.2.x'David Lordbefore adoption73739a2
lines 5–12why, one step back567def common_test(app):8 c = app.test_client()910 assert c.get("/").data == b"GET"11 assert c.post("/").data == b"POST"12 assert c.put("/").status_code == 405use header propertiesDavid Lordbefore adoption8b4fd5d
lines 13–14why, one step back13 meths = c.open("/", method="OPTIONS").allow14 assert meths == {"GET", "HEAD", "OPTIONS", "POST"}Merge branch '2.2.x'David Lordbefore adoption73739a2
lines 15–43why, one step back151617def test_basic_view(app):18 class Index(flask.views.View):19 methods = ["GET", "POST"]2021 def dispatch_request(self):22 return flask.request.method2324 app.add_url_rule("/", view_func=Index.as_view("index"))25 common_test(app)262728def test_method_based_view(app):29 class Index(flask.views.MethodView):30 def get(self):31 return "GET"3233 def post(self):34 return "POST"3536 app.add_url_rule("/", view_func=Index.as_view("index"))3738 common_test(app)394041def test_view_patching(app):42 class Index(flask.views.MethodView):43 def get(self):use ruff linter and formatter (#5331)David Lordbefore adoption9a12f34
line 44why, one step back44 raise ZeroDivisionErrorMerge branch '2.2.x'David Lordbefore adoption73739a2
lines 45–46why, one step back4546 def post(self):use ruff linter and formatter (#5331)David Lordbefore adoption9a12f34
line 47why, one step back47 raise ZeroDivisionErrorMerge branch '2.2.x'David Lordbefore adoption73739a2
lines 48–75why, one step back4849 class Other(Index):50 def get(self):51 return "GET"5253 def post(self):54 return "POST"5556 view = Index.as_view("index")57 view.view_class = Other58 app.add_url_rule("/", view_func=view)59 common_test(app)606162def test_view_inheritance(app, client):63 class Index(flask.views.MethodView):64 def get(self):65 return "GET"6667 def post(self):68 return "POST"6970 class BetterIndex(Index):71 def delete(self):72 return "DELETE"7374 app.add_url_rule("/", view_func=BetterIndex.as_view("index"))75use header propertiesDavid Lordbefore adoption8b4fd5d
lines 76–77why, one step back76 meths = client.open("/", method="OPTIONS").allow77 assert meths == {"DELETE", "GET", "HEAD", "OPTIONS", "POST"}Merge branch '2.2.x'David Lordbefore adoption73739a2
lines 78–100why, one step back787980def test_view_decorators(app, client):81 def add_x_parachute(f):82 def new_function(*args, **kwargs):83 resp = flask.make_response(f(*args, **kwargs))84 resp.headers["X-Parachute"] = "awesome"85 return resp8687 return new_function8889 class Index(flask.views.View):90 decorators = [add_x_parachute]9192 def dispatch_request(self):93 return "Awesome"9495 app.add_url_rule("/", view_func=Index.as_view("index"))96 rv = client.get("/")97 assert rv.headers["X-Parachute"] == "awesome"98 assert rv.data == b"Awesome"99100fix provide_automatic_options override (#5917)David Lordbefore adoption12e95c9
lines 101–104why, one step back101def test_view_provide_automatic_options_attr_disable(102 app: flask.Flask, client: FlaskClient103) -> None:104 """Automatic options can be disabled by the view class attribute."""Merge branch '2.2.x'David Lordbefore adoption73739a2
line 105why, one step back105fix provide_automatic_options override (#5917)David Lordbefore adoption12e95c9
line 106why, one step back106 class Index(flask.views.View):Merge branch '2.2.x'David Lordbefore adoption73739a2
lines 107–111why, one step back107 provide_automatic_options = False108109 def dispatch_request(self):110 return "Hello World!"111fix provide_automatic_options override (#5917)David Lordbefore adoption12e95c9
lines 112–113why, one step back112 app.add_url_rule("/", view_func=Index.as_view("index"))113 rv = client.options()Merge branch '2.2.x'David Lordbefore adoption73739a2
lines 114–116why, one step back114 assert rv.status_code == 405115116fix provide_automatic_options override (#5917)David Lordbefore adoption12e95c9
lines 117–125why, one step back117def test_view_provide_automatic_options_attr_enable(118 app: flask.Flask, client: FlaskClient119) -> None:120 """When default automatic options is disabled in config, it can still be121 enabled by the view class attribute.122 """123 app.config["PROVIDE_AUTOMATIC_OPTIONS"] = False124125 class Index(flask.views.View):Merge branch '2.2.x'David Lordbefore adoption73739a2
lines 126–130why, one step back126 provide_automatic_options = True127128 def dispatch_request(self):129 return "Hello World!"130fix provide_automatic_options override (#5917)David Lordbefore adoption12e95c9
lines 131–133why, one step back131 app.add_url_rule("/", view_func=Index.as_view("index"))132 rv = client.options("/")133 assert rv.allow == {"GET", "HEAD", "OPTIONS"}Merge branch '2.2.x'David Lordbefore adoption73739a2
lines 134–135why, one step back134135fix provide_automatic_options override (#5917)David Lordbefore adoption12e95c9
lines 136–143why, one step back136def test_provide_automatic_options_method_disable(137 app: flask.Flask, client: FlaskClient138) -> None:139 """Automatic options is ignored if the route handles options."""140141 class Index(flask.views.View):142 methods = ["OPTIONS"]143Merge branch '2.2.x'David Lordbefore adoption73739a2
line 144why, one step back144 def dispatch_request(self):fix provide_automatic_options override (#5917)David Lordbefore adoption12e95c9
line 145why, one step back145 return "", {"X-Test": "test"}Merge branch '2.2.x'David Lordbefore adoption73739a2
line 146why, one step back146fix provide_automatic_options override (#5917)David Lordbefore adoption12e95c9
lines 147–149why, one step back147 app.add_url_rule("/", view_func=Index.as_view("index"))148 rv = client.options()149 assert rv.headers["X-Test"] == "test"Merge branch '2.2.x'David Lordbefore adoption73739a2
lines 150–193why, one step back150151152def test_implicit_head(app, client):153 class Index(flask.views.MethodView):154 def get(self):155 return flask.Response("Blub", headers={"X-Method": flask.request.method})156157 app.add_url_rule("/", view_func=Index.as_view("index"))158 rv = client.get("/")159 assert rv.data == b"Blub"160 assert rv.headers["X-Method"] == "GET"161 rv = client.head("/")162 assert rv.data == b""163 assert rv.headers["X-Method"] == "HEAD"164165166def test_explicit_head(app, client):167 class Index(flask.views.MethodView):168 def get(self):169 return "GET"170171 def head(self):172 return flask.Response("", headers={"X-Method": "HEAD"})173174 app.add_url_rule("/", view_func=Index.as_view("index"))175 rv = client.get("/")176 assert rv.data == b"GET"177 rv = client.head("/")178 assert rv.data == b""179 assert rv.headers["X-Method"] == "HEAD"180181182def test_endpoint_override(app):183 app.debug = True184185 class Index(flask.views.View):186 methods = ["GET", "POST"]187188 def dispatch_request(self):189 return flask.request.method190191 app.add_url_rule("/", view_func=Index.as_view("index"))192193 with pytest.raises(AssertionError):fix provide_automatic_options override (#5917)David Lordbefore adoption12e95c9
line 194why, one step back194 app.add_url_rule("/other", view_func=Index.as_view("index"))Merge branch '2.2.x'David Lordbefore adoption73739a2
lines 195–271why, one step back195196 # But these tests should still pass. We just log a warning.197 common_test(app)198199200def test_methods_var_inheritance(app, client):201 class BaseView(flask.views.MethodView):202 methods = ["GET", "PROPFIND"]203204 class ChildView(BaseView):205 def get(self):206 return "GET"207208 def propfind(self):209 return "PROPFIND"210211 app.add_url_rule("/", view_func=ChildView.as_view("index"))212213 assert client.get("/").data == b"GET"214 assert client.open("/", method="PROPFIND").data == b"PROPFIND"215 assert ChildView.methods == {"PROPFIND", "GET"}216217218def test_multiple_inheritance(app, client):219 class GetView(flask.views.MethodView):220 def get(self):221 return "GET"222223 class DeleteView(flask.views.MethodView):224 def delete(self):225 return "DELETE"226227 class GetDeleteView(GetView, DeleteView):228 pass229230 app.add_url_rule("/", view_func=GetDeleteView.as_view("index"))231232 assert client.get("/").data == b"GET"233 assert client.delete("/").data == b"DELETE"234 assert sorted(GetDeleteView.methods) == ["DELETE", "GET"]235236237def test_remove_method_from_parent(app, client):238 class GetView(flask.views.MethodView):239 def get(self):240 return "GET"241242 class OtherView(flask.views.MethodView):243 def post(self):244 return "POST"245246 class View(GetView, OtherView):247 methods = ["GET"]248249 app.add_url_rule("/", view_func=View.as_view("index"))250251 assert client.get("/").data == b"GET"252 assert client.post("/").status_code == 405253 assert sorted(View.methods) == ["GET"]254255256def test_init_once(app, client):257 n = 0258259 class CountInit(flask.views.View):260 init_every_request = False261262 def __init__(self):263 nonlocal n264 n += 1265266 def dispatch_request(self):267 return str(n)268269 app.add_url_rule("/", view_func=CountInit.as_view("index"))270 assert client.get("/").data == b"1"271 assert client.get("/").data == b"1"