Code Duplication    Length = 29-36 lines in 3 locations

tests/test_protocol_for_ws.py 3 locations

@@ 153-188 (lines=36) @@
150
        handle_ws_message.assert_called_once()
151
152
153
async def test_rpc_call_with_an_empty_array(aiohttp_client, mocker):
154
    """
155
    --> []
156
    <-- {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}
157
    """
158
159
    rpc_server = aiohttp_rpc.WsJsonRpcServer()
160
161
    client = await utils.make_ws_client(aiohttp_client, rpc_server)
162
163
    future = asyncio.Future()
164
165
    def unprocessed_json_response_handler(*, ws_connect, ws_msg, json_response):
166
        future.set_result(json_response)
167
        assert json_response == {
168
            'jsonrpc': '2.0', 'error': {'code': -32600, 'message': errors.InvalidRequest.message}, 'id': None,
169
        }
170
171
    async with aiohttp_rpc.WsJsonRpcClient(
172
            '/rpc',
173
            session=client,
174
            unprocessed_json_response_handler=unprocessed_json_response_handler,
175
    ) as rpc:
176
        handle_ws_message = mocker.patch.object(
177
            rpc,
178
            '_handle_single_ws_message',
179
            side_effect=rpc._handle_single_ws_message,
180
        )
181
182
        with pytest.raises(errors.InvalidRequest):
183
            await rpc.batch([])
184
185
        handle_ws_message.assert_not_called()
186
        await rpc.send_json([])
187
        await asyncio.wait_for(future, timeout=3)
188
        handle_ws_message.assert_called_once()
189
190
191
async def test_rpc_call_with_an_invalid_batch(aiohttp_client, mocker):
@@ 222-256 (lines=35) @@
219
        handle_ws_message.assert_called_once()
220
221
222
async def test_rpc_call_with_invalid_batch(aiohttp_client, mocker):
223
    """
224
    --> [1,2,3]
225
    <-- [
226
      {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null},
227
      {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null},
228
      {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}
229
    ]
230
    """
231
232
    rpc_server = aiohttp_rpc.WsJsonRpcServer()
233
    client = await utils.make_ws_client(aiohttp_client, rpc_server)
234
    future = asyncio.Future()
235
236
    json_with_error = {
237
        'jsonrpc': '2.0', 'error': {'code': -32600, 'message': 'Data must be a dict or an list.'}, 'id': None,
238
    }
239
240
    def unprocessed_json_response_handler(*, ws_connect, ws_msg, json_response):
241
        future.set_result(json_response)
242
        assert json_response == [json_with_error, json_with_error, json_with_error]
243
244
    async with aiohttp_rpc.WsJsonRpcClient(
245
            '/rpc',
246
            session=client,
247
            unprocessed_json_response_handler=unprocessed_json_response_handler,
248
    ) as rpc:
249
        handle_ws_message = mocker.patch.object(
250
            rpc,
251
            '_handle_single_ws_message',
252
            side_effect=rpc._handle_single_ws_message,
253
        )
254
        await rpc.send_json([1, 2, 3])
255
        await asyncio.wait_for(future, timeout=3)
256
        handle_ws_message.assert_called_once()
257
258
259
async def test_rpc_call_with_different_invalid_batch(aiohttp_client):
@@ 191-219 (lines=29) @@
188
        handle_ws_message.assert_called_once()
189
190
191
async def test_rpc_call_with_an_invalid_batch(aiohttp_client, mocker):
192
    """
193
    --> [1]
194
    <-- {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}
195
    """
196
197
    rpc_server = aiohttp_rpc.WsJsonRpcServer()
198
    client = await utils.make_ws_client(aiohttp_client, rpc_server)
199
    future = asyncio.Future()
200
201
    def unprocessed_json_response_handler(*, ws_connect, ws_msg, json_response):
202
        future.set_result(json_response)
203
        assert json_response == {
204
            'jsonrpc': '2.0', 'error': {'code': -32600, 'message': 'Data must be a dict.'}, 'id': None,
205
        }
206
207
    async with aiohttp_rpc.WsJsonRpcClient(
208
            '/rpc',
209
            session=client,
210
            unprocessed_json_response_handler=unprocessed_json_response_handler,
211
    ) as rpc:
212
        handle_ws_message = mocker.patch.object(
213
            rpc,
214
            '_handle_single_ws_message',
215
            side_effect=rpc._handle_single_ws_message,
216
        )
217
        await rpc.send_json([1])
218
        await asyncio.wait_for(future, timeout=3)
219
        handle_ws_message.assert_called_once()
220
221
222
async def test_rpc_call_with_invalid_batch(aiohttp_client, mocker):