mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-06 23:11:21 +08:00
remove builtin setup hook: prepare post data
This commit is contained in:
@@ -133,26 +133,6 @@ def endswith(check_value, expect_value):
|
|||||||
|
|
||||||
""" built-in hooks
|
""" built-in hooks
|
||||||
"""
|
"""
|
||||||
def setup_hook_prepare_kwargs(request):
|
|
||||||
if request["method"] == "POST":
|
|
||||||
req_headers = lower_dict_keys(request.get("headers", {}))
|
|
||||||
content_type = req_headers.get("content-type")
|
|
||||||
if content_type and "data" in request:
|
|
||||||
req_data = request["data"]
|
|
||||||
|
|
||||||
# if request content-type is application/json, request data should be dumped
|
|
||||||
if content_type.startswith("application/json") and isinstance(req_data, (dict, list)):
|
|
||||||
req_data = json.dumps(req_data)
|
|
||||||
|
|
||||||
# if request content-type is application/x-www-form-urlencoded, request data should be in params format
|
|
||||||
elif content_type.startswith("application/x-www-form-urlencoded") and isinstance(req_data, dict):
|
|
||||||
req_data = convert_dict_to_params(req_data)
|
|
||||||
|
|
||||||
if isinstance(req_data, str):
|
|
||||||
req_data = req_data.encode('utf-8')
|
|
||||||
|
|
||||||
request["data"] = req_data
|
|
||||||
|
|
||||||
def sleep_N_secs(n_secs):
|
def sleep_N_secs(n_secs):
|
||||||
""" sleep n seconds
|
""" sleep n seconds
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -203,9 +203,9 @@ class Runner(object):
|
|||||||
self.session_context.update_test_variables("request", parsed_test_request)
|
self.session_context.update_test_variables("request", parsed_test_request)
|
||||||
|
|
||||||
# setup hooks
|
# setup hooks
|
||||||
setup_hooks = ["${setup_hook_prepare_kwargs($request)}"]
|
setup_hooks = test_dict.get("setup_hooks", [])
|
||||||
setup_hooks.extend(test_dict.get("setup_hooks", []))
|
if setup_hooks:
|
||||||
self.do_hook_actions(setup_hooks, "setup")
|
self.do_hook_actions(setup_hooks, "setup")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
url = parsed_test_request.pop('url')
|
url = parsed_test_request.pop('url')
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
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 httprunner.compat import bytes
|
||||||
from tests.base import ApiServerUnittest
|
from tests.base import ApiServerUnittest
|
||||||
@@ -39,52 +38,18 @@ class TestHttpClient(ApiServerUnittest):
|
|||||||
self.assertEqual(201, resp.status_code)
|
self.assertEqual(201, resp.status_code)
|
||||||
self.assertEqual(True, resp.json()['success'])
|
self.assertEqual(True, resp.json()['success'])
|
||||||
|
|
||||||
def test_prepare_kwargs_content_type_application_json_without_charset(self):
|
def test_request_post_data(self):
|
||||||
request = {
|
url = "/api/users/1000"
|
||||||
"url": "/path",
|
data = {
|
||||||
"method": "POST",
|
'name': 'user1',
|
||||||
"headers": {
|
'password': '123456'
|
||||||
"content-type": "application/json"
|
|
||||||
},
|
|
||||||
"data": {
|
|
||||||
"a": 1,
|
|
||||||
"b": 2
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
setup_hook_prepare_kwargs(request)
|
resp = self.api_client.post(url, json=data, headers=self.headers)
|
||||||
self.assertIsInstance(request["data"], bytes)
|
# b'{"name": "user1", "password": "123456"}'
|
||||||
self.assertIn(b'"a": 1', request["data"])
|
self.assertIn(b'"name": "user1"', resp.request.body)
|
||||||
self.assertIn(b'"b": 2', request["data"])
|
self.assertIn(b'"password": "123456"', resp.request.body)
|
||||||
|
resp = self.api_client.post(url, data=data, headers=self.headers)
|
||||||
def test_prepare_kwargs_content_type_application_json_charset_utf8(self):
|
# name=user1&password=123456
|
||||||
request = {
|
self.assertIn("name=user1", resp.request.body)
|
||||||
"url": "/path",
|
self.assertIn("&", resp.request.body)
|
||||||
"method": "POST",
|
self.assertIn("password=123456", resp.request.body)
|
||||||
"headers": {
|
|
||||||
"content-type": "application/json; charset=utf-8"
|
|
||||||
},
|
|
||||||
"data": {
|
|
||||||
"a": 1,
|
|
||||||
"b": 2
|
|
||||||
}
|
|
||||||
}
|
|
||||||
setup_hook_prepare_kwargs(request)
|
|
||||||
self.assertIsInstance(request["data"], bytes)
|
|
||||||
|
|
||||||
def test_prepare_kwargs_content_type_x_www_form_urlencoded(self):
|
|
||||||
request = {
|
|
||||||
"url": "/path",
|
|
||||||
"method": "POST",
|
|
||||||
"headers": {
|
|
||||||
"content-type": "application/x-www-form-urlencoded; charset=utf-8"
|
|
||||||
},
|
|
||||||
"data": {
|
|
||||||
"a": 1,
|
|
||||||
"b": 2
|
|
||||||
}
|
|
||||||
}
|
|
||||||
setup_hook_prepare_kwargs(request)
|
|
||||||
self.assertIsInstance(request["data"], bytes)
|
|
||||||
self.assertIn(b'a=1', request["data"])
|
|
||||||
self.assertIn(b'&', request["data"])
|
|
||||||
self.assertIn(b'b=2', request["data"])
|
|
||||||
|
|||||||
Reference in New Issue
Block a user