Skip to content

Commit

Permalink
Add URL params to ClientSession.ws_connect (#5869)
Browse files Browse the repository at this point in the history
* Fix URL params were unavailable in ws_connect (#5868)

* Update CHANGES/5868.bugfix

Co-authored-by: Anes Abismail <[email protected]>

* Update 5868.bugfix

Co-authored-by: Anes Abismail <[email protected]>
Co-authored-by: Andrew Svetlov <[email protected]>
  • Loading branch information
3 people committed Oct 27, 2021
1 parent 0b42855 commit 708dd98
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/5868.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added ``params`` keyword argument to ``ClientSession.ws_connect``. -- :user:`hoh`.
4 changes: 4 additions & 0 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,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,
Expand All @@ -683,6 +684,7 @@ def ws_connect(
heartbeat=heartbeat,
auth=auth,
origin=origin,
params=params,
headers=headers,
proxy=proxy,
proxy_auth=proxy_auth,
Expand All @@ -709,6 +711,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,
Expand Down Expand Up @@ -752,6 +755,7 @@ async def _ws_connect(
resp = await self.request(
method,
url,
params=params,
headers=real_headers,
read_until_eof=False,
auth=auth,
Expand Down
16 changes: 16 additions & 0 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down Expand Up @@ -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)

Expand Down
24 changes: 24 additions & 0 deletions tests/test_client_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,30 @@ async def test_ws_connect_with_origin(key_data, loop) -> None:
assert m_req.call_args[1]["headers"][hdrs.ORIGIN] == origin


async def test_ws_connect_with_params(ws_key, loop, key_data) -> 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, ws_key, key_data) -> None:
class CustomResponse(client.ClientWebSocketResponse):
def read(self, decode=False):
Expand Down

0 comments on commit 708dd98

Please sign in to comment.