|
2 | 2 |
|
3 | 3 | from binance.async_client import AsyncClient |
4 | 4 | from .conftest import proxy, api_key, api_secret, testnet |
| 5 | +from binance.exceptions import BinanceAPIException, BinanceRequestException |
| 6 | +from aiohttp import ClientResponse, hdrs |
| 7 | +from aiohttp.helpers import TimerNoop |
| 8 | +from yarl import URL |
5 | 9 |
|
6 | 10 | pytestmark = [pytest.mark.asyncio] |
7 | 11 |
|
@@ -253,3 +257,41 @@ async def test_time_unit_milloseconds(): |
253 | 257 | assert len(str(milli_trades[0]["time"])) == 13, ( |
254 | 258 | "Time should be in milliseconds (13 digits)" |
255 | 259 | ) |
| 260 | + |
| 261 | + |
| 262 | +async def test_handle_response(clientAsync): |
| 263 | + # Create base response object |
| 264 | + mock_response = ClientResponse( |
| 265 | + 'GET', URL('http://test.com'), |
| 266 | + request_info=None, |
| 267 | + writer=None, |
| 268 | + continue100=None, |
| 269 | + timer=TimerNoop(), |
| 270 | + traces=[], |
| 271 | + loop=clientAsync.loop, |
| 272 | + session=None, |
| 273 | + ) |
| 274 | + # Initialize headers |
| 275 | + mock_response._headers = {hdrs.CONTENT_TYPE: 'application/json'} |
| 276 | + |
| 277 | + # Test successful JSON response |
| 278 | + mock_response.status = 200 |
| 279 | + mock_response._body = b'{"key": "value"}' |
| 280 | + assert await clientAsync._handle_response(mock_response) == {"key": "value"} |
| 281 | + |
| 282 | + # Test empty response |
| 283 | + mock_response.status = 200 |
| 284 | + mock_response._body = b'' |
| 285 | + assert await clientAsync._handle_response(mock_response) == {} |
| 286 | + |
| 287 | + # Test invalid JSON response |
| 288 | + mock_response.status = 200 |
| 289 | + mock_response._body = b'invalid json' |
| 290 | + with pytest.raises(BinanceRequestException): |
| 291 | + await clientAsync._handle_response(mock_response) |
| 292 | + |
| 293 | + # Test error status code |
| 294 | + mock_response.status = 400 |
| 295 | + mock_response._body = b'error message' |
| 296 | + with pytest.raises(BinanceAPIException): |
| 297 | + await clientAsync._handle_response(mock_response) |
0 commit comments