MCPcopy Create free account
hub / github.com/anthropics/anthropic-sdk-python / test_validate_headers

Method test_validate_headers

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

Source from the content-addressed store, hash-verified

416 test_client2.close()
417
418 def test_validate_headers(self) -> None:
419 client = Anthropic(base_url=base_url, api_key=api_key, _strict_response_validation=True)
420 request = client._build_request(FinalRequestOptions(method="get", url="/foo"))
421 assert request.headers.get("X-Api-Key") == api_key
422
423 def no_default_credentials(**_kwargs: object) -> None:
424 return None
425
426 with pytest.MonkeyPatch.context() as monkeypatch:
427 monkeypatch.setattr("anthropic._client.default_credentials", no_default_credentials)
428 with update_env(**{"ANTHROPIC_API_KEY": Omit()}):
429 client2 = Anthropic(base_url=base_url, api_key=None, _strict_response_validation=True)
430
431 with pytest.raises(
432 TypeError,
433 match="Could not resolve authentication method. Expected one of api_key, auth_token, or credentials to be set. Or for one of the `X-Api-Key` or `Authorization` headers to be explicitly omitted",
434 ):
435 client2._build_request(FinalRequestOptions(method="get", url="/foo"))
436
437 request2 = client2._build_request(FinalRequestOptions(method="get", url="/foo", headers={"X-Api-Key": Omit()}))
438 assert request2.headers.get("X-Api-Key") is None
439 request3 = client2._build_request(FinalRequestOptions(method="get", url="/foo", headers={"x-api-key": Omit()}))
440 assert request3.headers.get("X-Api-Key") is None
441 request4 = client2._build_request(
442 FinalRequestOptions(method="get", url="/foo", headers={"x-api-key": "from-header"})
443 )
444 assert request4.headers.get_list("X-Api-Key") == ["from-header"]
445
446 def test_default_headers_case_insensitive(self) -> None:
447 # replaces the SDK's own `User-Agent` rather than being sent next to it

Callers

nothing calls this directly

Calls 6

AnthropicClass · 0.90
FinalRequestOptionsClass · 0.90
OmitClass · 0.90
update_envFunction · 0.85
_build_requestMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected