Skip to content

Commit 0f9448b

Browse files
fix: bug where init isn't called with cors enabled
1 parent a5f82a7 commit 0f9448b

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/firebase_functions/https_fn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ def on_request_wrapped(request: Request) -> Response:
438438
return _cross_origin(
439439
methods=options.cors.cors_methods,
440440
origins=options.cors.cors_origins,
441-
)(func)(request)
441+
)(_core._with_init(func))(request)
442442
return _core._with_init(func)(request)
443443

444444
_util.set_func_endpoint_attr(

tests/test_https_fn.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from werkzeug.test import EnvironBuilder
1010

1111
from firebase_functions import core, https_fn
12+
from firebase_functions.options import CorsOptions
1213

1314

1415
class TestHttps(unittest.TestCase):
@@ -42,6 +43,34 @@ def init():
4243

4344
self.assertEqual(hello, "world")
4445

46+
def test_on_request_calls_init_function_with_cors(self):
47+
app = Flask(__name__)
48+
49+
hello = None
50+
51+
@core.init
52+
def init():
53+
nonlocal hello
54+
hello = "world"
55+
56+
func = Mock(__name__="example_func", return_value="OK")
57+
58+
with app.test_request_context("/"):
59+
environ = EnvironBuilder(
60+
method="POST",
61+
json={
62+
"data": {"test": "value"},
63+
},
64+
).get_environ()
65+
request = Request(environ)
66+
decorated_func = https_fn.on_request(
67+
cors=CorsOptions(cors_origins="*", cors_methods="GET")
68+
)(func)
69+
70+
decorated_func(request)
71+
72+
self.assertEqual(hello, "world")
73+
4574
def test_on_call_calls_init_function(self):
4675
app = Flask(__name__)
4776

0 commit comments

Comments
 (0)