diff --git a/CHANGES/5868.bugfix b/CHANGES/5868.bugfix new file mode 100644 index 00000000000..e14a650b3b1 --- /dev/null +++ b/CHANGES/5868.bugfix @@ -0,0 +1 @@ +Added ``params`` keyword argument to ``ClientSession.ws_connect``. -- :user:`hoh`. diff --git a/aiohttp/client.py b/aiohttp/client.py index c14a682a404..a91c007e104 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -634,6 +634,7 @@ def ws_connect( heartbeat: Optional[float] = None, auth: Optional[BasicAuth] = None, origin: Optional[str] = None, + params: Optional[Mapping[str, str]] = None, headers: Optional[LooseHeaders] = None, proxy: Optional[StrOrURL] = None, proxy_auth: Optional[BasicAuth] = None, @@ -655,6 +656,7 @@ def ws_connect( heartbeat=heartbeat, auth=auth, origin=origin, + params=params, headers=headers, proxy=proxy, proxy_auth=proxy_auth, @@ -678,6 +680,7 @@ async def _ws_connect( heartbeat: Optional[float] = None, auth: Optional[BasicAuth] = None, origin: Optional[str] = None, + params: Optional[Mapping[str, str]] = None, headers: Optional[LooseHeaders] = None, proxy: Optional[StrOrURL] = None, proxy_auth: Optional[BasicAuth] = None, @@ -745,6 +748,7 @@ async def _ws_connect( resp = await self.request( method, url, + params=params, headers=real_headers, read_until_eof=False, auth=auth, diff --git a/docs/client_reference.rst b/docs/client_reference.rst index cb5bbecae39..23fd050e0d4 100644 --- a/docs/client_reference.rst +++ b/docs/client_reference.rst @@ -644,6 +644,7 @@ The client session supports the context manager protocol for self closing. autoping=True,\ heartbeat=None,\ origin=None, \ + params=None, \ headers=None, \ proxy=None, proxy_auth=None, ssl=None, \ verify_ssl=None, fingerprint=None, \ @@ -685,6 +686,21 @@ The client session supports the context manager protocol for self closing. :param str origin: Origin header to send to server(optional) + :param params: Mapping, iterable of tuple of *key*/*value* pairs or + string to be sent as parameters in the query + string of the new request. Ignored for subsequent + redirected requests (optional) + + Allowed values are: + + - :class:`collections.abc.Mapping` e.g. :class:`dict`, + :class:`multidict.MultiDict` or + :class:`multidict.MultiDictProxy` + - :class:`collections.abc.Iterable` e.g. :class:`tuple` or + :class:`list` + - :class:`str` with preferably url-encoded content + (**Warning:** content will not be encoded by *aiohttp*) + :param dict headers: HTTP Headers to send with the request (optional) diff --git a/tests/test_client_ws.py b/tests/test_client_ws.py index 14fae2fd5c3..babb6cc0ad4 100644 --- a/tests/test_client_ws.py +++ b/tests/test_client_ws.py @@ -73,6 +73,30 @@ async def test_ws_connect_with_origin(key_data: Any, loop: Any) -> None: assert m_req.call_args[1]["headers"][hdrs.ORIGIN] == origin +async def test_ws_connect_with_params(ws_key: Any, loop: Any, key_data: Any) -> None: + params = {"key1": "value1", "key2": "value2"} + + resp = mock.Mock() + resp.status = 101 + resp.headers = { + hdrs.UPGRADE: "websocket", + hdrs.CONNECTION: "upgrade", + hdrs.SEC_WEBSOCKET_ACCEPT: ws_key, + hdrs.SEC_WEBSOCKET_PROTOCOL: "chat", + } + with mock.patch("aiohttp.client.os") as m_os: + with mock.patch("aiohttp.client.ClientSession.request") as m_req: + m_os.urandom.return_value = key_data + m_req.return_value = loop.create_future() + m_req.return_value.set_result(resp) + + await aiohttp.ClientSession().ws_connect( + "http://test.org", protocols=("t1", "t2", "chat"), params=params + ) + + assert m_req.call_args[1]["params"] == params + + async def test_ws_connect_custom_response( loop: Any, ws_key: Any, key_data: Any ) -> None: