mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-31 13:29:35 +08:00
change: upload file
This commit is contained in:
@@ -82,12 +82,6 @@ def prepare_upload_test(test_dict):
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Returns:
|
||||
(dict, dict):
|
||||
- variables: prepared variables for upload test
|
||||
- request: prepared request for upload test
|
||||
|
||||
"""
|
||||
upload_json = test_dict["request"].pop("upload", {})
|
||||
if not upload_json:
|
||||
@@ -95,24 +89,15 @@ def prepare_upload_test(test_dict):
|
||||
|
||||
params_list = []
|
||||
for key, value in upload_json.items():
|
||||
|
||||
if os.path.isabs(value) and os.path.isfile(value):
|
||||
# value is absolute file path, do nothing
|
||||
pass
|
||||
else:
|
||||
# value is not absolute file path, check if it is relative file path
|
||||
from httprunner.loader import get_pwd
|
||||
_file_path = os.path.join(get_pwd(), value)
|
||||
if os.path.isfile(_file_path):
|
||||
# value is relative file path, convert it to be absolute path
|
||||
value = _file_path
|
||||
|
||||
test_dict["variables"][key] = value
|
||||
params_list.append("{}=${}".format(key, key))
|
||||
|
||||
params_str = ", ".join(params_list)
|
||||
test_dict["variables"]["m_encoder"] = "${multipart_encoder(" + params_str + ")}"
|
||||
|
||||
test_dict["request"].setdefault("headers", {})
|
||||
test_dict["request"]["headers"]["Content-Type"] = "${multipart_content_type($m_encoder)}"
|
||||
|
||||
test_dict["request"]["data"] = "$m_encoder"
|
||||
|
||||
|
||||
@@ -130,12 +115,21 @@ def multipart_encoder(**kwargs):
|
||||
fields_dict = {}
|
||||
for key, value in kwargs.items():
|
||||
|
||||
if os.path.isfile(value):
|
||||
# value is file path to upload,
|
||||
# it has been ensured to be absolute in prepare_upload_test
|
||||
filename = os.path.basename(value)
|
||||
with open(value, 'rb') as f:
|
||||
mime_type = get_filetype(value)
|
||||
if os.path.isabs(value):
|
||||
# value is absolute file path
|
||||
_file_path = value
|
||||
is_exists_file = os.path.isfile(value)
|
||||
else:
|
||||
# value is not absolute file path, check if it is relative file path
|
||||
from httprunner.loader import get_pwd
|
||||
_file_path = os.path.join(get_pwd(), value)
|
||||
is_exists_file = os.path.isfile(_file_path)
|
||||
|
||||
if is_exists_file:
|
||||
# value is file path to upload
|
||||
filename = os.path.basename(_file_path)
|
||||
with open(_file_path, 'rb') as f:
|
||||
mime_type = get_filetype(_file_path)
|
||||
fields_dict[key] = (filename, f.read(), mime_type)
|
||||
else:
|
||||
fields_dict[key] = value
|
||||
|
||||
Reference in New Issue
Block a user