Browse Source

View functions can opt out of the default OPTIONS implementation

pull/280/merge
Armin Ronacher 14 years ago
parent
commit
3b31df81ae
  1. 2
      CHANGES
  2. 17
      flask/app.py
  3. 17
      tests/flask_tests.py

2
CHANGES

@ -12,6 +12,8 @@ Relase date to be decided, codename to be chosen.
the implementation of the sessions can be changed without the implementation of the sessions can be changed without
having to override the Flask class. having to override the Flask class.
- Empty session cookies are now deleted properly automatically. - Empty session cookies are now deleted properly automatically.
- View functions can now opt out of getting the automatic
OPTIONS implementation.
Version 0.7.2 Version 0.7.2
------------- -------------

17
flask/app.py

@ -703,15 +703,24 @@ class Flask(_PackageBoundObject):
endpoint = _endpoint_from_view_func(view_func) endpoint = _endpoint_from_view_func(view_func)
options['endpoint'] = endpoint options['endpoint'] = endpoint
methods = options.pop('methods', None) methods = options.pop('methods', None)
# if the methods are not given and the view_func object knows its # if the methods are not given and the view_func object knows its
# methods we can use that instead. If neither exists, we go with # methods we can use that instead. If neither exists, we go with
# a tuple of only `GET` as default. # a tuple of only `GET` as default.
if methods is None: if methods is None:
methods = getattr(view_func, 'methods', None) or ('GET',) methods = getattr(view_func, 'methods', None) or ('GET',)
provide_automatic_options = False
if 'OPTIONS' not in methods: # starting with Flask 0.8 the view_func object can disable and
methods = tuple(methods) + ('OPTIONS',) # force-enable the automatic options handling.
provide_automatic_options = True provide_automatic_options = getattr(view_func,
'provide_automatic_options', None)
if provide_automatic_options is None:
if 'OPTIONS' not in methods:
methods = tuple(methods) + ('OPTIONS',)
provide_automatic_options = True
else:
provide_automatic_options = False
# due to a werkzeug bug we need to make sure that the defaults are # due to a werkzeug bug we need to make sure that the defaults are
# None if they are an empty dictionary. This should not be necessary # None if they are an empty dictionary. This should not be necessary

17
tests/flask_tests.py

@ -194,6 +194,23 @@ class BasicFunctionalityTestCase(unittest.TestCase):
rv = app.test_client().open('/', method='OPTIONS') rv = app.test_client().open('/', method='OPTIONS')
assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS', 'POST', 'PUT'] assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS', 'POST', 'PUT']
def test_options_handling_disabled(self):
app = flask.Flask(__name__)
def index():
return 'Hello World!'
index.provide_automatic_options = False
app.route('/')(index)
rv = app.test_client().open('/', method='OPTIONS')
assert rv.status_code == 405
app = flask.Flask(__name__)
def index2():
return 'Hello World!'
index2.provide_automatic_options = True
app.route('/', methods=['OPTIONS'])(index2)
rv = app.test_client().open('/', method='OPTIONS')
assert sorted(rv.allow) == ['OPTIONS']
def test_request_dispatching(self): def test_request_dispatching(self):
app = flask.Flask(__name__) app = flask.Flask(__name__)
@app.route('/') @app.route('/')

Loading…
Cancel
Save