Skip to content

Commit

Permalink
优化请求数据处理代码 (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-clan authored Sep 26, 2024
1 parent 2dd7c63 commit 6b715e3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
15 changes: 8 additions & 7 deletions httpfpt/common/send_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,16 @@ def send_request(
'body': parsed_data['body'],
'files': parsed_data['files'],
}
if parsed_data['body_type'] == BodyType.JSON or parsed_data['body_type'] == BodyType.GraphQL:
request_data_parsed.update({'json': request_data_parsed.pop('body')})
elif parsed_data['body_type'] == BodyType.binary:
if request_engin == EnginType.httpx:
request_data_parsed.update({'content': request_data_parsed.pop('body')})
else:
request_data_parsed.update({'data': request_data_parsed.pop('body')})
try:
request_data_parsed: dict = var_extractor.vars_replace(request_data_parsed, parsed_data['env']) # type: ignore # noqa: ignore
body = request_data_parsed.pop('body')
if parsed_data['body_type'] == BodyType.JSON or parsed_data['body_type'] == BodyType.GraphQL:
request_data_parsed.update({'json': body})
elif parsed_data['body_type'] == BodyType.binary:
if request_engin == EnginType.httpx:
request_data_parsed.update({'content': body})
else:
request_data_parsed.update({'data': body})
parsed_data.update(
body=request_data_parsed.get('json')
or request_data_parsed.get('data')
Expand Down
28 changes: 13 additions & 15 deletions httpfpt/utils/request/vars_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,23 @@ def vars_replace(self, target: str | dict, env: str) -> str | dict:
except OSError:
raise RequestDataParseError('运行环境获取失败, 请检查测试用例环境配置')

# 执行变量替换
def var_replace(_var_key: str, _str_target: str) -> str:
cache_value = variable_cache.get(_var_key)
if cache_value is None:
global_vars = read_yaml(httpfpt_path.data_dir, filename='global_vars.yaml')
var_value = env_vars.get(_var_key.upper(), global_vars.get(_var_key))
if var_value is None:
raise VariableError(_var_key)
log.info(f'变量 {_var_key}={var_value} 替换完成')
return self.vars_re.sub(str(var_value), _str_target, 1)
else:
log.info(f'变量 {_var_key}={cache_value} 替换完成')
return self.vars_re.sub(str(cache_value), _str_target, 1)

for match in self.vars_re.finditer(str_target):
var_key = match.group(1) or match.group(2)
if var_key is not None:
try:
str_target = var_replace(var_key, str_target)
cache_value = variable_cache.get(var_key)
if cache_value is None:
global_vars = read_yaml(httpfpt_path.data_dir, filename='global_vars.yaml')
var_value = env_vars.get(
var_key.upper(), global_vars.get(var_key) if global_vars is not None else None
)
if var_value is None:
raise VariableError(var_key)
log.info(f'变量 {var_key}={var_value} 替换完成')
str_target = self.vars_re.sub(str(var_value), str_target, 1)
else:
log.info(f'变量 {var_key}={cache_value} 替换完成')
str_target = self.vars_re.sub(str(cache_value), str_target, 1)
except Exception as e:
raise VariableError(f'变量 {var_key} 替换失败: {e}')

Expand Down

0 comments on commit 6b715e3

Please sign in to comment.