mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-30 21:09:36 +08:00
bugfix #34: handle exception when response content is empty
This commit is contained in:
@@ -1 +1 @@
|
||||
__version__ = '0.5.2'
|
||||
__version__ = '0.5.3'
|
||||
@@ -6,6 +6,9 @@ class MyBaseError(BaseException):
|
||||
class ParamsError(MyBaseError):
|
||||
pass
|
||||
|
||||
class ResponseError(MyBaseError):
|
||||
pass
|
||||
|
||||
class ParseResponseError(MyBaseError):
|
||||
pass
|
||||
|
||||
|
||||
@@ -31,10 +31,13 @@ class ResponseObject(object):
|
||||
"content.person.name.first_name"
|
||||
"""
|
||||
try:
|
||||
field += "."
|
||||
# string.split(sep=None, maxsplit=-1) -> list of strings
|
||||
# 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"]:
|
||||
json_content = self.parsed_body()
|
||||
|
||||
17
ate/utils.py
17
ate/utils.py
@@ -6,9 +6,10 @@ import os.path
|
||||
import random
|
||||
import re
|
||||
import string
|
||||
import yaml
|
||||
|
||||
import yaml
|
||||
from ate import exception
|
||||
from requests.structures import CaseInsensitiveDict
|
||||
|
||||
try:
|
||||
string_type = basestring
|
||||
@@ -131,15 +132,17 @@ def query_json(json_content, query, delimiter='.'):
|
||||
"person.cities.0" => "Guangzhou"
|
||||
@return queried result
|
||||
"""
|
||||
stripped_query = query.strip(delimiter)
|
||||
if not stripped_query:
|
||||
return None
|
||||
if json_content == "":
|
||||
raise exception.ResponseError("response content is empty!")
|
||||
|
||||
try:
|
||||
for key in stripped_query.split(delimiter):
|
||||
for key in query.split(delimiter):
|
||||
if isinstance(json_content, list):
|
||||
key = int(key)
|
||||
json_content = json_content[key]
|
||||
json_content = json_content[int(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):
|
||||
raise exception.ParseResponseError("failed to query json when extracting response!")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user