MCPcopy Create free account
hub / github.com/openai/openai-python / test_client_token_provider_refresh_sync

Function test_client_token_provider_refresh_sync

tests/lib/test_azure.py:231–265  ·  view source on GitHub ↗
(respx_mock: MockRouter)

Source from the content-addressed store, hash-verified

229
230@pytest.mark.respx()
231def test_client_token_provider_refresh_sync(respx_mock: MockRouter) -> None:
232 respx_mock.post(
233 "https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-02-01"
234 ).mock(
235 side_effect=[
236 httpx.Response(500, json={"error": "server error"}),
237 httpx.Response(200, json={"foo": "bar"}),
238 ]
239 )
240
241 counter = 0
242
243 def token_provider() -> str:
244 nonlocal counter
245
246 counter += 1
247
248 if counter == 1:
249 return "first"
250
251 return "second"
252
253 client = AzureOpenAI(
254 api_version="2024-02-01",
255 azure_ad_token_provider=token_provider,
256 azure_endpoint="https://example-resource.azure.openai.com",
257 )
258 client.chat.completions.create(messages=[], model="gpt-4")
259
260 calls = cast("list[MockRequestCall]", respx_mock.calls)
261
262 assert len(calls) == 2
263
264 assert calls[0].request.headers.get("Authorization") == "Bearer first"
265 assert calls[1].request.headers.get("Authorization") == "Bearer second"
266
267
268@pytest.mark.asyncio

Callers

nothing calls this directly

Calls 4

AzureOpenAIClass · 0.90
postMethod · 0.45
createMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected