Skip to content
This repository was archived by the owner on Jun 5, 2020. It is now read-only.

Let _make_validate method supports the list structures JSON #18

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions har2case/core.py
Original file line number Diff line number Diff line change
@@ -12,7 +12,6 @@
except ImportError:
JSONDecodeError = ValueError


IGNORE_REQUEST_HEADERS = [
"host",
"accept",
@@ -183,7 +182,7 @@ def _make_request_data(self, teststep_dict, entry_json):
# post_data = utils.x_www_form_urlencoded(post_data)
pass
else:
#TODO: make compatible with more mimeType
# TODO: make compatible with more mimeType
pass

teststep_dict["request"][request_data_key] = post_data
@@ -252,16 +251,18 @@ def _make_validate(self, teststep_dict, entry_json):
else:
resp_content_json = json.loads(text)

if not isinstance(resp_content_json, dict):
return

for key, value in resp_content_json.items():
if isinstance(value, (dict, list)):
continue

teststep_dict["validate"].append(
{"eq": ["content.{}".format(key), value]}
)
if isinstance(resp_content_json, dict):
for key, value in resp_content_json.items():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if isinstance(value, (dict, list)):
    continue

这一部分还是有必要的,因为假如 value 为 dict/list,已经算是复杂结构体了,保持不变的可能性较小,所以不宜直接将其作为比较项。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

的确,不过目前我这边用的还没有这个问题 😅

teststep_dict["validate"].append(
{"eq": ["content.{}".format(key), value]}
)
elif isinstance(resp_content_json, list):
for i in range(0, len(resp_content_json)):
resp_content_json = resp_content_json[i]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里有两个问题:
1、重复使用 resp_content_json 这个变量名容易产生混淆,建议对 list 的元素采用不同的变量名;
2、你这里假设 list 始终是 [{a:a},{b:b},...] 的形式,那假如出现 [{a:a},b,...] 的情况就会出现问题。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,想要通用的话,还是要多考虑一下这块,期待后续 validator 支持 jmespath 的语法~~

for key, value in resp_content_json.items():
teststep_dict["validate"].append(
{"eq": ["content.{}.{}".format(i, key), value]}
)

def _prepare_teststep(self, entry_json):
""" extract info from entry dict and make teststep
@@ -318,6 +319,7 @@ def _prepare_teststeps(self, testcase):
teststeps list are parsed from HAR log entries list.
"""

def is_exclude(url, exclude_str):
exclude_str_list = exclude_str.split("|")
for exclude_str in exclude_str_list: