mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-11 10:00:23 +08:00
Merge pull request #1122 from cyandragon88/master
feat: add --profile flag for har2case to support overwrite headers/cookies with specified yaml/json configuration file
This commit is contained in:
@@ -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"
|
||||
|
||||
ga_client.track_event("ConvertTests", 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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
4
tests/ext/har2case/data/profile.yml
Normal file
4
tests/ext/har2case/data/profile.yml
Normal file
@@ -0,0 +1,4 @@
|
||||
headers:
|
||||
Content-Type: "application/x-www-form-urlencoded"
|
||||
cookies:
|
||||
CASTGC: "TGT"
|
||||
Reference in New Issue
Block a user