|
14 | 14 |
|
15 | 15 | # [START functions_http_integration_test] |
16 | 16 | import os |
17 | | -import subprocess |
18 | 17 | import uuid |
19 | 18 |
|
20 | | -import requests |
21 | | -from requests.packages.urllib3.util.retry import Retry |
| 19 | +from functions_framework import create_app |
22 | 20 |
|
23 | 21 |
|
24 | 22 | def test_args(): |
25 | 23 | name = str(uuid.uuid4()) |
26 | | - port = os.getenv( |
27 | | - "PORT", 8080 |
28 | | - ) # Each functions framework instance needs a unique port |
29 | | - |
30 | | - process = subprocess.Popen( |
31 | | - ["functions-framework", "--target", "hello_http", "--port", str(port)], |
32 | | - cwd=os.path.dirname(__file__), |
33 | | - stdout=subprocess.PIPE, |
34 | | - ) |
35 | | - |
36 | | - # Send HTTP request simulating Pub/Sub message |
37 | | - # (GCF translates Pub/Sub messages to HTTP requests internally) |
38 | | - BASE_URL = f"http://localhost:{port}" |
39 | | - |
40 | | - retry_policy = Retry(total=6, backoff_factor=1) |
41 | | - retry_adapter = requests.adapters.HTTPAdapter(max_retries=retry_policy) |
42 | | - |
43 | | - session = requests.Session() |
44 | | - session.mount(BASE_URL, retry_adapter) |
45 | | - |
46 | | - name = str(uuid.uuid4()) |
47 | | - res = session.post(BASE_URL, json={"name": name}) |
48 | | - assert res.text == f"Hello {name}!" |
49 | | - |
50 | | - # Stop the functions framework process |
51 | | - process.kill() |
52 | | - process.wait() |
53 | | - |
| 24 | + target = "hello_http" |
| 25 | + source = os.path.join(os.path.dirname(__file__), "main.py") |
| 26 | + client = create_app(target, source).test_client() |
| 27 | + resp = client.post("/my_path", json={"name": name}) |
| 28 | + assert resp.status_code == 200 |
| 29 | + expected_response = f"Hello {name}!" |
| 30 | + assert resp.data.decode("utf-8") == expected_response |
54 | 31 |
|
55 | 32 | # [END functions_http_integration_test] |
0 commit comments