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
Provenance
- Commit
9569e8c- Tree
fc6e542- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
f4a1ca8- Parent 2
97e2cd0
3 files changed+61 added−4 removed
modified:flask/app.py+8 added-3 removed
978 unmodified lines979 """980 return iter(self._blueprint_order)981 982 @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 lines1019 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 limited21 unmodified lines1052 required_methods = set(getattr(view_func, 'required_methods', ()))1053 1054 # 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)1059 1060 if provide_automatic_options is None:1061 if 'OPTIONS' not in methods:1062 provide_automatic_options = True968 unmodified linesmodified:tests/test_basic.py+49 added-1 removed
48 unmodified lines49 rv = app.test_client().open('/', method='OPTIONS')50 assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS', 'POST', 'PUT']51 52 53-def test_options_handling_disabled():53+def test_provide_automatic_options_attr():54 app = flask.Flask(__name__)55 56 def index():57 return 'Hello World!'11 unmodified lines69 rv = app.test_client().open('/', method='OPTIONS')70 assert sorted(rv.allow) == ['OPTIONS']71 72 73+def test_provide_automatic_options_kwarg():74+ app = flask.Flask(__name__)75+76+ def index():77+ return flask.request.method78+79+ def more():80+ return flask.request.method81+82+ 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+ )87+88+ c = app.test_client()89+ assert c.get('/').data == b'GET'90+91+ rv = c.post('/')92+ assert rv.status_code == 40593+ assert sorted(rv.allow) == ['GET', 'HEAD']94+95+ # 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')100+101+ assert rv.status_code == 405102+103+ 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'108+109+ rv = c.delete('/more')110+ assert rv.status_code == 405111+ assert sorted(rv.allow) == ['GET', 'HEAD', 'POST']112+113+ if hasattr(c, 'options'):114+ rv = c.options('/more')115+ else:116+ rv = c.open('/more', method='OPTIONS')117+118+ assert rv.status_code == 405119+120+121 def test_request_dispatching():122 app = flask.Flask(__name__)123 124 @app.route('/')1677 unmodified linesmodified:CHANGES+4 added
19 unmodified lines20 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`_).27 28+.. _#1489: https://github.com/pallets/flask/pull/148929 .. _#2017: https://github.com/pallets/flask/pull/201730 .. _#2223: https://github.com/pallets/flask/pull/222331 32 Version 0.12.1610 unmodified lines