refactor: extract encoding/ok/reason/url

This commit is contained in:
debugtalk
2018-07-25 16:22:08 +08:00
parent 6c7e1be517
commit b88b66b904
2 changed files with 36 additions and 3 deletions

View File

@@ -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)

View File

@@ -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",