diff --git a/ate/utils.py b/ate/utils.py index d7bedbab..6004f55b 100644 --- a/ate/utils.py +++ b/ate/utils.py @@ -33,6 +33,19 @@ def parse_response_object(resp_obj): '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): diff_content = {} 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', {}) - for header_key, expected_header_value in expected_headers.items(): - header_value = resp_info['headers'].get(header_key, None) - if str(header_value) != str(expected_header_value): + headers_diff = diff_json(resp_info['headers'], expected_headers) + if headers_diff: + diff_content['headers'] = headers_diff - if 'headers' not in diff_content: - diff_content['headers'] = {} + expected_body = expected_resp_json.get('body', None) - diff_content['headers'][header_key] = { - 'value': header_value, - 'expected': str(expected_header_value) + if expected_body is None: + body_diff = {} + 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 diff --git a/test/api_server.py b/test/api_server.py index b65c659d..c762c36a 100644 --- a/test/api_server.py +++ b/test/api_server.py @@ -28,9 +28,8 @@ def get_customized_response(): expected_resp_json = request.get_json() status_code = expected_resp_json.get('status_code', 200) headers_dict = expected_resp_json.get('headers', {}) - body = expected_resp_json.get('body', "") - content = "Response: %s" % json.dumps(expected_resp_json) - response = make_response(content, status_code) + body = expected_resp_json.get('body', {}) + response = make_response(json.dumps(body), status_code) for header_key, header_value in headers_dict.items(): response.headers[header_key] = header_value diff --git a/test/test_utils.py b/test/test_utils.py index b1226b35..182c6d7d 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -131,6 +131,109 @@ class TestUtils(ApiServerUnittest): diff_content['headers'], { '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 + } } )