From 09a833d61bc033b9712c00dbe9c62a2dedbff29b Mon Sep 17 00:00:00 2001 From: cyandragon88 <> Date: Sun, 31 Oct 2021 21:49:57 +0800 Subject: [PATCH] har2case add profile args --- httprunner/ext/har2case/__init__.py | 7 +++++- httprunner/ext/har2case/core.py | 34 ++++++++++++++++++----------- tests/ext/har2case/core_test.py | 13 +++++++++++ tests/ext/har2case/data/profile.yml | 4 ++++ 4 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 tests/ext/har2case/data/profile.yml diff --git a/httprunner/ext/har2case/__init__.py b/httprunner/ext/har2case/__init__.py index dcc316b5..6b2d2bec 100644 --- a/httprunner/ext/har2case/__init__.py +++ b/httprunner/ext/har2case/__init__.py @@ -45,6 +45,11 @@ def init_har2case_parser(subparsers): help="Specify exclude keyword, url that includes exclude string will be ignored, " "multiple keywords can be joined with '|'", ) + parser.add_argument( + "--profile", + dest="profile", + help="Specify yaml file to overwrite headers and cookies in HAR.", + ) return parser @@ -60,6 +65,6 @@ def main_har2case(args): output_file_type = "pytest" capture_message(f"har2case {output_file_type}") - HarParser(har_source_file, args.filter, args.exclude).gen_testcase(output_file_type) + HarParser(har_source_file, args.filter, args.exclude, args.profile).gen_testcase(output_file_type) return 0 diff --git a/httprunner/ext/har2case/core.py b/httprunner/ext/har2case/core.py index 31796862..efff6a4c 100644 --- a/httprunner/ext/har2case/core.py +++ b/httprunner/ext/har2case/core.py @@ -11,6 +11,7 @@ from sentry_sdk import capture_exception from httprunner.ext.har2case import utils from httprunner.make import make_testcase, format_pytest_with_black +from httprunner.loader import load_test_file try: from json.decoder import JSONDecodeError @@ -36,10 +37,11 @@ def ensure_file_path(path: Text) -> Text: class HarParser(object): - def __init__(self, har_file_path, filter_str=None, exclude_str=None): + def __init__(self, har_file_path, filter_str=None, exclude_str=None, profile=None): self.har_file_path = ensure_file_path(har_file_path) self.filter_str = filter_str self.exclude_str = exclude_str or "" + self.profile = profile and load_test_file(profile) def __make_request_url(self, teststep_dict, entry_json): """ parse HAR entry request url and queryString, and make teststep url and params @@ -97,12 +99,15 @@ class HarParser(object): teststep_dict["request"]["method"] = method def __make_request_cookies(self, teststep_dict, entry_json): - cookies = {} - for cookie in entry_json["request"].get("cookies", []): - cookies[cookie["name"]] = cookie["value"] + if self.profile and self.profile.get("cookies"): + teststep_dict["request"]["cookies"] = self.profile.get("cookies") + else: + cookies = {} + for cookie in entry_json["request"].get("cookies", []): + cookies[cookie["name"]] = cookie["value"] - if cookies: - teststep_dict["request"]["cookies"] = cookies + if cookies: + teststep_dict["request"]["cookies"] = cookies def __make_request_headers(self, teststep_dict, entry_json): """ parse HAR entry request headers, and make teststep headers. @@ -128,15 +133,18 @@ class HarParser(object): } """ - teststep_headers = {} - for header in entry_json["request"].get("headers", []): - if header["name"] == "cookie" or header["name"].startswith(":"): - continue + if self.profile and self.profile.get("headers"): + teststep_dict["request"]["headers"] = self.profile.get("headers") + else: + teststep_headers = {} + for header in entry_json["request"].get("headers", []): + if header["name"] == "cookie" or header["name"].startswith(":"): + continue - teststep_headers[header["name"]] = header["value"] + teststep_headers[header["name"]] = header["value"] - if teststep_headers: - teststep_dict["request"]["headers"] = teststep_headers + if teststep_headers: + teststep_dict["request"]["headers"] = teststep_headers def _make_request_data(self, teststep_dict, entry_json): """ parse HAR entry request data, and make teststep request data diff --git a/tests/ext/har2case/core_test.py b/tests/ext/har2case/core_test.py index f696e5c0..8d79e1db 100644 --- a/tests/ext/har2case/core_test.py +++ b/tests/ext/har2case/core_test.py @@ -9,6 +9,7 @@ class TestHar(TestHar2CaseUtils): def setUp(self): self.har_path = os.path.join(os.path.dirname(__file__), "data", "demo.har") self.har_parser = HarParser(self.har_path) + self.profile_path = os.path.join(os.path.dirname(__file__), "data", "profile.yml") def test_prepare_teststep(self): log_entries = load_har_log_entries(self.har_path) @@ -47,6 +48,18 @@ class TestHar(TestHar2CaseUtils): self.assertTrue(os.path.isfile(json_file)) os.remove(json_file) + def test_profile(self): + har_parser = HarParser(self.har_path, profile=self.profile_path) + teststeps = har_parser._prepare_teststeps() + self.assertEqual( + teststeps[0]["request"]["headers"], + {"Content-Type": "application/x-www-form-urlencoded"}, + ) + self.assertEqual( + teststeps[0]["request"]["cookies"], + {"CASTGC": "TGT"}, + ) + def test_filter(self): filter_str = "httprunner" har_parser = HarParser(self.har_path, filter_str) diff --git a/tests/ext/har2case/data/profile.yml b/tests/ext/har2case/data/profile.yml new file mode 100644 index 00000000..ef8695e3 --- /dev/null +++ b/tests/ext/har2case/data/profile.yml @@ -0,0 +1,4 @@ +headers: + Content-Type: "application/x-www-form-urlencoded" +cookies: + CASTGC: "TGT" \ No newline at end of file