Implement support for X-HTTP-Method-Override in flask.views.MethodView. #582

Closed
wants to merge 1 commit into from
View
19 flask/testsuite/views.py
@@ -145,6 +145,25 @@ def head(self):
self.assert_equal(rv.data, '')
self.assert_equal(rv.headers['X-Method'], 'HEAD')
+ def test_method_override(self):
+ app = flask.Flask(__name__)
+
+ class Override(flask.views.MethodView):
+ def put(self):
+ return 'PUT'
+
+ def post(self):
+ return 'POST'
+
+ app.add_url_rule('/', view_func=Override.as_view('override'))
+ c = app.test_client()
+ rv = c.post('/')
+ self.assert_equal(rv.data, 'POST')
+ rv = c.put('/')
+ self.assert_equal(rv.data, 'PUT')
+ rv = c.post('/', headers=[('X-HTTP-Method-Override', 'PUT')])
+ self.assert_equal(rv.data, 'PUT')
+
def suite():
suite = unittest.TestSuite()
View
7 flask/views.py
@@ -142,6 +142,13 @@ def post(self):
def dispatch_request(self, *args, **kwargs):
meth = getattr(self, request.method.lower(), None)
+
+ # If the request method has been explicitly overriden, use that
+ # instead of the original HTTP verb.
+ if 'X-HTTP-Method-Override' in request.headers:
+ override = request.headers['X-HTTP-Method-Override']
+ meth = getattr(self, override.lower(), None)
+
# if the request method is HEAD and we don't have a handler for it
# retry with GET
if meth is None and request.method == 'HEAD':