8000 Add legacy GCF Python 3.7 behavior (#77) · jrmfg/functions-framework-python@81b1c32 · GitHub
[go: up one dir, main page]

Skip to content

Commit 81b1c32

Browse files
authored
Add legacy GCF Python 3.7 behavior (GoogleCloudPlatform#77)
* Add legacy GCF Python 3.7 behavior * Add test * Modify tests
1 parent 95d0b35 commit 81b1c32

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

src/functions_framework/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,19 @@ def create_app(target=None, source=None, signature_type=None):
174174
app = flask.Flask(target, template_folder=template_folder)
175175
app.config["MAX_CONTENT_LENGTH"] = MAX_CONTENT_LENGTH
176176

177+
# 6. Handle legacy GCF Python 3.7 behavior
178+
if os.environ.get("ENTRY_POINT"):
179+
os.environ["FUNCTION_TRIGGER_TYPE"] = signature_type
180+
os.environ["FUNCTION_NAME"] = os.environ.get("K_SERVICE", target)
181+
app.make_response_original = app.make_response
182+
183+
def handle_none(rv):
184+
if rv is None:
185+
rv = "OK"
186+
return app.make_response_original(rv)
187+
188+
app.make_response = handle_none
189+
177190
# Extract the target function from the source file
178191
try:
179192
function = getattr(source_module, target)

tests/test_functions.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
1516
import pathlib
1617
import re
1718
import time
@@ -455,3 +456,42 @@ def test_class_in_main_is_in_right_module():
455456
resp = client.get("/")
456457

457458
assert resp.status_code == 200
459+
460+
461+
def test_function_returns_none():
462+
source = TEST_FUNCTIONS_DIR / "returns_none" / "main.py"
463+
target = "function"
464+
465+
client = create_app(target, source).test_client()
466+
resp = client.get("/")
467+
468+
assert resp.status_code == 500
469+
470+
471+
def test_legacy_function_check_env(monkeypatch):
472+
source = TEST_FUNCTIONS_DIR / "http_check_env" / "main.py"
473+
target = "function"
474+
475+
monkeypatch.setenv("ENTRY_POINT", target)
476+
477+
client = create_app(target, source).test_client()
478+
resp = client.post("/", json={"mode": "FUNCTION_TRIGGER_TYPE"})
479+
assert resp.status_code == 200
480+
assert resp.data == b"http"
481+
482+
resp = client.post("/", json={"mode": "FUNCTION_NAME"})
483+
assert resp.status_code == 200
484+
assert resp.data.decode("utf-8") == target
485+
486+
487+
def test_legacy_function_returns_none(monkeypatch):
488+
source = TEST_FUNCTIONS_DIR / "returns_none" / "main.py"
489+
target = "function"
490+
491+
monkeypatch.setenv("ENTRY_POINT", target)
492+
493+
client = create_app(target, source).test_client()
494+
resp = client.get("/")
495+
496+
assert resp.status_code == 200
497+
assert resp.data == b"OK"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2020 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+
def function(request):
17+
"""Test HTTP function when using legacy GCF behavior.
18+
19+
The function returns None, which should be a 200 response.
20+
21+
Args:
22+
request: The HTTP request which triggered this function.
23+
24+
Returns:
25+
None.
26+
"""
27+
return None

0 commit comments

Comments
 (0)
0