Skip to content
Myr

Add kwarg to disable auto OPTIONS on add_url_rule

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

Adds support for a kwarg `provide_automatic_options` on `add_url_rule`, which lets you turn off the automatic OPTIONS response on a per-URL basis even if your view functions are functions, not classes (so you can't provide attrs on them).

authored by Jimmy McCarthy · committed by Jimmy McCarthy · parent db09c67 · Browse files · 2 files+45 added−2 removed
This commit

State

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

Diff stats

2 files+45 added−2 removed

  • tests:+41 added−0 removed
  • other:+4 added−2 removed

2 files+45 added−2 removed

modified:flask/app.py+4 added-2 removed
1011 unmodified lines
1012 required_methods = set(getattr(view_func, 'required_methods', ()))10131014 # starting with Flask 0.8 the view_func object can disable and1015 # force-enable the automatic options handling.1016 provide_automatic_options = getattr(view_func,1017 'provide_automatic_options', None)1016 provide_automatic_options = options.pop(1017 'provide_automatic_options', getattr(view_func,1018 'provide_automatic_options', None))101910201021 if provide_automatic_options is None:1022 if 'OPTIONS' not in methods:1023 provide_automatic_options = True1024 required_methods.add('OPTIONS')
952 unmodified lines
modified:tests/test_basic.py+41 added
1602 unmodified lines
1603 monkeypatch.setattr(werkzeug.serving, 'run_simple', run_simple_mock)1604 hostname, port = 'localhost', 80001605 app.run(hostname, port, debug=True)1606 assert rv['result'] == 'running on %s:%s ...' % (hostname, port)160716081609def test_disable_automatic_options():1610 # Issue 1488: Add support for a kwarg to add_url_rule to disable the auto OPTIONS response1611 app = flask.Flask(__name__)16121613 def index():1614 return flask.request.method16151616 def more():1617 return flask.request.method16181619 app.add_url_rule('/', 'index', index, provide_automatic_options=False)1620 app.add_url_rule('/more', 'more', more, methods=['GET', 'POST'], provide_automatic_options=False)16211622 c = app.test_client()1623 assert c.get('/').data == b'GET'1624 rv = c.post('/')1625 assert rv.status_code == 4051626 assert sorted(rv.allow) == ['GET', 'HEAD']1627 # Older versions of Werkzeug.test.Client don't have an options method1628 if hasattr(c, 'options'):1629 rv = c.options('/')1630 else:1631 rv = c.open('/', method='OPTIONS')1632 assert rv.status_code == 40516331634 rv = c.head('/')1635 assert rv.status_code == 2001636 assert not rv.data # head truncates1637 assert c.post('/more').data == b'POST'1638 assert c.get('/more').data == b'GET'1639 rv = c.delete('/more')1640 assert rv.status_code == 4051641 assert sorted(rv.allow) == ['GET', 'HEAD', 'POST']1642 # Older versions of Werkzeug.test.Client don't have an options method1643 if hasattr(c, 'options'):1644 rv = c.options('/more')1645 else:1646 rv = c.open('/more', method='OPTIONS')1647 assert rv.status_code == 405