(self)
| 589 | test_client2.close() |
| 590 | |
| 591 | def test_validate_headers(self) -> None: |
| 592 | client = Anthropic(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 593 | request = client._build_request(FinalRequestOptions(method="get", url="/foo")) |
| 594 | assert request.headers.get("Authorization") == f"Bearer {api_key}" |
| 595 | |
| 596 | def no_default_credentials(**_kwargs: object) -> None: |
| 597 | return None |
| 598 | |
| 599 | with pytest.MonkeyPatch.context() as monkeypatch: |
| 600 | monkeypatch.setattr("anthropic._client.default_credentials", no_default_credentials) |
| 601 | with update_env(**{"ANTHROPIC_API_KEY": Omit()}): |
| 602 | client2 = Anthropic(base_url=base_url, api_key=None, _strict_response_validation=True) |
| 603 | |
| 604 | with pytest.raises( |
| 605 | TypeError, |
| 606 | 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", |
| 607 | ): |
| 608 | client2._build_request(FinalRequestOptions(method="get", url="/foo")) |
| 609 | |
| 610 | request2 = client2._build_request( |
| 611 | FinalRequestOptions(method="get", url="/foo", headers={"Authorization": Omit()}) |
| 612 | ) |
| 613 | assert request2.headers.get("Authorization") is None |
| 614 | request3 = client2._build_request( |
| 615 | FinalRequestOptions(method="get", url="/foo", headers={"authorization": Omit()}) |
| 616 | ) |
| 617 | assert request3.headers.get("Authorization") is None |
| 618 | request4 = client2._build_request( |
| 619 | FinalRequestOptions(method="get", url="/foo", headers={"authorization": "from-header"}) |
| 620 | ) |
| 621 | assert request4.headers.get_list("Authorization") == ["from-header"] |
| 622 | |
| 623 | def test_default_headers_case_insensitive(self) -> None: |
| 624 | # replaces the SDK's own `User-Agent` rather than being sent next to it |
nothing calls this directly
no test coverage detected