Skip to content

Commit d3c4543

Browse files
authored
[PR #10672/35c979b backport][3.12] Fix headers being mutated if passed to web.Response as a CIMultiDict (#10674)
1 parent a60d447 commit d3c4543

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

CHANGES/10672.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed :class:`multidict.CIMultiDict` being mutated when passed to :class:`aiohttp.web.Response` -- by :user:`bdraco`.

aiohttp/web_response.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,10 +639,8 @@ def __init__(
639639

640640
if headers is None:
641641
real_headers: CIMultiDict[str] = CIMultiDict()
642-
elif not isinstance(headers, CIMultiDict):
643-
real_headers = CIMultiDict(headers)
644642
else:
645-
real_headers = headers # = cast('CIMultiDict[str]', headers)
643+
real_headers = CIMultiDict(headers)
646644

647645
if content_type is not None and "charset" in content_type:
648646
raise ValueError("charset must not be in content_type argument")

tests/test_web_response.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import aiosignal
1313
import pytest
14-
from multidict import CIMultiDict, CIMultiDictProxy
14+
from multidict import CIMultiDict, CIMultiDictProxy, MultiDict
1515
from re_assert import Matches
1616

1717
from aiohttp import HttpVersion, HttpVersion10, HttpVersion11, hdrs
@@ -1501,3 +1501,15 @@ def test_text_is_json_encoded(self) -> None:
15011501
def test_content_type_is_overrideable(self) -> None:
15021502
resp = json_response({"foo": 42}, content_type="application/vnd.json+api")
15031503
assert "application/vnd.json+api" == resp.content_type
1504+
1505+
1506+
@pytest.mark.parametrize("loose_header_type", (MultiDict, CIMultiDict, dict))
1507+
async def test_passing_cimultidict_to_web_response_not_mutated(
1508+
loose_header_type: type,
1509+
) -> None:
1510+
req = make_request("GET", "/")
1511+
headers = loose_header_type({})
1512+
resp = Response(body=b"answer", headers=headers)
1513+
await resp.prepare(req)
1514+
assert resp.content_length == 6
1515+
assert not headers

0 commit comments

Comments
 (0)