Skip to content
Myr

test_views.py

This file

Path

tests/test_views.py

260 lines · 6.6 KiB · Python

Last changed by

use ruff linter and formatterDavid Lordbefore adoption54e05a2

History of this file

Take it

1import pytest2from werkzeug.http import parse_set_header34import flask.views567def 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 == 40513    meths = parse_set_header(c.open("/", method="OPTIONS").headers["Allow"])14    assert sorted(meths) == ["GET", "HEAD", "OPTIONS", "POST"]151617def 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):44            raise ZeroDivisionError4546        def post(self):47            raise ZeroDivisionError4849    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"))7576    meths = parse_set_header(client.open("/", method="OPTIONS").headers["Allow"])77    assert sorted(meths) == ["DELETE", "GET", "HEAD", "OPTIONS", "POST"]787980def 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"99100101def test_view_provide_automatic_options_attr():102    app = flask.Flask(__name__)103104    class Index1(flask.views.View):105        provide_automatic_options = False106107        def dispatch_request(self):108            return "Hello World!"109110    app.add_url_rule("/", view_func=Index1.as_view("index"))111    c = app.test_client()112    rv = c.open("/", method="OPTIONS")113    assert rv.status_code == 405114115    app = flask.Flask(__name__)116117    class Index2(flask.views.View):118        methods = ["OPTIONS"]119        provide_automatic_options = True120121        def dispatch_request(self):122            return "Hello World!"123124    app.add_url_rule("/", view_func=Index2.as_view("index"))125    c = app.test_client()126    rv = c.open("/", method="OPTIONS")127    assert sorted(rv.allow) == ["OPTIONS"]128129    app = flask.Flask(__name__)130131    class Index3(flask.views.View):132        def dispatch_request(self):133            return "Hello World!"134135    app.add_url_rule("/", view_func=Index3.as_view("index"))136    c = app.test_client()137    rv = c.open("/", method="OPTIONS")138    assert "OPTIONS" in rv.allow139140141def test_implicit_head(app, client):142    class Index(flask.views.MethodView):143        def get(self):144            return flask.Response("Blub", headers={"X-Method": flask.request.method})145146    app.add_url_rule("/", view_func=Index.as_view("index"))147    rv = client.get("/")148    assert rv.data == b"Blub"149    assert rv.headers["X-Method"] == "GET"150    rv = client.head("/")151    assert rv.data == b""152    assert rv.headers["X-Method"] == "HEAD"153154155def test_explicit_head(app, client):156    class Index(flask.views.MethodView):157        def get(self):158            return "GET"159160        def head(self):161            return flask.Response("", headers={"X-Method": "HEAD"})162163    app.add_url_rule("/", view_func=Index.as_view("index"))164    rv = client.get("/")165    assert rv.data == b"GET"166    rv = client.head("/")167    assert rv.data == b""168    assert rv.headers["X-Method"] == "HEAD"169170171def test_endpoint_override(app):172    app.debug = True173174    class Index(flask.views.View):175        methods = ["GET", "POST"]176177        def dispatch_request(self):178            return flask.request.method179180    app.add_url_rule("/", view_func=Index.as_view("index"))181182    with pytest.raises(AssertionError):183        app.add_url_rule("/", view_func=Index.as_view("index"))184185    # But these tests should still pass. We just log a warning.186    common_test(app)187188189def test_methods_var_inheritance(app, client):190    class BaseView(flask.views.MethodView):191        methods = ["GET", "PROPFIND"]192193    class ChildView(BaseView):194        def get(self):195            return "GET"196197        def propfind(self):198            return "PROPFIND"199200    app.add_url_rule("/", view_func=ChildView.as_view("index"))201202    assert client.get("/").data == b"GET"203    assert client.open("/", method="PROPFIND").data == b"PROPFIND"204    assert ChildView.methods == {"PROPFIND", "GET"}205206207def test_multiple_inheritance(app, client):208    class GetView(flask.views.MethodView):209        def get(self):210            return "GET"211212    class DeleteView(flask.views.MethodView):213        def delete(self):214            return "DELETE"215216    class GetDeleteView(GetView, DeleteView):217        pass218219    app.add_url_rule("/", view_func=GetDeleteView.as_view("index"))220221    assert client.get("/").data == b"GET"222    assert client.delete("/").data == b"DELETE"223    assert sorted(GetDeleteView.methods) == ["DELETE", "GET"]224225226def test_remove_method_from_parent(app, client):227    class GetView(flask.views.MethodView):228        def get(self):229            return "GET"230231    class OtherView(flask.views.MethodView):232        def post(self):233            return "POST"234235    class View(GetView, OtherView):236        methods = ["GET"]237238    app.add_url_rule("/", view_func=View.as_view("index"))239240    assert client.get("/").data == b"GET"241    assert client.post("/").status_code == 405242    assert sorted(View.methods) == ["GET"]243244245def test_init_once(app, client):246    n = 0247248    class CountInit(flask.views.View):249        init_every_request = False250251        def __init__(self):252            nonlocal n253            n += 1254255        def dispatch_request(self):256            return str(n)257258    app.add_url_rule("/", view_func=CountInit.as_view("index"))259    assert client.get("/").data == b"1"260    assert client.get("/").data == b"1"