|
25 | 25 | from google.adk.auth.auth_credential import OAuth2Auth |
26 | 26 | from google.adk.integrations.agent_registry import AgentRegistry |
27 | 27 | from google.adk.integrations.agent_registry.agent_registry import _ProtocolType |
| 28 | +from google.adk.integrations.agent_registry.agent_registry import _should_use_mtls_endpoint |
28 | 29 | from google.adk.telemetry.tracing import GCP_MCP_SERVER_DESTINATION_ID |
29 | 30 | from google.adk.tools.mcp_tool.mcp_toolset import McpToolset |
30 | 31 | import httpx |
@@ -904,17 +905,36 @@ def test_use_client_cert_effective( |
904 | 905 | def test_should_use_mtls_endpoint( |
905 | 906 | self, use_mtls_env, client_cert_source, expected, registry |
906 | 907 | ): |
907 | | - from google.adk.integrations.agent_registry.agent_registry import _should_use_mtls_endpoint |
908 | | - |
909 | 908 | env_patch = {} |
910 | 909 | if use_mtls_env is not None: |
911 | 910 | env_patch["GOOGLE_API_USE_MTLS_ENDPOINT"] = use_mtls_env |
912 | 911 | else: |
913 | 912 | # Ensure any ambient env var doesn't leak into the test |
914 | 913 | env_patch = {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"} |
915 | 914 |
|
| 915 | + # Scenario 1: Library function does not exist (fallback path) |
916 | 916 | with patch.dict(os.environ, env_patch): |
917 | | - assert expected == _should_use_mtls_endpoint(client_cert_source) |
| 917 | + with patch( |
| 918 | + "google.auth.transport.mtls.should_use_mtls_endpoint", create=True |
| 919 | + ) as mock_func: |
| 920 | + mock_func.side_effect = AttributeError("Mocked missing attribute") |
| 921 | + assert expected == _should_use_mtls_endpoint(client_cert_source) |
| 922 | + |
| 923 | + # Scenario 2: Library function exists (standard path) |
| 924 | + def mock_impl(client_cert_available=None): |
| 925 | + use_mtls = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() |
| 926 | + if use_mtls == "always": |
| 927 | + return True |
| 928 | + if use_mtls == "never": |
| 929 | + return False |
| 930 | + return bool(client_cert_available) |
| 931 | + |
| 932 | + with patch.dict(os.environ, env_patch): |
| 933 | + with patch( |
| 934 | + "google.auth.transport.mtls.should_use_mtls_endpoint", create=True |
| 935 | + ) as mock_func: |
| 936 | + mock_func.side_effect = mock_impl |
| 937 | + assert expected == _should_use_mtls_endpoint(client_cert_source) |
918 | 938 |
|
919 | 939 | def test_make_request_error_handling(self, registry): |
920 | 940 | mock_session = registry._session |
|
0 commit comments