Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion binance/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
42 changes: 42 additions & 0 deletions tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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)
35 changes: 35 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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)
Loading