|
| 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 | + |
| 16 | +"""Tests for custom clients.""" |
| 17 | +import asyncio |
| 18 | +from unittest import mock |
| 19 | + |
| 20 | +from google.oauth2 import credentials |
| 21 | +import httpx |
| 22 | +import pytest |
| 23 | + |
| 24 | +from ... import _api_client as api_client |
| 25 | +from ... import Client |
| 26 | + |
| 27 | + |
| 28 | +try: |
| 29 | + import aiohttp |
| 30 | + |
| 31 | + AIOHTTP_NOT_INSTALLED = False |
| 32 | +except ImportError: |
| 33 | + AIOHTTP_NOT_INSTALLED = True |
| 34 | + aiohttp = mock.MagicMock() |
| 35 | + |
| 36 | +requires_aiohttp = pytest.mark.skipif( |
| 37 | + AIOHTTP_NOT_INSTALLED, reason='aiohttp is not installed, skipping test.' |
| 38 | +) |
| 39 | + |
| 40 | + |
| 41 | +# Httpx |
| 42 | +def test_constructor_with_httpx_clients(): |
| 43 | + mldev_http_options = { |
| 44 | + 'httpx_client': httpx.Client(trust_env=False), |
| 45 | + 'httpx_async_client': httpx.AsyncClient(trust_env=False), |
| 46 | + } |
| 47 | + vertexai_http_options = { |
| 48 | + 'httpx_client': httpx.Client(trust_env=False), |
| 49 | + 'httpx_async_client': httpx.AsyncClient(trust_env=False), |
| 50 | + } |
| 51 | + |
| 52 | + mldev_client = Client( |
| 53 | + api_key='google_api_key', http_options=mldev_http_options |
| 54 | + ) |
| 55 | + assert not mldev_client.models._api_client._httpx_client.trust_env |
| 56 | + assert not mldev_client.models._api_client._async_httpx_client.trust_env |
| 57 | + |
| 58 | + vertexai_client = Client( |
| 59 | + vertexai=True, |
| 60 | + project='fake_project_id', |
| 61 | + location='fake-location', |
| 62 | + http_options=vertexai_http_options, |
| 63 | + ) |
| 64 | + assert not vertexai_client.models._api_client._httpx_client.trust_env |
| 65 | + assert not vertexai_client.models._api_client._async_httpx_client.trust_env |
| 66 | + |
0 commit comments