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 3f4a257a678..ef97bbf2c39 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -213,6 +213,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 28d1117502f..4aafad475f7 100644 --- a/aiohttp/client_ws.py +++ b/aiohttp/client_ws.py @@ -155,6 +155,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 d5092692b7e..794b64c69cb 100644 --- a/docs/client_reference.rst +++ b/docs/client_reference.rst @@ -1525,6 +1525,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 81ad41c2347..db21a2603d9 100644 --- a/tests/test_client_ws.py +++ b/tests/test_client_ws.py @@ -40,6 +40,9 @@ async def test_ws_connect(ws_key: Any, loop: Any, key_data: Any) -> None: 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: Any, loop: Any, key_data: Any