Skip to content
Merged
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
39 changes: 8 additions & 31 deletions functions/helloworld/sample_http_test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,19 @@

# [START functions_http_integration_test]
import os
import subprocess
import uuid

import requests
from requests.packages.urllib3.util.retry import Retry
from functions_framework import create_app


def test_args():
name = str(uuid.uuid4())
port = os.getenv(
"PORT", 8080
) # Each functions framework instance needs a unique port

process = subprocess.Popen(
["functions-framework", "--target", "hello_http", "--port", str(port)],
cwd=os.path.dirname(__file__),
stdout=subprocess.PIPE,
)

# Send HTTP request simulating Pub/Sub message
# (GCF translates Pub/Sub messages to HTTP requests internally)
BASE_URL = f"http://localhost:{port}"

retry_policy = Retry(total=6, backoff_factor=1)
retry_adapter = requests.adapters.HTTPAdapter(max_retries=retry_policy)

session = requests.Session()
session.mount(BASE_URL, retry_adapter)

name = str(uuid.uuid4())
res = session.post(BASE_URL, json={"name": name})
assert res.text == f"Hello {name}!"

# Stop the functions framework process
process.kill()
process.wait()

target = "hello_http"
source = os.path.join(os.path.dirname(__file__), "main.py")
Comment thread
irudykss marked this conversation as resolved.
client = create_app(target, source).test_client()
resp = client.post("/my_path", json={"name": name})
assert resp.status_code == 200
expected_response = f"Hello {name}!"
assert resp.data.decode("utf-8") == expected_response

# [END functions_http_integration_test]