Skip to content

Commit d6085c5

Browse files
committed
Add test for use_text_frames over ws; fix lint
1 parent 0ffaef5 commit d6085c5

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

tests/core/providers/test_websocket_provider.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ class WSException(Exception):
5555
pass
5656

5757

58+
GET_BLOCK_JSON_MESSAGE = {
59+
"id": 0,
60+
"jsonrpc": "2.0",
61+
"method": "eth_getBlockByNumber",
62+
"params": ["latest", False],
63+
}
64+
65+
5866
def test_get_endpoint_uri_or_ipc_path_returns_endpoint_uri():
5967
provider = WebSocketProvider("ws://mocked")
6068
assert (
@@ -67,6 +75,14 @@ def test_get_endpoint_uri_or_ipc_path_returns_endpoint_uri():
6775
# -- async -- #
6876

6977

78+
def test_websocket_provider_default_values():
79+
ws_uri = "ws://127.0.0.1:1337"
80+
with patch.dict("os.environ", {"WEB3_WS_PROVIDER_URI": ws_uri}):
81+
provider = WebSocketProvider()
82+
assert provider.endpoint_uri == ws_uri
83+
assert provider.use_text_frames is False
84+
85+
7086
@pytest.mark.asyncio
7187
async def test_disconnect_cleanup():
7288
provider = WebSocketProvider("ws://mocked")
@@ -454,3 +470,26 @@ async def test_persistent_connection_provider_empty_batch_response():
454470
# assert that even though there was an error, we have reset the batching
455471
# state
456472
assert not async_w3.provider._is_batching
473+
474+
475+
@pytest.mark.parametrize(
476+
"use_text_frames, expected_send_arg",
477+
(
478+
(False, to_bytes(text=json.dumps(GET_BLOCK_JSON_MESSAGE))),
479+
(True, json.dumps(GET_BLOCK_JSON_MESSAGE)),
480+
),
481+
)
482+
@pytest.mark.asyncio
483+
async def test_websocket_provider_use_text_frames(use_text_frames, expected_send_arg):
484+
provider = WebSocketProvider("ws://mocked", use_text_frames=use_text_frames)
485+
assert provider.use_text_frames is use_text_frames
486+
487+
# mock provider and add a mocked response to the cache
488+
_mock_ws(provider)
489+
provider._ws.send = AsyncMock()
490+
provider._request_processor._request_response_cache.cache(
491+
generate_cache_key(0), "0x1337"
492+
)
493+
494+
await provider.make_request(RPCEndpoint("eth_getBlockByNumber"), ["latest", False])
495+
provider._ws.send.assert_called_once_with(expected_send_arg)

web3/providers/persistent/websocket.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def __init__(
6363
self,
6464
endpoint_uri: Optional[Union[URI, str]] = None,
6565
websocket_kwargs: Optional[Dict[str, Any]] = None,
66+
# uses binary frames by default
6667
use_text_frames: Optional[bool] = False,
6768
# `PersistentConnectionProvider` kwargs can be passed through
6869
**kwargs: Any,
@@ -120,12 +121,11 @@ async def socket_send(self, request_data: bytes) -> None:
120121
"Connection to websocket has not been initiated for the provider."
121122
)
122123

124+
payload: Union[bytes, str] = request_data
123125
if self.use_text_frames:
124-
request_data = request_data.decode("utf-8")
126+
payload = request_data.decode("utf-8")
125127

126-
await asyncio.wait_for(
127-
self._ws.send(request_data), timeout=self.request_timeout
128-
)
128+
await asyncio.wait_for(self._ws.send(payload), timeout=self.request_timeout)
129129

130130
async def socket_recv(self) -> RPCResponse:
131131
raw_response = await self._ws.recv()

0 commit comments

Comments
 (0)