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 authored Oct 27, 2021
1 parent 0fef0a3 commit b8f4d70
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 @@ -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,
Expand All @@ -655,6 +656,7 @@ def ws_connect(
heartbeat=heartbeat,
auth=auth,
origin=origin,
params=params,
headers=headers,
proxy=proxy,
proxy_auth=proxy_auth,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
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 @@ -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:
Expand Down

0 comments on commit b8f4d70

Please sign in to comment.