feat: log request/response headers and body with indent

This commit is contained in:
debugtalk
2020-05-19 16:07:41 +08:00
parent aa8a21577e
commit ba7cc9df62
2 changed files with 10 additions and 1 deletions
+1
View File
@@ -7,6 +7,7 @@
- feat: make testsuite and run testsuite - feat: make testsuite and run testsuite
- feat: testcase/testsuite config support getting variables by function - feat: testcase/testsuite config support getting variables by function
- feat: har2case with request cookies - feat: har2case with request cookies
- feat: log request/response headers and body with indent
**Fixed** **Fixed**
+9 -1
View File
@@ -1,3 +1,4 @@
import json
import time import time
import requests import requests
@@ -32,12 +33,19 @@ def get_req_resp_record(resp_obj: Response) -> ReqRespData:
def log_print(req_or_resp, r_type): def log_print(req_or_resp, r_type):
msg = f"\n================== {r_type} details ==================\n" msg = f"\n================== {r_type} details ==================\n"
for key, value in req_or_resp.dict().items(): for key, value in req_or_resp.dict().items():
msg += "{:<16} : {}\n".format(key, repr(value)) if isinstance(value, dict):
value = json.dumps(value, indent=4)
msg += "{:<8} : {}\n".format(key, value)
logger.debug(msg) logger.debug(msg)
# record actual request info # record actual request info
request_headers = dict(resp_obj.request.headers) request_headers = dict(resp_obj.request.headers)
request_body = resp_obj.request.body request_body = resp_obj.request.body
try:
request_body = json.loads(request_body)
except json.JSONDecodeError:
pass
if request_body: if request_body:
request_content_type = lower_dict_keys(request_headers).get("content-type") request_content_type = lower_dict_keys(request_headers).get("content-type")