Skip to content
Myr

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 file

Take it

1import pytest23import flask.views4from flask.testing import FlaskClient567def 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 = c.open("/", method="OPTIONS").allow14    assert 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 = client.open("/", method="OPTIONS").allow77    assert 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_disable(102    app: flask.Flask, client: FlaskClient103) -> None:104    """Automatic options can be disabled by the view class attribute."""105106    class Index(flask.views.View):107        provide_automatic_options = False108109        def dispatch_request(self):110            return "Hello World!"111112    app.add_url_rule("/", view_func=Index.as_view("index"))113    rv = client.options()114    assert rv.status_code == 405115116117def 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):126        provide_automatic_options = True127128        def dispatch_request(self):129            return "Hello World!"130131    app.add_url_rule("/", view_func=Index.as_view("index"))132    rv = client.options("/")133    assert rv.allow == {"GET", "HEAD", "OPTIONS"}134135136def 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"]143144        def dispatch_request(self):145            return "", {"X-Test": "test"}146147    app.add_url_rule("/", view_func=Index.as_view("index"))148    rv = client.options()149    assert rv.headers["X-Test"] == "test"150151152def 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):194        app.add_url_rule("/other", view_func=Index.as_view("index"))195196    # 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"