mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-04 23:16:57 +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", {})
|
upload_json = test_dict["request"].pop("upload", {})
|
||||||
if not upload_json:
|
if not upload_json:
|
||||||
@@ -95,24 +89,15 @@ def prepare_upload_test(test_dict):
|
|||||||
|
|
||||||
params_list = []
|
params_list = []
|
||||||
for key, value in upload_json.items():
|
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
|
test_dict["variables"][key] = value
|
||||||
params_list.append("{}=${}".format(key, key))
|
params_list.append("{}=${}".format(key, key))
|
||||||
|
|
||||||
params_str = ", ".join(params_list)
|
params_str = ", ".join(params_list)
|
||||||
test_dict["variables"]["m_encoder"] = "${multipart_encoder(" + params_str + ")}"
|
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"]["headers"]["Content-Type"] = "${multipart_content_type($m_encoder)}"
|
||||||
|
|
||||||
test_dict["request"]["data"] = "$m_encoder"
|
test_dict["request"]["data"] = "$m_encoder"
|
||||||
|
|
||||||
|
|
||||||
@@ -130,12 +115,21 @@ def multipart_encoder(**kwargs):
|
|||||||
fields_dict = {}
|
fields_dict = {}
|
||||||
for key, value in kwargs.items():
|
for key, value in kwargs.items():
|
||||||
|
|
||||||
if os.path.isfile(value):
|
if os.path.isabs(value):
|
||||||
# value is file path to upload,
|
# value is absolute file path
|
||||||
# it has been ensured to be absolute in prepare_upload_test
|
_file_path = value
|
||||||
filename = os.path.basename(value)
|
is_exists_file = os.path.isfile(value)
|
||||||
with open(value, 'rb') as f:
|
else:
|
||||||
mime_type = get_filetype(value)
|
# 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)
|
fields_dict[key] = (filename, f.read(), mime_type)
|
||||||
else:
|
else:
|
||||||
fields_dict[key] = value
|
fields_dict[key] = value
|
||||||
|
|||||||
@@ -16,5 +16,15 @@ teststeps:
|
|||||||
data: $m_encoder
|
data: $m_encoder
|
||||||
validate:
|
validate:
|
||||||
- eq: ["status_code", 200]
|
- eq: ["status_code", 200]
|
||||||
- startswith: ["content.form.file", "data/test.env"]
|
- startswith: ["content.files.file", "UserName=test"]
|
||||||
|
|
||||||
|
-
|
||||||
|
name: upload file with keyword
|
||||||
|
request:
|
||||||
|
url: /post
|
||||||
|
method: POST
|
||||||
|
upload:
|
||||||
|
file: "data/test.env"
|
||||||
|
validate:
|
||||||
|
- eq: ["status_code", 200]
|
||||||
|
- startswith: ["content.files.file", "UserName=test"]
|
||||||
|
|||||||
@@ -15,4 +15,15 @@
|
|||||||
data: $m_encoder
|
data: $m_encoder
|
||||||
validate:
|
validate:
|
||||||
- eq: ["status_code", 200]
|
- eq: ["status_code", 200]
|
||||||
- startswith: ["content.form.file", "data/test.env"]
|
- startswith: ["content.files.file", "UserName=test"]
|
||||||
|
|
||||||
|
- test:
|
||||||
|
name: upload file with keyword
|
||||||
|
request:
|
||||||
|
url: /post
|
||||||
|
method: POST
|
||||||
|
upload:
|
||||||
|
file: "data/test.env"
|
||||||
|
validate:
|
||||||
|
- eq: ["status_code", 200]
|
||||||
|
- startswith: ["content.files.file", "UserName=test"]
|
||||||
|
|||||||
+1
-1
@@ -230,7 +230,7 @@ class TestHttpRunner(ApiServerUnittest):
|
|||||||
summary = self.runner.run(upload_case)
|
summary = self.runner.run(upload_case)
|
||||||
self.assertTrue(summary["success"])
|
self.assertTrue(summary["success"])
|
||||||
self.assertEqual(summary["stat"]["testcases"]["total"], 1)
|
self.assertEqual(summary["stat"]["testcases"]["total"], 1)
|
||||||
self.assertEqual(summary["stat"]["teststeps"]["total"], 1)
|
self.assertEqual(summary["stat"]["teststeps"]["total"], 2)
|
||||||
self.assertIn("details", summary)
|
self.assertIn("details", summary)
|
||||||
self.assertIn("records", summary["details"][0])
|
self.assertIn("records", summary["details"][0])
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user