parse_response_object: load response content as json format if possible

This commit is contained in:
debugtalk
2017-06-20 20:24:38 +08:00
parent 278aaff290
commit 4d69342d40
3 changed files with 25 additions and 2 deletions

View File

@@ -22,8 +22,13 @@ def load_testcases(testcase_file_path):
raise ParamsError("Bad testcase file name!")
def parse_response_object(resp_obj):
try:
resp_content = resp_obj.json()
except json.decoder.JSONDecodeError:
resp_content = resp_obj.text
return {
'status_code': resp_obj.status_code,
'headers': resp_obj.headers,
'content': resp_obj.content
'content': resp_content
}

View File

@@ -19,6 +19,10 @@ data structure:
"""
users_dict = {}
@app.route('/')
def index():
return "Hello World!"
@app.route('/api/users')
def get_users():
users_list = [user for uid, user in users_dict.items()]

View File

@@ -31,10 +31,24 @@ class TestUtils(ApiServerUnittest):
self.assertIn('url', testcases[0]['request'])
self.assertIn('method', testcases[0]['request'])
def test_parse_response_object(self):
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('content', parse_result)
self.assertIn('Content-Type', parse_result['headers'])
self.assertIn('Content-Length', parse_result['headers'])
self.assertIn('success', parse_result['content'])
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('content', parse_result)
self.assertIn('Content-Type', parse_result['headers'])
self.assertIn('Content-Length', parse_result['headers'])
self.assertTrue(str, type(parse_result['content']))