Browse files

View functions can opt out of the default OPTIONS implementation

  • Loading branch information...
1 parent f8b9f4d commit 3b31df81aeccb889810b4b49ce6e608e67ebeeea @mitsuhiko mitsuhiko committed Jul 14, 2011
Showing with 32 additions and 4 deletions.
  1. +2 −0 CHANGES
  2. +13 −4 flask/app.py
  3. +17 −0 tests/flask_tests.py
View
2 CHANGES
@@ -12,6 +12,8 @@ Relase date to be decided, codename to be chosen.
the implementation of the sessions can be changed without
having to override the Flask class.
- Empty session cookies are now deleted properly automatically.
+- View functions can now opt out of getting the automatic
+ OPTIONS implementation.
Version 0.7.2
-------------
View
17 flask/app.py
@@ -703,15 +703,24 @@ def index():
endpoint = _endpoint_from_view_func(view_func)
options['endpoint'] = endpoint
methods = options.pop('methods', None)
+
# 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
# a tuple of only `GET` as default.
if methods is None:
methods = getattr(view_func, 'methods', None) or ('GET',)
- provide_automatic_options = False
- if 'OPTIONS' not in methods:
- methods = tuple(methods) + ('OPTIONS',)
- provide_automatic_options = True
+
+ # 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)
+
+ 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
# None if they are an empty dictionary. This should not be necessary
View
17 tests/flask_tests.py
@@ -194,6 +194,23 @@ def index_put():
rv = app.test_client().open('/', method='OPTIONS')
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):
app = flask.Flask(__name__)
@app.route('/')

0 comments on commit 3b31df8

Please sign in to comment.