move response relevant code to ate/response.py

This commit is contained in:
debugtalk
2017-06-28 15:29:40 +08:00
parent fbb7df5826
commit 4250992ad9
5 changed files with 267 additions and 261 deletions
+53
View File
@@ -0,0 +1,53 @@
from ate import utils
def parse_response_object(resp_obj):
try:
resp_body = resp_obj.json()
except ValueError:
resp_body = resp_obj.text
return {
'status_code': resp_obj.status_code,
'headers': resp_obj.headers,
'body': resp_body
}
def diff_response(resp_obj, expected_resp_json):
diff_content = {}
resp_info = parse_response_object(resp_obj)
expected_status_code = expected_resp_json.get('status_code', 200)
if resp_info['status_code'] != int(expected_status_code):
diff_content['status_code'] = {
'value': resp_info['status_code'],
'expected': expected_status_code
}
expected_headers = expected_resp_json.get('headers', {})
headers_diff = utils.diff_json(resp_info['headers'], expected_headers)
if headers_diff:
diff_content['headers'] = headers_diff
expected_body = expected_resp_json.get('body', None)
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 = utils.diff_json(resp_info['body'], expected_body)
if body_diff:
diff_content['body'] = body_diff
return diff_content
+2 -2
View File
@@ -1,6 +1,6 @@
import requests import requests
from ate import exception, utils from ate import exception, response
from ate.context import Context from ate.context import Context
from ate.testcase import TestcaseParser from ate.testcase import TestcaseParser
@@ -101,7 +101,7 @@ class TestRunner(object):
raise exception.ParamsError("URL or METHOD missed!") raise exception.ParamsError("URL or METHOD missed!")
resp_obj = self.client.request(url=url, method=method, **req_kwargs) resp_obj = self.client.request(url=url, method=method, **req_kwargs)
diff_content = utils.diff_response(resp_obj, testcase['response']) diff_content = response.diff_response(resp_obj, testcase['response'])
success = False if diff_content else True success = False if diff_content else True
return success, diff_content return success, diff_content
-51
View File
@@ -62,18 +62,6 @@ def load_testcases(testcase_file_path):
# '' or other suffix # '' or other suffix
raise ParamsError("Bad testcase file name!") raise ParamsError("Bad testcase file name!")
def parse_response_object(resp_obj):
try:
resp_body = resp_obj.json()
except ValueError:
resp_body = resp_obj.text
return {
'status_code': resp_obj.status_code,
'headers': resp_obj.headers,
'body': resp_body
}
def diff_json(current_json, expected_json): def diff_json(current_json, expected_json):
json_diff = {} json_diff = {}
@@ -87,45 +75,6 @@ def diff_json(current_json, expected_json):
return json_diff return json_diff
def diff_response(resp_obj, expected_resp_json):
diff_content = {}
resp_info = parse_response_object(resp_obj)
expected_status_code = expected_resp_json.get('status_code', 200)
if resp_info['status_code'] != int(expected_status_code):
diff_content['status_code'] = {
'value': resp_info['status_code'],
'expected': expected_status_code
}
expected_headers = expected_resp_json.get('headers', {})
headers_diff = diff_json(resp_info['headers'], expected_headers)
if headers_diff:
diff_content['headers'] = headers_diff
expected_body = expected_resp_json.get('body', None)
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
def load_foler_files(folder_path): def load_foler_files(folder_path):
""" load folder path, return all files in list format. """ load folder path, return all files in list format.
""" """
+212
View File
@@ -0,0 +1,212 @@
import random
import requests
from ate import response
from test.base import ApiServerUnittest
class TestUtils(ApiServerUnittest):
def test_parse_response_object_json(self):
url = "http://127.0.0.1:5000/api/users"
resp_obj = requests.get(url)
parse_result = response.parse_response_object(resp_obj)
self.assertIn('status_code', parse_result)
self.assertIn('headers', parse_result)
self.assertIn('body', parse_result)
self.assertIn('Content-Type', parse_result['headers'])
self.assertIn('Content-Length', parse_result['headers'])
self.assertIn('success', parse_result['body'])
def test_parse_response_object_text(self):
url = "http://127.0.0.1:5000/"
resp_obj = requests.get(url)
parse_result = response.parse_response_object(resp_obj)
self.assertIn('status_code', parse_result)
self.assertIn('headers', parse_result)
self.assertIn('body', parse_result)
self.assertIn('Content-Type', parse_result['headers'])
self.assertIn('Content-Length', parse_result['headers'])
self.assertTrue(str, type(parse_result['body']))
def test_diff_response_status_code_equal(self):
status_code = random.randint(200, 511)
resp_obj = requests.post(
url="http://127.0.0.1:5000/customize-response",
json={
'status_code': status_code,
}
)
expected_resp_json = {
'status_code': status_code
}
diff_content = response.diff_response(resp_obj, expected_resp_json)
self.assertFalse(diff_content)
def test_diff_response_status_code_not_equal(self):
status_code = random.randint(200, 511)
resp_obj = requests.post(
url="http://127.0.0.1:5000/customize-response",
json={
'status_code': status_code,
}
)
expected_resp_json = {
'status_code': 512
}
diff_content = response.diff_response(resp_obj, expected_resp_json)
self.assertIn('value', diff_content['status_code'])
self.assertIn('expected', diff_content['status_code'])
self.assertEqual(diff_content['status_code']['value'], status_code)
self.assertEqual(diff_content['status_code']['expected'], 512)
def test_diff_response_headers_equal(self):
resp_obj = requests.post(
url="http://127.0.0.1:5000/customize-response",
json={
'headers': {
'abc': 123,
'def': 456
}
}
)
expected_resp_json = {
'headers': {
'abc': 123,
'def': '456'
}
}
diff_content = response.diff_response(resp_obj, expected_resp_json)
self.assertFalse(diff_content)
def test_diff_response_headers_not_equal(self):
resp_obj = requests.post(
url="http://127.0.0.1:5000/customize-response",
json={
'headers': {
'a': 123,
'b': '456',
'c': '789'
}
}
)
expected_resp_json = {
'headers': {
'a': '123',
'b': '457',
'd': 890
}
}
diff_content = response.diff_response(resp_obj, expected_resp_json)
self.assertEqual(
diff_content['headers'],
{
'b': {'expected': '457', 'value': '456'},
'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 = response.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 = response.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 = response.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 = response.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 = response.diff_response(resp_obj, expected_resp_json)
self.assertEqual(
diff_content['body'],
{
'success': {
'value': False,
'expected': True
},
'count': {
'value': None,
'expected': 10
}
}
)
-208
View File
@@ -1,6 +1,4 @@
import os import os
import random
import requests
from ate import utils from ate import utils
from ate import exception from ate import exception
from test.base import ApiServerUnittest from test.base import ApiServerUnittest
@@ -36,212 +34,6 @@ class TestUtils(ApiServerUnittest):
self.assertIn('url', testcase['request']) self.assertIn('url', testcase['request'])
self.assertIn('method', testcase['request']) self.assertIn('method', testcase['request'])
def test_parse_response_object_json(self):
url = "http://127.0.0.1:5000/api/users"
resp_obj = requests.get(url)
parse_result = utils.parse_response_object(resp_obj)
self.assertIn('status_code', parse_result)
self.assertIn('headers', parse_result)
self.assertIn('body', parse_result)
self.assertIn('Content-Type', parse_result['headers'])
self.assertIn('Content-Length', parse_result['headers'])
self.assertIn('success', parse_result['body'])
def test_parse_response_object_text(self):
url = "http://127.0.0.1:5000/"
resp_obj = requests.get(url)
parse_result = utils.parse_response_object(resp_obj)
self.assertIn('status_code', parse_result)
self.assertIn('headers', parse_result)
self.assertIn('body', parse_result)
self.assertIn('Content-Type', parse_result['headers'])
self.assertIn('Content-Length', parse_result['headers'])
self.assertTrue(str, type(parse_result['body']))
def test_diff_response_status_code_equal(self):
status_code = random.randint(200, 511)
resp_obj = requests.post(
url="http://127.0.0.1:5000/customize-response",
json={
'status_code': status_code,
}
)
expected_resp_json = {
'status_code': status_code
}
diff_content = utils.diff_response(resp_obj, expected_resp_json)
self.assertFalse(diff_content)
def test_diff_response_status_code_not_equal(self):
status_code = random.randint(200, 511)
resp_obj = requests.post(
url="http://127.0.0.1:5000/customize-response",
json={
'status_code': status_code,
}
)
expected_resp_json = {
'status_code': 512
}
diff_content = utils.diff_response(resp_obj, expected_resp_json)
self.assertIn('value', diff_content['status_code'])
self.assertIn('expected', diff_content['status_code'])
self.assertEqual(diff_content['status_code']['value'], status_code)
self.assertEqual(diff_content['status_code']['expected'], 512)
def test_diff_response_headers_equal(self):
resp_obj = requests.post(
url="http://127.0.0.1:5000/customize-response",
json={
'headers': {
'abc': 123,
'def': 456
}
}
)
expected_resp_json = {
'headers': {
'abc': 123,
'def': '456'
}
}
diff_content = utils.diff_response(resp_obj, expected_resp_json)
self.assertFalse(diff_content)
def test_diff_response_headers_not_equal(self):
resp_obj = requests.post(
url="http://127.0.0.1:5000/customize-response",
json={
'headers': {
'a': 123,
'b': '456',
'c': '789'
}
}
)
expected_resp_json = {
'headers': {
'a': '123',
'b': '457',
'd': 890
}
}
diff_content = utils.diff_response(resp_obj, expected_resp_json)
self.assertEqual(
diff_content['headers'],
{
'b': {'expected': '457', 'value': '456'},
'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
}
}
)
def test_load_foler_files(self): def test_load_foler_files(self):
folder = os.path.join(os.getcwd(), 'test') folder = os.path.join(os.getcwd(), 'test')
files = utils.load_foler_files(folder) files = utils.load_foler_files(folder)