|
| 1 | +# Copyright 2025 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 sys |
| 16 | + |
| 17 | +import pretend |
| 18 | +import pytest |
| 19 | + |
| 20 | +from click.testing import CliRunner |
| 21 | + |
| 22 | +from functions_framework._cli import _cli as cli_command |
| 23 | + |
| 24 | + |
| 25 | +# Test for CLI gateway flag |
| 26 | +def test_cli_gateway_default(monkeypatch): |
| 27 | + """Test default gateway is WSGI""" |
| 28 | + server = pretend.stub(run=pretend.call_recorder(lambda *a, **kw: None)) |
| 29 | + app = pretend.stub() |
| 30 | + |
| 31 | + create_app = pretend.call_recorder(lambda *a, **kw: app) |
| 32 | + create_server = pretend.call_recorder(lambda *a, **kw: server) |
| 33 | + |
| 34 | + from functions_framework import _cli |
| 35 | + |
| 36 | + monkeypatch.setattr(_cli, "create_app", create_app) |
| 37 | + monkeypatch.setattr(_cli, "create_server", create_server) |
| 38 | + |
| 39 | + runner = CliRunner() |
| 40 | + result = runner.invoke(cli_command, ["--target", "foo"]) |
| 41 | + |
| 42 | + assert result.exit_code == 0 |
| 43 | + assert create_app.calls == [pretend.call("foo", None, "http")] |
| 44 | + assert server.run.calls == [pretend.call("0.0.0.0", 8080)] |
| 45 | + |
| 46 | + |
| 47 | +def test_cli_gateway_wsgi_explicit(monkeypatch): |
| 48 | + """Test explicit --gateway wsgi""" |
| 49 | + server = pretend.stub(run=pretend.call_recorder(lambda *a, **kw: None)) |
| 50 | + app = pretend.stub() |
| 51 | + |
| 52 | + create_app = pretend.call_recorder(lambda *a, **kw: app) |
| 53 | + create_server = pretend.call_recorder(lambda *a, **kw: server) |
| 54 | + |
| 55 | + from functions_framework import _cli |
| 56 | + |
| 57 | + monkeypatch.setattr(_cli, "create_app", create_app) |
| 58 | + monkeypatch.setattr(_cli, "create_server", create_server) |
| 59 | + |
| 60 | + runner = CliRunner() |
| 61 | + result = runner.invoke(cli_command, ["--target", "foo", "--gateway", "wsgi"]) |
| 62 | + |
| 63 | + assert result.exit_code == 0 |
| 64 | + assert create_app.calls == [pretend.call("foo", None, "http")] |
| 65 | + |
| 66 | + |
| 67 | +def test_cli_gateway_asgi(monkeypatch): |
| 68 | + """Test --gateway asgi""" |
| 69 | + # Skip this test if aio module is not available |
| 70 | + try: |
| 71 | + import functions_framework.aio |
| 72 | + except ImportError: |
| 73 | + pytest.skip("Async support not available") |
| 74 | + |
| 75 | + server = pretend.stub(run=pretend.call_recorder(lambda *a, **kw: None)) |
| 76 | + app = pretend.stub() |
| 77 | + |
| 78 | + # Mock the create_asgi_app function |
| 79 | + create_asgi_app = pretend.call_recorder(lambda *a, **kw: app) |
| 80 | + monkeypatch.setattr("functions_framework.aio.create_asgi_app", create_asgi_app) |
| 81 | + |
| 82 | + create_server = pretend.call_recorder(lambda *a, **kw: server) |
| 83 | + |
| 84 | + from functions_framework import _cli |
| 85 | + |
| 86 | + monkeypatch.setattr(_cli, "create_server", create_server) |
| 87 | + |
| 88 | + runner = CliRunner() |
| 89 | + result = runner.invoke(cli_command, ["--target", "foo", "--gateway", "asgi"]) |
| 90 | + |
| 91 | + assert result.exit_code == 0 |
| 92 | + assert create_asgi_app.calls == [pretend.call("foo", None, "http")] |
| 93 | + |
| 94 | + |
| 95 | +def test_cli_gateway_asgi_cloudevent(monkeypatch): |
| 96 | + """Test --gateway asgi with cloudevent signature""" |
| 97 | + # Skip this test if aio module is not available |
| 98 | + try: |
| 99 | + import functions_framework.aio |
| 100 | + except ImportError: |
| 101 | + pytest.skip("Async support not available") |
| 102 | + |
| 103 | + server = pretend.stub(run=pretend.call_recorder(lambda *a, **kw: None)) |
| 104 | + app = pretend.stub() |
| 105 | + |
| 106 | + # Mock the create_asgi_app function |
| 107 | + create_asgi_app = pretend.call_recorder(lambda *a, **kw: app) |
| 108 | + monkeypatch.setattr("functions_framework.aio.create_asgi_app", create_asgi_app) |
| 109 | + |
| 110 | + create_server = pretend.call_recorder(lambda *a, **kw: server) |
| 111 | + |
| 112 | + from functions_framework import _cli |
| 113 | + |
| 114 | + monkeypatch.setattr(_cli, "create_server", create_server) |
| 115 | + |
| 116 | + runner = CliRunner() |
| 117 | + result = runner.invoke( |
| 118 | + cli_command, |
| 119 | + ["--target", "foo", "--gateway", "asgi", "--signature-type", "cloudevent"], |
| 120 | + ) |
| 121 | + |
| 122 | + assert result.exit_code == 0 |
| 123 | + assert create_asgi_app.calls == [pretend.call("foo", None, "cloudevent")] |
| 124 | + |
| 125 | + |
| 126 | +def test_cli_gateway_env_var(monkeypatch): |
| 127 | + """Test GATEWAY environment variable""" |
| 128 | + # Skip this test if aio module is not available |
| 129 | + try: |
| 130 | + import functions_framework.aio |
| 131 | + except ImportError: |
| 132 | + pytest.skip("Async support not available") |
| 133 | + |
| 134 | + server = pretend.stub(run=pretend.call_recorder(lambda *a, **kw: None)) |
| 135 | + app = pretend.stub() |
| 136 | + |
| 137 | + # Mock the create_asgi_app function |
| 138 | + create_asgi_app = pretend.call_recorder(lambda *a, **kw: app) |
| 139 | + monkeypatch.setattr("functions_framework.aio.create_asgi_app", create_asgi_app) |
| 140 | + |
| 141 | + create_server = pretend.call_recorder(lambda *a, **kw: server) |
| 142 | + |
| 143 | + from functions_framework import _cli |
| 144 | + |
| 145 | + monkeypatch.setattr(_cli, "create_server", create_server) |
| 146 | + |
| 147 | + runner = CliRunner(env={"GATEWAY": "asgi"}) |
| 148 | + result = runner.invoke(cli_command, ["--target", "foo"]) |
| 149 | + |
| 150 | + assert result.exit_code == 0 |
| 151 | + assert create_asgi_app.calls == [pretend.call("foo", None, "http")] |
| 152 | + |
| 153 | + |
| 154 | +def test_cli_invalid_gateway(): |
| 155 | + """Test that invalid gateway values are rejected""" |
| 156 | + runner = CliRunner() |
| 157 | + result = runner.invoke(cli_command, ["--target", "foo", "--gateway", "invalid"]) |
| 158 | + |
| 159 | + assert result.exit_code == 2 |
| 160 | + assert "Invalid value" in result.output |
| 161 | + assert "'invalid' is not one of 'wsgi', 'asgi'" in result.output |
0 commit comments