From 0e39517d278682ec70e344aee0650d4ea845186e Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 17 Dec 2019 22:35:22 +0800 Subject: [PATCH] feat: add keyword 'body' to reference response body change: unify response field(content/json/text) to 'body' in html report --- docs/CHANGELOG.md | 5 +++++ httprunner/client.py | 8 ++++---- httprunner/report.py | 2 +- httprunner/response.py | 2 +- httprunner/static/report_template.html | 8 ++++---- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 2763cbb4..3a7debdb 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -2,10 +2,15 @@ ## 2.4.4 (2019-12-17) +**Added** + +- feat: add keyword `body` to reference response body + **Changed** - refactor: dumps request/response headers, display indented json in html report - refactor: dumps request/response body if it is in json format, display indented json in html report +- change: unify response field(content/json/text) to `body` in html report ## 2.4.3 (2019-12-16) diff --git a/httprunner/client.py b/httprunner/client.py index 0a2af89e..1b5676e1 100644 --- a/httprunner/client.py +++ b/httprunner/client.py @@ -111,18 +111,18 @@ class HttpSession(requests.Session): if "image" in content_type: # response is image type, record bytes content only - req_resp_dict["response"]["content"] = resp_obj.content + req_resp_dict["response"]["body"] = resp_obj.content else: try: # try to record json data if isinstance(resp_obj, response.ResponseObject): - req_resp_dict["response"]["json"] = resp_obj.json + req_resp_dict["response"]["body"] = resp_obj.json else: - req_resp_dict["response"]["json"] = resp_obj.json() + req_resp_dict["response"]["body"] = resp_obj.json() except ValueError: # only record at most 512 text charactors resp_text = resp_obj.text - req_resp_dict["response"]["text"] = omit_long_data(resp_text) + req_resp_dict["response"]["body"] = omit_long_data(resp_text) # log response details in debug mode log_print(req_resp_dict, "response") diff --git a/httprunner/report.py b/httprunner/report.py index d0c603d1..37decadf 100644 --- a/httprunner/report.py +++ b/httprunner/report.py @@ -217,7 +217,7 @@ def __stringify_response(response_data): if not encoding or encoding == "None": encoding = "utf-8" - if key == "content" and "image" in response_data["content_type"]: + if key == "body" and "image" in response_data["content_type"]: # display image value = "data:{};base64,{}".format( response_data["content_type"], diff --git a/httprunner/response.py b/httprunner/response.py index b6062e63..9de3a8cd 100644 --- a/httprunner/response.py +++ b/httprunner/response.py @@ -175,7 +175,7 @@ class ResponseObject(object): raise exceptions.ExtractFailure(err_msg) # response body - elif top_query in ["content", "text", "json"]: + elif top_query in ["body", "content", "text", "json"]: try: body = self.json except exceptions.JSONDecodeError: diff --git a/httprunner/static/report_template.html b/httprunner/static/report_template.html index f943172a..cef1ed19 100644 --- a/httprunner/static/report_template.html +++ b/httprunner/static/report_template.html @@ -250,14 +250,14 @@ {{key}} - {% if key == "content" %} + {% if key == "headers" %} +
{{ value | e }}
+ {% elif key == "body" %} {% if "image" in req_resp.response.content_type %} {% else %} - {{ value }} - {% endif %} - {% elif key in ["headers", "text", "json"] %}
{{ value | e }}
+ {% endif %} {% else %} {{ value }} {% endif %}