bugfix: stringfy data before generating report

This commit is contained in:
debugtalk
2018-07-21 12:47:39 +08:00
parent c8cabedad8
commit 1c8d38a2c8
4 changed files with 35 additions and 29 deletions

View File

@@ -1,7 +1,7 @@
__title__ = 'HttpRunner'
__description__ = 'One-stop solution for HTTP(S) testing.'
__url__ = 'https://github.com/HttpRunner/HttpRunner'
__version__ = '1.5.4'
__version__ = '1.5.5'
__author__ = 'debugtalk'
__author_email__ = 'mail@debugtalk.com'
__license__ = 'MIT'

View File

@@ -67,7 +67,9 @@ class HttpSession(requests.Session):
"content_size": "N/A",
"response_time_ms": "N/A",
"elapsed_ms": "N/A",
"content": None
"encoding": None,
"content": None,
"content_type": ""
}
}
@@ -151,6 +153,7 @@ class HttpSession(requests.Session):
self.meta_data["response"]["encoding"] = response.encoding
self.meta_data["response"]["content"] = response.content
self.meta_data["response"]["text"] = response.text
self.meta_data["response"]["content_type"] = response.headers.get("Content-Type", "")
try:
self.meta_data["response"]["json"] = response.json()

View File

@@ -93,8 +93,8 @@ def render_html_report(summary, html_report_name=None, html_report_template=None
for suite_summary in summary["details"]:
for record in suite_summary.get("records"):
meta_data = record['meta_data']
stringify_body(meta_data, 'request')
stringify_body(meta_data, 'response')
stringify_data(meta_data, 'request')
stringify_data(meta_data, 'response')
with io.open(html_report_template, "r", encoding='utf-8') as fp_r:
template_content = fp_r.read()
@@ -107,36 +107,39 @@ def render_html_report(summary, html_report_name=None, html_report_template=None
return report_path
def stringify_body(meta_data, request_or_response):
headers = meta_data.get('{}_headers'.format(request_or_response), {})
body = meta_data.get('{}_body'.format(request_or_response))
def stringify_data(meta_data, request_or_response):
headers = meta_data[request_or_response]["headers"]
if isinstance(body, CaseInsensitiveDict):
body = json.dumps(dict(body), ensure_ascii=False)
request_or_response_dict = meta_data[request_or_response]
elif isinstance(body, (dict, list)):
body = json.dumps(body, indent=2, ensure_ascii=False)
for key, value in request_or_response_dict.items():
elif isinstance(body, bytes):
resp_content_type = headers.get("Content-Type", "")
try:
if "image" in resp_content_type:
meta_data["response"]["data_type"] = "image"
body = "data:{};base64,{}".format(
resp_content_type,
b64encode(body).decode('utf-8')
)
else:
body = escape(body.decode("utf-8"))
except UnicodeDecodeError:
pass
if isinstance(value, list):
value = json.dumps(value, indent=2, ensure_ascii=False)
elif not isinstance(body, (basestring, numeric_types, Iterable)):
# class instance, e.g. MultipartEncoder()
body = repr(body)
elif isinstance(value, bytes):
try:
encoding = meta_data["response"].get("encoding")
if not encoding or encoding == "None":
encoding = "utf-8"
meta_data['{}_body'.format(request_or_response)] = body
content_type = meta_data["response"]["content_type"]
if "image" in content_type:
meta_data["response"]["content_type"] = "image"
value = "data:{};base64,{}".format(
content_type,
b64encode(value).decode(encoding)
)
else:
value = escape(value.decode(encoding))
except UnicodeDecodeError:
pass
elif not isinstance(value, (basestring, numeric_types, Iterable)):
# class instance, e.g. MultipartEncoder()
value = repr(value)
meta_data[request_or_response][key] = value
class HtmlTestResult(unittest.TextTestResult):
"""A html result class that can generate formatted html results.

View File

@@ -281,7 +281,7 @@
<strong>{{ header_key }}</strong>: {{ header_value }}
</div>
{% endfor %}
{% elif key == "data_type" %}
{% elif key == "content_type" %}
{% if value == "image" %}
<img src="{{ record.meta_data.response.body }}" />
{% else %}