Skip to content

Commit

Permalink
Add kwarg to disable auto OPTIONS on add_url_rule
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Jimmy McCarthy committed Jun 8, 2015
1 parent b471df6 commit 3b01207
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
6 changes: 4 additions & 2 deletions flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,8 +1013,10 @@ def index():

# 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:
Expand Down
41 changes: 41 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1604,3 +1604,44 @@ def run_simple_mock(hostname, port, application, *args, **kwargs):
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

0 comments on commit 3b01207

Please sign in to comment.