Skip to content
Myr

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 file

Take 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 back
1# -*- coding: utf-8 -*-
2"""3 tests.views4 ~~~~~~~~~~~56 Pluggable views.7

Merge pull request #2624 from pallets/metadataDavid Lordbefore adoption4309c7c

line 8why, one step back
8 :copyright: © 2010 by the Pallets team.

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 9–12why, one step back
9 :license: BSD, see LICENSE for more details.
10"""1112import pytest

Merge pull request #3179 from Lee-W/MethodView-inheritanceDavid Lordbefore adoptionaade460

line 13why, one step back
13from werkzeug.http import parse_set_header

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 14–21why, one step back
14
15import flask16import flask.views171819def common_test(app):20 c = app.test_client()21

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 22–26why, one step back
22 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 back
27
2829def test_basic_view(app):30 class Index(flask.views.View):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 31why, one step back
31 methods = ["GET", "POST"]

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 32–35why, one step back
32
33 def dispatch_request(self):34 return flask.request.method35

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 36why, one step back
36 app.add_url_rule("/", view_func=Index.as_view("index"))

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 37–42why, one step back
37 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 back
43 return "GET"

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 44–45why, one step back
44
45 def post(self):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 46why, one step back
46 return "POST"

one more secret key in docsDavid Lordbefore adoption01e2d80

line 47why, one step back
47

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 48why, one step back
48 app.add_url_rule("/", view_func=Index.as_view("index"))

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 49–62why, one step back
49
50 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 back
63 return "GET"

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 64–65why, one step back
64
65 def post(self):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 66why, one step back
66 return "POST"

one more secret key in docsDavid Lordbefore adoption01e2d80

line 67why, one step back
67

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 68why, one step back
68 view = Index.as_view("index")

one more secret key in docsDavid Lordbefore adoption01e2d80

line 69why, one step back
69 view.view_class = Other

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 70why, one step back
70 app.add_url_rule("/", view_func=view)

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 71–76why, one step back
71 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 back
77 return "GET"

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 78–79why, one step back
78
79 def post(self):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 80why, one step back
80 return "POST"

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 81–83why, one step back
81
82 class BetterIndex(Index):83 def delete(self):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 84why, one step back
84 return "DELETE"

one more secret key in docsDavid Lordbefore adoption01e2d80

line 85why, one step back
85

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 86why, one step back
86 app.add_url_rule("/", view_func=BetterIndex.as_view("index"))

one more secret key in docsDavid Lordbefore adoption01e2d80

line 87why, one step back
87

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 88–89why, one step back
88 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 back
90
9192def 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 back
96 resp.headers["X-Parachute"] = "awesome"

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 97–104why, one step back
97 return resp
9899 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 back
105 return "Awesome"

one more secret key in docsDavid Lordbefore adoption01e2d80

line 106why, one step back
106

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 107–110why, one step back
107 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 back
111
112113def 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 back
120 return "Hello World!"

one more secret key in docsDavid Lordbefore adoption01e2d80

line 121why, one step back
121

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 122why, one step back
122 app.add_url_rule("/", view_func=Index1.as_view("index"))

one more secret key in docsDavid Lordbefore adoption01e2d80

line 123why, one step back
123 c = app.test_client()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 124why, one step back
124 rv = c.open("/", method="OPTIONS")

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 125–129why, one step back
125 assert rv.status_code == 405
126127 app = flask.Flask(__name__)128129 class Index2(flask.views.View):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 130why, one step back
130 methods = ["OPTIONS"]

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 131–133why, one step back
131 provide_automatic_options = True
132133 def dispatch_request(self):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 134why, one step back
134 return "Hello World!"

one more secret key in docsDavid Lordbefore adoption01e2d80

line 135why, one step back
135

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 136why, one step back
136 app.add_url_rule("/", view_func=Index2.as_view("index"))

one more secret key in docsDavid Lordbefore adoption01e2d80

line 137why, one step back
137 c = app.test_client()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 138–139why, one step back
138 rv = c.open("/", method="OPTIONS")
139 assert sorted(rv.allow) == ["OPTIONS"]

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 140–144why, one step back
140
141 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 back
145 return "Hello World!"

one more secret key in docsDavid Lordbefore adoption01e2d80

line 146why, one step back
146

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 147why, one step back
147 app.add_url_rule("/", view_func=Index3.as_view("index"))

one more secret key in docsDavid Lordbefore adoption01e2d80

line 148why, one step back
148 c = app.test_client()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 149–150why, one step back
149 rv = c.open("/", method="OPTIONS")
150 assert "OPTIONS" in rv.allow

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 151–155why, one step back
151
152153def 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 back
156 return flask.Response("Blub", headers={"X-Method": flask.request.method})

one more secret key in docsDavid Lordbefore adoption01e2d80

line 157why, one step back
157

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 158–164why, one step back
158 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 back
165
166167def 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 back
170 return "GET"

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 171–172why, one step back
171
172 def head(self):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 173why, one step back
173 return flask.Response("", headers={"X-Method": "HEAD"})

one more secret key in docsDavid Lordbefore adoption01e2d80

line 174why, one step back
174

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 175–180why, one step back
175 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 back
181
182183def 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 back
187 methods = ["GET", "POST"]

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 188–191why, one step back
188
189 def dispatch_request(self):190 return flask.request.method191

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 192why, one step back
192 app.add_url_rule("/", view_func=Index.as_view("index"))

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 193–194why, one step back
193
194 with pytest.raises(AssertionError):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 195why, one step back
195 app.add_url_rule("/", view_func=Index.as_view("index"))

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 196–198why, one step back
196
197 # 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 back
199
200201def 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 back
217
218219def 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 back
222 return "GET"

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 223–225why, one step back
223
224 class DeleteView(flask.views.MethodView):225 def delete(self):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 226why, one step back
226 return "DELETE"

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 227–230why, one step back
227
228 class GetDeleteView(GetView, DeleteView):229 pass230

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 231why, one step back
231 app.add_url_rule("/", view_func=GetDeleteView.as_view("index"))

one more secret key in docsDavid Lordbefore adoption01e2d80

line 232why, one step back
232

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 233–235why, one step back
233 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 back
236
237238def 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 back
241 return "GET"

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 242–244why, one step back
242
243 class OtherView(flask.views.MethodView):244 def post(self):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 245why, one step back
245 return "POST"

one more secret key in docsDavid Lordbefore adoption01e2d80

lines 246–247why, one step back
246
247 class View(GetView, OtherView):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 248why, one step back
248 methods = ["GET"]

one more secret key in docsDavid Lordbefore adoption01e2d80

line 249why, one step back
249

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 250why, one step back
250 app.add_url_rule("/", view_func=View.as_view("index"))

one more secret key in docsDavid Lordbefore adoption01e2d80

line 251why, one step back
251

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 252–254why, one step back
252 assert client.get("/").data == b"GET"
253 assert client.post("/").status_code == 405254 assert sorted(View.methods) == ["GET"]