diff --git a/CHANGES/3207.bugfix b/CHANGES/3207.bugfix new file mode 100644 index 00000000000..4966019d307 --- /dev/null +++ b/CHANGES/3207.bugfix @@ -0,0 +1,2 @@ +Accept ``CIMultiDictProxy`` instances for ``headers`` argument in ``web.Response`` +constructor. diff --git a/aiohttp/web_response.py b/aiohttp/web_response.py index f3fc59e43d6..73c71fd3285 100644 --- a/aiohttp/web_response.py +++ b/aiohttp/web_response.py @@ -458,7 +458,7 @@ def __init__(self, *, if headers is None: headers = CIMultiDict() - elif not isinstance(headers, (CIMultiDict, CIMultiDictProxy)): + elif not isinstance(headers, CIMultiDict): headers = CIMultiDict(headers) else: headers = cast(CIMultiDict, headers) diff --git a/tests/test_web_response.py b/tests/test_web_response.py index 8e970fe1a66..94b0e410295 100644 --- a/tests/test_web_response.py +++ b/tests/test_web_response.py @@ -5,7 +5,7 @@ from unittest import mock import pytest -from multidict import CIMultiDict +from multidict import CIMultiDict, CIMultiDictProxy from aiohttp import HttpVersion, HttpVersion10, HttpVersion11, hdrs, signals from aiohttp.payload import BytesPayload @@ -1105,6 +1105,13 @@ def test_response_with_content_length_header_without_body() -> None: assert resp.content_length == 123 +def test_response_with_immutable_headers() -> None: + resp = Response(text='text', + headers=CIMultiDictProxy(CIMultiDict({'Header': 'Value'}))) + assert resp.headers == {'Header': 'Value', + 'Content-Type': 'text/plain; charset=utf-8'} + + class TestJSONResponse: def test_content_type_is_application_json_by_default(self) -> None: