diff --git a/binance/async_client.py b/binance/async_client.py index 113483c08..923d6bb30 100644 --- a/binance/async_client.py +++ b/binance/async_client.py @@ -162,7 +162,8 @@ async def _handle_response(self, response: aiohttp.ClientResponse): if not str(response.status).startswith("2"): raise BinanceAPIException(response, response.status, await response.text()) - if response.text == "": + text = await response.text() + if text == "": return {} try: diff --git a/tests/test_async_client.py b/tests/test_async_client.py index 22eb6f9f6..66f072b32 100644 --- a/tests/test_async_client.py +++ b/tests/test_async_client.py @@ -2,6 +2,10 @@ from binance.async_client import AsyncClient from .conftest import proxy, api_key, api_secret, testnet +from binance.exceptions import BinanceAPIException, BinanceRequestException +from aiohttp import ClientResponse, hdrs +from aiohttp.helpers import TimerNoop +from yarl import URL pytestmark = [pytest.mark.asyncio] @@ -253,3 +257,41 @@ async def test_time_unit_milloseconds(): assert len(str(milli_trades[0]["time"])) == 13, ( "Time should be in milliseconds (13 digits)" ) + + +async def test_handle_response(clientAsync): + # Create base response object + mock_response = ClientResponse( + 'GET', URL('http://test.com'), + request_info=None, + writer=None, + continue100=None, + timer=TimerNoop(), + traces=[], + loop=clientAsync.loop, + session=None, + ) + # Initialize headers + mock_response._headers = {hdrs.CONTENT_TYPE: 'application/json'} + + # Test successful JSON response + mock_response.status = 200 + mock_response._body = b'{"key": "value"}' + assert await clientAsync._handle_response(mock_response) == {"key": "value"} + + # Test empty response + mock_response.status = 200 + mock_response._body = b'' + assert await clientAsync._handle_response(mock_response) == {} + + # Test invalid JSON response + mock_response.status = 200 + mock_response._body = b'invalid json' + with pytest.raises(BinanceRequestException): + await clientAsync._handle_response(mock_response) + + # Test error status code + mock_response.status = 400 + mock_response._body = b'error message' + with pytest.raises(BinanceAPIException): + await clientAsync._handle_response(mock_response) diff --git a/tests/test_client.py b/tests/test_client.py index 52194e0f0..48b977b82 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,5 +1,6 @@ import pytest from binance.client import Client +from binance.exceptions import BinanceAPIException, BinanceRequestException from .conftest import proxies, api_key, api_secret, testnet @@ -215,3 +216,37 @@ def test_time_unit_milloseconds(): assert len(str(milli_trades[0]["time"])) == 13, ( "Time should be in milliseconds (13 digits)" ) + + +def test_handle_response(client): + # Test successful JSON response + mock_response = type('Response', (), { + 'status_code': 200, + 'text': '{"key": "value"}', + 'json': lambda: {"key": "value"} + }) + assert client._handle_response(mock_response) == {"key": "value"} + + # Test empty response + mock_empty_response = type('Response', (), { + 'status_code': 200, + 'text': '' + }) + assert client._handle_response(mock_empty_response) == {} + + # Test invalid JSON response + mock_invalid_response = type('Response', (), { + 'status_code': 200, + 'text': 'invalid json', + 'json': lambda: exec('raise ValueError()') + }) + with pytest.raises(BinanceRequestException): + client._handle_response(mock_invalid_response) + + # Test error status code + mock_error_response = type('Response', (), { + 'status_code': 400, + 'text': 'error message' + }) + with pytest.raises(BinanceAPIException): + client._handle_response(mock_error_response)