Skip to content
Draft
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
6 changes: 3 additions & 3 deletions src/hpack/hpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ def _to_bytes(value: bytes | str | Any) -> bytes:
Convert anything to bytes through a UTF-8 encoded string
"""
t = type(value)
if t is bytes:
return value # type: ignore
if isinstance(value, bytes):
return value
if t is not str:
value = str(value)
return value.encode("utf-8") # type: ignore
return value.encode("utf-8")


class Encoder:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_hpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
from hpack.hpack import _dict_to_iterable, _to_bytes


class BytesSubclass(bytes):
pass


@pytest.mark.parametrize('value', [b'', b'foobar', b'\x00\xff'])
def test_to_bytes_preserves_bytes_subclasses(value):
assert _to_bytes(BytesSubclass(value)) == value


def test_to_bytes():
assert _to_bytes(b"foobar") == b"foobar"
assert _to_bytes("foobar") == b"foobar"
Expand All @@ -26,6 +35,23 @@ def test_to_bytes():


class TestHPACKEncoder:
@pytest.mark.parametrize('huffman', [False, True])
@pytest.mark.parametrize('as_dict', [False, True])
def test_bytes_subclass_headers(self, huffman, as_dict):
headers = [
(BytesSubclass(b':method'), BytesSubclass(b'GET')),
(BytesSubclass(b'custom-key'), BytesSubclass(b'\x00\xff')),
]
expected = [(b':method', b'GET'), (b'custom-key', b'\x00\xff')]
if as_dict:
# Dictionary pseudo-headers must move ahead of ordinary headers.
headers = dict(reversed(headers))

encoded = Encoder().encode(headers, huffman=huffman)

assert encoded == Encoder().encode(expected, huffman=huffman)
assert Decoder().decode(encoded, raw=True) == expected

# These tests are stolen entirely from the IETF specification examples.
def test_literal_header_field_with_indexing(self):
"""
Expand Down