mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-04 23:16:57 +08:00
bugfix #34: handle exception when response content is empty
This commit is contained in:
+1
-1
@@ -1 +1 @@
|
|||||||
__version__ = '0.5.2'
|
__version__ = '0.5.3'
|
||||||
@@ -6,6 +6,9 @@ class MyBaseError(BaseException):
|
|||||||
class ParamsError(MyBaseError):
|
class ParamsError(MyBaseError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class ResponseError(MyBaseError):
|
||||||
|
pass
|
||||||
|
|
||||||
class ParseResponseError(MyBaseError):
|
class ParseResponseError(MyBaseError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
+5
-2
@@ -31,10 +31,13 @@ class ResponseObject(object):
|
|||||||
"content.person.name.first_name"
|
"content.person.name.first_name"
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
field += "."
|
|
||||||
# string.split(sep=None, maxsplit=-1) -> list of strings
|
# string.split(sep=None, maxsplit=-1) -> list of strings
|
||||||
# e.g. "content.person.name" => ["content", "person.name"]
|
# e.g. "content.person.name" => ["content", "person.name"]
|
||||||
top_query, sub_query = field.split(delimiter, 1)
|
try:
|
||||||
|
top_query, sub_query = field.split(delimiter, 1)
|
||||||
|
except ValueError:
|
||||||
|
top_query = field
|
||||||
|
sub_query = None
|
||||||
|
|
||||||
if top_query in ["body", "content", "text"]:
|
if top_query in ["body", "content", "text"]:
|
||||||
json_content = self.parsed_body()
|
json_content = self.parsed_body()
|
||||||
|
|||||||
+10
-7
@@ -6,9 +6,10 @@ import os.path
|
|||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
import string
|
import string
|
||||||
import yaml
|
|
||||||
|
|
||||||
|
import yaml
|
||||||
from ate import exception
|
from ate import exception
|
||||||
|
from requests.structures import CaseInsensitiveDict
|
||||||
|
|
||||||
try:
|
try:
|
||||||
string_type = basestring
|
string_type = basestring
|
||||||
@@ -131,15 +132,17 @@ def query_json(json_content, query, delimiter='.'):
|
|||||||
"person.cities.0" => "Guangzhou"
|
"person.cities.0" => "Guangzhou"
|
||||||
@return queried result
|
@return queried result
|
||||||
"""
|
"""
|
||||||
stripped_query = query.strip(delimiter)
|
if json_content == "":
|
||||||
if not stripped_query:
|
raise exception.ResponseError("response content is empty!")
|
||||||
return None
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for key in stripped_query.split(delimiter):
|
for key in query.split(delimiter):
|
||||||
if isinstance(json_content, list):
|
if isinstance(json_content, list):
|
||||||
key = int(key)
|
json_content = json_content[int(key)]
|
||||||
json_content = json_content[key]
|
elif isinstance(json_content, (dict, CaseInsensitiveDict)):
|
||||||
|
json_content = json_content[key]
|
||||||
|
else:
|
||||||
|
raise exception.ParseResponseError("response content is in text format! failed to query key {}!".format(key))
|
||||||
except (KeyError, ValueError, IndexError):
|
except (KeyError, ValueError, IndexError):
|
||||||
raise exception.ParseResponseError("failed to query json when extracting response!")
|
raise exception.ParseResponseError("failed to query json when extracting response!")
|
||||||
|
|
||||||
|
|||||||
@@ -149,6 +149,34 @@ class TestResponse(ApiServerUnittest):
|
|||||||
"abc"
|
"abc"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_extract_response_empty(self):
|
||||||
|
resp = requests.post(
|
||||||
|
url="http://127.0.0.1:5000/customize-response",
|
||||||
|
json={
|
||||||
|
'headers': {
|
||||||
|
'Content-Type': "application/json"
|
||||||
|
},
|
||||||
|
'body': ""
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
extract_binds_list = [
|
||||||
|
{"resp_content_body": "content"}
|
||||||
|
]
|
||||||
|
resp_obj = response.ResponseObject(resp)
|
||||||
|
extract_binds_dict_list = resp_obj.extract_response(extract_binds_list)
|
||||||
|
self.assertEqual(
|
||||||
|
extract_binds_dict_list[0]["resp_content_body"],
|
||||||
|
""
|
||||||
|
)
|
||||||
|
|
||||||
|
extract_binds_list = [
|
||||||
|
{"resp_content_body": "content.abc"}
|
||||||
|
]
|
||||||
|
resp_obj = response.ResponseObject(resp)
|
||||||
|
with self.assertRaises(exception.ResponseError):
|
||||||
|
resp_obj.extract_response(extract_binds_list)
|
||||||
|
|
||||||
def test_validate(self):
|
def test_validate(self):
|
||||||
url = "http://127.0.0.1:5000/"
|
url = "http://127.0.0.1:5000/"
|
||||||
resp = requests.get(url)
|
resp = requests.get(url)
|
||||||
|
|||||||
@@ -154,6 +154,17 @@ class TestUtils(ApiServerUnittest):
|
|||||||
result = utils.query_json(json_content, query)
|
result = utils.query_json(json_content, query)
|
||||||
self.assertEqual(result, "Leo")
|
self.assertEqual(result, "Leo")
|
||||||
|
|
||||||
|
def test_query_json_content_is_text(self):
|
||||||
|
json_content = ""
|
||||||
|
query = "key"
|
||||||
|
with self.assertRaises(exception.ResponseError):
|
||||||
|
utils.query_json(json_content, query)
|
||||||
|
|
||||||
|
json_content = "<html><body>content</body></html>"
|
||||||
|
query = "key"
|
||||||
|
with self.assertRaises(exception.ParseResponseError):
|
||||||
|
utils.query_json(json_content, query)
|
||||||
|
|
||||||
def test_match_expected(self):
|
def test_match_expected(self):
|
||||||
self.assertTrue(utils.match_expected(1, 1, "eq"))
|
self.assertTrue(utils.match_expected(1, 1, "eq"))
|
||||||
self.assertTrue(utils.match_expected("abc", "abc", "=="))
|
self.assertTrue(utils.match_expected("abc", "abc", "=="))
|
||||||
|
|||||||
Reference in New Issue
Block a user