Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions flask/testsuite/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions flask/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down