diff --git a/src/fishaudio/core/client_wrapper.py b/src/fishaudio/core/client_wrapper.py index 31cbf7f..aa3e755 100644 --- a/src/fishaudio/core/client_wrapper.py +++ b/src/fishaudio/core/client_wrapper.py @@ -154,19 +154,26 @@ def get_headers( ) -> dict[str, str]: """Build headers including authentication and user agent. - A ``User-Agent`` in ``additional_headers`` (any casing), or else one set - on a user-supplied httpx client, is appended to the SDK's own. + A name in ``additional_headers`` replaces the SDK's header of that + name whatever its case, so the two are never sent side by side. The + exception is ``User-Agent``: one in ``additional_headers`` (any + casing), or else one set on a user-supplied httpx client, is appended + to the SDK's own. """ headers = {"Authorization": f"Bearer {self.api_key}"} # Read from the client on every call, so a User-Agent changed on a # supplied httpx client after construction is used. The SDK's own # client carries USER_AGENT, which user_agent() does not double. caller = _caller_user_agent(getattr(self, "_client", None)) + extra = {} for name, value in (additional_headers or {}).items(): if name.lower() == "user-agent": caller = value else: - headers[name] = value + extra[name] = value + replaced = {name.lower() for name in extra} + headers = {k: v for k, v in headers.items() if k.lower() not in replaced} + headers.update(extra) headers["User-Agent"] = user_agent(caller) return headers diff --git a/tests/unit/test_core.py b/tests/unit/test_core.py index ce38383..85bec3a 100644 --- a/tests/unit/test_core.py +++ b/tests/unit/test_core.py @@ -148,6 +148,39 @@ def test_additional_header_is_kept_without_an_explicit_one(self, mock_api_key): assert kwargs["headers"]["Model"] == "transcribe-1-pro" assert kwargs["headers"]["Authorization"] == f"Bearer {mock_api_key}" + @pytest.mark.parametrize( + "name", ["authorization", "AUTHORIZATION", "Authorization"] + ) + def test_additional_header_replaces_the_sdk_header_of_other_case( + self, mock_api_key, name + ): + # Sent through httpx, which would send two spellings as two headers. + seen = [] + + def handler(request: httpx.Request) -> httpx.Response: + seen.append(request.headers.get_list("authorization")) + return httpx.Response(200, json={}) + + http = httpx.Client( + base_url="https://api.test", transport=httpx.MockTransport(handler) + ) + wrapper = ClientWrapper(api_key=mock_api_key, httpx_client=http) + wrapper.request( + "GET", + "/x", + request_options=RequestOptions(additional_headers={name: "Bearer other"}), + ) + wrapper.request("GET", "/x") + assert seen == [["Bearer other"], [f"Bearer {mock_api_key}"]] + + def test_additional_user_agent_is_sent_once_after_the_sdk_one(self, mock_api_key): + wrapper = ClientWrapper(api_key=mock_api_key) + headers = wrapper.get_headers({"user-agent": "mine"}) + sent = {k: v for k, v in headers.items() if k.lower() == "user-agent"} + assert list(sent) == ["User-Agent"] + assert sent["User-Agent"].startswith("fish-audio-python/") + assert sent["User-Agent"].endswith(" mine") + class TestAsyncClientWrapper: """Test async AsyncClientWrapper."""