|
| 1 | +"""Tests for NO_PROXY env var sanitization during httpx client init. |
| 2 | +
|
| 3 | +Regression tests for issue #3303: Docker/.env files can leave newline or |
| 4 | +carriage return characters in NO_PROXY, which httpx's comma-only splitter |
| 5 | +treats as part of the hostname, causing InvalidURL. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import os |
| 11 | +from typing import Iterator |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +from openai._base_client import ( |
| 16 | + _save_no_proxy_env, |
| 17 | + _DefaultHttpxClient, |
| 18 | + _restore_no_proxy_env, |
| 19 | + _sanitize_no_proxy_env, |
| 20 | + _DefaultAsyncHttpxClient, |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(autouse=True) |
| 25 | +def clean_no_proxy() -> Iterator[None]: |
| 26 | + """Remove NO_PROXY/no_proxy before and after each test.""" |
| 27 | + for var in ("NO_PROXY", "no_proxy"): |
| 28 | + os.environ.pop(var, None) |
| 29 | + yield |
| 30 | + for var in ("NO_PROXY", "no_proxy"): |
| 31 | + os.environ.pop(var, None) |
| 32 | + |
| 33 | + |
| 34 | +class TestSanitizeNoProxyEnv: |
| 35 | + def test_newlines_replaced_with_commas(self) -> None: |
| 36 | + os.environ["NO_PROXY"] = "localhost\n127.0.0.1\n.example.com" |
| 37 | + _sanitize_no_proxy_env(trust_env=True) |
| 38 | + assert os.environ["NO_PROXY"] == "localhost,127.0.0.1,.example.com" |
| 39 | + |
| 40 | + def test_carriage_returns_replaced(self) -> None: |
| 41 | + os.environ["NO_PROXY"] = "localhost\r127.0.0.1" |
| 42 | + _sanitize_no_proxy_env(trust_env=True) |
| 43 | + assert os.environ["NO_PROXY"] == "localhost,127.0.0.1" |
| 44 | + |
| 45 | + def test_crlf_handled(self) -> None: |
| 46 | + os.environ["NO_PROXY"] = "localhost\r\n127.0.0.1\r\n" |
| 47 | + _sanitize_no_proxy_env(trust_env=True) |
| 48 | + assert os.environ["NO_PROXY"] == "localhost,127.0.0.1" |
| 49 | + |
| 50 | + def test_trailing_newline_stripped(self) -> None: |
| 51 | + os.environ["NO_PROXY"] = "localhost,127.0.0.1\n" |
| 52 | + _sanitize_no_proxy_env(trust_env=True) |
| 53 | + assert os.environ["NO_PROXY"] == "localhost,127.0.0.1" |
| 54 | + |
| 55 | + def test_already_clean_value_unchanged(self) -> None: |
| 56 | + os.environ["NO_PROXY"] = "localhost,127.0.0.1" |
| 57 | + _sanitize_no_proxy_env(trust_env=True) |
| 58 | + assert os.environ["NO_PROXY"] == "localhost,127.0.0.1" |
| 59 | + |
| 60 | + def test_no_env_var_unchanged(self) -> None: |
| 61 | + _sanitize_no_proxy_env(trust_env=True) |
| 62 | + assert "NO_PROXY" not in os.environ |
| 63 | + assert "no_proxy" not in os.environ |
| 64 | + |
| 65 | + def test_lowercase_var_sanitized(self) -> None: |
| 66 | + os.environ["no_proxy"] = "localhost\n127.0.0.1" |
| 67 | + _sanitize_no_proxy_env(trust_env=True) |
| 68 | + assert os.environ["no_proxy"] == "localhost,127.0.0.1" |
| 69 | + |
| 70 | + def test_trust_env_false_skips_sanitization(self) -> None: |
| 71 | + os.environ["NO_PROXY"] = "localhost\n127.0.0.1" |
| 72 | + _sanitize_no_proxy_env(trust_env=False) |
| 73 | + # Should remain unchanged when trust_env=False |
| 74 | + assert os.environ["NO_PROXY"] == "localhost\n127.0.0.1" |
| 75 | + |
| 76 | + def test_empty_lines_skipped(self) -> None: |
| 77 | + os.environ["NO_PROXY"] = "localhost\n\n127.0.0.1\n" |
| 78 | + _sanitize_no_proxy_env(trust_env=True) |
| 79 | + assert os.environ["NO_PROXY"] == "localhost,127.0.0.1" |
| 80 | + |
| 81 | + def test_whitespace_stripped(self) -> None: |
| 82 | + os.environ["NO_PROXY"] = " localhost \n 127.0.0.1 " |
| 83 | + _sanitize_no_proxy_env(trust_env=True) |
| 84 | + assert os.environ["NO_PROXY"] == "localhost,127.0.0.1" |
| 85 | + |
| 86 | + |
| 87 | +class TestSaveRestoreNoProxyEnv: |
| 88 | + def test_save_and_restore_unchanged(self) -> None: |
| 89 | + os.environ["NO_PROXY"] = "localhost,127.0.0.1" |
| 90 | + saved = _save_no_proxy_env() |
| 91 | + _sanitize_no_proxy_env(trust_env=True) |
| 92 | + _restore_no_proxy_env(saved) |
| 93 | + assert os.environ["NO_PROXY"] == "localhost,127.0.0.1" |
| 94 | + |
| 95 | + def test_save_and_restore_with_newlines(self) -> None: |
| 96 | + os.environ["NO_PROXY"] = "localhost\n127.0.0.1" |
| 97 | + saved = _save_no_proxy_env() |
| 98 | + _sanitize_no_proxy_env(trust_env=True) |
| 99 | + # After sanitization, the env is changed |
| 100 | + assert os.environ["NO_PROXY"] == "localhost,127.0.0.1" |
| 101 | + # After restore, the original value is back |
| 102 | + _restore_no_proxy_env(saved) |
| 103 | + assert os.environ["NO_PROXY"] == "localhost\n127.0.0.1" |
| 104 | + |
| 105 | + def test_save_and_restore_missing_var(self) -> None: |
| 106 | + saved = _save_no_proxy_env() |
| 107 | + os.environ["NO_PROXY"] = "should-be-removed" |
| 108 | + _restore_no_proxy_env(saved) |
| 109 | + assert "NO_PROXY" not in os.environ |
| 110 | + |
| 111 | + |
| 112 | +class TestClientConstruction: |
| 113 | + def test_sync_client_restores_env_after_init(self) -> None: |
| 114 | + """The sync client should restore the original NO_PROXY value after init.""" |
| 115 | + original = "localhost\n127.0.0.1" |
| 116 | + os.environ["NO_PROXY"] = original |
| 117 | + client = _DefaultHttpxClient() |
| 118 | + client.close() |
| 119 | + # The original (unsanitized) value should be restored |
| 120 | + assert os.environ["NO_PROXY"] == original |
| 121 | + |
| 122 | + def test_async_client_restores_env_after_init(self) -> None: |
| 123 | + """The async client should restore the original NO_PROXY value after init.""" |
| 124 | + original = "localhost\n127.0.0.1" |
| 125 | + os.environ["NO_PROXY"] = original |
| 126 | + client = _DefaultAsyncHttpxClient() |
| 127 | + # Close the client to clean up |
| 128 | + import asyncio |
| 129 | + |
| 130 | + asyncio.run(client.aclose()) |
| 131 | + # The original (unsanitized) value should be restored |
| 132 | + assert os.environ["NO_PROXY"] == original |
| 133 | + |
| 134 | + def test_sync_client_with_trust_env_false_no_mutation(self) -> None: |
| 135 | + """When trust_env=False, NO_PROXY should not be touched at all.""" |
| 136 | + original = "localhost\n127.0.0.1" |
| 137 | + os.environ["NO_PROXY"] = original |
| 138 | + client = _DefaultHttpxClient(trust_env=False) |
| 139 | + client.close() |
| 140 | + assert os.environ["NO_PROXY"] == original |
| 141 | + |
| 142 | + def test_sync_client_with_clean_no_proxy(self) -> None: |
| 143 | + """A clean NO_PROXY value should work fine with the sync client.""" |
| 144 | + os.environ["NO_PROXY"] = "localhost,127.0.0.1" |
| 145 | + client = _DefaultHttpxClient() |
| 146 | + client.close() |
| 147 | + assert os.environ["NO_PROXY"] == "localhost,127.0.0.1" |
0 commit comments