diff --git a/httprunner/response.py b/httprunner/response.py index 47711022..4d59e339 100644 --- a/httprunner/response.py +++ b/httprunner/response.py @@ -72,14 +72,14 @@ class ResponseObject(object): sub_query = None # status_code - if top_query == "status_code": + if top_query in ["status_code", "encoding", "ok", "reason", "url"]: if sub_query: # status_code.XX err_msg = u"ParamsError: {}\n".format(field) logger.log_error(err_msg) raise exceptions.ParamsError(err_msg) - return self.status_code + return getattr(self, top_query) # cookies elif top_query == "cookies": @@ -153,7 +153,7 @@ class ResponseObject(object): # others else: err_msg = u"ParamsError: Failed to extract attribute from response! => {}\n".format(field) - err_msg += u"available response attributes: status_code, cookies, elapsed, headers, content, text, json." + err_msg += u"available response attributes: status_code, cookies, elapsed, headers, content, text, json, encoding, ok, reason, url." logger.log_error(err_msg) raise exceptions.ParamsError(err_msg) diff --git a/tests/test_response.py b/tests/test_response.py index 20146baf..44cf333d 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -47,6 +47,39 @@ class TestResponse(ApiServerUnittest): with self.assertRaises(exceptions.ParamsError): resp_obj.extract_response(extract_binds_list) + def test_extract_response_encoding_ok_reason_url(self): + resp = requests.get(url="http://127.0.0.1:3458/status/200") + resp_obj = response.ResponseObject(resp) + + extract_binds_list = [ + {"resp_encoding": "encoding"}, + {"resp_ok": "ok"}, + {"resp_reason": "reason"}, + {"resp_url": "url"} + ] + extract_binds_dict = resp_obj.extract_response(extract_binds_list) + + self.assertEqual(extract_binds_dict["resp_encoding"], "utf-8") + self.assertEqual(extract_binds_dict["resp_ok"], True) + self.assertEqual(extract_binds_dict["resp_reason"], "OK") + self.assertEqual(extract_binds_dict["resp_url"], "http://127.0.0.1:3458/status/200") + + extract_binds_list = [{"resp_encoding": "encoding.xx"}] + with self.assertRaises(exceptions.ParamsError): + resp_obj.extract_response(extract_binds_list) + + extract_binds_list = [{"resp_ok": "ok.xx"}] + with self.assertRaises(exceptions.ParamsError): + resp_obj.extract_response(extract_binds_list) + + extract_binds_list = [{"resp_reason": "reason.xx"}] + with self.assertRaises(exceptions.ParamsError): + resp_obj.extract_response(extract_binds_list) + + extract_binds_list = [{"resp_url": "url.xx"}] + 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",