Skip to content

Commit de981be

Browse files
committed
Added samples for handling CORS requests.
1 parent de2cb86 commit de981be

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

functions/cors/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>
2+
3+
# Google Cloud Functions - Hello World sample
4+
5+
See:
6+
7+
* [Cloud Functions Handling CORS requests tutorial][tutorial]
8+
* [Cloud Functions Handling CORS requests sample source code][code]
9+
10+
[tutorial]: https://cloud.google.com/functions/docs/writing/http#handling_cors_requests
11+
[code]: main.py

functions/cors/main.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
# [START functions_cors]
17+
def cors_enabled_function(request):
18+
# Set CORS headers for the preflight request
19+
# e.g. allows GETs from any origin with the Content-Type header
20+
# and cache preflight response for an 3600s
21+
preflight_request_headers = {
22+
'Access-Control-Allow-Origin': '*',
23+
'Access-Control-Allow-Methods': 'GET',
24+
'Access-Control-Allow-Headers': 'Content-Type',
25+
'Access-Control-Max-Age': '3600'
26+
}
27+
28+
# Send response to OPTIONS requests and terminate the function execution
29+
if request.method == 'OPTIONS':
30+
return ('', 204, preflight_request_headers)
31+
32+
# Set CORS headers for the main request
33+
main_request_headers = {
34+
'Access-Control-Allow-Origin': '*'
35+
}
36+
37+
return ('Hello World!', 200, main_request_headers)
38+
# [END functions_cors]
39+
40+
41+
# [START functions_cors_authentication]
42+
def cors_enabled_function_auth(request):
43+
# Set CORS headers for preflight requests
44+
# e.g. allows GETS from origin https://mydomain.com with Authorization
45+
# header
46+
preflight_request_headers = {
47+
'Access-Control-Allow-Origin': 'https://mydomain.com',
48+
'Access-Control-Allow-Methods': 'GET',
49+
'Access-Control-Allow-Headers': 'Authorization',
50+
'Access-Control-Max-Age': '3600',
51+
'Access-Control-Allow-Credentials': 'true'
52+
}
53+
54+
# Send response to OPTIONS requests and terminate the function execution
55+
if request.method == 'OPTIONS':
56+
return ('', 204, preflight_request_headers)
57+
58+
# Set CORS headers for main requests
59+
main_request_headers = {
60+
'Access-Control-Allow-Origin': 'https://mydomain.com',
61+
'Access-Control-Allow-Credentials': 'true'
62+
}
63+
64+
return ('Hello World!', 200, main_request_headers)
65+
# [END functions_cors_authentication]

functions/cors/main_test.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import flask
16+
import pytest
17+
18+
import main
19+
20+
21+
# Create a fake "app" for generating test request contexts.
22+
@pytest.fixture(scope="module")
23+
def app():
24+
return flask.Flask(__name__)
25+
26+
27+
def test_cors_enabled_function_preflight(app):
28+
with app.test_request_context(method='OPTIONS'):
29+
res = main.cors_enabled_function(flask.request)
30+
assert res[2].get('Access-Control-Allow-Origin') == '*'
31+
assert res[2].get('Access-Control-Allow-Methods') == 'GET'
32+
assert res[2].get('Access-Control-Allow-Headers') == 'Content-Type'
33+
assert res[2].get('Access-Control-Max-Age') == '3600'
34+
35+
36+
def test_cors_enabled_function_main(app):
37+
with app.test_request_context(method='GET'):
38+
res = main.cors_enabled_function(flask.request)
39+
assert res[2].get('Access-Control-Allow-Origin') == '*'
40+
41+
42+
def test_cors_enabled_function_auth_preflight(app):
43+
with app.test_request_context(method='OPTIONS'):
44+
res = main.cors_enabled_function_auth(flask.request)
45+
assert res[2].get('Access-Control-Allow-Origin') == \
46+
'https://mydomain.com'
47+
assert res[2].get('Access-Control-Allow-Methods') == 'GET'
48+
assert res[2].get('Access-Control-Allow-Headers') == 'Authorization'
49+
assert res[2].get('Access-Control-Max-Age') == '3600'
50+
assert res[2].get('Access-Control-Allow-Credentials') == 'true'
51+
52+
53+
def test_cors_enabled_function_auth_main(app):
54+
with app.test_request_context(method='GET'):
55+
res = main.cors_enabled_function_auth(flask.request)
56+
assert res[2].get('Access-Control-Allow-Origin') == \
57+
'https://mydomain.com'
58+
assert res[2].get('Access-Control-Allow-Credentials') == 'true'

0 commit comments

Comments
 (0)