mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-07 08:49:37 +08:00
request in config should also be parsed
This commit is contained in:
@@ -32,7 +32,6 @@ class Context(object):
|
|||||||
# testcase config shall inherit from testset configs,
|
# testcase config shall inherit from testset configs,
|
||||||
# but can not change testset configs, that's why we use copy.deepcopy here.
|
# but can not change testset configs, that's why we use copy.deepcopy here.
|
||||||
self.testcase_functions_config = copy.deepcopy(self.testset_functions_config)
|
self.testcase_functions_config = copy.deepcopy(self.testset_functions_config)
|
||||||
self.testcase_request_config = {}
|
|
||||||
self.testcase_variables_mapping = copy.deepcopy(self.testset_shared_variables_mapping)
|
self.testcase_variables_mapping = copy.deepcopy(self.testset_shared_variables_mapping)
|
||||||
|
|
||||||
self.testcase_parser.bind_functions(self.testcase_functions_config)
|
self.testcase_parser.bind_functions(self.testcase_functions_config)
|
||||||
@@ -131,7 +130,11 @@ class Context(object):
|
|||||||
self.testcase_functions_config.update(config_mapping)
|
self.testcase_functions_config.update(config_mapping)
|
||||||
self.testcase_parser.bind_functions(self.testcase_functions_config)
|
self.testcase_parser.bind_functions(self.testcase_functions_config)
|
||||||
|
|
||||||
def register_request(self, request_dict, level="testcase"):
|
def get_parsed_request(self, request_dict, level="testcase"):
|
||||||
|
""" get parsed request with bind variables and functions.
|
||||||
|
@param request_dict: request config mapping
|
||||||
|
@param level: testset or testcase
|
||||||
|
"""
|
||||||
if "headers" in request_dict:
|
if "headers" in request_dict:
|
||||||
# convert keys in request headers to lowercase
|
# convert keys in request headers to lowercase
|
||||||
headers = request_dict.pop("headers")
|
headers = request_dict.pop("headers")
|
||||||
@@ -139,27 +142,18 @@ class Context(object):
|
|||||||
raise ParamsError("HTTP Request Headers invalid!")
|
raise ParamsError("HTTP Request Headers invalid!")
|
||||||
request_dict["headers"] = {key.lower(): headers[key] for key in headers}
|
request_dict["headers"] = {key.lower(): headers[key] for key in headers}
|
||||||
|
|
||||||
self.__update_context_request_config(level, request_dict)
|
|
||||||
|
|
||||||
def __update_context_request_config(self, level, config_mapping):
|
|
||||||
"""
|
|
||||||
@param level: testset or testcase
|
|
||||||
@param config_type: request
|
|
||||||
@param config_mapping: request config mapping
|
|
||||||
"""
|
|
||||||
if level == "testset":
|
if level == "testset":
|
||||||
self.testset_request_config.update(config_mapping)
|
request_dict = self.testcase_parser.parse_content_with_bindings(
|
||||||
|
request_dict
|
||||||
|
)
|
||||||
|
self.testset_request_config.update(request_dict)
|
||||||
|
|
||||||
self.testcase_request_config = utils.deep_update_dict(
|
testcase_request_config = utils.deep_update_dict(
|
||||||
copy.deepcopy(self.testset_request_config),
|
copy.deepcopy(self.testset_request_config),
|
||||||
config_mapping
|
request_dict
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_parsed_request(self):
|
|
||||||
""" get parsed request, with bind variables and functions.
|
|
||||||
"""
|
|
||||||
parsed_request = self.testcase_parser.parse_content_with_bindings(
|
parsed_request = self.testcase_parser.parse_content_with_bindings(
|
||||||
self.testcase_request_config
|
testcase_request_config
|
||||||
)
|
)
|
||||||
|
|
||||||
return parsed_request
|
return parsed_request
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ class Runner(object):
|
|||||||
"headers": {
|
"headers": {
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
"json": {
|
"json": {
|
||||||
"sign": "f1219719911caae89ccc301679857ebfda115ca2"
|
"sign": "f1219719911caae89ccc301679857ebfda115ca2"
|
||||||
}
|
}
|
||||||
@@ -52,10 +52,12 @@ class Runner(object):
|
|||||||
self.context.config_context(config_dict, level)
|
self.context.config_context(config_dict, level)
|
||||||
|
|
||||||
request_config = config_dict.get('request', {})
|
request_config = config_dict.get('request', {})
|
||||||
base_url = request_config.pop("base_url", None)
|
parsed_request = self.context.get_parsed_request(request_config, level)
|
||||||
|
|
||||||
|
base_url = parsed_request.pop("base_url", None)
|
||||||
self.http_client_session = self.http_client_session or HttpSession(base_url)
|
self.http_client_session = self.http_client_session or HttpSession(base_url)
|
||||||
|
|
||||||
self.context.register_request(request_config, level)
|
return parsed_request
|
||||||
|
|
||||||
def run_test(self, testcase):
|
def run_test(self, testcase):
|
||||||
""" run single testcase.
|
""" run single testcase.
|
||||||
@@ -83,8 +85,7 @@ class Runner(object):
|
|||||||
}
|
}
|
||||||
@return True or raise exception during test
|
@return True or raise exception during test
|
||||||
"""
|
"""
|
||||||
self.init_config(testcase, level="testcase")
|
parsed_request = self.init_config(testcase, level="testcase")
|
||||||
parsed_request = self.context.get_parsed_request()
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
url = parsed_request.pop('url')
|
url = parsed_request.pop('url')
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ except NameError:
|
|||||||
import urllib.parse as urllib
|
import urllib.parse as urllib
|
||||||
|
|
||||||
SECRET_KEY = "DebugTalk"
|
SECRET_KEY = "DebugTalk"
|
||||||
|
BASE_URL = "http://127.0.0.1:5000"
|
||||||
|
|
||||||
def get_sign(*args):
|
def get_sign(*args):
|
||||||
content = ''.join(args).encode('ascii')
|
content = ''.join(args).encode('ascii')
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
- os_platform: 'ios'
|
- os_platform: 'ios'
|
||||||
- app_version: '2.8.6'
|
- app_version: '2.8.6'
|
||||||
request:
|
request:
|
||||||
base_url: http://127.0.0.1:5000
|
base_url: $BASE_URL
|
||||||
headers:
|
headers:
|
||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
device_sn: $device_sn
|
device_sn: $device_sn
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
- os_platform: 'ios'
|
- os_platform: 'ios'
|
||||||
- app_version: '2.8.6'
|
- app_version: '2.8.6'
|
||||||
request:
|
request:
|
||||||
base_url: http://127.0.0.1:5000
|
base_url: $BASE_URL
|
||||||
headers:
|
headers:
|
||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
device_sn: $device_sn
|
device_sn: $device_sn
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
variable_binds:
|
variable_binds:
|
||||||
- device_sn: 'HZfFBh6tU59EdXJ'
|
- device_sn: 'HZfFBh6tU59EdXJ'
|
||||||
request:
|
request:
|
||||||
base_url: http://127.0.0.1:5000
|
base_url: $BASE_URL
|
||||||
headers:
|
headers:
|
||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
device_sn: $device_sn
|
device_sn: $device_sn
|
||||||
|
|||||||
@@ -178,7 +178,7 @@ class VariableBindsUnittest(unittest.TestCase):
|
|||||||
SECRET_KEY = context_variables["SECRET_KEY"]
|
SECRET_KEY = context_variables["SECRET_KEY"]
|
||||||
self.assertEqual(SECRET_KEY, "DebugTalk")
|
self.assertEqual(SECRET_KEY, "DebugTalk")
|
||||||
|
|
||||||
def test_register_request(self):
|
def test_parse_request(self):
|
||||||
request_dict = {
|
request_dict = {
|
||||||
"url": "http://debugtalk.com",
|
"url": "http://debugtalk.com",
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
@@ -187,9 +187,8 @@ class VariableBindsUnittest(unittest.TestCase):
|
|||||||
"USER-AGENT": "ios/10.3"
|
"USER-AGENT": "ios/10.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.context.register_request(request_dict)
|
|
||||||
|
|
||||||
parsed_request = self.context.get_parsed_request()
|
parsed_request = self.context.get_parsed_request(request_dict)
|
||||||
self.assertIn("content-type", parsed_request["headers"])
|
self.assertIn("content-type", parsed_request["headers"])
|
||||||
self.assertIn("user-agent", parsed_request["headers"])
|
self.assertIn("user-agent", parsed_request["headers"])
|
||||||
|
|
||||||
@@ -197,7 +196,7 @@ class VariableBindsUnittest(unittest.TestCase):
|
|||||||
"headers": "invalid headers"
|
"headers": "invalid headers"
|
||||||
}
|
}
|
||||||
with self.assertRaises(ParamsError):
|
with self.assertRaises(ParamsError):
|
||||||
self.context.register_request(request_dict)
|
self.context.get_parsed_request(request_dict)
|
||||||
|
|
||||||
def test_get_parsed_request(self):
|
def test_get_parsed_request(self):
|
||||||
test_runner = runner.Runner()
|
test_runner = runner.Runner()
|
||||||
@@ -221,8 +220,7 @@ class VariableBindsUnittest(unittest.TestCase):
|
|||||||
"data": "$data"
|
"data": "$data"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
test_runner.init_config(testcase, level="testcase")
|
parsed_request = test_runner.init_config(testcase, level="testcase")
|
||||||
parsed_request = test_runner.context.get_parsed_request()
|
|
||||||
self.assertIn("authorization", parsed_request["headers"])
|
self.assertIn("authorization", parsed_request["headers"])
|
||||||
self.assertEqual(len(parsed_request["headers"]["authorization"]), 32)
|
self.assertEqual(len(parsed_request["headers"]["authorization"]), 32)
|
||||||
self.assertIn("random", parsed_request["headers"])
|
self.assertIn("random", parsed_request["headers"])
|
||||||
|
|||||||
Reference in New Issue
Block a user