|
| 1 | +import asyncio |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from e2b.api.client_async import AsyncTransportWithLogger |
| 6 | +from e2b.api.client_async import get_api_client as get_async_api_client |
| 7 | +from e2b.api.client_sync import TransportWithLogger |
| 8 | +from e2b.api.client_sync import get_api_client as get_sync_api_client |
| 9 | +from e2b.connection_config import ConnectionConfig |
| 10 | + |
| 11 | + |
| 12 | +def test_sync_api_client_proxy_uses_explicit_transport(): |
| 13 | + TransportWithLogger.singleton = None |
| 14 | + config = ConnectionConfig( |
| 15 | + api_key="test", |
| 16 | + proxy="http://127.0.0.1:9999", |
| 17 | + ) |
| 18 | + |
| 19 | + api_client = get_sync_api_client(config) |
| 20 | + httpx_client = api_client.get_httpx_client() |
| 21 | + |
| 22 | + try: |
| 23 | + assert "proxy" not in api_client._httpx_args |
| 24 | + assert httpx_client._transport is TransportWithLogger.singleton |
| 25 | + assert httpx_client._mounts == {} |
| 26 | + finally: |
| 27 | + httpx_client.close() |
| 28 | + TransportWithLogger.singleton = None |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.asyncio |
| 32 | +async def test_async_api_client_proxy_uses_explicit_transport(): |
| 33 | + AsyncTransportWithLogger._instances.clear() |
| 34 | + config = ConnectionConfig( |
| 35 | + api_key="test", |
| 36 | + proxy="http://127.0.0.1:9999", |
| 37 | + ) |
| 38 | + |
| 39 | + api_client = get_async_api_client(config) |
| 40 | + httpx_client = api_client.get_async_httpx_client() |
| 41 | + transport = AsyncTransportWithLogger._instances[id(asyncio.get_running_loop())] |
| 42 | + |
| 43 | + try: |
| 44 | + assert "proxy" not in api_client._httpx_args |
| 45 | + assert httpx_client._transport is transport |
| 46 | + assert httpx_client._mounts == {} |
| 47 | + finally: |
| 48 | + await httpx_client.aclose() |
| 49 | + AsyncTransportWithLogger._instances.clear() |
0 commit comments