mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-10 02:09:51 +08:00
fix: catch UnicodeDecodeError when json loads request body
change: detect request/response bytes encoding, instead of assuming utf-8
This commit is contained in:
@@ -1,5 +1,15 @@
|
|||||||
# Release History
|
# Release History
|
||||||
|
|
||||||
|
## 2.4.5 (2019-12-18)
|
||||||
|
|
||||||
|
**Fixed**
|
||||||
|
|
||||||
|
- fix: catch UnicodeDecodeError when json loads request body
|
||||||
|
|
||||||
|
**Changed**
|
||||||
|
|
||||||
|
- change: detect request/response bytes encoding, instead of assuming utf-8
|
||||||
|
|
||||||
## 2.4.4 (2019-12-17)
|
## 2.4.4 (2019-12-17)
|
||||||
|
|
||||||
**Added**
|
**Added**
|
||||||
|
|||||||
@@ -144,9 +144,7 @@ def __stringify_request(request_data):
|
|||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Content-Length": "52"
|
"Content-Length": "52"
|
||||||
},
|
},
|
||||||
"json": {
|
"body": b'{"sign": "cb9d60acd09080ea66c8e63a1c78c6459ea00168"}',
|
||||||
"sign": "cb9d60acd09080ea66c8e63a1c78c6459ea00168"
|
|
||||||
},
|
|
||||||
"verify": false
|
"verify": false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,18 +156,18 @@ def __stringify_request(request_data):
|
|||||||
|
|
||||||
elif isinstance(value, bytes):
|
elif isinstance(value, bytes):
|
||||||
try:
|
try:
|
||||||
encoding = "utf-8"
|
encoding = json.detect_encoding(value)
|
||||||
value = escape(value.decode(encoding))
|
value = value.decode(encoding)
|
||||||
|
if key == "body":
|
||||||
|
try:
|
||||||
|
# request body is in json format
|
||||||
|
value = json.loads(value)
|
||||||
|
except JSONDecodeError:
|
||||||
|
pass
|
||||||
|
value = escape(value)
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if key == "body":
|
|
||||||
try:
|
|
||||||
# request body is in json format
|
|
||||||
value = json.loads(value)
|
|
||||||
except JSONDecodeError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
elif not isinstance(value, (basestring, numeric_types, Iterable)):
|
elif not isinstance(value, (basestring, numeric_types, Iterable)):
|
||||||
# class instance, e.g. MultipartEncoder()
|
# class instance, e.g. MultipartEncoder()
|
||||||
value = repr(value)
|
value = repr(value)
|
||||||
@@ -200,7 +198,7 @@ def __stringify_response(response_data):
|
|||||||
"url": "http://127.0.0.1:5000/api/users/9001",
|
"url": "http://127.0.0.1:5000/api/users/9001",
|
||||||
"reason": "NOT FOUND",
|
"reason": "NOT FOUND",
|
||||||
"cookies": {},
|
"cookies": {},
|
||||||
"json": {
|
"body": {
|
||||||
"success": false,
|
"success": false,
|
||||||
"data": {}
|
"data": {}
|
||||||
}
|
}
|
||||||
@@ -216,7 +214,7 @@ def __stringify_response(response_data):
|
|||||||
try:
|
try:
|
||||||
encoding = response_data.get("encoding")
|
encoding = response_data.get("encoding")
|
||||||
if not encoding or encoding == "None":
|
if not encoding or encoding == "None":
|
||||||
encoding = "utf-8"
|
encoding = json.detect_encoding(value)
|
||||||
|
|
||||||
if key == "body" and "image" in response_data["content_type"]:
|
if key == "body" and "image" in response_data["content_type"]:
|
||||||
# display image
|
# display image
|
||||||
|
|||||||
Reference in New Issue
Block a user