From 76402a21271586a201518fff60c3c16ec878e6f9 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 12 Dec 2019 21:52:15 +0800 Subject: [PATCH] change: upload file --- httprunner/plugins/uploader/__init__.py | 42 +++++++++++-------------- tests/httpbin/upload.v2.yml | 12 ++++++- tests/httpbin/upload.yml | 13 +++++++- tests/test_api.py | 2 +- 4 files changed, 42 insertions(+), 27 deletions(-) diff --git a/httprunner/plugins/uploader/__init__.py b/httprunner/plugins/uploader/__init__.py index 3cfadcda..b432a1a6 100644 --- a/httprunner/plugins/uploader/__init__.py +++ b/httprunner/plugins/uploader/__init__.py @@ -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 diff --git a/tests/httpbin/upload.v2.yml b/tests/httpbin/upload.v2.yml index c489a125..1f96d037 100644 --- a/tests/httpbin/upload.v2.yml +++ b/tests/httpbin/upload.v2.yml @@ -16,5 +16,15 @@ teststeps: data: $m_encoder validate: - 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"] diff --git a/tests/httpbin/upload.yml b/tests/httpbin/upload.yml index 08cd43e0..a858cb05 100644 --- a/tests/httpbin/upload.yml +++ b/tests/httpbin/upload.yml @@ -15,4 +15,15 @@ data: $m_encoder validate: - 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"] diff --git a/tests/test_api.py b/tests/test_api.py index e67f8599..9b77d8bd 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -230,7 +230,7 @@ class TestHttpRunner(ApiServerUnittest): summary = self.runner.run(upload_case) self.assertTrue(summary["success"]) 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("records", summary["details"][0])