Skip to content

Commit 06c721e

Browse files
authored
fix: async client receive empty text (#1569)
* add timeout to jobs * fix: fix response for async client when receiving empty text response
1 parent 80e1ff5 commit 06c721e

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

binance/async_client.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ async def _handle_response(self, response: aiohttp.ClientResponse):
162162
if not str(response.status).startswith("2"):
163163
raise BinanceAPIException(response, response.status, await response.text())
164164

165-
if response.text == "":
165+
text = await response.text()
166+
if text == "":
166167
return {}
167168

168169
try:

tests/test_async_client.py

+42
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
from binance.async_client import AsyncClient
44
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
59

610
pytestmark = [pytest.mark.asyncio]
711

@@ -253,3 +257,41 @@ async def test_time_unit_milloseconds():
253257
assert len(str(milli_trades[0]["time"])) == 13, (
254258
"Time should be in milliseconds (13 digits)"
255259
)
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)

tests/test_client.py

+35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
from binance.client import Client
3+
from binance.exceptions import BinanceAPIException, BinanceRequestException
34
from .conftest import proxies, api_key, api_secret, testnet
45

56

@@ -215,3 +216,37 @@ def test_time_unit_milloseconds():
215216
assert len(str(milli_trades[0]["time"])) == 13, (
216217
"Time should be in milliseconds (13 digits)"
217218
)
219+
220+
221+
def test_handle_response(client):
222+
# Test successful JSON response
223+
mock_response = type('Response', (), {
224+
'status_code': 200,
225+
'text': '{"key": "value"}',
226+
'json': lambda: {"key": "value"}
227+
})
228+
assert client._handle_response(mock_response) == {"key": "value"}
229+
230+
# Test empty response
231+
mock_empty_response = type('Response', (), {
232+
'status_code': 200,
233+
'text': ''
234+
})
235+
assert client._handle_response(mock_empty_response) == {}
236+
237+
# Test invalid JSON response
238+
mock_invalid_response = type('Response', (), {
239+
'status_code': 200,
240+
'text': 'invalid json',
241+
'json': lambda: exec('raise ValueError()')
242+
})
243+
with pytest.raises(BinanceRequestException):
244+
client._handle_response(mock_invalid_response)
245+
246+
# Test error status code
247+
mock_error_response = type('Response', (), {
248+
'status_code': 400,
249+
'text': 'error message'
250+
})
251+
with pytest.raises(BinanceAPIException):
252+
client._handle_response(mock_error_response)

0 commit comments

Comments
 (0)