remove builtin setup hook: prepare post data

This commit is contained in:
debugtalk
2018-12-17 20:55:19 +08:00
parent d53133cfea
commit 9ef84a38e8
3 changed files with 17 additions and 72 deletions

View File

@@ -133,26 +133,6 @@ def endswith(check_value, expect_value):
""" 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):
""" sleep n seconds
"""

View File

@@ -203,9 +203,9 @@ class Runner(object):
self.session_context.update_test_variables("request", parsed_test_request)
# setup hooks
setup_hooks = ["${setup_hook_prepare_kwargs($request)}"]
setup_hooks.extend(test_dict.get("setup_hooks", []))
self.do_hook_actions(setup_hooks, "setup")
setup_hooks = test_dict.get("setup_hooks", [])
if setup_hooks:
self.do_hook_actions(setup_hooks, "setup")
try:
url = parsed_test_request.pop('url')

View File

@@ -1,4 +1,3 @@
from httprunner.built_in import setup_hook_prepare_kwargs
from httprunner.client import HttpSession
from httprunner.compat import bytes
from tests.base import ApiServerUnittest
@@ -39,52 +38,18 @@ class TestHttpClient(ApiServerUnittest):
self.assertEqual(201, resp.status_code)
self.assertEqual(True, resp.json()['success'])
def test_prepare_kwargs_content_type_application_json_without_charset(self):
request = {
"url": "/path",
"method": "POST",
"headers": {
"content-type": "application/json"
},
"data": {
"a": 1,
"b": 2
}
def test_request_post_data(self):
url = "/api/users/1000"
data = {
'name': 'user1',
'password': '123456'
}
setup_hook_prepare_kwargs(request)
self.assertIsInstance(request["data"], bytes)
self.assertIn(b'"a": 1', request["data"])
self.assertIn(b'"b": 2', request["data"])
def test_prepare_kwargs_content_type_application_json_charset_utf8(self):
request = {
"url": "/path",
"method": "POST",
"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"])
resp = self.api_client.post(url, json=data, headers=self.headers)
# b'{"name": "user1", "password": "123456"}'
self.assertIn(b'"name": "user1"', resp.request.body)
self.assertIn(b'"password": "123456"', resp.request.body)
resp = self.api_client.post(url, data=data, headers=self.headers)
# name=user1&password=123456
self.assertIn("name=user1", resp.request.body)
self.assertIn("&", resp.request.body)
self.assertIn("password=123456", resp.request.body)