8000 Added samples for handling CORS requests. (#1652) · cevaris/python-docs-samples@779a525 · GitHub
[go: up one dir, main page]

Skip to content

Commit 779a525

Browse files
author
chenyumic
authored
Added samples for handling CORS requests. (GoogleCloudPlatform#1652)
* Added samples for handling CORS requests. * Minor fix * Moved samples to http/ * Minor fix.
1 parent 1c7775c commit 779a525

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

functions/http/main.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,60 @@ def get_signed_url(request):
118118

119119
return url
120120
# [END functions_http_signed_url]
121+
122+
123+
# [START functions_http_cors]
124+
def cors_enabled_function(request):
125+
# For more information about CORS and CORS preflight requests, see
126+
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
127+
# for more information.
128+
129+
# Set CORS headers for the preflight request
130+
if request.method == 'OPTIONS':
131+
# Allows GET requests from any origin with the Content-Type
132+
# header and caches preflight response for an 3600s
133+
headers = {
134+
'Access-Control-Allow-Origin': '*',
135+
'Access-Control-Allow-Methods': 'GET',
136+
'Access-Control-Allow-Headers': 'Content-Type',
137+
'Access-Control-Max-Age': '3600'
138+
}
139+
140+
return ('', 204, headers)
141+
142+
# Set CORS headers for the main request
143+
headers = {
144+
'Access-Control-Allow-Origin': '*'
145+
}
146+
147+
return ('Hello World!', 200, headers)
148+
# [END functions_http_cors]
149+
150+
151+
# [START functions_http_cors_auth]
152+
def cors_enabled_function_auth(request):
153+
# For more information about CORS and CORS preflight requests, see
154+
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
155+
# for more information.
156+
157+
# Set CORS headers for preflight requests
158+
if request.method == 'OPTIONS':
159+
# Allows GET requests from origin https://mydomain.com with
160+
# Authorization header
161+
headers = {
162+
'Access-Control-Allow-Origin': 'https://mydomain.com',
163+
'Access-Control-Allow-Methods': 'GET',
164+
'Access-Control-Allow-Headers': 'Authorization',
165+
'Access-Control-Max-Age': '3600',
166+
'Access-Control-Allow-Credentials': 'true'
167+
}
168+
return ('', 204, headers)
169+
170+
# Set CORS headers for main requests
171+
headers = {
172+
'Access-Control-Allow-Origin': 'https://mydomain.com',
173+
'Access-Control-Allow-Credentials': 'true'
174+
}
175+
176+
return ('Hello World!', 200, headers)
177+
# [END functions_http_cors_auth]

functions/http/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 8000 +
# 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)
0