Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/httpx2/httpx2/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,14 @@ def __init__(

if isinstance(headers, Headers):
self._list = list(headers._list)
elif isinstance(headers, Mapping):
for k, v in headers.items():
bytes_key = _normalize_header_key(k, encoding)
bytes_value = _normalize_header_value(v, encoding)
self._list.append((bytes_key, bytes_key.lower(), bytes_value))
elif headers is not None:
for k, v in headers:
header_items = headers.items() if isinstance(headers, Mapping) else headers
for k, v in header_items:
bytes_key = _normalize_header_key(k, encoding)
bytes_value = _normalize_header_value(v, encoding)
try:
bytes_value = _normalize_header_value(v, encoding)
except UnicodeEncodeError as exc:
raise ValueError(f"Unable to encode header value for {k!r}") from exc
self._list.append((bytes_key, bytes_key.lower(), bytes_value))

self._encoding = encoding
Expand Down
16 changes: 16 additions & 0 deletions tests/httpx2/models/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ def test_headers() -> None:
assert repr(h) == "Headers({'a': '123', 'b': '789'})"


@pytest.mark.parametrize(
"headers",
[
{"auth": "שלום"},
[("auth", "שלום")],
],
)
def test_header_value_encoding_error_includes_header_name(
headers: dict[str, str] | list[tuple[str, str]],
) -> None:
with pytest.raises(ValueError, match="auth") as exc_info:
httpx2.Headers(headers)

assert isinstance(exc_info.value.__cause__, UnicodeEncodeError)


def test_header_mutations() -> None:
h = httpx2.Headers()
assert dict(h) == {}
Expand Down
Loading