Browse Source

Add kwarg to disable auto OPTIONS on add_url_rule

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).
pull/1489/head
Jimmy McCarthy 10 years ago
parent
commit
011b129b6b
  1. 6
      flask/app.py
  2. 41
      tests/test_basic.py

6
flask/app.py

@ -1013,8 +1013,10 @@ class Flask(_PackageBoundObject):
# starting with Flask 0.8 the view_func object can disable and
# force-enable the automatic options handling.
provide_automatic_options = getattr(view_func,
'provide_automatic_options', None)
provide_automatic_options = options.pop(
'provide_automatic_options', getattr(view_func,
'provide_automatic_options', None))
if provide_automatic_options is None:
if 'OPTIONS' not in methods:

41
tests/test_basic.py

@ -1604,3 +1604,44 @@ def test_run_server_port(monkeypatch):
hostname, port = 'localhost', 8000
app.run(hostname, port, debug=True)
assert rv['result'] == 'running on %s:%s ...' % (hostname, port)
def test_disable_automatic_options():
# Issue 1488: Add support for a kwarg to add_url_rule to disable the auto OPTIONS response
app = flask.Flask(__name__)
def index():
return flask.request.method
def more():
return flask.request.method
app.add_url_rule('/', 'index', index, provide_automatic_options=False)
app.add_url_rule('/more', 'more', more, methods=['GET', 'POST'], provide_automatic_options=False)
c = app.test_client()
assert c.get('/').data == b'GET'
rv = c.post('/')
assert rv.status_code == 405
assert sorted(rv.allow) == ['GET', 'HEAD']
# Older versions of Werkzeug.test.Client don't have an options method
if hasattr(c, 'options'):
rv = c.options('/')
else:
rv = c.open('/', method='OPTIONS')
assert rv.status_code == 405
rv = c.head('/')
assert rv.status_code == 200
assert not rv.data # head truncates
assert c.post('/more').data == b'POST'
assert c.get('/more').data == b'GET'
rv = c.delete('/more')
assert rv.status_code == 405
assert sorted(rv.allow) == ['GET', 'HEAD', 'POST']
# Older versions of Werkzeug.test.Client don't have an options method
if hasattr(c, 'options'):
rv = c.options('/more')
else:
rv = c.open('/more', method='OPTIONS')
assert rv.status_code == 405

Loading…
Cancel
Save