apitest_unittest/common/http_client/request_processor.py

65 lines
1.7 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from common.data_extraction.dependent_parameter import DependentParameter
from common.crypto.encryption_main import do_encrypt
from common.http_client.request_hooks import Hooks
from common.utils.mylogger import MyLogger
dep_par = DependentParameter() # 参数提取类实例化
logger = MyLogger()
hooks = Hooks()
@logger.log_decorator()
@hooks.before_request
def update_url(request):
"""更新url"""
request.url = dep_par.replace_dependent_parameter(request.url)
print(request.url)
return request
@logger.log_decorator()
@hooks.before_request
def update_header(request):
"""更新请求头"""
request.headers = dep_par.replace_dependent_parameter(request.headers)
print(request.headers)
return request
@logger.log_decorator()
@hooks.before_request
def update_body(request):
"""更新请求参数"""
if request.json:
request.json = dep_par.replace_dependent_parameter(request.json)
if request.encryption:
request.data = do_encrypt(request.encryption, request.json) # 数据加密MD5  
else:
request.data = dep_par.replace_dependent_parameter(request.data)
if request.encryption:
request.data = do_encrypt(request.encryption, request.data) # 数据加密MD5  
return request
@logger.log_decorator()
@hooks.before_request
def update_expected(request):
"""更新预期结果"""
request.expected = dep_par.replace_dependent_parameter(request.expected)
print(request.expected)
return request
@logger.log_decorator()
@hooks.after_request
def parse_json(response):
"""
尝试将响应中的内容解析为 JSON 格式
"""
try:
response.json_data = response.json()
except ValueError:
response.json_data = None
return response