Skip to content
Myr

Merge pull request #2249 from davidism/provide-automatic-options

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Continue #1489: Add kwarg to disable auto OPTIONS on add_url_rule

authored by David Lord · committed by GitHub · parents f4a1ca8, 97e2cd0 · Browse files · 3 files+61 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+61 added−4 removed

  • tests:+49 added−1 removed
  • other:+12 added−3 removed

3 files+61 added−4 removed

modified:flask/app.py+8 added-3 removed
978 unmodified lines
979 """980 return iter(self._blueprint_order)981982 @setupmethod983 def add_url_rule(self, rule, endpoint=None, view_func=None, **options):983 def add_url_rule(self, rule, endpoint=None, view_func=None, provide_automatic_options=None, **options):984 """Connects a URL rule. Works exactly like the :meth:`route`985 decorator. If a view_func is provided it will be registered with the986 endpoint.987
31 unmodified lines
1019 itself assumes the name of the view function as1020 endpoint1021 :param view_func: the function to call when serving a request to the1022 provided endpoint1023 :param provide_automatic_options: controls whether the ``OPTIONS``1024 method should be added automatically. This can also be controlled1025 by setting the ``view_func.provide_automatic_options = False``1026 before adding the rule.1027 :param options: the options to be forwarded to the underlying1028 :class:`~werkzeug.routing.Rule` object. A change1029 to Werkzeug is handling of method options. methods1030 is a list of methods this rule should be limited
21 unmodified lines
1052 required_methods = set(getattr(view_func, 'required_methods', ()))10531054 # starting with Flask 0.8 the view_func object can disable and1055 # force-enable the automatic options handling.1056 if provide_automatic_options is None:1052 provide_automatic_options = getattr(view_func,1053 'provide_automatic_options', None)1057 provide_automatic_options = getattr(view_func,1058 'provide_automatic_options', None)10591060 if provide_automatic_options is None:1061 if 'OPTIONS' not in methods:1062 provide_automatic_options = True
968 unmodified lines
modified:tests/test_basic.py+49 added-1 removed
48 unmodified lines
49 rv = app.test_client().open('/', method='OPTIONS')50 assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS', 'POST', 'PUT']515253def test_options_handling_disabled():53def test_provide_automatic_options_attr():54 app = flask.Flask(__name__)5556 def index():57 return 'Hello World!'
11 unmodified lines
69 rv = app.test_client().open('/', method='OPTIONS')70 assert sorted(rv.allow) == ['OPTIONS']717273def test_provide_automatic_options_kwarg():74 app = flask.Flask(__name__)7576 def index():77 return flask.request.method7879 def more():80 return flask.request.method8182 app.add_url_rule('/', view_func=index, provide_automatic_options=False)83 app.add_url_rule(84 '/more', view_func=more, methods=['GET', 'POST'],85 provide_automatic_options=False86 )8788 c = app.test_client()89 assert c.get('/').data == b'GET'9091 rv = c.post('/')92 assert rv.status_code == 40593 assert sorted(rv.allow) == ['GET', 'HEAD']9495 # Older versions of Werkzeug.test.Client don't have an options method96 if hasattr(c, 'options'):97 rv = c.options('/')98 else:99 rv = c.open('/', method='OPTIONS')100101 assert rv.status_code == 405102103 rv = c.head('/')104 assert rv.status_code == 200105 assert not rv.data # head truncates106 assert c.post('/more').data == b'POST'107 assert c.get('/more').data == b'GET'108109 rv = c.delete('/more')110 assert rv.status_code == 405111 assert sorted(rv.allow) == ['GET', 'HEAD', 'POST']112113 if hasattr(c, 'options'):114 rv = c.options('/more')115 else:116 rv = c.open('/more', method='OPTIONS')117118 assert rv.status_code == 405119120121def test_request_dispatching():122 app = flask.Flask(__name__)123124 @app.route('/')
1677 unmodified lines
modified:CHANGES+4 added
19 unmodified lines
20 be properly associated with the required host. (``#1559``)21- ``send_file`` supports Unicode in ``attachment_filename``. (`#2223`_)22- Pass ``_scheme`` argument from ``url_for`` to ``handle_build_error``.23 (`#2017`_)24- Add support for ``provide_automatic_options`` in ``add_url_rule`` to disable25 adding OPTIONS method when the ``view_func`` argument is not a class.26 (`#1489`_).2728.. _#1489: https://github.com/pallets/flask/pull/148929.. _#2017: https://github.com/pallets/flask/pull/201730.. _#2223: https://github.com/pallets/flask/pull/22233132Version 0.12.1
610 unmodified lines