Skip to content

Add decorator support for async flask endpoints #374

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion flask_cors/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def wrapped_function(*args, **kwargs):
if options.get('automatic_options') and request.method == 'OPTIONS':
resp = current_app.make_default_options_response()
else:
resp = make_response(f(*args, **kwargs))
resp = make_response(current_app.ensure_sync(f)(*args, **kwargs))

set_cors_headers(resp, options)
setattr(resp, FLASK_CORS_EVALUATED, True)
Expand Down
28 changes: 28 additions & 0 deletions tests/decorator/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ def defaults():
def test_get():
return 'Only allow POST'

@self.app.route('/defaults_async')
@cross_origin()
async def defaults_async():
return 'Should only return headers on pre-flight OPTIONS request'

@self.app.route('/test_methods_defined_async')
@cross_origin(methods=['POST'])
async def test_get_async():
return 'Only allow POST'

def test_defaults(self):
''' Access-Control-Allow-Methods headers should only be returned
if the client makes an OPTIONS request.
Expand All @@ -41,6 +51,12 @@ def test_defaults(self):
for method in ALL_METHODS:
self.assertTrue(method in res.headers.get(ACL_METHODS))

self.assertFalse(ACL_METHODS in self.get('/defaults_async', origin='www.example.com').headers)
self.assertFalse(ACL_METHODS in self.head('/defaults_async', origin='www.example.com').headers)
res = self.preflight('/defaults_async', 'POST', origin='www.example.com')
for method in ALL_METHODS:
self.assertTrue(method in res.headers.get(ACL_METHODS))

def test_methods_defined(self):
''' If the methods parameter is defined, it should override the default
methods defined by the user.
Expand All @@ -57,5 +73,17 @@ def test_methods_defined(self):
res = self.get('/test_methods_defined', origin='www.example.com')
self.assertFalse(ACL_METHODS in res.headers)

self.assertFalse(ACL_METHODS in self.get('/test_methods_defined_async').headers)
self.assertFalse(ACL_METHODS in self.head('/test_methods_defined_async').headers)

res = self.preflight('/test_methods_defined_async', 'POST', origin='www.example.com')
self.assertTrue('POST' in res.headers.get(ACL_METHODS))

res = self.preflight('/test_methods_defined_async', 'PUT', origin='www.example.com')
self.assertFalse(ACL_METHODS in res.headers)

res = self.get('/test_methods_defined_async', origin='www.example.com')
self.assertFalse(ACL_METHODS in res.headers)

if __name__ == "__main__":
unittest.main()