mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-11 18:11:21 +08:00
handle_req_data: sort data with keys if request data is json type
This commit is contained in:
@@ -35,3 +35,4 @@ Python `2.7`, `3.3`, `3.4`, `3.5`, and `3.6`.
|
||||
|
||||
- [《接口自动化测试的最佳工程实践(ApiTestEngine)》](http://debugtalk.com/post/ApiTestEngine-api-test-best-practice/)
|
||||
- [《ApiTestEngine 演化之路(0)开发未动,测试先行》](http://debugtalk.com/post/ApiTestEngine-0-setup-CI-test/)
|
||||
- [《ApiTestEngine 演化之路(1)搭建基础框架》](http://debugtalk.com/post/ApiTestEngine-1-setup-basic-framework/)
|
||||
|
||||
17
ate/utils.py
17
ate/utils.py
@@ -17,6 +17,23 @@ def gen_md5(str_list):
|
||||
authorization_str = "".join(str_list)
|
||||
return hashlib.md5(authorization_str.encode('utf-8')).hexdigest()
|
||||
|
||||
def handle_req_data(data):
|
||||
if not data:
|
||||
return data
|
||||
|
||||
if isinstance(data, str):
|
||||
# check if data in str can be converted to dict
|
||||
try:
|
||||
data = json.loads(data)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if isinstance(data, dict):
|
||||
# sort data in dict with keys, then convert to str
|
||||
data = json.dumps(data, sort_keys=True)
|
||||
|
||||
return data
|
||||
|
||||
def load_yaml_file(yaml_file):
|
||||
with open(yaml_file, 'r+') as stream:
|
||||
return yaml.load(stream)
|
||||
|
||||
@@ -36,7 +36,7 @@ def validate_request(func):
|
||||
req_headers = request.headers
|
||||
req_authorization = req_headers['Authorization']
|
||||
random_str = req_headers['Random']
|
||||
data = request.data.decode("utf-8")
|
||||
data = utils.handle_req_data(request.data)
|
||||
authorization = utils.gen_md5([TOKEN, data, random_str])
|
||||
assert authorization == req_authorization
|
||||
return func(*args, **kwds)
|
||||
|
||||
@@ -27,6 +27,7 @@ class ApiServerUnittest(unittest.TestCase):
|
||||
|
||||
def prepare_headers(self, data=""):
|
||||
token = api_server.TOKEN
|
||||
data = utils.handle_req_data(data)
|
||||
random_str = utils.gen_random_string(5)
|
||||
authorization = utils.gen_md5([token, data, random_str])
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import json
|
||||
import random
|
||||
import requests
|
||||
|
||||
@@ -37,7 +36,7 @@ class TestApiServerV2(ApiServerUnittest):
|
||||
'name': name,
|
||||
'password': password
|
||||
}
|
||||
headers = self.prepare_headers(json.dumps(data))
|
||||
headers = self.prepare_headers(data)
|
||||
return self.api_client.post(url, headers=headers, json=data)
|
||||
|
||||
def get_user(self, uid):
|
||||
@@ -50,7 +49,7 @@ class TestApiServerV2(ApiServerUnittest):
|
||||
'name': name,
|
||||
'password': password
|
||||
}
|
||||
headers = self.prepare_headers(json.dumps(data))
|
||||
headers = self.prepare_headers(data)
|
||||
return self.api_client.put(url, headers=headers, json=data)
|
||||
|
||||
def delete_user(self, uid):
|
||||
@@ -129,7 +128,7 @@ class TestApiServerV2(ApiServerUnittest):
|
||||
}
|
||||
resp = self.api_client.post(
|
||||
url,
|
||||
headers=self.prepare_headers(json.dumps(expected_response)),
|
||||
headers=self.prepare_headers(expected_response),
|
||||
json=expected_response
|
||||
)
|
||||
self.assertEqual(status_code, resp.status_code)
|
||||
@@ -144,7 +143,7 @@ class TestApiServerV2(ApiServerUnittest):
|
||||
url = "%s/customize-response" % self.host
|
||||
resp = self.api_client.post(
|
||||
url,
|
||||
headers=self.prepare_headers(json.dumps(expected_response)),
|
||||
headers=self.prepare_headers(expected_response),
|
||||
json=expected_response
|
||||
)
|
||||
self.assertIn('abc', resp.headers)
|
||||
|
||||
Reference in New Issue
Block a user