@@ -55,6 +55,14 @@ class WSException(Exception):
55
55
pass
56
56
57
57
58
+ GET_BLOCK_JSON_MESSAGE = {
59
+ "id" : 0 ,
60
+ "jsonrpc" : "2.0" ,
61
+ "method" : "eth_getBlockByNumber" ,
62
+ "params" : ["latest" , False ],
63
+ }
64
+
65
+
58
66
def test_get_endpoint_uri_or_ipc_path_returns_endpoint_uri ():
59
67
provider = WebSocketProvider ("ws://mocked" )
60
68
assert (
@@ -67,6 +75,14 @@ def test_get_endpoint_uri_or_ipc_path_returns_endpoint_uri():
67
75
# -- async -- #
68
76
69
77
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
+
70
86
@pytest .mark .asyncio
71
87
async def test_disconnect_cleanup ():
72
88
provider = WebSocketProvider ("ws://mocked" )
@@ -454,3 +470,26 @@ async def test_persistent_connection_provider_empty_batch_response():
454
470
# assert that even though there was an error, we have reset the batching
455
471
# state
456
472
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 )
0 commit comments