test_views.py
This file
Path
tests/test_views.py
254 lines · 6.3 KiB · Python
Last changed by
Merge pull request #3179 from Lee-W/MethodView-inheritanceDavid Lordbefore adoptionaade460
History of this fileTake it
The walk stopped after 200 commits: lines older than 01e2d80 point at it.
one more secret key in docsDavid Lordbefore adoption01e2d80
lines 1–7why, one step back1# -*- coding: utf-8 -*-2"""3 tests.views4 ~~~~~~~~~~~56 Pluggable views.7Merge pull request #2624 from pallets/metadataDavid Lordbefore adoption4309c7c
line 8why, one step back8 :copyright: © 2010 by the Pallets team.one more secret key in docsDavid Lordbefore adoption01e2d80
lines 9–12why, one step back9 :license: BSD, see LICENSE for more details.10"""1112import pytestMerge pull request #3179 from Lee-W/MethodView-inheritanceDavid Lordbefore adoptionaade460
line 13why, one step back13from werkzeug.http import parse_set_headerone more secret key in docsDavid Lordbefore adoption01e2d80
lines 14–21why, one step back1415import flask16import flask.views171819def common_test(app):20 c = app.test_client()21Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 22–26why, one step back22 assert c.get("/").data == b"GET"23 assert c.post("/").data == b"POST"24 assert c.put("/").status_code == 40525 meths = parse_set_header(c.open("/", method="OPTIONS").headers["Allow"])26 assert sorted(meths) == ["GET", "HEAD", "OPTIONS", "POST"]one more secret key in docsDavid Lordbefore adoption01e2d80
lines 27–30why, one step back272829def test_basic_view(app):30 class Index(flask.views.View):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 31why, one step back31 methods = ["GET", "POST"]one more secret key in docsDavid Lordbefore adoption01e2d80
lines 32–35why, one step back3233 def dispatch_request(self):34 return flask.request.method35Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 36why, one step back36 app.add_url_rule("/", view_func=Index.as_view("index"))one more secret key in docsDavid Lordbefore adoption01e2d80
lines 37–42why, one step back37 common_test(app)383940def test_method_based_view(app):41 class Index(flask.views.MethodView):42 def get(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 43why, one step back43 return "GET"one more secret key in docsDavid Lordbefore adoption01e2d80
lines 44–45why, one step back4445 def post(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 46why, one step back46 return "POST"one more secret key in docsDavid Lordbefore adoption01e2d80
line 47why, one step back47Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 48why, one step back48 app.add_url_rule("/", view_func=Index.as_view("index"))one more secret key in docsDavid Lordbefore adoption01e2d80
lines 49–62why, one step back4950 common_test(app)515253def test_view_patching(app):54 class Index(flask.views.MethodView):55 def get(self):56 1 // 05758 def post(self):59 1 // 06061 class Other(Index):62 def get(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 63why, one step back63 return "GET"one more secret key in docsDavid Lordbefore adoption01e2d80
lines 64–65why, one step back6465 def post(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 66why, one step back66 return "POST"one more secret key in docsDavid Lordbefore adoption01e2d80
line 67why, one step back67Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 68why, one step back68 view = Index.as_view("index")one more secret key in docsDavid Lordbefore adoption01e2d80
line 69why, one step back69 view.view_class = OtherMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 70why, one step back70 app.add_url_rule("/", view_func=view)one more secret key in docsDavid Lordbefore adoption01e2d80
lines 71–76why, one step back71 common_test(app)727374def test_view_inheritance(app, client):75 class Index(flask.views.MethodView):76 def get(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 77why, one step back77 return "GET"one more secret key in docsDavid Lordbefore adoption01e2d80
lines 78–79why, one step back7879 def post(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 80why, one step back80 return "POST"one more secret key in docsDavid Lordbefore adoption01e2d80
lines 81–83why, one step back8182 class BetterIndex(Index):83 def delete(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 84why, one step back84 return "DELETE"one more secret key in docsDavid Lordbefore adoption01e2d80
line 85why, one step back85Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 86why, one step back86 app.add_url_rule("/", view_func=BetterIndex.as_view("index"))one more secret key in docsDavid Lordbefore adoption01e2d80
line 87why, one step back87Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 88–89why, one step back88 meths = parse_set_header(client.open("/", method="OPTIONS").headers["Allow"])89 assert sorted(meths) == ["DELETE", "GET", "HEAD", "OPTIONS", "POST"]one more secret key in docsDavid Lordbefore adoption01e2d80
lines 90–95why, one step back909192def test_view_decorators(app, client):93 def add_x_parachute(f):94 def new_function(*args, **kwargs):95 resp = flask.make_response(f(*args, **kwargs))Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 96why, one step back96 resp.headers["X-Parachute"] = "awesome"one more secret key in docsDavid Lordbefore adoption01e2d80
lines 97–104why, one step back97 return resp9899 return new_function100101 class Index(flask.views.View):102 decorators = [add_x_parachute]103104 def dispatch_request(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 105why, one step back105 return "Awesome"one more secret key in docsDavid Lordbefore adoption01e2d80
line 106why, one step back106Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 107–110why, one step back107 app.add_url_rule("/", view_func=Index.as_view("index"))108 rv = client.get("/")109 assert rv.headers["X-Parachute"] == "awesome"110 assert rv.data == b"Awesome"one more secret key in docsDavid Lordbefore adoption01e2d80
lines 111–119why, one step back111112113def test_view_provide_automatic_options_attr():114 app = flask.Flask(__name__)115116 class Index1(flask.views.View):117 provide_automatic_options = False118119 def dispatch_request(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 120why, one step back120 return "Hello World!"one more secret key in docsDavid Lordbefore adoption01e2d80
line 121why, one step back121Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 122why, one step back122 app.add_url_rule("/", view_func=Index1.as_view("index"))one more secret key in docsDavid Lordbefore adoption01e2d80
line 123why, one step back123 c = app.test_client()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 124why, one step back124 rv = c.open("/", method="OPTIONS")one more secret key in docsDavid Lordbefore adoption01e2d80
lines 125–129why, one step back125 assert rv.status_code == 405126127 app = flask.Flask(__name__)128129 class Index2(flask.views.View):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 130why, one step back130 methods = ["OPTIONS"]one more secret key in docsDavid Lordbefore adoption01e2d80
lines 131–133why, one step back131 provide_automatic_options = True132133 def dispatch_request(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 134why, one step back134 return "Hello World!"one more secret key in docsDavid Lordbefore adoption01e2d80
line 135why, one step back135Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 136why, one step back136 app.add_url_rule("/", view_func=Index2.as_view("index"))one more secret key in docsDavid Lordbefore adoption01e2d80
line 137why, one step back137 c = app.test_client()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 138–139why, one step back138 rv = c.open("/", method="OPTIONS")139 assert sorted(rv.allow) == ["OPTIONS"]one more secret key in docsDavid Lordbefore adoption01e2d80
lines 140–144why, one step back140141 app = flask.Flask(__name__)142143 class Index3(flask.views.View):144 def dispatch_request(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 145why, one step back145 return "Hello World!"one more secret key in docsDavid Lordbefore adoption01e2d80
line 146why, one step back146Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 147why, one step back147 app.add_url_rule("/", view_func=Index3.as_view("index"))one more secret key in docsDavid Lordbefore adoption01e2d80
line 148why, one step back148 c = app.test_client()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 149–150why, one step back149 rv = c.open("/", method="OPTIONS")150 assert "OPTIONS" in rv.allowone more secret key in docsDavid Lordbefore adoption01e2d80
lines 151–155why, one step back151152153def test_implicit_head(app, client):154 class Index(flask.views.MethodView):155 def get(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 156why, one step back156 return flask.Response("Blub", headers={"X-Method": flask.request.method})one more secret key in docsDavid Lordbefore adoption01e2d80
line 157why, one step back157Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 158–164why, one step back158 app.add_url_rule("/", view_func=Index.as_view("index"))159 rv = client.get("/")160 assert rv.data == b"Blub"161 assert rv.headers["X-Method"] == "GET"162 rv = client.head("/")163 assert rv.data == b""164 assert rv.headers["X-Method"] == "HEAD"one more secret key in docsDavid Lordbefore adoption01e2d80
lines 165–169why, one step back165166167def test_explicit_head(app, client):168 class Index(flask.views.MethodView):169 def get(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 170why, one step back170 return "GET"one more secret key in docsDavid Lordbefore adoption01e2d80
lines 171–172why, one step back171172 def head(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 173why, one step back173 return flask.Response("", headers={"X-Method": "HEAD"})one more secret key in docsDavid Lordbefore adoption01e2d80
line 174why, one step back174Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 175–180why, one step back175 app.add_url_rule("/", view_func=Index.as_view("index"))176 rv = client.get("/")177 assert rv.data == b"GET"178 rv = client.head("/")179 assert rv.data == b""180 assert rv.headers["X-Method"] == "HEAD"one more secret key in docsDavid Lordbefore adoption01e2d80
lines 181–186why, one step back181182183def test_endpoint_override(app):184 app.debug = True185186 class Index(flask.views.View):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 187why, one step back187 methods = ["GET", "POST"]one more secret key in docsDavid Lordbefore adoption01e2d80
lines 188–191why, one step back188189 def dispatch_request(self):190 return flask.request.method191Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 192why, one step back192 app.add_url_rule("/", view_func=Index.as_view("index"))one more secret key in docsDavid Lordbefore adoption01e2d80
lines 193–194why, one step back193194 with pytest.raises(AssertionError):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 195why, one step back195 app.add_url_rule("/", view_func=Index.as_view("index"))one more secret key in docsDavid Lordbefore adoption01e2d80
lines 196–198why, one step back196197 # But these tests should still pass. We just log a warning.198 common_test(app)Merge pull request #3179 from Lee-W/MethodView-inheritanceDavid Lordbefore adoptionaade460
lines 199–216why, one step back199200201def test_methods_var_inheritance(app, client):202 class BaseView(flask.views.MethodView):203 methods = ["GET", "PROPFIND"]204205 class ChildView(BaseView):206 def get(self):207 return "GET"208209 def propfind(self):210 return "PROPFIND"211212 app.add_url_rule("/", view_func=ChildView.as_view("index"))213214 assert client.get("/").data == b"GET"215 assert client.open("/", method="PROPFIND").data == b"PROPFIND"216 assert ChildView.methods == {"PROPFIND", "GET"}one more secret key in docsDavid Lordbefore adoption01e2d80
lines 217–221why, one step back217218219def test_multiple_inheritance(app, client):220 class GetView(flask.views.MethodView):221 def get(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 222why, one step back222 return "GET"one more secret key in docsDavid Lordbefore adoption01e2d80
lines 223–225why, one step back223224 class DeleteView(flask.views.MethodView):225 def delete(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 226why, one step back226 return "DELETE"one more secret key in docsDavid Lordbefore adoption01e2d80
lines 227–230why, one step back227228 class GetDeleteView(GetView, DeleteView):229 pass230Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 231why, one step back231 app.add_url_rule("/", view_func=GetDeleteView.as_view("index"))one more secret key in docsDavid Lordbefore adoption01e2d80
line 232why, one step back232Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 233–235why, one step back233 assert client.get("/").data == b"GET"234 assert client.delete("/").data == b"DELETE"235 assert sorted(GetDeleteView.methods) == ["DELETE", "GET"]one more secret key in docsDavid Lordbefore adoption01e2d80
lines 236–240why, one step back236237238def test_remove_method_from_parent(app, client):239 class GetView(flask.views.MethodView):240 def get(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 241why, one step back241 return "GET"one more secret key in docsDavid Lordbefore adoption01e2d80
lines 242–244why, one step back242243 class OtherView(flask.views.MethodView):244 def post(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 245why, one step back245 return "POST"one more secret key in docsDavid Lordbefore adoption01e2d80
lines 246–247why, one step back246247 class View(GetView, OtherView):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 248why, one step back248 methods = ["GET"]one more secret key in docsDavid Lordbefore adoption01e2d80
line 249why, one step back249Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 250why, one step back250 app.add_url_rule("/", view_func=View.as_view("index"))one more secret key in docsDavid Lordbefore adoption01e2d80
line 251why, one step back251Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 252–254why, one step back252 assert client.get("/").data == b"GET"253 assert client.post("/").status_code == 405254 assert sorted(View.methods) == ["GET"]