mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-04 23:16:57 +08:00
refactor: extract cookie
This commit is contained in:
@@ -84,13 +84,16 @@ class ResponseObject(object):
|
|||||||
|
|
||||||
# cookies
|
# cookies
|
||||||
elif top_query == "cookies":
|
elif top_query == "cookies":
|
||||||
cookies = self.cookies
|
cookies = self.cookies.get_dict()
|
||||||
|
if not sub_query:
|
||||||
|
# extract cookies
|
||||||
|
return cookies
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return cookies[sub_query]
|
return cookies[sub_query]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
err_msg = u"Failed to extract attribute from cookies!\n"
|
err_msg = u"ParamsError: Failed to extract cookie! => {}\n".format(field)
|
||||||
err_msg += u"cookies: {}\n".format(cookies)
|
err_msg += u"response cookies: {}\n".format(cookies)
|
||||||
err_msg += u"attribute: {}".format(sub_query)
|
|
||||||
logger.log_error(err_msg)
|
logger.log_error(err_msg)
|
||||||
raise exceptions.ParamsError(err_msg)
|
raise exceptions.ParamsError(err_msg)
|
||||||
|
|
||||||
|
|||||||
@@ -60,6 +60,30 @@ class TestResponse(ApiServerUnittest):
|
|||||||
with self.assertRaises(exceptions.ParamsError):
|
with self.assertRaises(exceptions.ParamsError):
|
||||||
resp_obj.extract_response(extract_binds_list)
|
resp_obj.extract_response(extract_binds_list)
|
||||||
|
|
||||||
|
def test_extract_response_cookies(self):
|
||||||
|
resp = requests.get(
|
||||||
|
url="http://127.0.0.1:3458/cookies",
|
||||||
|
headers={
|
||||||
|
"accept": "application/json"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
resp_obj = response.ResponseObject(resp)
|
||||||
|
|
||||||
|
extract_binds_list = [
|
||||||
|
{"resp_cookies": "cookies"}
|
||||||
|
]
|
||||||
|
extract_binds_dict = resp_obj.extract_response(extract_binds_list)
|
||||||
|
self.assertEqual(
|
||||||
|
extract_binds_dict["resp_cookies"],
|
||||||
|
{}
|
||||||
|
)
|
||||||
|
|
||||||
|
extract_binds_list = [
|
||||||
|
{"resp_cookies": "cookies.xx"}
|
||||||
|
]
|
||||||
|
with self.assertRaises(exceptions.ParamsError):
|
||||||
|
resp_obj.extract_response(extract_binds_list)
|
||||||
|
|
||||||
def test_extract_response_json(self):
|
def test_extract_response_json(self):
|
||||||
resp = requests.post(
|
resp = requests.post(
|
||||||
url="http://127.0.0.1:3458/anything",
|
url="http://127.0.0.1:3458/anything",
|
||||||
|
|||||||
Reference in New Issue
Block a user