Skip to content

Commit 3de48b3

Browse files
committed
add json decode error test
1 parent 9bf6500 commit 3de48b3

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

qiniu/http.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
import json
32
import logging
43
import platform
54

@@ -26,7 +25,7 @@ def __return_wrapper(resp):
2625
return None, ResponseInfo(resp)
2726
resp.encoding = 'utf-8'
2827
try:
29-
ret = json.loads(resp.text)
28+
ret = resp.json()
3029
except ValueError:
3130
logging.debug("response body decode error: %s" % resp.text)
3231
ret = {}

test_qiniu.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
from qiniu.services.storage.uploader import _form_put
2323

24+
from qiniu.http import __return_wrapper as return_wrapper
25+
2426
import qiniu.config
2527

2628
if is_py2:
@@ -75,6 +77,23 @@ def is_travis():
7577
return os.environ['QINIU_TEST_ENV'] == 'travis'
7678

7779

80+
class HttpTest(unittest.TestCase):
81+
def test_json_decode_error(self):
82+
def mock_res():
83+
r = requests.Response()
84+
r.status_code = 200
85+
r.headers.__setitem__('X-Reqid', 'mockedReqid')
86+
87+
def json_func():
88+
raise ValueError('%s: line %d column %d (char %d)' % ('Expecting value', 0, 0, 0))
89+
r.json = json_func
90+
91+
return r
92+
mocked_res = mock_res()
93+
ret, _ = return_wrapper(mocked_res)
94+
assert ret == {}
95+
96+
7897
class UtilsTest(unittest.TestCase):
7998
def test_urlsafe(self):
8099
a = 'hello\x96'

0 commit comments

Comments
 (0)