Skip to content

Commit

Permalink
update from_bytes_dict() to handle tuple and raise ValueError if non …
Browse files Browse the repository at this point in the history
…str or bytes
  • Loading branch information
BurnzZ committed Apr 11, 2022
1 parent eb18427 commit ddb7d20
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
16 changes: 14 additions & 2 deletions tests/test_page_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ def test_http_response_headers_from_bytes_dict():
b"Content-Encoding": [b"gzip", b"br"],
b"server": b"sffe",
"X-string": "string",
"X-missing": None
"X-missing": None,
"X-tuple": (b"x", "y"),
}
headers = HttpResponseHeaders.from_bytes_dict(raw_headers)

Expand All @@ -110,7 +111,18 @@ def test_http_response_headers_from_bytes_dict():
assert headers.getall("Content-Encoding") == ["gzip", "br"]
assert headers.get("server") == "sffe"
assert headers.get("x-string") == "string"
assert headers.get("X-missing") is None
assert headers.get("x-missing") is None
assert headers.get("x-tuple") == "x"
assert headers.getall("x-tuple") == ["x", "y"]


def test_http_response_headers_from_bytes_dict_err():

with pytest.raises(ValueError):
HttpResponseHeaders.from_bytes_dict({b"Content-Length": [316]})

with pytest.raises(ValueError):
HttpResponseHeaders.from_bytes_dict({b"Content-Length": 316})


def test_http_response_headers_init_requests():
Expand Down
18 changes: 10 additions & 8 deletions web_poet/page_inputs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from typing import Optional, Dict, List, TypeVar, Type, Union
from typing import Optional, Dict, List, TypeVar, Type, Union, Tuple, AnyStr

import attrs
from multidict import CIMultiDict
Expand All @@ -14,7 +14,7 @@
from .utils import memoizemethod_noargs

T_headers = TypeVar("T_headers", bound="HttpResponseHeaders")
BytesDict = Dict[bytes, Union[bytes, List[bytes]]]
AnyStrDict = Dict[AnyStr, Union[AnyStr, List[AnyStr], Tuple[AnyStr, ...]]]


class HttpResponseBody(bytes):
Expand Down Expand Up @@ -77,13 +77,14 @@ def from_name_value_pairs(cls: Type[T_headers], arg: List[Dict]) -> T_headers:

@classmethod
def from_bytes_dict(
cls: Type[T_headers], arg: BytesDict, encoding: str = "utf-8"
cls: Type[T_headers], arg: AnyStrDict, encoding: str = "utf-8"
) -> T_headers:
"""An alternative constructor for instantiation where the header-value
pairs are in raw bytes form.
pairs could be in raw bytes form.
This supports multiple header values in the form of ``List[bytes]``
alongside a plain ``bytes`` value.
This supports multiple header values in the form of ``List[bytes]`` and
``Tuple[bytes]]`` alongside a plain ``bytes`` value. A value in ``str``
also works and wouldn't break the decoding process at all.
By default, it converts the ``bytes`` value using "utf-8". However, this
can easily be overridden using the ``encoding`` parameter.
Expand All @@ -99,15 +100,16 @@ def from_bytes_dict(
"""

def _norm(data):
if isinstance(data, str):
if isinstance(data, str) or data is None:
return data
elif isinstance(data, bytes):
return data.decode(encoding)
raise ValueError(f"Expecting str or bytes. Received {type(data)}")

converted = []

for header, value in arg.items():
if isinstance(value, list):
if isinstance(value, list) or isinstance(value, tuple):
converted.extend([(_norm(header), _norm(v)) for v in value])
else:
converted.append((_norm(header), _norm(value)))
Expand Down

0 comments on commit ddb7d20

Please sign in to comment.