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

Method test_validate_headers

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

Source from the content-addressed store, hash-verified

446 test_client2.close()
447
448 def test_validate_headers(self) -> None:
449 client = Anthropic(base_url=base_url, api_key=api_key, _strict_response_validation=True)
450 request = client._build_request(FinalRequestOptions(method="get", url="/foo"))
451 assert request.headers.get("X-Api-Key") == api_key
452
453 def no_default_credentials(**_kwargs: object) -> None:
454 return None
455
456 with pytest.MonkeyPatch.context() as monkeypatch:
457 monkeypatch.setattr("anthropic._client.default_credentials", no_default_credentials)
458 with update_env(**{"ANTHROPIC_API_KEY": Omit()}):
459 client2 = Anthropic(base_url=base_url, api_key=None, _strict_response_validation=True)
460
461 with pytest.raises(
462 TypeError,
463 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",
464 ):
465 client2._build_request(FinalRequestOptions(method="get", url="/foo"))
466
467 request2 = client2._build_request(FinalRequestOptions(method="get", url="/foo", headers={"X-Api-Key": Omit()}))
468 assert request2.headers.get("X-Api-Key") is None
469 request3 = client2._build_request(FinalRequestOptions(method="get", url="/foo", headers={"x-api-key": Omit()}))
470 assert request3.headers.get("X-Api-Key") is None
471 request4 = client2._build_request(
472 FinalRequestOptions(method="get", url="/foo", headers={"x-api-key": "from-header"})
473 )
474 assert request4.headers.get_list("X-Api-Key") == ["from-header"]
475
476 def test_default_headers_case_insensitive(self) -> None:
477 # 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