MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / _make_mock_transport

Function _make_mock_transport

python/openshell/sandbox_test.py:532–572  ·  view source on GitHub ↗

Build an httpx.MockTransport that serves OIDC discovery + token refresh from an in-memory script. `refresh_responses` is consumed in order across successive POSTs to the token endpoint (which lets tests assert refresh-token rotation semantics across multiple refreshes).

(
    *,
    discovery: dict | None = None,
    refresh_responses: list[dict] | None = None,
    discovery_status: int = 200,
    refresh_status: int = 200,
    seen_refresh: list[Any] | None = None,
    seen_discovery: list[Any] | None = None,
)

Source from the content-addressed store, hash-verified

530
531
532def _make_mock_transport(
533 *,
534 discovery: dict | None = None,
535 refresh_responses: list[dict] | None = None,
536 discovery_status: int = 200,
537 refresh_status: int = 200,
538 seen_refresh: list[Any] | None = None,
539 seen_discovery: list[Any] | None = None,
540):
541 """Build an httpx.MockTransport that serves OIDC discovery + token
542 refresh from an in-memory script.
543
544 `refresh_responses` is consumed in order across successive POSTs to
545 the token endpoint (which lets tests assert refresh-token rotation
546 semantics across multiple refreshes).
547 """
548 import httpx as _httpx
549
550 refresh_iter = iter(
551 refresh_responses or [{"access_token": "refreshed-jwt", "expires_in": 3600}]
552 )
553
554 def handler(request: _httpx.Request) -> _httpx.Response:
555 if request.url.path.endswith("/.well-known/openid-configuration"):
556 if seen_discovery is not None:
557 seen_discovery.append(str(request.url))
558 body = discovery or {
559 "issuer": DEFAULT_ISSUER,
560 "token_endpoint": DEFAULT_TOKEN_ENDPOINT,
561 }
562 return _httpx.Response(discovery_status, json=body)
563 # Anything else is a refresh exchange.
564 if seen_refresh is not None:
565 seen_refresh.append((str(request.url), bytes(request.content)))
566 try:
567 body = next(refresh_iter)
568 except StopIteration:
569 return _httpx.Response(500, json={"error": "test_script_exhausted"})
570 return _httpx.Response(refresh_status, json=body)
571
572 return _httpx.MockTransport(handler)
573
574
575def _install_mock_transport(refresher: Any, transport: Any) -> None:

Calls

no outgoing calls

Tested by

no test coverage detected