From 98af31d9a3ec9c007d544e5ebbf59ff4e6e06ffb Mon Sep 17 00:00:00 2001 From: xucong053 Date: Mon, 27 Jun 2022 18:45:31 +0800 Subject: [PATCH] fix: failed to resolve variable references in upload --- httprunner/ext/uploader/__init__.py | 13 +++++-------- httprunner/step_request.py | 8 +++++--- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/httprunner/ext/uploader/__init__.py b/httprunner/ext/uploader/__init__.py index c284f7b3..6bbd86cf 100644 --- a/httprunner/ext/uploader/__init__.py +++ b/httprunner/ext/uploader/__init__.py @@ -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)}" diff --git a/httprunner/step_request.py b/httprunner/step_request.py index c4e63413..2db1def7 100644 --- a/httprunner/step_request.py +++ b/httprunner/step_request.py @@ -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)