add no_auto_head option in app route decorator
This commit is contained in:
parent
660994efc7
commit
7a4c9772be
2 changed files with 22 additions and 0 deletions
|
|
@ -1046,6 +1046,7 @@ class Flask(Scaffold):
|
||||||
endpoint = _endpoint_from_view_func(view_func) # type: ignore
|
endpoint = _endpoint_from_view_func(view_func) # type: ignore
|
||||||
options["endpoint"] = endpoint
|
options["endpoint"] = endpoint
|
||||||
methods = options.pop("methods", None)
|
methods = options.pop("methods", None)
|
||||||
|
no_auto_head = options.pop("no_auto_head", None)
|
||||||
|
|
||||||
# if the methods are not given and the view_func object knows its
|
# if the methods are not given and the view_func object knows its
|
||||||
# methods we can use that instead. If neither exists, we go with
|
# methods we can use that instead. If neither exists, we go with
|
||||||
|
|
@ -1082,6 +1083,14 @@ class Flask(Scaffold):
|
||||||
rule = self.url_rule_class(rule, methods=methods, **options)
|
rule = self.url_rule_class(rule, methods=methods, **options)
|
||||||
rule.provide_automatic_options = provide_automatic_options # type: ignore
|
rule.provide_automatic_options = provide_automatic_options # type: ignore
|
||||||
|
|
||||||
|
# If GET is only specified method, Werkzeug automatically adds HEAD method.
|
||||||
|
# This option will disable that feature to allow the user to write their
|
||||||
|
# own handling for HEAD.
|
||||||
|
only_get = methods == {"GET", "OPTIONS"}
|
||||||
|
|
||||||
|
if only_get and no_auto_head is not None:
|
||||||
|
rule.methods.discard("HEAD")
|
||||||
|
|
||||||
self.url_map.add(rule)
|
self.url_map.add(rule)
|
||||||
if view_func is not None:
|
if view_func is not None:
|
||||||
old_func = self.view_functions.get(endpoint)
|
old_func = self.view_functions.get(endpoint)
|
||||||
|
|
|
||||||
|
|
@ -2024,3 +2024,16 @@ def test_app_freed_on_zero_refcount():
|
||||||
assert weak() is None
|
assert weak() is None
|
||||||
finally:
|
finally:
|
||||||
gc.enable()
|
gc.enable()
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_auto_head_option(app, client):
|
||||||
|
@app.route("/", methods=["GET"], no_auto_head=True)
|
||||||
|
def index():
|
||||||
|
return "Hello world", 200
|
||||||
|
|
||||||
|
@app.route("/", methods=["HEAD"])
|
||||||
|
def index_head():
|
||||||
|
return "", 200, {"test-header": "test-value"}
|
||||||
|
|
||||||
|
assert client.get("/").data == b"Hello world"
|
||||||
|
assert "test-header" in client.head("/").headers
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue