Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-python): keep the repeated lines of an httpx.Headers default

Yudhi Armyndharis0194d1c44cfd
diff --git a/sdk/python/openwa/_http.py b/sdk/python/openwa/_http.py
index d1f733da2..71b1e666b 100644
--- a/sdk/python/openwa/_http.py
+++ b/sdk/python/openwa/_http.py
@@ -117,8 +117,11 @@ def clean_default_headers(default_headers: Any) -> httpx.Headers:
"""
if default_headers is None:
return httpx.Headers()
- if isinstance(default_headers, Mapping):
- pairs: list[Any] = list(default_headers.items())
+ if isinstance(default_headers, httpx.Headers):
+ # Its repeated names are lines of their own; `.items()` would join them into one.
+ pairs: list[Any] = list(default_headers.multi_items())
+ elif isinstance(default_headers, Mapping):
+ pairs = list(default_headers.items())
else:
try:
pairs = list(default_headers)
diff --git a/sdk/python/tests/test_sdk.py b/sdk/python/tests/test_sdk.py
index ffc977d78..bed9deaf7 100644
--- a/sdk/python/tests/test_sdk.py
+++ b/sdk/python/tests/test_sdk.py
@@ -326,6 +326,13 @@ class TestClientCore:
with pytest.raises(ValueError, match="X-Trace") as raised:
OpenWAClient(base_url="https://x", api_key="k", default_headers={"X-Trace": b"s3cret\n"}) # type: ignore[dict-item]
assert "s3cret" not in str(raised.value)
+ # An httpx.Headers keeps a repeated name as separate lines, and so does the client.
+ repeated = OpenWAClient(
+ base_url="https://x",
+ api_key="k",
+ default_headers=httpx.Headers([("Cookie", "a=1"), ("Cookie", "b=2")]),
+ )
+ assert repeated._http._client.headers.get_list("Cookie") == ["a=1", "b=2"]
for bad in (42, ["X-Trace"], [("X-Trace", "a", "b")]):
with pytest.raises(ValueError, match="default_headers"):
OpenWAClient(base_url="https://x", api_key="k", default_headers=bad) # type: ignore[arg-type]