mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-02 22:39:42 +08:00
encode request data if it is in unicode/str type
This commit is contained in:
@@ -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__ = '1.3.4'
|
__version__ = '1.3.5'
|
||||||
__author__ = 'debugtalk'
|
__author__ = 'debugtalk'
|
||||||
__author_email__ = 'mail@debugtalk.com'
|
__author_email__ = 'mail@debugtalk.com'
|
||||||
__license__ = 'MIT'
|
__license__ = 'MIT'
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import re
|
|||||||
import string
|
import string
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from httprunner.compat import basestring
|
from httprunner.compat import basestring, builtin_str, integer_types, str
|
||||||
from httprunner.exception import ParamsError
|
from httprunner.exception import ParamsError
|
||||||
|
|
||||||
|
|
||||||
@@ -21,8 +21,8 @@ def gen_random_string(str_len):
|
|||||||
def get_timestamp(str_len=13):
|
def get_timestamp(str_len=13):
|
||||||
""" get timestamp string, length can only between 0 and 16
|
""" get timestamp string, length can only between 0 and 16
|
||||||
"""
|
"""
|
||||||
if isinstance(str_len, int) and 0 < str_len < 17:
|
if isinstance(str_len, integer_types) and 0 < str_len < 17:
|
||||||
return str(time.time()).replace(".", "")[:str_len]
|
return builtin_str(time.time()).replace(".", "")[:str_len]
|
||||||
|
|
||||||
raise ParamsError("timestamp length can only between 0 and 16.")
|
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
|
assert check_value != expect_value
|
||||||
|
|
||||||
def string_equals(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):
|
def length_equals(check_value, expect_value):
|
||||||
assert isinstance(expect_value, int)
|
assert isinstance(expect_value, integer_types)
|
||||||
assert len(check_value) == expect_value
|
assert len(check_value) == expect_value
|
||||||
|
|
||||||
def length_greater_than(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
|
assert len(check_value) > expect_value
|
||||||
|
|
||||||
def length_greater_than_or_equals(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
|
assert len(check_value) >= expect_value
|
||||||
|
|
||||||
def length_less_than(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
|
assert len(check_value) < expect_value
|
||||||
|
|
||||||
def length_less_than_or_equals(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
|
assert len(check_value) <= expect_value
|
||||||
|
|
||||||
def contains(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):
|
def get_type(name):
|
||||||
if isinstance(name, type):
|
if isinstance(name, type):
|
||||||
return name
|
return name
|
||||||
elif isinstance(name, str):
|
elif isinstance(name, basestring):
|
||||||
try:
|
try:
|
||||||
return __builtins__[name]
|
return __builtins__[name]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
@@ -103,29 +103,13 @@ def regex_match(check_value, expect_value):
|
|||||||
assert re.match(expect_value, check_value)
|
assert re.match(expect_value, check_value)
|
||||||
|
|
||||||
def startswith(check_value, expect_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):
|
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
|
""" 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):
|
def setup_hook_prepare_kwargs(method, url, kwargs):
|
||||||
if method == "POST":
|
if method == "POST":
|
||||||
content_type = kwargs.get("headers", {}).get("content-type")
|
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"):
|
if content_type.startswith("application/json"):
|
||||||
kwargs["data"] = json.dumps(kwargs["data"])
|
kwargs["data"] = json.dumps(kwargs["data"])
|
||||||
|
|
||||||
# if charset is specified in content-type, request data should be encoded with charset encoding
|
if isinstance(kwargs["data"], str):
|
||||||
charset = get_charset_from_content_type(content_type)
|
kwargs["data"] = kwargs["data"].encode('utf-8')
|
||||||
if charset:
|
|
||||||
kwargs["data"] = kwargs["data"].encode(charset)
|
|
||||||
|
|
||||||
def setup_hook_httpntlmauth(method, url, kwargs):
|
def setup_hook_httpntlmauth(method, url, kwargs):
|
||||||
if "httpntlmauth" in kwargs:
|
if "httpntlmauth" in kwargs:
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import time
|
|||||||
import requests
|
import requests
|
||||||
import urllib3
|
import urllib3
|
||||||
from httprunner import logger
|
from httprunner import logger
|
||||||
from httprunner.compat import is_py2
|
from httprunner.compat import str
|
||||||
from httprunner.exception import ParamsError
|
from httprunner.exception import ParamsError
|
||||||
from requests import Request, Response
|
from requests import Request, Response
|
||||||
from requests.exceptions import (InvalidSchema, InvalidURL, MissingSchema,
|
from requests.exceptions import (InvalidSchema, InvalidURL, MissingSchema,
|
||||||
@@ -116,7 +116,7 @@ class HttpSession(requests.Session):
|
|||||||
self.meta_data["response_headers"] = response.headers
|
self.meta_data["response_headers"] = response.headers
|
||||||
|
|
||||||
self.meta_data["response_body"] = response.text
|
self.meta_data["response_body"] = response.text
|
||||||
if is_py2 and isinstance(self.meta_data["response_body"], unicode):
|
if isinstance(self.meta_data["response_body"], str):
|
||||||
self.meta_data["response_body"] = self.meta_data["response_body"].encode("utf-8")
|
self.meta_data["response_body"] = self.meta_data["response_body"].encode("utf-8")
|
||||||
|
|
||||||
logger.log_debug("response status_code: {}".format(self.meta_data["status_code"]))
|
logger.log_debug("response status_code: {}".format(self.meta_data["status_code"]))
|
||||||
|
|||||||
@@ -196,13 +196,13 @@ def get_imported_module(module_name):
|
|||||||
def get_imported_module_from_file(file_path):
|
def get_imported_module_from_file(file_path):
|
||||||
""" import module from python file path and return imported module
|
""" import module from python file path and return imported module
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if is_py3:
|
if is_py3:
|
||||||
imported_module = importlib.machinery.SourceFileLoader(
|
imported_module = importlib.machinery.SourceFileLoader(
|
||||||
'module_name', file_path).load_module()
|
'module_name', file_path).load_module()
|
||||||
else:
|
elif is_py2:
|
||||||
# Python 2.7
|
|
||||||
imported_module = imp.load_source('module_name', file_path)
|
imported_module = imp.load_source('module_name', file_path)
|
||||||
|
else:
|
||||||
|
raise RuntimeError("Neither Python 3 nor Python 2.")
|
||||||
|
|
||||||
return imported_module
|
return imported_module
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from httprunner.built_in import setup_hook_prepare_kwargs
|
from httprunner.built_in import setup_hook_prepare_kwargs
|
||||||
from httprunner.client import HttpSession
|
from httprunner.client import HttpSession
|
||||||
|
from httprunner.compat import bytes
|
||||||
from tests.base import ApiServerUnittest
|
from tests.base import ApiServerUnittest
|
||||||
|
|
||||||
|
|
||||||
@@ -49,8 +50,9 @@ class TestHttpClient(ApiServerUnittest):
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
setup_hook_prepare_kwargs("POST", "/path", kwargs)
|
setup_hook_prepare_kwargs("POST", "/path", kwargs)
|
||||||
self.assertIn('"a": 1', kwargs["data"])
|
self.assertIsInstance(kwargs["data"], bytes)
|
||||||
self.assertIn('"b": 2', kwargs["data"])
|
self.assertIn(b'"a": 1', kwargs["data"])
|
||||||
|
self.assertIn(b'"b": 2', kwargs["data"])
|
||||||
|
|
||||||
def test_prepare_kwargs_content_type_application_json_charset_utf8(self):
|
def test_prepare_kwargs_content_type_application_json_charset_utf8(self):
|
||||||
kwargs = {
|
kwargs = {
|
||||||
|
|||||||
Reference in New Issue
Block a user