change: reformat code

This commit is contained in:
debugtalk
2019-12-24 21:33:00 +08:00
parent 033f17294f
commit 6f42703e76
2 changed files with 72 additions and 70 deletions
+52 -51
View File
@@ -14,55 +14,7 @@ from httprunner.utils import lower_dict_keys, omit_long_data
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class ApiResponse(Response): def get_req_resp_record(resp_obj):
def raise_for_status(self):
if hasattr(self, 'error') and self.error:
raise self.error
Response.raise_for_status(self)
class HttpSession(requests.Session):
"""
Class for performing HTTP requests and holding (session-) cookies between requests (in order
to be able to log in and out of websites). Each request is logged so that HttpRunner can
display statistics.
This is a slightly extended version of `python-request <http://python-requests.org>`_'s
:py:class:`requests.Session` class and mostly this class works exactly the same.
"""
def __init__(self):
super(HttpSession, self).__init__()
self.init_meta_data()
def init_meta_data(self):
""" initialize meta_data, it will store detail data of request and response
"""
self.meta_data = {
"name": "",
"data": [
{
"request": {
"url": "N/A",
"method": "N/A",
"headers": {}
},
"response": {
"status_code": "N/A",
"headers": {},
"encoding": None,
"content_type": ""
}
}
],
"stat": {
"content_size": "N/A",
"response_time_ms": "N/A",
"elapsed_ms": "N/A",
}
}
def get_req_resp_record(self, resp_obj):
""" get request and response info from Response() object. """ get request and response info from Response() object.
""" """
def log_print(req_resp_dict, r_type): def log_print(req_resp_dict, r_type):
@@ -129,12 +81,61 @@ class HttpSession(requests.Session):
return req_resp_dict return req_resp_dict
class ApiResponse(Response):
def raise_for_status(self):
if hasattr(self, 'error') and self.error:
raise self.error
Response.raise_for_status(self)
class HttpSession(requests.Session):
"""
Class for performing HTTP requests and holding (session-) cookies between requests (in order
to be able to log in and out of websites). Each request is logged so that HttpRunner can
display statistics.
This is a slightly extended version of `python-request <http://python-requests.org>`_'s
:py:class:`requests.Session` class and mostly this class works exactly the same.
"""
def __init__(self):
super(HttpSession, self).__init__()
self.init_meta_data()
def init_meta_data(self):
""" initialize meta_data, it will store detail data of request and response
"""
self.meta_data = {
"name": "",
"data": [
{
"request": {
"url": "N/A",
"method": "N/A",
"headers": {}
},
"response": {
"status_code": "N/A",
"headers": {},
"encoding": None,
"content_type": ""
}
}
],
"stat": {
"content_size": "N/A",
"response_time_ms": "N/A",
"elapsed_ms": "N/A",
}
}
def update_last_req_resp_record(self, resp_obj): def update_last_req_resp_record(self, resp_obj):
""" """
update request and response info from Response() object. update request and response info from Response() object.
""" """
self.meta_data["data"].pop() self.meta_data["data"].pop()
self.meta_data["data"].append(self.get_req_resp_record(resp_obj)) self.meta_data["data"].append(get_req_resp_record(resp_obj))
def request(self, method, url, name=None, **kwargs): def request(self, method, url, name=None, **kwargs):
""" """
@@ -207,7 +208,7 @@ class HttpSession(requests.Session):
# record request and response histories, include 30X redirection # record request and response histories, include 30X redirection
response_list = response.history + [response] response_list = response.history + [response]
self.meta_data["data"] = [ self.meta_data["data"] = [
self.get_req_resp_record(resp_obj) get_req_resp_record(resp_obj)
for resp_obj in response_list for resp_obj in response_list
] ]
+2 -1
View File
@@ -222,7 +222,8 @@ class ResponseObject(object):
# others # others
else: else:
err_msg = u"Failed to extract attribute from response! => {}\n".format(field) err_msg = u"Failed to extract attribute from response! => {}\n".format(field)
err_msg += u"available response attributes: status_code, cookies, elapsed, headers, content, text, json, encoding, ok, reason, url.\n\n" err_msg += u"available response attributes: status_code, cookies, elapsed, headers, content, " \
u"text, json, encoding, ok, reason, url.\n\n"
err_msg += u"If you want to set attribute in teardown_hooks, take the following example as reference:\n" err_msg += u"If you want to set attribute in teardown_hooks, take the following example as reference:\n"
err_msg += u"response.new_attribute = 'new_attribute_value'\n" err_msg += u"response.new_attribute = 'new_attribute_value'\n"
logger.log_error(err_msg) logger.log_error(err_msg)