Skip to content

Commit

Permalink
Fix typing errors on content_disposition_header() (#8698)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamsorcerer authored Aug 14, 2024
1 parent 61af615 commit 28cb7d7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGES/8698.breaking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed signature of ``content_disposition_header()`` so ``params`` is now passed as a dict, in order to reduce typing errors -- by :user:`Dreamsorcerer`.
5 changes: 4 additions & 1 deletion aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,10 @@ def quoted_string(content: str) -> str:


def content_disposition_header(
disptype: str, quote_fields: bool = True, _charset: str = "utf-8", **params: str
disptype: str,
quote_fields: bool = True,
_charset: str = "utf-8",
params: Optional[Dict[str, str]] = None,
) -> str:
"""Sets ``Content-Disposition`` header for MIME.
Expand Down
4 changes: 2 additions & 2 deletions aiohttp/payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ def set_content_disposition(
disptype: str,
quote_fields: bool = True,
_charset: str = "utf-8",
**params: Any,
**params: str,
) -> None:
"""Sets ``Content-Disposition`` header."""
self._headers[hdrs.CONTENT_DISPOSITION] = content_disposition_header(
disptype, quote_fields=quote_fields, _charset=_charset, **params
disptype, quote_fields=quote_fields, _charset=_charset, params=params
)

@abstractmethod
Expand Down
37 changes: 21 additions & 16 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,24 +493,29 @@ async def test_ceil_timeout_small_with_overriden_threshold(loop) -> None:


@pytest.mark.parametrize(
"kwargs, result",
"params, quote_fields, _charset, expected",
[
(dict(foo="bar"), 'attachment; foo="bar"'),
(dict(foo="bar[]"), 'attachment; foo="bar[]"'),
(dict(foo=' a""b\\'), 'attachment; foo="\\ a\\"\\"b\\\\"'),
(dict(foo="bär"), "attachment; foo*=utf-8''b%C3%A4r"),
(dict(foo='bär "\\', quote_fields=False), 'attachment; foo="bär \\"\\\\"'),
(dict(foo="bär", _charset="latin-1"), "attachment; foo*=latin-1''b%E4r"),
(dict(filename="bär"), 'attachment; filename="b%C3%A4r"'),
(dict(filename="bär", _charset="latin-1"), 'attachment; filename="b%E4r"'),
(dict(foo="bar"), True, "utf-8", 'attachment; foo="bar"'),
(dict(foo="bar[]"), True, "utf-8", 'attachment; foo="bar[]"'),
(dict(foo=' a""b\\'), True, "utf-8", 'attachment; foo="\\ a\\"\\"b\\\\"'),
(dict(foo="bär"), True, "utf-8", "attachment; foo*=utf-8''b%C3%A4r"),
(dict(foo='bär "\\'), False, "utf-8", 'attachment; foo="bär \\"\\\\"'),
(dict(foo="bär"), True, "latin-1", "attachment; foo*=latin-1''b%E4r"),
(dict(filename="bär"), True, "utf-8", 'attachment; filename="b%C3%A4r"'),
(dict(filename="bär"), True, "latin-1", 'attachment; filename="b%E4r"'),
(
dict(filename='bär "\\', quote_fields=False),
dict(filename='bär "\\'),
False,
"utf-8",
'attachment; filename="bär \\"\\\\"',
),
],
)
def test_content_disposition(kwargs, result) -> None:
assert helpers.content_disposition_header("attachment", **kwargs) == result
def test_content_disposition(params, quote_fields, _charset, expected) -> None:
result = helpers.content_disposition_header(
"attachment", quote_fields=quote_fields, _charset=_charset, params=params
)
assert result == expected


def test_content_disposition_bad_type() -> None:
Expand All @@ -526,13 +531,13 @@ def test_content_disposition_bad_type() -> None:

def test_set_content_disposition_bad_param() -> None:
with pytest.raises(ValueError):
helpers.content_disposition_header("inline", **{"foo bar": "baz"})
helpers.content_disposition_header("inline", params={"foo bar": "baz"})
with pytest.raises(ValueError):
helpers.content_disposition_header("inline", **{"—Ç–µ—Å—Ç": "baz"})
helpers.content_disposition_header("inline", params={"—Ç–µ—Å—Ç": "baz"})
with pytest.raises(ValueError):
helpers.content_disposition_header("inline", **{"": "baz"})
helpers.content_disposition_header("inline", params={"": "baz"})
with pytest.raises(ValueError):
helpers.content_disposition_header("inline", **{"foo\x00bar": "baz"})
helpers.content_disposition_header("inline", params={"foo\x00bar": "baz"})


# --------------------- proxies_from_env ------------------------------
Expand Down

0 comments on commit 28cb7d7

Please sign in to comment.