mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-12 16:01:27 +08:00
diff http response: body
This commit is contained in:
42
ate/utils.py
42
ate/utils.py
@@ -33,6 +33,19 @@ def parse_response_object(resp_obj):
|
|||||||
'body': resp_body
|
'body': resp_body
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def diff_json(current_json, expected_json):
|
||||||
|
json_diff = {}
|
||||||
|
|
||||||
|
for key, expected_value in expected_json.items():
|
||||||
|
value = current_json.get(key, None)
|
||||||
|
if str(value) != str(expected_value):
|
||||||
|
json_diff[key] = {
|
||||||
|
'value': value,
|
||||||
|
'expected': expected_value
|
||||||
|
}
|
||||||
|
|
||||||
|
return json_diff
|
||||||
|
|
||||||
def diff_response(resp_obj, expected_resp_json):
|
def diff_response(resp_obj, expected_resp_json):
|
||||||
diff_content = {}
|
diff_content = {}
|
||||||
resp_info = parse_response_object(resp_obj)
|
resp_info = parse_response_object(resp_obj)
|
||||||
@@ -45,16 +58,29 @@ def diff_response(resp_obj, expected_resp_json):
|
|||||||
}
|
}
|
||||||
|
|
||||||
expected_headers = expected_resp_json.get('headers', {})
|
expected_headers = expected_resp_json.get('headers', {})
|
||||||
for header_key, expected_header_value in expected_headers.items():
|
headers_diff = diff_json(resp_info['headers'], expected_headers)
|
||||||
header_value = resp_info['headers'].get(header_key, None)
|
if headers_diff:
|
||||||
if str(header_value) != str(expected_header_value):
|
diff_content['headers'] = headers_diff
|
||||||
|
|
||||||
if 'headers' not in diff_content:
|
expected_body = expected_resp_json.get('body', None)
|
||||||
diff_content['headers'] = {}
|
|
||||||
|
|
||||||
diff_content['headers'][header_key] = {
|
if expected_body is None:
|
||||||
'value': header_value,
|
body_diff = {}
|
||||||
'expected': str(expected_header_value)
|
elif type(expected_body) != type(resp_info['body']):
|
||||||
|
body_diff = {
|
||||||
|
'value': resp_info['body'],
|
||||||
|
'expected': expected_body
|
||||||
|
}
|
||||||
|
elif isinstance(expected_body, str):
|
||||||
|
if expected_body != resp_info['body']:
|
||||||
|
body_diff = {
|
||||||
|
'value': resp_info['body'],
|
||||||
|
'expected': expected_body
|
||||||
}
|
}
|
||||||
|
elif isinstance(expected_body, dict):
|
||||||
|
body_diff = diff_json(resp_info['body'], expected_body)
|
||||||
|
|
||||||
|
if body_diff:
|
||||||
|
diff_content['body'] = body_diff
|
||||||
|
|
||||||
return diff_content
|
return diff_content
|
||||||
|
|||||||
@@ -28,9 +28,8 @@ def get_customized_response():
|
|||||||
expected_resp_json = request.get_json()
|
expected_resp_json = request.get_json()
|
||||||
status_code = expected_resp_json.get('status_code', 200)
|
status_code = expected_resp_json.get('status_code', 200)
|
||||||
headers_dict = expected_resp_json.get('headers', {})
|
headers_dict = expected_resp_json.get('headers', {})
|
||||||
body = expected_resp_json.get('body', "")
|
body = expected_resp_json.get('body', {})
|
||||||
content = "Response: %s" % json.dumps(expected_resp_json)
|
response = make_response(json.dumps(body), status_code)
|
||||||
response = make_response(content, status_code)
|
|
||||||
|
|
||||||
for header_key, header_value in headers_dict.items():
|
for header_key, header_value in headers_dict.items():
|
||||||
response.headers[header_key] = header_value
|
response.headers[header_key] = header_value
|
||||||
|
|||||||
@@ -131,6 +131,109 @@ class TestUtils(ApiServerUnittest):
|
|||||||
diff_content['headers'],
|
diff_content['headers'],
|
||||||
{
|
{
|
||||||
'b': {'expected': '457', 'value': '456'},
|
'b': {'expected': '457', 'value': '456'},
|
||||||
'd': {'expected': '890', 'value': None}
|
'd': {'expected': 890, 'value': None}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_diff_response_body_equal(self):
|
||||||
|
resp_obj = requests.post(
|
||||||
|
url="http://127.0.0.1:5000/customize-response",
|
||||||
|
json={
|
||||||
|
'body': {
|
||||||
|
'success': True,
|
||||||
|
'count': 10
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# expected response body is not specified
|
||||||
|
expected_resp_json = {}
|
||||||
|
diff_content = utils.diff_response(resp_obj, expected_resp_json)
|
||||||
|
self.assertFalse(diff_content)
|
||||||
|
|
||||||
|
# response body is the same as expected response body
|
||||||
|
expected_resp_json = {
|
||||||
|
'body': {
|
||||||
|
'success': True,
|
||||||
|
'count': '10'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
diff_content = utils.diff_response(resp_obj, expected_resp_json)
|
||||||
|
self.assertFalse(diff_content)
|
||||||
|
|
||||||
|
def test_diff_response_body_not_equal_type_unmatch(self):
|
||||||
|
resp_obj = requests.post(
|
||||||
|
url="http://127.0.0.1:5000/customize-response",
|
||||||
|
json={
|
||||||
|
'body': {
|
||||||
|
'success': True,
|
||||||
|
'count': 10
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# response body content type not match
|
||||||
|
expected_resp_json = {
|
||||||
|
'body': "ok"
|
||||||
|
}
|
||||||
|
diff_content = utils.diff_response(resp_obj, expected_resp_json)
|
||||||
|
self.assertEqual(
|
||||||
|
diff_content['body'],
|
||||||
|
{
|
||||||
|
'value': {'success': True, 'count': 10},
|
||||||
|
'expected': 'ok'
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_diff_response_body_not_equal_string_unmatch(self):
|
||||||
|
resp_obj = requests.post(
|
||||||
|
url="http://127.0.0.1:5000/customize-response",
|
||||||
|
json={
|
||||||
|
'body': "success"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# response body content type matched to be string, while value unmatch
|
||||||
|
expected_resp_json = {
|
||||||
|
'body': "ok"
|
||||||
|
}
|
||||||
|
diff_content = utils.diff_response(resp_obj, expected_resp_json)
|
||||||
|
self.assertEqual(
|
||||||
|
diff_content['body'],
|
||||||
|
{
|
||||||
|
'value': 'success',
|
||||||
|
'expected': 'ok'
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_diff_response_body_not_equal_json_unmatch(self):
|
||||||
|
resp_obj = requests.post(
|
||||||
|
url="http://127.0.0.1:5000/customize-response",
|
||||||
|
json={
|
||||||
|
'body': {
|
||||||
|
'success': False
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# response body is the same as expected response body
|
||||||
|
expected_resp_json = {
|
||||||
|
'body': {
|
||||||
|
'success': True,
|
||||||
|
'count': 10
|
||||||
|
}
|
||||||
|
}
|
||||||
|
diff_content = utils.diff_response(resp_obj, expected_resp_json)
|
||||||
|
self.assertEqual(
|
||||||
|
diff_content['body'],
|
||||||
|
{
|
||||||
|
'success': {
|
||||||
|
'value': False,
|
||||||
|
'expected': True
|
||||||
|
},
|
||||||
|
'count': {
|
||||||
|
'value': None,
|
||||||
|
'expected': 10
|
||||||
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user