diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 3a7debdb..a6ab030a 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,5 +1,15 @@ # 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) **Added** diff --git a/httprunner/report.py b/httprunner/report.py index cc8e38c6..7cc84704 100644 --- a/httprunner/report.py +++ b/httprunner/report.py @@ -144,9 +144,7 @@ def __stringify_request(request_data): "Content-Type": "application/json", "Content-Length": "52" }, - "json": { - "sign": "cb9d60acd09080ea66c8e63a1c78c6459ea00168" - }, + "body": b'{"sign": "cb9d60acd09080ea66c8e63a1c78c6459ea00168"}', "verify": false } @@ -158,18 +156,18 @@ def __stringify_request(request_data): elif isinstance(value, bytes): try: - encoding = "utf-8" - value = escape(value.decode(encoding)) + encoding = json.detect_encoding(value) + 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: 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)): # class instance, e.g. MultipartEncoder() value = repr(value) @@ -200,7 +198,7 @@ def __stringify_response(response_data): "url": "http://127.0.0.1:5000/api/users/9001", "reason": "NOT FOUND", "cookies": {}, - "json": { + "body": { "success": false, "data": {} } @@ -216,7 +214,7 @@ def __stringify_response(response_data): try: encoding = response_data.get("encoding") if not encoding or encoding == "None": - encoding = "utf-8" + encoding = json.detect_encoding(value) if key == "body" and "image" in response_data["content_type"]: # display image