diff --git a/httprunner/report.py b/httprunner/report.py index 7fe978ff..9dedd1a5 100644 --- a/httprunner/report.py +++ b/httprunner/report.py @@ -5,6 +5,7 @@ import os import platform import time import unittest +from base64 import b64encode from collections import Iterable, OrderedDict from datetime import datetime @@ -113,10 +114,18 @@ def stringify_body(meta_data, request_or_response): body = json.dumps(dict(body), ensure_ascii=False) elif isinstance(body, (dict, list)): - body = json.dumps(body, ensure_ascii=False) + body = json.dumps(body, indent=2, ensure_ascii=False) elif isinstance(body, bytes): - body = body.decode("utf-8") + resp_content_type = headers.get("Content-Type", "") + 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 = body.decode("utf-8") elif not isinstance(body, (basestring, numeric_types, Iterable)): # class instance, e.g. MultipartEncoder() diff --git a/httprunner/templates/default_report_template.html b/httprunner/templates/default_report_template.html index 293de353..c2c16852 100644 --- a/httprunner/templates/default_report_template.html +++ b/httprunner/templates/default_report_template.html @@ -245,7 +245,11 @@ body -
{{ record.meta_data.response_body | safe }}
+ {% if record.meta_data.response_data_type == "image" %} + + {% else %} +
{{ record.meta_data.response_body | safe }}
+ {% endif %} diff --git a/tests/httpbin/load_image.yml b/tests/httpbin/load_image.yml new file mode 100644 index 00000000..6a131fdd --- /dev/null +++ b/tests/httpbin/load_image.yml @@ -0,0 +1,37 @@ +- config: + name: load images + request: + base_url: https://httpbin.org + +- test: + name: get png image + request: + url: /image/png + method: GET + validators: + - eq: ["status_code", 200] + +- test: + name: get jpeg image + request: + url: /image/jpeg + method: GET + validators: + - eq: ["status_code", 200] + +- test: + name: get webp image + request: + url: /image/webp + method: GET + validators: + - eq: ["status_code", 200] + +- test: + name: get svg image + request: + url: /image/svg + method: GET + validators: + - eq: ["status_code", 200] + diff --git a/tests/test_httprunner.py b/tests/test_httprunner.py index e06a243a..992bd775 100644 --- a/tests/test_httprunner.py +++ b/tests/test_httprunner.py @@ -135,3 +135,13 @@ class TestHttpRunner(ApiServerUnittest): self.assertTrue(summary["success"]) self.assertEqual(summary["stat"]["testsRun"], 1) self.assertEqual(summary["records"][0]["meta_data"]["response_body"]["data"], "abc") + + def test_html_report_repsonse_image(self): + testset_path = "tests/httpbin/load_image.yml" + runner = HttpRunner().run(testset_path) + summary = runner.summary + output_folder_name = os.path.basename(os.path.splitext(testset_path)[0]) + report = runner.gen_html_report(html_report_name=output_folder_name) + self.assertTrue(os.path.isfile(report)) + report_save_dir = os.path.join(os.getcwd(), 'reports', output_folder_name) + shutil.rmtree(report_save_dir)