encode request data if it is in unicode/str type

This commit is contained in:
httprunner
2018-03-26 00:05:06 +08:00
parent 81f34ccc1a
commit 3ffcc8813e
5 changed files with 24 additions and 40 deletions

View File

@@ -8,7 +8,7 @@ import re
import string
import time
from httprunner.compat import basestring
from httprunner.compat import basestring, builtin_str, integer_types, str
from httprunner.exception import ParamsError
@@ -21,8 +21,8 @@ def gen_random_string(str_len):
def get_timestamp(str_len=13):
""" get timestamp string, length can only between 0 and 16
"""
if isinstance(str_len, int) and 0 < str_len < 17:
return str(time.time()).replace(".", "")[:str_len]
if isinstance(str_len, integer_types) and 0 < str_len < 17:
return builtin_str(time.time()).replace(".", "")[:str_len]
raise ParamsError("timestamp length can only between 0 and 16.")
@@ -53,26 +53,26 @@ def not_equals(check_value, expect_value):
assert check_value != expect_value
def string_equals(check_value, expect_value):
assert str(check_value) == str(expect_value)
assert builtin_str(check_value) == builtin_str(expect_value)
def length_equals(check_value, expect_value):
assert isinstance(expect_value, int)
assert isinstance(expect_value, integer_types)
assert len(check_value) == expect_value
def length_greater_than(check_value, expect_value):
assert isinstance(expect_value, int)
assert isinstance(expect_value, integer_types)
assert len(check_value) > expect_value
def length_greater_than_or_equals(check_value, expect_value):
assert isinstance(expect_value, int)
assert isinstance(expect_value, integer_types)
assert len(check_value) >= expect_value
def length_less_than(check_value, expect_value):
assert isinstance(expect_value, int)
assert isinstance(expect_value, integer_types)
assert len(check_value) < expect_value
def length_less_than_or_equals(check_value, expect_value):
assert isinstance(expect_value, int)
assert isinstance(expect_value, integer_types)
assert len(check_value) <= expect_value
def contains(check_value, expect_value):
@@ -87,7 +87,7 @@ def type_match(check_value, expect_value):
def get_type(name):
if isinstance(name, type):
return name
elif isinstance(name, str):
elif isinstance(name, basestring):
try:
return __builtins__[name]
except KeyError:
@@ -103,29 +103,13 @@ def regex_match(check_value, expect_value):
assert re.match(expect_value, check_value)
def startswith(check_value, expect_value):
assert str(check_value).startswith(str(expect_value))
assert builtin_str(check_value).startswith(builtin_str(expect_value))
def endswith(check_value, expect_value):
assert str(check_value).endswith(str(expect_value))
assert builtin_str(check_value).endswith(builtin_str(expect_value))
""" built-in hooks
"""
def get_charset_from_content_type(content_type):
""" extract charset encoding type from Content-Type
@param content_type
e.g.
application/json; charset=UTF-8
application/x-www-form-urlencoded; charset=UTF-8
@return: charset encoding type
UTF-8
"""
content_type = content_type.lower()
if "charset=" not in content_type:
return None
index = content_type.index("charset=") + len("charset=")
return content_type[index:]
def setup_hook_prepare_kwargs(method, url, kwargs):
if method == "POST":
content_type = kwargs.get("headers", {}).get("content-type")
@@ -134,10 +118,8 @@ def setup_hook_prepare_kwargs(method, url, kwargs):
if content_type.startswith("application/json"):
kwargs["data"] = json.dumps(kwargs["data"])
# if charset is specified in content-type, request data should be encoded with charset encoding
charset = get_charset_from_content_type(content_type)
if charset:
kwargs["data"] = kwargs["data"].encode(charset)
if isinstance(kwargs["data"], str):
kwargs["data"] = kwargs["data"].encode('utf-8')
def setup_hook_httpntlmauth(method, url, kwargs):
if "httpntlmauth" in kwargs: