|
20 | 20 | from werkzeug.routing import RequestRedirect |
21 | 21 |
|
22 | 22 | import flask |
| 23 | +from flask.testing import FlaskClient |
23 | 24 |
|
24 | 25 | require_cpython_gc = pytest.mark.skipif( |
25 | 26 | python_implementation() != "CPython", |
@@ -67,63 +68,61 @@ def test_method_route_no_methods(app): |
67 | 68 | app.get("/", methods=["GET", "POST"]) |
68 | 69 |
|
69 | 70 |
|
70 | | -def test_provide_automatic_options_attr(): |
71 | | - app = flask.Flask(__name__) |
| 71 | +def test_provide_automatic_options_attr_disable( |
| 72 | + app: flask.Flask, client: FlaskClient |
| 73 | +) -> None: |
| 74 | + """Automatic options can be disabled by the view func attribute.""" |
72 | 75 |
|
73 | 76 | def index(): |
74 | 77 | return "Hello World!" |
75 | 78 |
|
76 | 79 | index.provide_automatic_options = False |
77 | | - app.route("/")(index) |
78 | | - rv = app.test_client().open("/", method="OPTIONS") |
| 80 | + app.add_url_rule("/", view_func=index) |
| 81 | + rv = client.options() |
79 | 82 | assert rv.status_code == 405 |
80 | 83 |
|
81 | | - app = flask.Flask(__name__) |
82 | 84 |
|
83 | | - def index2(): |
| 85 | +def test_provide_automatic_options_attr_enable( |
| 86 | + app: flask.Flask, client: FlaskClient |
| 87 | +) -> None: |
| 88 | + """When default automatic options is disabled in config, it can still be |
| 89 | + enabled by the view function attribute. |
| 90 | + """ |
| 91 | + app.config["PROVIDE_AUTOMATIC_OPTIONS"] = False |
| 92 | + |
| 93 | + def index(): |
84 | 94 | return "Hello World!" |
85 | 95 |
|
86 | | - index2.provide_automatic_options = True |
87 | | - app.route("/", methods=["OPTIONS"])(index2) |
88 | | - rv = app.test_client().open("/", method="OPTIONS") |
89 | | - assert sorted(rv.allow) == ["OPTIONS"] |
| 96 | + index.provide_automatic_options = True |
| 97 | + app.add_url_rule("/", view_func=index) |
| 98 | + rv = client.options() |
| 99 | + assert rv.allow == {"GET", "HEAD", "OPTIONS"} |
90 | 100 |
|
91 | 101 |
|
92 | | -def test_provide_automatic_options_kwarg(app, client): |
93 | | - def index(): |
94 | | - return flask.request.method |
95 | | - |
96 | | - def more(): |
97 | | - return flask.request.method |
| 102 | +def test_provide_automatic_options_arg_disable( |
| 103 | + app: flask.Flask, client: FlaskClient |
| 104 | +) -> None: |
| 105 | + """Automatic options can be disabled by the route argument.""" |
98 | 106 |
|
99 | | - app.add_url_rule("/", view_func=index, provide_automatic_options=False) |
100 | | - app.add_url_rule( |
101 | | - "/more", |
102 | | - view_func=more, |
103 | | - methods=["GET", "POST"], |
104 | | - provide_automatic_options=False, |
105 | | - ) |
106 | | - assert client.get("/").data == b"GET" |
| 107 | + @app.get("/", provide_automatic_options=False) |
| 108 | + def index(): |
| 109 | + return "Hello World!" |
107 | 110 |
|
108 | | - rv = client.post("/") |
| 111 | + rv = client.options() |
109 | 112 | assert rv.status_code == 405 |
110 | | - assert sorted(rv.allow) == ["GET", "HEAD"] |
111 | 113 |
|
112 | | - rv = client.open("/", method="OPTIONS") |
113 | | - assert rv.status_code == 405 |
114 | 114 |
|
115 | | - rv = client.head("/") |
116 | | - assert rv.status_code == 200 |
117 | | - assert not rv.data # head truncates |
118 | | - assert client.post("/more").data == b"POST" |
119 | | - assert client.get("/more").data == b"GET" |
| 115 | +def test_provide_automatic_options_method_disable( |
| 116 | + app: flask.Flask, client: FlaskClient |
| 117 | +) -> None: |
| 118 | + """Automatic options is ignored if the route handles options.""" |
120 | 119 |
|
121 | | - rv = client.delete("/more") |
122 | | - assert rv.status_code == 405 |
123 | | - assert sorted(rv.allow) == ["GET", "HEAD", "POST"] |
| 120 | + @app.route("/", methods=["OPTIONS"]) |
| 121 | + def index(): |
| 122 | + return "", {"X-Test": "test"} |
124 | 123 |
|
125 | | - rv = client.open("/more", method="OPTIONS") |
126 | | - assert rv.status_code == 405 |
| 124 | + rv = client.options() |
| 125 | + assert rv.headers["X-Test"] == "test" |
127 | 126 |
|
128 | 127 |
|
129 | 128 | def test_request_dispatching(app, client): |
|
0 commit comments