fix: failed to resolve variable references in upload

This commit is contained in:
xucong053
2022-06-27 18:45:31 +08:00
parent 2caf5d5159
commit 63e0bb2d85
2 changed files with 10 additions and 11 deletions

View File

@@ -46,7 +46,7 @@ import os
import sys
from typing import Text
from httprunner.models import FunctionsMapping, TStep
from httprunner.models import VariablesMapping, FunctionsMapping, TStep
from httprunner.parser import parse_data, parse_variables_mapping
from loguru import logger
@@ -75,7 +75,7 @@ def ensure_upload_ready():
sys.exit(1)
def prepare_upload_step(step: TStep, functions: FunctionsMapping):
def prepare_upload_step(step: TStep, step_variables: VariablesMapping, functions: FunctionsMapping):
"""preprocess for upload test
replace `upload` info with MultipartEncoder
@@ -102,19 +102,16 @@ def prepare_upload_step(step: TStep, functions: FunctionsMapping):
return
# parse upload info
step.request.upload = parse_data(step.request.upload, step.variables, functions)
step.request.upload = parse_data(step.request.upload, step_variables, functions)
ensure_upload_ready()
params_list = []
for key, value in step.request.upload.items():
step.variables[key] = value
step_variables[key] = value
params_list.append(f"{key}=${key}")
params_str = ", ".join(params_list)
step.variables["m_encoder"] = "${multipart_encoder(" + params_str + ")}"
# parse variables
step.variables = parse_variables_mapping(step.variables, functions)
step_variables["m_encoder"] = "${multipart_encoder(" + params_str + ")}"
step.request.headers["Content-Type"] = "${multipart_content_type($m_encoder)}"

View File

@@ -17,7 +17,7 @@ from httprunner.models import (
TStep,
VariablesMapping,
)
from httprunner.parser import build_url
from httprunner.parser import build_url, parse_variables_mapping
from httprunner.response import ResponseObject
from httprunner.runner import ALLURE, HttpRunner
@@ -88,9 +88,11 @@ def run_step_request(runner: HttpRunner, step: TStep) -> StepResult:
# parse
functions = runner.parser.functions_mapping
prepare_upload_step(step, functions)
# step_variables should be defined after prepare_upload_step
step_variables = runner.merge_step_variables(step.variables)
prepare_upload_step(step, step_variables, functions)
# parse variables
step_variables = parse_variables_mapping(step_variables, functions)
request_dict = step.request.dict()
request_dict.pop("upload", None)
parsed_request_dict = runner.parser.parse_data(request_dict, step_variables)