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
1# -*- coding: utf-8 -*-2"""3 tests.views4 ~~~~~~~~~~~56 Pluggable views.78 :copyright: © 2010 by the Pallets team.9 :license: BSD, see LICENSE for more details.10"""1112import pytest13from werkzeug.http import parse_set_header1415import flask16import flask.views171819def common_test(app):20 c = app.test_client()2122 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"]272829def test_basic_view(app):30 class Index(flask.views.View):31 methods = ["GET", "POST"]3233 def dispatch_request(self):34 return flask.request.method3536 app.add_url_rule("/", view_func=Index.as_view("index"))37 common_test(app)383940def test_method_based_view(app):41 class Index(flask.views.MethodView):42 def get(self):43 return "GET"4445 def post(self):46 return "POST"4748 app.add_url_rule("/", view_func=Index.as_view("index"))4950 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):63 return "GET"6465 def post(self):66 return "POST"6768 view = Index.as_view("index")69 view.view_class = Other70 app.add_url_rule("/", view_func=view)71 common_test(app)727374def test_view_inheritance(app, client):75 class Index(flask.views.MethodView):76 def get(self):77 return "GET"7879 def post(self):80 return "POST"8182 class BetterIndex(Index):83 def delete(self):84 return "DELETE"8586 app.add_url_rule("/", view_func=BetterIndex.as_view("index"))8788 meths = parse_set_header(client.open("/", method="OPTIONS").headers["Allow"])89 assert sorted(meths) == ["DELETE", "GET", "HEAD", "OPTIONS", "POST"]909192def 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))96 resp.headers["X-Parachute"] = "awesome"97 return resp9899 return new_function100101 class Index(flask.views.View):102 decorators = [add_x_parachute]103104 def dispatch_request(self):105 return "Awesome"106107 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"111112113def 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):120 return "Hello World!"121122 app.add_url_rule("/", view_func=Index1.as_view("index"))123 c = app.test_client()124 rv = c.open("/", method="OPTIONS")125 assert rv.status_code == 405126127 app = flask.Flask(__name__)128129 class Index2(flask.views.View):130 methods = ["OPTIONS"]131 provide_automatic_options = True132133 def dispatch_request(self):134 return "Hello World!"135136 app.add_url_rule("/", view_func=Index2.as_view("index"))137 c = app.test_client()138 rv = c.open("/", method="OPTIONS")139 assert sorted(rv.allow) == ["OPTIONS"]140141 app = flask.Flask(__name__)142143 class Index3(flask.views.View):144 def dispatch_request(self):145 return "Hello World!"146147 app.add_url_rule("/", view_func=Index3.as_view("index"))148 c = app.test_client()149 rv = c.open("/", method="OPTIONS")150 assert "OPTIONS" in rv.allow151152153def test_implicit_head(app, client):154 class Index(flask.views.MethodView):155 def get(self):156 return flask.Response("Blub", headers={"X-Method": flask.request.method})157158 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"165166167def test_explicit_head(app, client):168 class Index(flask.views.MethodView):169 def get(self):170 return "GET"171172 def head(self):173 return flask.Response("", headers={"X-Method": "HEAD"})174175 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"181182183def test_endpoint_override(app):184 app.debug = True185186 class Index(flask.views.View):187 methods = ["GET", "POST"]188189 def dispatch_request(self):190 return flask.request.method191192 app.add_url_rule("/", view_func=Index.as_view("index"))193194 with pytest.raises(AssertionError):195 app.add_url_rule("/", view_func=Index.as_view("index"))196197 # But these tests should still pass. We just log a warning.198 common_test(app)199200201def 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"}217218219def test_multiple_inheritance(app, client):220 class GetView(flask.views.MethodView):221 def get(self):222 return "GET"223224 class DeleteView(flask.views.MethodView):225 def delete(self):226 return "DELETE"227228 class GetDeleteView(GetView, DeleteView):229 pass230231 app.add_url_rule("/", view_func=GetDeleteView.as_view("index"))232233 assert client.get("/").data == b"GET"234 assert client.delete("/").data == b"DELETE"235 assert sorted(GetDeleteView.methods) == ["DELETE", "GET"]236237238def test_remove_method_from_parent(app, client):239 class GetView(flask.views.MethodView):240 def get(self):241 return "GET"242243 class OtherView(flask.views.MethodView):244 def post(self):245 return "POST"246247 class View(GetView, OtherView):248 methods = ["GET"]249250 app.add_url_rule("/", view_func=View.as_view("index"))251252 assert client.get("/").data == b"GET"253 assert client.post("/").status_code == 405254 assert sorted(View.methods) == ["GET"]