Starlette 1.3.x prefers httpx2 for starlette.testclient.TestClient; plain httpx still works but emits StarletteDeprecationWarning. - Pin httpx2==2.9.1 (keep httpx for MCP/runtime) - Centralize Web UI TestClient import via tests/webui_testclient.py - Point all test_webui_* modules at the helper - Add regression tests that the deprecation warning is gone Closes #682
67 lines
2.2 KiB
Python
67 lines
2.2 KiB
Python
"""Starlette TestClient uses httpx2 without deprecation warning (#682)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import importlib
|
|
import sys
|
|
import unittest
|
|
import warnings
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
|
|
|
|
class TestHttpx2Installed(unittest.TestCase):
|
|
def test_httpx2_importable(self) -> None:
|
|
httpx2 = importlib.import_module("httpx2")
|
|
self.assertTrue(hasattr(httpx2, "Client") or hasattr(httpx2, "AsyncClient"))
|
|
|
|
|
|
class TestNoStarletteTestClientDeprecation(unittest.TestCase):
|
|
def test_starlette_testclient_import_does_not_warn(self) -> None:
|
|
# Force a fresh import path so a previous httpx-fallback warning cannot
|
|
# hide a regression after httpx2 is present.
|
|
for name in list(sys.modules):
|
|
if name == "starlette.testclient" or name.startswith("starlette.testclient."):
|
|
del sys.modules[name]
|
|
|
|
with warnings.catch_warnings(record=True) as caught:
|
|
warnings.simplefilter("always")
|
|
import starlette.testclient as testclient # noqa: F401
|
|
|
|
dep = [
|
|
w
|
|
for w in caught
|
|
if "httpx" in str(w.message).lower()
|
|
and "deprecated" in str(w.message).lower()
|
|
]
|
|
self.assertEqual(
|
|
dep,
|
|
[],
|
|
f"expected no Starlette httpx deprecation warning; got: "
|
|
f"{[str(w.message) for w in dep]}",
|
|
)
|
|
|
|
def test_canonical_webui_testclient_import(self) -> None:
|
|
with warnings.catch_warnings(record=True) as caught:
|
|
warnings.simplefilter("always")
|
|
from webui_testclient import TestClient
|
|
from webui.app import create_app
|
|
|
|
client = TestClient(create_app())
|
|
response = client.get("/")
|
|
self.assertIn(response.status_code, {200, 302, 404})
|
|
|
|
dep = [
|
|
w
|
|
for w in caught
|
|
if "httpx" in str(w.message).lower()
|
|
and "deprecated" in str(w.message).lower()
|
|
]
|
|
self.assertEqual(dep, [], [str(w.message) for w in dep])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|