MCPcopy
hub / github.com/openai/openai-python / test_validate_headers

Method test_validate_headers

tests/test_client.py:1713–1788  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1711 await test_client2.close()
1712
1713 async def test_validate_headers(self) -> None:
1714 client = AsyncOpenAI(
1715 base_url=base_url, api_key=api_key, admin_api_key=admin_api_key, _strict_response_validation=True
1716 )
1717 options = await client._prepare_options(FinalRequestOptions(method="get", url="/foo"))
1718 request = client._build_request(options)
1719 assert request.headers.get("Authorization") == f"Bearer {api_key}"
1720
1721 admin_request = client._build_request(
1722 FinalRequestOptions(
1723 method="get",
1724 url="/organization/projects",
1725 security={"admin_api_key_auth": True},
1726 )
1727 )
1728 assert admin_request.headers.get("Authorization") == f"Bearer {admin_api_key}"
1729
1730 with update_env(**{"OPENAI_API_KEY": Omit()}):
1731 admin_only = AsyncOpenAI(
1732 base_url=base_url,
1733 api_key=None,
1734 admin_api_key=admin_api_key,
1735 _strict_response_validation=True,
1736 )
1737 admin_only_request = admin_only._build_request(
1738 FinalRequestOptions(
1739 method="get",
1740 url="/organization/projects",
1741 security={"admin_api_key_auth": True},
1742 )
1743 )
1744 assert admin_only_request.headers.get("Authorization") == f"Bearer {admin_api_key}"
1745
1746 with pytest.raises(
1747 TypeError,
1748 match="Could not resolve authentication method",
1749 ):
1750 admin_only._build_request(
1751 FinalRequestOptions(
1752 method="post",
1753 url="/responses",
1754 security={"bearer_auth": True},
1755 )
1756 )
1757
1758 with update_env(
1759 **{
1760 "OPENAI_API_KEY": Omit(),
1761 "OPENAI_ADMIN_KEY": Omit(),
1762 }
1763 ):
1764 no_credentials = AsyncOpenAI(
1765 base_url=base_url,
1766 api_key=None,
1767 admin_api_key=None,
1768 _enforce_credentials=False,
1769 _strict_response_validation=True,
1770 )

Callers

nothing calls this directly

Calls 7

_prepare_optionsMethod · 0.95
AsyncOpenAIClass · 0.90
FinalRequestOptionsClass · 0.90
OmitClass · 0.90
update_envFunction · 0.85
_build_requestMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected