diff --git a/CHANGES/8572.feature.rst b/CHANGES/8572.feature.rst new file mode 100644 index 00000000000..28b7a7cfb26 --- /dev/null +++ b/CHANGES/8572.feature.rst @@ -0,0 +1,2 @@ +Added ``response`` attribute to the :py:class:`~aiohttp.ClientWebSocketResponse` class. It allows users to have access to response headers and cookies of a websocket request +-- by :user:`leszekhanusz`. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 7a7f882c885..cb6cdcbb12e 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -220,6 +220,7 @@ Kyrylo Perevozchikov Kyungmin Lee Lars P. Søndergaard Lee LieWhite +Leszek Hanusz Liu Hua Louis-Philippe Huberdeau Loïc Lajeanne diff --git a/aiohttp/client_ws.py b/aiohttp/client_ws.py index 757f5f0388f..5ca413f64ad 100644 --- a/aiohttp/client_ws.py +++ b/aiohttp/client_ws.py @@ -212,6 +212,10 @@ def compress(self) -> int: def client_notakeover(self) -> bool: return self._client_notakeover + @property + def response(self) -> ClientResponse: + return self._response + def get_extra_info(self, name: str, default: Any = None) -> Any: """extra info from connection transport""" conn = self._response.connection diff --git a/docs/client_reference.rst b/docs/client_reference.rst index 9a76bfbd14b..d9b6f0b551a 100644 --- a/docs/client_reference.rst +++ b/docs/client_reference.rst @@ -1511,6 +1511,13 @@ manually. May be ``None`` if server and client protocols are not overlapping. + .. attribute:: response + + The :class:`ClientResponse ` response object. + Can be used to get the response headers and cookies. + + .. versionadded:: 4.0 + .. method:: get_extra_info(name, default=None) Reads optional extra information from the connection's transport. diff --git a/tests/test_client_ws.py b/tests/test_client_ws.py index 3b1bd00b093..3382db69e44 100644 --- a/tests/test_client_ws.py +++ b/tests/test_client_ws.py @@ -47,6 +47,9 @@ async def test_ws_connect( assert res.protocol == "chat" assert hdrs.ORIGIN not in m_req.call_args[1]["headers"] + # Checking new response attribute + assert res.response.headers == resp.headers + async def test_ws_connect_read_timeout_is_reset_to_inf( ws_key: str, loop: asyncio.AbstractEventLoop, key_data: bytes