Skip to content

Commit bc93712

Browse files
chore(tests): add tests for httpx client instantiation & proxies
1 parent b1a31e5 commit bc93712

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

tests/test_client.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@
2828
from openai._constants import RAW_RESPONSE_HEADER
2929
from openai._streaming import Stream, AsyncStream
3030
from openai._exceptions import OpenAIError, APIStatusError, APITimeoutError, APIResponseValidationError
31-
from openai._base_client import DEFAULT_TIMEOUT, HTTPX_DEFAULT_TIMEOUT, BaseClient, make_request_options
31+
from openai._base_client import (
32+
DEFAULT_TIMEOUT,
33+
HTTPX_DEFAULT_TIMEOUT,
34+
BaseClient,
35+
DefaultHttpxClient,
36+
DefaultAsyncHttpxClient,
37+
make_request_options,
38+
)
3239
from openai.types.chat.completion_create_params import CompletionCreateParamsNonStreaming
3340

3441
from .utils import update_env
@@ -908,6 +915,28 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
908915
assert response.retries_taken == failures_before_success
909916
assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success
910917

918+
def test_proxy_environment_variables(self, monkeypatch: pytest.MonkeyPatch) -> None:
919+
# Test that the proxy environment variables are set correctly
920+
monkeypatch.setenv("HTTPS_PROXY", "https://example.org")
921+
922+
client = DefaultHttpxClient()
923+
924+
mounts = tuple(client._mounts.items())
925+
assert len(mounts) == 1
926+
assert mounts[0][0].pattern == "https://"
927+
928+
@pytest.mark.filterwarnings("ignore:.*deprecated.*:DeprecationWarning")
929+
def test_default_client_creation(self) -> None:
930+
# Ensure that the client can be initialized without any exceptions
931+
DefaultHttpxClient(
932+
verify=True,
933+
cert=None,
934+
trust_env=True,
935+
http1=True,
936+
http2=False,
937+
limits=httpx.Limits(max_connections=100, max_keepalive_connections=20),
938+
)
939+
911940
@pytest.mark.respx(base_url=base_url)
912941
def test_follow_redirects(self, respx_mock: MockRouter) -> None:
913942
# Test that the default follow_redirects=True allows following redirects
@@ -1857,6 +1886,28 @@ async def test_main() -> None:
18571886

18581887
time.sleep(0.1)
18591888

1889+
async def test_proxy_environment_variables(self, monkeypatch: pytest.MonkeyPatch) -> None:
1890+
# Test that the proxy environment variables are set correctly
1891+
monkeypatch.setenv("HTTPS_PROXY", "https://example.org")
1892+
1893+
client = DefaultAsyncHttpxClient()
1894+
1895+
mounts = tuple(client._mounts.items())
1896+
assert len(mounts) == 1
1897+
assert mounts[0][0].pattern == "https://"
1898+
1899+
@pytest.mark.filterwarnings("ignore:.*deprecated.*:DeprecationWarning")
1900+
async def test_default_client_creation(self) -> None:
1901+
# Ensure that the client can be initialized without any exceptions
1902+
DefaultAsyncHttpxClient(
1903+
verify=True,
1904+
cert=None,
1905+
trust_env=True,
1906+
http1=True,
1907+
http2=False,
1908+
limits=httpx.Limits(max_connections=100, max_keepalive_connections=20),
1909+
)
1910+
18601911
@pytest.mark.respx(base_url=base_url)
18611912
async def test_follow_redirects(self, respx_mock: MockRouter) -> None:
18621913
# Test that the default follow_redirects=True allows following redirects

0 commit comments

Comments
 (0)