mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-09 06:23:52 +08:00
Merge pull request #557 from HttpRunner/upload_files
2.1.1 Upload files
This commit is contained in:
@@ -1,5 +1,14 @@
|
|||||||
# Release History
|
# Release History
|
||||||
|
|
||||||
|
## 2.1.1 (2019-04-11)
|
||||||
|
|
||||||
|
**Features**
|
||||||
|
|
||||||
|
refactor upload files mechanism with [requests-toolbelt](https://toolbelt.readthedocs.io/en/latest/user.html#multipart-form-data-encoder):
|
||||||
|
|
||||||
|
- simplify usage syntax, detect mimetype with [filetype](https://github.com/h2non/filetype.py).
|
||||||
|
- support upload multiple fields.
|
||||||
|
|
||||||
## 2.1.0 (2019-04-10)
|
## 2.1.0 (2019-04-10)
|
||||||
|
|
||||||
**Features**
|
**Features**
|
||||||
|
|||||||
1
Pipfile
1
Pipfile
@@ -11,6 +11,7 @@ har2case = "*"
|
|||||||
colorama = "*"
|
colorama = "*"
|
||||||
colorlog = "*"
|
colorlog = "*"
|
||||||
requests-toolbelt = "*"
|
requests-toolbelt = "*"
|
||||||
|
filetype = "*"
|
||||||
|
|
||||||
[dev-packages]
|
[dev-packages]
|
||||||
Flask = "<1.0.0"
|
Flask = "<1.0.0"
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
__title__ = 'HttpRunner'
|
__title__ = 'HttpRunner'
|
||||||
__description__ = 'One-stop solution for HTTP(S) testing.'
|
__description__ = 'One-stop solution for HTTP(S) testing.'
|
||||||
__url__ = 'https://github.com/HttpRunner/HttpRunner'
|
__url__ = 'https://github.com/HttpRunner/HttpRunner'
|
||||||
__version__ = '2.1.0'
|
__version__ = '2.1.1'
|
||||||
__author__ = 'debugtalk'
|
__author__ = 'debugtalk'
|
||||||
__author_email__ = 'mail@debugtalk.com'
|
__author_email__ = 'mail@debugtalk.com'
|
||||||
__license__ = 'Apache-2.0'
|
__license__ = 'Apache-2.0'
|
||||||
|
|||||||
@@ -12,13 +12,18 @@ import re
|
|||||||
import string
|
import string
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
import filetype
|
||||||
from httprunner.compat import basestring, builtin_str, integer_types, str
|
from httprunner.compat import basestring, builtin_str, integer_types, str
|
||||||
from httprunner.exceptions import ParamsError
|
from httprunner.exceptions import ParamsError
|
||||||
from requests_toolbelt import MultipartEncoder
|
from requests_toolbelt import MultipartEncoder
|
||||||
|
|
||||||
|
PWD = os.getcwd()
|
||||||
|
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
## built-in functions
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
""" built-in functions
|
|
||||||
"""
|
|
||||||
def gen_random_string(str_len):
|
def gen_random_string(str_len):
|
||||||
""" generate random string with specified length
|
""" generate random string with specified length
|
||||||
"""
|
"""
|
||||||
@@ -38,24 +43,68 @@ def get_current_date(fmt="%Y-%m-%d"):
|
|||||||
"""
|
"""
|
||||||
return datetime.datetime.now().strftime(fmt)
|
return datetime.datetime.now().strftime(fmt)
|
||||||
|
|
||||||
def multipart_encoder(field_name, file_path, file_type=None, file_headers=None):
|
|
||||||
if not os.path.isabs(file_path):
|
|
||||||
file_path = os.path.join(os.getcwd(), file_path)
|
|
||||||
|
|
||||||
filename = os.path.basename(file_path)
|
###############################################################################
|
||||||
with open(file_path, 'rb') as f:
|
## upload files with requests-toolbelt
|
||||||
fields = {
|
# e.g.
|
||||||
field_name: (filename, f.read(), file_type)
|
# - test:
|
||||||
}
|
# name: upload file
|
||||||
|
# variables:
|
||||||
|
# file_path: "data/test.env"
|
||||||
|
# multipart_encoder: ${multipart_encoder(file=$file_path)}
|
||||||
|
# request:
|
||||||
|
# url: /post
|
||||||
|
# method: POST
|
||||||
|
# headers:
|
||||||
|
# Content-Type: ${multipart_content_type($multipart_encoder)}
|
||||||
|
# data: $multipart_encoder
|
||||||
|
# validate:
|
||||||
|
# - eq: ["status_code", 200]
|
||||||
|
# - startswith: ["content.files.file", "UserName=test"]
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
def multipart_encoder(**kwargs):
|
||||||
|
""" initialize MultipartEncoder with uploading fields.
|
||||||
|
"""
|
||||||
|
def get_filetype(file_path):
|
||||||
|
file_type = filetype.guess(file_path)
|
||||||
|
if file_type:
|
||||||
|
return file_type.mime
|
||||||
|
else:
|
||||||
|
return "text/html"
|
||||||
|
|
||||||
|
fields_dict = {}
|
||||||
|
for key, value in kwargs.items():
|
||||||
|
|
||||||
|
if os.path.isabs(value):
|
||||||
|
_file_path = value
|
||||||
|
is_file = True
|
||||||
|
else:
|
||||||
|
global PWD
|
||||||
|
_file_path = os.path.join(PWD, value)
|
||||||
|
is_file = os.path.isfile(_file_path)
|
||||||
|
|
||||||
|
if is_file:
|
||||||
|
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
|
||||||
|
|
||||||
|
return MultipartEncoder(fields=fields_dict)
|
||||||
|
|
||||||
return MultipartEncoder(fields)
|
|
||||||
|
|
||||||
def multipart_content_type(multipart_encoder):
|
def multipart_content_type(multipart_encoder):
|
||||||
|
""" prepare Content-Type for request headers
|
||||||
|
"""
|
||||||
return multipart_encoder.content_type
|
return multipart_encoder.content_type
|
||||||
|
|
||||||
|
|
||||||
""" built-in comparators
|
###############################################################################
|
||||||
"""
|
## built-in comparators
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
def equals(check_value, expect_value):
|
def equals(check_value, expect_value):
|
||||||
assert check_value == expect_value
|
assert check_value == expect_value
|
||||||
|
|
||||||
|
|||||||
@@ -8,8 +8,7 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
from httprunner import exceptions, logger, parser, utils, validator
|
from httprunner import built_in, exceptions, logger, parser, utils, validator
|
||||||
|
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
## file loader
|
## file loader
|
||||||
@@ -263,7 +262,6 @@ def load_module_functions(module):
|
|||||||
def load_builtin_functions():
|
def load_builtin_functions():
|
||||||
""" load built_in module functions
|
""" load built_in module functions
|
||||||
"""
|
"""
|
||||||
from httprunner import built_in
|
|
||||||
return load_module_functions(built_in)
|
return load_module_functions(built_in)
|
||||||
|
|
||||||
|
|
||||||
@@ -703,6 +701,7 @@ def load_project_tests(test_path, dot_env_path=None):
|
|||||||
# locate PWD and load debugtalk.py functions
|
# locate PWD and load debugtalk.py functions
|
||||||
|
|
||||||
project_mapping["PWD"] = project_working_directory
|
project_mapping["PWD"] = project_working_directory
|
||||||
|
built_in.PWD = project_working_directory
|
||||||
project_mapping["functions"] = debugtalk_functions
|
project_mapping["functions"] = debugtalk_functions
|
||||||
|
|
||||||
# load api
|
# load api
|
||||||
|
|||||||
@@ -267,7 +267,7 @@ class Runner(object):
|
|||||||
self.session_context.update_session_variables(extracted_variables_mapping)
|
self.session_context.update_session_variables(extracted_variables_mapping)
|
||||||
|
|
||||||
# validate
|
# validate
|
||||||
validators = test_dict.get("validate", [])
|
validators = test_dict.get("validate") or test_dict.get("validators") or []
|
||||||
try:
|
try:
|
||||||
self.session_context.validate(validators, resp_obj)
|
self.session_context.validate(validators, resp_obj)
|
||||||
except (exceptions.ParamsError, exceptions.ValidationFailure, exceptions.ExtractFailure):
|
except (exceptions.ParamsError, exceptions.ValidationFailure, exceptions.ExtractFailure):
|
||||||
|
|||||||
3
setup.py
3
setup.py
@@ -21,7 +21,8 @@ install_requires = [
|
|||||||
"har2case",
|
"har2case",
|
||||||
"colorama",
|
"colorama",
|
||||||
"colorlog",
|
"colorlog",
|
||||||
"requests_toolbelt"
|
"requests_toolbelt",
|
||||||
|
"filetype"
|
||||||
]
|
]
|
||||||
|
|
||||||
class UploadCommand(Command):
|
class UploadCommand(Command):
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
request:
|
request:
|
||||||
url: /image/png
|
url: /image/png
|
||||||
method: GET
|
method: GET
|
||||||
validators:
|
validate:
|
||||||
- eq: ["status_code", 200]
|
- eq: ["status_code", 200]
|
||||||
|
|
||||||
- test:
|
- test:
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
request:
|
request:
|
||||||
url: /image/jpeg
|
url: /image/jpeg
|
||||||
method: GET
|
method: GET
|
||||||
validators:
|
validate:
|
||||||
- eq: ["status_code", 200]
|
- eq: ["status_code", 200]
|
||||||
|
|
||||||
- test:
|
- test:
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
request:
|
request:
|
||||||
url: /image/webp
|
url: /image/webp
|
||||||
method: GET
|
method: GET
|
||||||
validators:
|
validate:
|
||||||
- eq: ["status_code", 200]
|
- eq: ["status_code", 200]
|
||||||
|
|
||||||
- test:
|
- test:
|
||||||
@@ -31,6 +31,6 @@
|
|||||||
request:
|
request:
|
||||||
url: /image/svg
|
url: /image/svg
|
||||||
method: GET
|
method: GET
|
||||||
validators:
|
validate:
|
||||||
- eq: ["status_code", 200]
|
- eq: ["status_code", 200]
|
||||||
|
|
||||||
|
|||||||
@@ -5,17 +5,15 @@
|
|||||||
- test:
|
- test:
|
||||||
name: upload file
|
name: upload file
|
||||||
variables:
|
variables:
|
||||||
field_name: "file"
|
file_path: "data/test.env"
|
||||||
file_path: "LICENSE"
|
multipart_encoder: ${multipart_encoder(file=$file_path)}
|
||||||
file_type: "text/html"
|
|
||||||
multipart_encoder: ${multipart_encoder($field_name, $file_path, $file_type)}
|
|
||||||
request:
|
request:
|
||||||
url: /post
|
url: /post
|
||||||
method: POST
|
method: POST
|
||||||
headers:
|
headers:
|
||||||
Content-Type: ${multipart_content_type($multipart_encoder)}
|
Content-Type: ${multipart_content_type($multipart_encoder)}
|
||||||
data: $multipart_encoder
|
data: $multipart_encoder
|
||||||
validators:
|
validate:
|
||||||
- eq: ["status_code", 200]
|
- eq: ["status_code", 200]
|
||||||
- startswith: ["content.files.file", "MIT License"]
|
- startswith: ["content.files.file", "UserName=test"]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user