diff --git a/httprunner/response.py b/httprunner/response.py index fc9c36b8..a36cbb35 100644 --- a/httprunner/response.py +++ b/httprunner/response.py @@ -84,13 +84,16 @@ class ResponseObject(object): # cookies elif top_query == "cookies": - cookies = self.cookies + cookies = self.cookies.get_dict() + if not sub_query: + # extract cookies + return cookies + try: return cookies[sub_query] except KeyError: - err_msg = u"Failed to extract attribute from cookies!\n" - err_msg += u"cookies: {}\n".format(cookies) - err_msg += u"attribute: {}".format(sub_query) + err_msg = u"ParamsError: Failed to extract cookie! => {}\n".format(field) + err_msg += u"response cookies: {}\n".format(cookies) logger.log_error(err_msg) raise exceptions.ParamsError(err_msg) diff --git a/tests/test_response.py b/tests/test_response.py index da28be27..963acc81 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -60,6 +60,30 @@ class TestResponse(ApiServerUnittest): with self.assertRaises(exceptions.ParamsError): 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): resp = requests.post( url="http://127.0.0.1:3458/anything",