|
1 | 1 | import asyncio |
2 | | -import sys |
3 | | -import traceback |
4 | 2 | import pytest |
5 | 3 | import pytest_mock |
6 | 4 | import reactivex |
|
15 | 13 | import json |
16 | 14 | from websockets.exceptions import ConnectionClosedOK, ConnectionClosedError |
17 | 15 |
|
| 16 | +from deriv_api.middlewares import MiddleWares |
| 17 | + |
| 18 | + |
18 | 19 | class MockedWs: |
19 | 20 | def __init__(self): |
20 | 21 | self.data = [] |
@@ -154,6 +155,55 @@ async def test_simple_send(): |
154 | 155 | wsconnection.clear() |
155 | 156 | await api.clear() |
156 | 157 |
|
| 158 | +@pytest.mark.asyncio |
| 159 | +async def test_middleware(): |
| 160 | + wsconnection = MockedWs() |
| 161 | + send_will_be_called_args = None |
| 162 | + send_is_called_args = None |
| 163 | + send_will_be_called_return = None |
| 164 | + send_is_called_return = None |
| 165 | + def send_will_be_called(args): |
| 166 | + nonlocal send_will_be_called_args |
| 167 | + send_will_be_called_args = args |
| 168 | + nonlocal send_will_be_called_return |
| 169 | + return send_will_be_called_return |
| 170 | + |
| 171 | + def send_is_called(args): |
| 172 | + nonlocal send_is_called_args |
| 173 | + send_is_called_args = args |
| 174 | + nonlocal send_is_called_return |
| 175 | + return send_is_called_return |
| 176 | + |
| 177 | + api = deriv_api.DerivAPI(connection = wsconnection, middlewares = MiddleWares({'sendWillBeCalled': send_will_be_called, 'sendIsCalled': send_is_called})) |
| 178 | + req1 = {"ping": 2} |
| 179 | + |
| 180 | + # test sendWillBeCalled return true value, sendIsCalled will be not called and the request will not be sent to ws |
| 181 | + data1 = {"echo_req":req1,"msg_type": "ping", "pong": 1,} |
| 182 | + wsconnection.add_data(data1) |
| 183 | + send_will_be_called_return = {'value': 1} # middleware sendWillBeCalled will return a dict |
| 184 | + response = await api.send(req1) |
| 185 | + assert response == send_will_be_called_return # api will return the value of sendWillBeCalled returned |
| 186 | + assert send_will_be_called_args == {'request': req1} |
| 187 | + assert len(wsconnection.called['send']) == 0 # ws send is not called |
| 188 | + |
| 189 | + # test sendWillBeCalled return false value, ws send will be called and sendIsCalled will be called |
| 190 | + send_will_be_called_return = None |
| 191 | + send_is_called_return = None # middleware sendIsCalled will return false |
| 192 | + response = await api.send(req1) |
| 193 | + expected_response = data1.copy() |
| 194 | + expected_response['req_id'] = 1 |
| 195 | + assert response == expected_response # response is the value that ws returned |
| 196 | + assert send_is_called_args == {'response': expected_response, 'request': req1} |
| 197 | + |
| 198 | + # test sendWillBeCalled return false , and sendIsCalled return a true value |
| 199 | + send_is_called_return = {'value': 2} |
| 200 | + wsconnection.add_data(data1) |
| 201 | + response = await api.send(req1) |
| 202 | + assert response == {'value': 2} # will get what sendIsCalled return |
| 203 | + |
| 204 | + wsconnection.clear() |
| 205 | + await api.clear() |
| 206 | + |
157 | 207 | @pytest.mark.asyncio |
158 | 208 | async def test_subscription(): |
159 | 209 | wsconnection = MockedWs() |
@@ -383,18 +433,13 @@ async def recv(self): |
383 | 433 | api.wsconnection_from_inside = True |
384 | 434 | last_error = api.sanity_errors.pipe(op.first(), op.to_future()) |
385 | 435 | await asyncio.sleep(0.1) # waiting for init finished |
386 | | - print("here 382") |
387 | 436 | await api.disconnect() # it will set connected as 'Closed by disconnect', and cause MockedWs2 raising `test disconnect` |
388 | | - print("here 384") |
389 | 437 | assert isinstance((await last_error), ConnectionClosedOK), 'sanity error get errors' |
390 | | - print("here 386") |
391 | 438 | with pytest.raises(ConnectionClosedOK, match='Closed by disconnect'): |
392 | 439 | await api.send({'ping': 1}) # send will get same error |
393 | | - print("here 389") |
394 | 440 | with pytest.raises(ConnectionClosedOK, match='Closed by disconnect'): |
395 | 441 | await api.connected # send will get same error |
396 | 442 | wsconnection.clear() |
397 | | - print("here 391") |
398 | 443 | await api.clear() |
399 | 444 |
|
400 | 445 | # closed by remote |
|
0 commit comments