MCPcopy
hub / github.com/encode/starlette / TestClient

Class TestClient

starlette/testclient.py:376–748  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

374
375
376class TestClient(httpx.Client):
377 __test__ = False
378 task: Future[None]
379 portal: anyio.abc.BlockingPortal | None = None
380
381 def __init__(
382 self,
383 app: ASGIApp,
384 base_url: str = "http://testserver",
385 raise_server_exceptions: bool = True,
386 root_path: str = "",
387 backend: Literal["asyncio", "trio"] = "asyncio",
388 backend_options: dict[str, Any] | None = None,
389 cookies: httpx._types.CookieTypes | None = None,
390 headers: dict[str, str] | None = None,
391 follow_redirects: bool = True,
392 client: tuple[str, int] = ("testclient", 50000),
393 ) -> None:
394 self.async_backend = _AsyncBackend(backend=backend, backend_options=backend_options or {})
395 if _is_asgi3(app):
396 asgi_app = app
397 else:
398 app = cast(ASGI2App, app) # type: ignore[assignment]
399 asgi_app = _WrapASGI2(app) # type: ignore[arg-type]
400 self.app = asgi_app
401 self.app_state: dict[str, Any] = {}
402 transport = _TestClientTransport(
403 self.app,
404 portal_factory=self._portal_factory,
405 raise_server_exceptions=raise_server_exceptions,
406 root_path=root_path,
407 app_state=self.app_state,
408 client=client,
409 )
410 if headers is None:
411 headers = {}
412 headers.setdefault("user-agent", "testclient")
413 super().__init__(
414 base_url=base_url,
415 headers=headers,
416 transport=transport,
417 follow_redirects=follow_redirects,
418 cookies=cookies,
419 )
420
421 @contextlib.contextmanager
422 def _portal_factory(self) -> Generator[anyio.abc.BlockingPortal, None, None]:
423 if self.portal is not None:
424 yield self.portal
425 else:
426 with anyio.from_thread.start_blocking_portal(**self.async_backend) as portal:
427 yield portal
428
429 def request( # type: ignore[override]
430 self,
431 method: str,
432 url: httpx._types.URLTypes,
433 *,

Calls

no outgoing calls