(self)
| 552 | test_client2.close() |
| 553 | |
| 554 | def test_validate_headers(self) -> None: |
| 555 | client = Anthropic(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 556 | request = client._build_request(FinalRequestOptions(method="get", url="/foo")) |
| 557 | assert request.headers.get("Authorization") == f"Bearer {api_key}" |
| 558 | |
| 559 | def no_default_credentials(**_kwargs: object) -> None: |
| 560 | return None |
| 561 | |
| 562 | with pytest.MonkeyPatch.context() as monkeypatch: |
| 563 | monkeypatch.setattr("anthropic._client.default_credentials", no_default_credentials) |
| 564 | with update_env(**{"ANTHROPIC_API_KEY": Omit()}): |
| 565 | client2 = Anthropic(base_url=base_url, api_key=None, _strict_response_validation=True) |
| 566 | |
| 567 | with pytest.raises( |
| 568 | TypeError, |
| 569 | match="Could not resolve authentication method. Expected one of api_key, auth_token, or credentials to be set. Or for the `Authorization` header to be explicitly omitted", |
| 570 | ): |
| 571 | client2._build_request(FinalRequestOptions(method="get", url="/foo")) |
| 572 | |
| 573 | request2 = client2._build_request( |
| 574 | FinalRequestOptions(method="get", url="/foo", headers={"Authorization": Omit()}) |
| 575 | ) |
| 576 | assert request2.headers.get("Authorization") is None |
| 577 | request3 = client2._build_request( |
| 578 | FinalRequestOptions(method="get", url="/foo", headers={"authorization": Omit()}) |
| 579 | ) |
| 580 | assert request3.headers.get("Authorization") is None |
| 581 | request4 = client2._build_request( |
| 582 | FinalRequestOptions(method="get", url="/foo", headers={"authorization": "from-header"}) |
| 583 | ) |
| 584 | assert request4.headers.get_list("Authorization") == ["from-header"] |
| 585 | |
| 586 | def test_default_headers_case_insensitive(self) -> None: |
| 587 | # replaces the SDK's own `User-Agent` rather than being sent next to it |
nothing calls this directly
no test coverage detected