Skip to content
This repository
Browse code

View functions can opt out of the default OPTIONS implementation

  • Loading branch information...
commit 3b31df81aeccb889810b4b49ce6e608e67ebeeea 1 parent f8b9f4d
Armin Ronacher authored July 14, 2011
2  CHANGES
@@ -12,6 +12,8 @@ Relase date to be decided, codename to be chosen.
12 12
   the implementation of the sessions can be changed without
13 13
   having to override the Flask class.
14 14
 - Empty session cookies are now deleted properly automatically.
  15
+- View functions can now opt out of getting the automatic
  16
+  OPTIONS implementation.
15 17
 
16 18
 Version 0.7.2
17 19
 -------------
17  flask/app.py
@@ -703,15 +703,24 @@ def index():
703 703
             endpoint = _endpoint_from_view_func(view_func)
704 704
         options['endpoint'] = endpoint
705 705
         methods = options.pop('methods', None)
  706
+
706 707
         # if the methods are not given and the view_func object knows its
707 708
         # methods we can use that instead.  If neither exists, we go with
708 709
         # a tuple of only `GET` as default.
709 710
         if methods is None:
710 711
             methods = getattr(view_func, 'methods', None) or ('GET',)
711  
-        provide_automatic_options = False
712  
-        if 'OPTIONS' not in methods:
713  
-            methods = tuple(methods) + ('OPTIONS',)
714  
-            provide_automatic_options = True
  712
+
  713
+        # starting with Flask 0.8 the view_func object can disable and
  714
+        # force-enable the automatic options handling.
  715
+        provide_automatic_options = getattr(view_func,
  716
+            'provide_automatic_options', None)
  717
+
  718
+        if provide_automatic_options is None:
  719
+            if 'OPTIONS' not in methods:
  720
+                methods = tuple(methods) + ('OPTIONS',)
  721
+                provide_automatic_options = True
  722
+            else:
  723
+                provide_automatic_options = False
715 724
 
716 725
         # due to a werkzeug bug we need to make sure that the defaults are
717 726
         # None if they are an empty dictionary.  This should not be necessary
17  tests/flask_tests.py
@@ -194,6 +194,23 @@ def index_put():
194 194
         rv = app.test_client().open('/', method='OPTIONS')
195 195
         assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS', 'POST', 'PUT']
196 196
 
  197
+    def test_options_handling_disabled(self):
  198
+        app = flask.Flask(__name__)
  199
+        def index():
  200
+            return 'Hello World!'
  201
+        index.provide_automatic_options = False
  202
+        app.route('/')(index)
  203
+        rv = app.test_client().open('/', method='OPTIONS')
  204
+        assert rv.status_code == 405
  205
+
  206
+        app = flask.Flask(__name__)
  207
+        def index2():
  208
+            return 'Hello World!'
  209
+        index2.provide_automatic_options = True
  210
+        app.route('/', methods=['OPTIONS'])(index2)
  211
+        rv = app.test_client().open('/', method='OPTIONS')
  212
+        assert sorted(rv.allow) == ['OPTIONS']
  213
+
197 214
     def test_request_dispatching(self):
198 215
         app = flask.Flask(__name__)
199 216
         @app.route('/')

0 notes on commit 3b31df8

Please sign in to comment.
Something went wrong with that request. Please try again.