@@ -254,6 +254,40 @@ def test_websocket_base_url_no_auth_with_custom_base_url():
254254 assert api_client ._websocket_base_url () == base_url
255255
256256
257+ def test_websocket_base_url_with_http_options ():
258+ api_client = gl_client .BaseApiClient (
259+ api_key = 'google_api_key' ,
260+ http_options = {'base_url' : 'https://test.com' },
261+ )
262+ assert (
263+ api_client ._websocket_base_url (
264+ types .HttpOptions (base_url = 'https://session-test.com' )
265+ )
266+ == 'wss://session-test.com'
267+ )
268+
269+
270+ def test_websocket_base_url_no_auth_with_http_options ():
271+ api_client = gl_client .BaseApiClient (
272+ vertexai = True ,
273+ http_options = {
274+ 'base_url' : 'https://test-api-gateway-proxy.com' ,
275+ 'headers' : {'Authorization' : 'Bearer test_token' },
276+ },
277+ )
278+ api_client .project = None
279+ api_client .location = None
280+
281+ # The base url of the given http options is passed as is, like the client's.
282+ session_base_url = 'https://session-api-gateway-proxy.com'
283+ assert (
284+ api_client ._websocket_base_url (
285+ types .HttpOptions (base_url = session_base_url )
286+ )
287+ == session_base_url
288+ )
289+
290+
257291@pytest .mark .parametrize ('vertexai' , [True , False ])
258292@pytest .mark .asyncio
259293async def test_async_session_send_text (mock_websocket , vertexai ):
@@ -2286,6 +2320,165 @@ async def _test_connect():
22862320 await _test_connect ()
22872321
22882322
2323+ async def _capture_connect (api_client , config ):
2324+ """Connects through a mock websocket and returns what reached it."""
2325+ mock_ws = AsyncMock ()
2326+ mock_ws .recv = AsyncMock (
2327+ return_value = b'{"setupComplete": {"sessionId": "test_session_id"}}'
2328+ )
2329+ capture = {}
2330+
2331+ @contextlib .asynccontextmanager
2332+ async def mock_connect (uri , additional_headers = None , ** kwargs ):
2333+ capture ['uri' ] = uri
2334+ capture ['headers' ] = additional_headers
2335+ yield mock_ws
2336+
2337+ with patch .object (live , 'ws_connect' , new = mock_connect ):
2338+ async with live .AsyncLive (api_client ).connect (
2339+ model = 'test-model' , config = config
2340+ ):
2341+ pass
2342+ capture ['request' ] = mock_ws .send .call_args [0 ][0 ]
2343+ return capture
2344+
2345+
2346+ @pytest .mark .asyncio
2347+ async def test_connect_with_session_http_options ():
2348+ client = gl_client .BaseApiClient (
2349+ vertexai = False ,
2350+ api_key = 'TEST_API_KEY' ,
2351+ http_options = {'headers' : {'x-client-header' : 'client-value' }},
2352+ )
2353+ config = types .LiveConnectConfig (
2354+ http_options = types .HttpOptions (
2355+ base_url = 'https://session-base-url.com' ,
2356+ api_version = 'v1alpha' ,
2357+ headers = {
2358+ 'x-client-header' : 'session-value' ,
2359+ 'x-session-header' : 'session-value' ,
2360+ },
2361+ )
2362+ )
2363+
2364+ capture = await _capture_connect (client , config )
2365+
2366+ assert capture ['uri' ] == (
2367+ 'wss://session-base-url.com/ws/google.ai.generativelanguage.v1alpha'
2368+ '.GenerativeService.BidiGenerateContent'
2369+ )
2370+ assert capture ['headers' ]['x-goog-api-key' ] == 'TEST_API_KEY'
2371+ assert capture ['headers' ]['x-client-header' ] == 'session-value'
2372+ assert capture ['headers' ]['x-session-header' ] == 'session-value'
2373+ # The session-level options apply to this session only.
2374+ assert client ._http_options .headers ['x-client-header' ] == 'client-value'
2375+ assert 'x-session-header' not in client ._http_options .headers
2376+ # They configure the connection and are not sent in the setup message.
2377+ assert 'x-session-header' not in capture ['request' ]
2378+
2379+
2380+ @pytest .mark .asyncio
2381+ async def test_connect_keeps_client_base_url ():
2382+ client = gl_client .BaseApiClient (
2383+ vertexai = False ,
2384+ api_key = 'TEST_API_KEY' ,
2385+ http_options = {'base_url' : 'https://client-base-url.com' },
2386+ )
2387+ config = types .LiveConnectConfig (
2388+ http_options = types .HttpOptions (
2389+ headers = {'x-session-header' : 'session-value' }
2390+ )
2391+ )
2392+
2393+ capture = await _capture_connect (client , config )
2394+
2395+ # Without a session-level base url, the client's is used.
2396+ assert capture ['uri' ] == (
2397+ 'wss://client-base-url.com/ws/google.ai.generativelanguage.v1beta'
2398+ '.GenerativeService.BidiGenerateContent'
2399+ )
2400+
2401+
2402+ @pytest .mark .asyncio
2403+ async def test_connect_with_session_http_options_and_credentials ():
2404+ client = gl_client .BaseApiClient (
2405+ vertexai = True ,
2406+ project = 'test-project' ,
2407+ location = 'us-central1' ,
2408+ credentials = Credentials (token = 'provided_fake_token' ),
2409+ http_options = {'base_url' : 'https://client-base-url.com' },
2410+ )
2411+ config = types .LiveConnectConfig (
2412+ http_options = types .HttpOptions (
2413+ api_version = 'v1' ,
2414+ headers = {'x-session-header' : 'session-value' },
2415+ )
2416+ )
2417+
2418+ capture = await _capture_connect (client , config )
2419+
2420+ # The client's base url is kept, the api version is overridden.
2421+ assert capture ['uri' ] == (
2422+ 'wss://client-base-url.com/ws/google.cloud.aiplatform.v1'
2423+ '.LlmBidiService/BidiGenerateContent'
2424+ )
2425+ assert capture ['headers' ]['Authorization' ] == 'Bearer provided_fake_token'
2426+ assert capture ['headers' ]['x-session-header' ] == 'session-value'
2427+
2428+
2429+ @pytest .mark .asyncio
2430+ async def test_connect_with_session_http_options_and_custom_base_url (
2431+ monkeypatch ,
2432+ ):
2433+ for env_var in (
2434+ 'GOOGLE_CLOUD_PROJECT' ,
2435+ 'GOOGLE_CLOUD_LOCATION' ,
2436+ 'GOOGLE_API_KEY' ,
2437+ 'GEMINI_API_KEY' ,
2438+ ):
2439+ monkeypatch .delenv (env_var , raising = False )
2440+ client = gl_client .BaseApiClient (
2441+ vertexai = True ,
2442+ http_options = {
2443+ 'base_url' : 'https://custom-base-url.com' ,
2444+ 'headers' : {'Authorization' : 'Bearer custom_test_token' },
2445+ },
2446+ )
2447+ config = types .LiveConnectConfig (
2448+ http_options = types .HttpOptions (
2449+ base_url = 'https://session-base-url.com' ,
2450+ headers = {'Authorization' : 'Bearer session_test_token' },
2451+ )
2452+ )
2453+
2454+ capture = await _capture_connect (client , config )
2455+
2456+ assert capture ['uri' ] == 'https://session-base-url.com'
2457+ assert capture ['headers' ]['Authorization' ] == 'Bearer session_test_token'
2458+
2459+
2460+ @pytest .mark .parametrize (
2461+ 'field, value' ,
2462+ [
2463+ ('timeout' , 1000 ),
2464+ ('extra_body' , {'setup' : {}}),
2465+ ('retry_options' , {'attempts' : 1 }),
2466+ ('async_client_args' , {'open_timeout' : 5 }),
2467+ ],
2468+ )
2469+ @pytest .mark .asyncio
2470+ async def test_connect_with_unsupported_session_http_options (field , value ):
2471+ config = {'http_options' : {'headers' : {'x-header' : 'value' }, field : value }}
2472+
2473+ with patch .object (live , 'ws_connect' ) as mock_ws_connect :
2474+ with pytest .raises (ValueError , match = field ):
2475+ async with live .AsyncLive (mock_api_client ()).connect (
2476+ model = 'test-model' , config = config
2477+ ):
2478+ pass
2479+ mock_ws_connect .assert_not_called ()
2480+
2481+
22892482@pytest .mark .parametrize ('vertexai' , [False ])
22902483@pytest .mark .asyncio
22912484async def test_bidi_setup_to_api_with_auth_tokens (mock_websocket , vertexai ):
0 commit comments