diff --git a/httprunner/__about__.py b/httprunner/__about__.py index b4532369..7e565489 100644 --- a/httprunner/__about__.py +++ b/httprunner/__about__.py @@ -1,7 +1,7 @@ __title__ = 'HttpRunner' __description__ = 'One-stop solution for HTTP(S) testing.' __url__ = 'https://github.com/HttpRunner/HttpRunner' -__version__ = '1.3.4' +__version__ = '1.3.5' __author__ = 'debugtalk' __author_email__ = 'mail@debugtalk.com' __license__ = 'MIT' diff --git a/httprunner/built_in.py b/httprunner/built_in.py index 1fe4bc0f..b36df3f1 100644 --- a/httprunner/built_in.py +++ b/httprunner/built_in.py @@ -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: diff --git a/httprunner/client.py b/httprunner/client.py index 82f20fdf..c43f6f38 100644 --- a/httprunner/client.py +++ b/httprunner/client.py @@ -4,7 +4,7 @@ import time import requests import urllib3 from httprunner import logger -from httprunner.compat import is_py2 +from httprunner.compat import str from httprunner.exception import ParamsError from requests import Request, Response 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_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") logger.log_debug("response status_code: {}".format(self.meta_data["status_code"])) diff --git a/httprunner/utils.py b/httprunner/utils.py index 5e055d2c..6bb1a1f1 100644 --- a/httprunner/utils.py +++ b/httprunner/utils.py @@ -196,13 +196,13 @@ def get_imported_module(module_name): def get_imported_module_from_file(file_path): """ import module from python file path and return imported module """ - if is_py3: imported_module = importlib.machinery.SourceFileLoader( 'module_name', file_path).load_module() - else: - # Python 2.7 + elif is_py2: imported_module = imp.load_source('module_name', file_path) + else: + raise RuntimeError("Neither Python 3 nor Python 2.") return imported_module diff --git a/tests/test_client.py b/tests/test_client.py index ef5a3e88..be51953d 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,5 +1,6 @@ from httprunner.built_in import setup_hook_prepare_kwargs from httprunner.client import HttpSession +from httprunner.compat import bytes from tests.base import ApiServerUnittest @@ -49,8 +50,9 @@ class TestHttpClient(ApiServerUnittest): } } setup_hook_prepare_kwargs("POST", "/path", kwargs) - self.assertIn('"a": 1', kwargs["data"]) - self.assertIn('"b": 2', kwargs["data"]) + self.assertIsInstance(kwargs["data"], bytes) + 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): kwargs = {